Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/eclipse/ui/internal/EditorAreaHelper.java


1   /*******************************************************************************
2    * Copyright (c) 2000, 2004 IBM Corporation and others.
3    * All rights reserved. This program and the accompanying materials 
4    * are made available under the terms of the Common Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/cpl-v10.html
7    * 
8    * Contributors:
9    *     IBM Corporation - initial API and implementation
10   *******************************************************************************/
11  
12  package org.eclipse.ui.internal;
13  
14  import java.util.ArrayList;
15  import java.util.Map;
16  
17  import org.eclipse.core.runtime.IStatus;
18  import org.eclipse.swt.widgets.Shell;
19  import org.eclipse.ui.IEditorPart;
20  import org.eclipse.ui.IEditorReference;
21  import org.eclipse.ui.IMemento;
22  import org.eclipse.ui.IPageLayout;
23  import org.eclipse.ui.PlatformUI;
24  import org.eclipse.ui.commands.AbstractHandler;
25  import org.eclipse.ui.commands.ExecutionException;
26  import org.eclipse.ui.commands.HandlerSubmission;
27  import org.eclipse.ui.commands.IHandler;
28  import org.eclipse.ui.commands.Priority;
29  import org.eclipse.ui.part.MultiEditor;
30  
31  /**
32   * EditorAreaHelper is a wrapper for PartTabworkbook.
33   */
34  public class EditorAreaHelper {
35    private WorkbenchPage page;
36    private ArrayList editorTable = new ArrayList(4);
37    private EditorSashContainer editorArea;
38    private HandlerSubmission openEditorDropDownHandlerSubmission;
39    /**
40     * Creates a new EditorAreaHelper.
41     */
42    public EditorAreaHelper(WorkbenchPage page) {
43  
44      this.page = page;
45      this.editorArea = new EditorSashContainer(IPageLayout.ID_EDITOR_AREA, page);
46      
47      this.editorArea.createControl(page.getClientComposite());
48      
49          final Shell shell = page.getWorkbenchWindow().getShell();
50          IHandler openEditorDropDownHandler = new AbstractHandler() {
51  
52              public Object execute(Map parameterValuesByName) throws ExecutionException {
53                displayEditorList();
54                  return null;
55              }
56          };
57          openEditorDropDownHandlerSubmission = new HandlerSubmission(null,
58                  shell, null, "org.eclipse.ui.window.openEditorDropDown", //$NON-NLS-1$
59                  openEditorDropDownHandler, Priority.MEDIUM);
60          
61        PlatformUI.getWorkbench().getCommandSupport().addHandlerSubmission(
62                  openEditorDropDownHandlerSubmission);
63    }
64    /**
65     * Displays a list of open editors
66     */
67    public void displayEditorList() {
68      EditorStack activeWorkbook = editorArea.getActiveWorkbook();
69        if (activeWorkbook != null) {
70          activeWorkbook.showPartList();
71        }
72    }
73    
74    /**
75     * Closes all of the editors.
76     */
77    public void closeAllEditors() {
78      editorArea.removeAllEditors();
79      ArrayList editorsToDispose = (ArrayList) editorTable.clone();
80      editorTable.clear();
81      for (int i = 0; i < editorsToDispose.size(); i++) {
82        ((EditorPane) editorsToDispose.get(i)).dispose();
83      }
84    }
85    /**
86     * Closes an editor.   
87     *
88     * @param part the editor to close
89     */
90    public void closeEditor(IEditorReference ref) {
91      EditorPane pane = (EditorPane) ((WorkbenchPartReference) ref).getPane();
92      closeEditor(pane);
93    }
94    /**
95     * Closes an editor.   
96     *
97     * @param part the editor to close
98     */
99    public void closeEditor(IEditorPart part) {
100     EditorPane pane = (EditorPane) ((PartSite) part.getEditorSite()).getPane();
101     closeEditor(pane);
102   }
103   /**
104    * Closes an editor.   
105    *
106    * @param part the editor to close
107    */
108   private void closeEditor(EditorPane pane) {
109     if (pane != null) {
110       if (!(pane instanceof MultiEditorInnerPane))
111         editorArea.removeEditor(pane);
112       editorTable.remove(pane);
113       pane.dispose();
114     }
115   }
116   /**
117    * Deref a given part.  Deconstruct its container as required.
118    * Do not remove drag listeners.
119    */
120   public static void derefPart(LayoutPart part) {
121 
122     // Get vital part stats before reparenting.
123     ILayoutContainer oldContainer = part.getContainer();
124 
125     // Reparent the part back to the main window
126     //part.reparent(editorArea.getParent());
127     // Update container.
128     if (oldContainer == null)
129       return;
130     oldContainer.remove(part);
131     LayoutPart[] children = oldContainer.getChildren();
132     if (children == null || children.length == 0) {
133       // There are no more children in this container, so get rid of it
134       if (oldContainer instanceof LayoutPart) {
135         LayoutPart parent = (LayoutPart) oldContainer;
136         ILayoutContainer parentContainer = parent.getContainer();
137         if (parentContainer != null) {
138           parentContainer.remove(parent);
139           parent.dispose();
140         }
141       }
142     }
143   }
144   /**
145    * Dispose of the editor presentation. 
146    */
147   public void dispose() {
148         PlatformUI.getWorkbench().getCommandSupport()
149         .removeHandlerSubmission(
150                 openEditorDropDownHandlerSubmission);
151 
152     
153     if (editorArea != null) {
154       editorArea.dispose();
155     }
156   }
157   /**
158    * @see IEditorPresentation
159    */
160   public String getActiveEditorWorkbookID() {
161     return editorArea.getActiveWorkbookID();
162   }
163   /**
164    * Returns an array of the open editors.
165    *
166    * @return an array of open editors
167    */
168   public IEditorReference[] getEditors() {
169     int nSize = editorTable.size();
170     IEditorReference[] retArray = new IEditorReference[nSize];
171     for (int i = 0; i < retArray.length; i++) {
172       retArray[i] = ((EditorPane) editorTable.get(i)).getEditorReference();
173     }
174     return retArray;
175   }
176   /**
177    * Returns the editor area.
178    */
179   public LayoutPart getLayoutPart() {
180     return editorArea;
181   }
182   /**
183    * Returns the active editor in this perspective.  If the editors appear
184    * in a workbook this will be the visible editor.  If the editors are
185    * scattered around the workbench this will be the most recent editor
186    * to hold focus.
187    *
188    * @return the active editor, or <code>null</code> if no editor is active
189    */
190   public IEditorReference getVisibleEditor() {
191     EditorStack activeWorkbook = editorArea.getActiveWorkbook();
192     EditorPane pane = activeWorkbook.getVisibleEditor();
193     if (pane != null) {
194       IEditorReference result = pane.getEditorReference();
195       IEditorPart editorPart = (IEditorPart) result.getPart(false);
196       if ((editorPart != null) && (editorPart instanceof MultiEditor)) {
197         editorPart = ((MultiEditor) editorPart).getActiveEditor();
198         EditorSite site = (EditorSite) editorPart.getSite();
199         result = (IEditorReference) site.getPane().getPartReference();
200       }
201       return result;
202     }
203     return null;
204   }
205   /**
206    * The active editor has failed to be restored. Find another editor, restore it
207    * and make it visible.
208    */
209   public void fixVisibleEditor() {
210     EditorStack activeWorkbook = editorArea.getActiveWorkbook();
211     EditorPane pane = activeWorkbook.getVisibleEditor();
212     if (pane == null) {
213       LayoutPart editors[] = activeWorkbook.getChildren();
214       if (editors.length > 0)
215         pane = (EditorPane) editors[0];
216     }
217     if (pane != null) {
218       IEditorReference result = pane.getEditorReference();
219       IEditorPart editorPart = (IEditorPart) result.getPart(true);
220       if (editorPart != null)
221         activeWorkbook.setVisibleEditor(pane);
222     }
223   }
224 
225   public void moveEditor(IEditorPart part, int position) {
226     EditorPane pane = (EditorPane) ((EditorSite) part.getSite()).getPane();
227     //TODO commented this out during presentations works
228     //pane.getWorkbook().reorderTab(pane, position);
229   }
230   /**
231    * Opens an editor within the presentation but does not give it focus.
232    * </p>
233    * @param part the editor
234    */
235   public void openEditor(
236     IEditorReference ref,
237     IEditorReference[] innerEditors,
238     boolean setVisible) {
239     EditorPane pane = new MultiEditorOuterPane(ref, page, editorArea.getActiveWorkbook());
240     initPane(pane, ref);
241     for (int i = 0; i < innerEditors.length; i++) {
242       EditorPane innerPane =
243         new MultiEditorInnerPane(
244           pane,
245           innerEditors[i],
246           page,
247           editorArea.getActiveWorkbook());
248       initPane(innerPane, innerEditors[i]);
249     }
250     // Show the editor.
251     editorArea.addEditor(pane);
252     if (setVisible)
253       setVisibleEditor(ref, false);
254   }
255   /**
256    * Opens an editor within the presentation but does not give it focus.
257    * </p>
258    * @param part the editor
259    */
260   public void openEditor(IEditorReference ref, boolean setVisible) {
261 
262     EditorPane pane = new EditorPane(ref, page, editorArea.getActiveWorkbook());
263     initPane(pane, ref);
264 
265     // Show the editor.
266     editorArea.addEditor(pane);
267     if (setVisible)
268       setVisibleEditor(ref, false);
269   }
270   private EditorPane initPane(EditorPane pane, IEditorReference ref) {
271     ((WorkbenchPartReference) ref).setPane(pane);
272     // Record the new editor.
273     editorTable.add(pane);
274     return pane;
275   }
276   /**
277    * @see IPersistablePart
278    */
279   public IStatus restoreState(IMemento memento) {
280     // Restore the editor area workbooks layout/relationship
281     return editorArea.restoreState(memento);
282   }
283 
284   /**
285    * Restore the presentation
286    * @param areaMem
287    * @return
288    */
289   public IStatus restorePresentationState(IMemento areaMem) {
290     return editorArea.restorePresentationState(areaMem);
291   }  
292 
293   /**
294    * @see IPersistablePart
295    */
296   public IStatus saveState(IMemento memento) {
297     // Save the editor area workbooks layout/relationship
298     return editorArea.saveState(memento);
299   }
300   /**
301    * @see IEditorPresentation
302    */
303   public void setActiveEditorWorkbookFromID(String id) {
304     editorArea.setActiveWorkbookFromID(id);
305   }
306   /**
307    * Makes sure the visible editor's tab is visible.
308    */
309   public void showVisibleEditor() {
310     EditorStack activeWorkbook = editorArea.getActiveWorkbook();
311     if (activeWorkbook != null)
312       activeWorkbook.showVisibleEditor();
313   }
314   /**
315    * Brings an editor to the front and optionally gives it focus.
316    *
317    * @param part the editor to make visible
318    * @param setFocus whether to give the editor focus
319    * @return true if the visible editor was changed, false if not.
320    */
321   public boolean setVisibleEditor(IEditorReference ref, boolean setFocus) {
322     IEditorReference visibleEditor = getVisibleEditor();
323     if (ref != visibleEditor) {
324       IEditorPart part = (IEditorPart) ref.getPart(true);
325       EditorPane pane = null;
326       if (part != null)
327         pane = (EditorPane) ((PartSite) part.getEditorSite()).getPane();
328       if (pane != null) {
329         if (pane instanceof MultiEditorInnerPane) {
330           EditorPane parentPane = ((MultiEditorInnerPane) pane).getParentPane();
331           EditorStack activeWorkbook = parentPane.getWorkbook();
332           EditorPane activePane = activeWorkbook.getVisibleEditor();
333           if (activePane != parentPane)
334             parentPane.getWorkbook().setVisibleEditor(parentPane);
335           else
336             return false;
337         } else {
338           pane.getWorkbook().setVisibleEditor(pane);
339         }
340         if (setFocus)
341           part.setFocus();
342         return true;
343       }
344     }
345     return false;
346   }
347 
348 
349 /**
350  * Method getWorkbooks.
351  * @return ArrayList
352  */
353 public ArrayList getWorkbooks() {
354   return editorArea.getEditorWorkbooks();
355 }
356 
357 }