KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > EditorAreaHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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 JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
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.part.MultiEditor;
24
25 /**
26  * EditorAreaHelper is a wrapper for PartTabworkbook.
27  */

28 public class EditorAreaHelper {
29
30     //private ArrayList editorTable = new ArrayList(4);
31

32     private EditorSashContainer editorArea;
33
34     /**
35      * Creates a new EditorAreaHelper.
36      */

37     public EditorAreaHelper(WorkbenchPage page) {
38         this.editorArea = new EditorSashContainer(IPageLayout.ID_EDITOR_AREA,
39                 page, page.getClientComposite());
40
41         this.editorArea.createControl(page.getClientComposite());
42         this.editorArea.setActive(true);
43     }
44
45     /**
46      * Displays a list of open editors
47      */

48     public void displayEditorList() {
49         EditorStack activeWorkbook = editorArea.getActiveWorkbook();
50         if (activeWorkbook != null) {
51             activeWorkbook.showPartList();
52         }
53     }
54
55     /**
56      * Closes an editor.
57      *
58      * @param part the editor to close
59      */

60     public void closeEditor(IEditorReference ref) {
61         EditorPane pane = (EditorPane) ((WorkbenchPartReference) ref).getPane();
62         closeEditor(pane);
63     }
64
65     /**
66      * Closes an editor.
67      *
68      * @param part the editor to close
69      */

70     public void closeEditor(IEditorPart part) {
71         EditorPane pane = (EditorPane) ((PartSite) part.getEditorSite())
72                 .getPane();
73         closeEditor(pane);
74     }
75
76     /**
77      * Closes an editor.
78      *
79      * @param part the editor to close
80      */

81     private void closeEditor(EditorPane pane) {
82         if (pane != null) {
83             if (!(pane instanceof MultiEditorInnerPane)) {
84                 editorArea.removeEditor(pane);
85             }
86         }
87     }
88
89     /**
90      * Deref a given part. Deconstruct its container as required.
91      * Do not remove drag listeners.
92      */

93     public static void derefPart(LayoutPart part) {
94
95         // Get vital part stats before reparenting.
96
ILayoutContainer oldContainer = part.getContainer();
97
98         // Reparent the part back to the main window
99
//part.reparent(editorArea.getParent());
100
// Update container.
101
if (oldContainer == null) {
102             return;
103         }
104         oldContainer.remove(part);
105         LayoutPart[] children = oldContainer.getChildren();
106         if (children == null || children.length == 0) {
107             // There are no more children in this container, so get rid of it
108
if (oldContainer instanceof LayoutPart) {
109                 LayoutPart parent = (LayoutPart) oldContainer;
110                 ILayoutContainer parentContainer = parent.getContainer();
111                 if (parentContainer != null) {
112                     parentContainer.remove(parent);
113                     parent.dispose();
114                 }
115             }
116         }
117     }
118
119     /**
120      * Dispose of the editor presentation.
121      */

122     public void dispose() {
123         if (editorArea != null) {
124             editorArea.setActive(false);
125             editorArea.dispose();
126         }
127     }
128
129     /**
130      * @see IEditorPresentation
131      */

132     public String JavaDoc getActiveEditorWorkbookID() {
133         return editorArea.getActiveWorkbookID();
134     }
135
136     public EditorStack getActiveWorkbook() {
137         return editorArea.getActiveWorkbook();
138     }
139
140     /**
141      * Returns the editor area.
142      */

143     public LayoutPart getLayoutPart() {
144         return editorArea;
145     }
146
147     /**
148      * Returns the active editor in this perspective. If the editors appear
149      * in a workbook this will be the visible editor. If the editors are
150      * scattered around the workbench this will be the most recent editor
151      * to hold focus.
152      *
153      * @return the active editor, or <code>null</code> if no editor is active
154      */

155     public IEditorReference getVisibleEditor() {
156         EditorStack activeWorkbook = editorArea.getActiveWorkbook();
157         EditorPane pane = (EditorPane)activeWorkbook.getSelection();
158         if (pane != null) {
159             IEditorReference result = pane.getEditorReference();
160             IEditorPart editorPart = (IEditorPart) result.getPart(false);
161             if ((editorPart != null) && (editorPart instanceof MultiEditor)) {
162                 editorPart = ((MultiEditor) editorPart).getActiveEditor();
163                 EditorSite site = (EditorSite) editorPart.getSite();
164                 result = (IEditorReference) site.getPartReference();
165             }
166             return result;
167         }
168         return null;
169     }
170
171     public void moveEditor(IEditorPart part, int position) {
172         /*EditorPane pane = (EditorPane)*/ ((EditorSite) part.getSite()).getPane();
173         //TODO commented this out during presentations works
174
//pane.getWorkbook().reorderTab(pane, position);
175
}
176
177
178
179     /**
180      * Main entry point for adding an editor. Adds the editor to the layout in the given
181      * stack, and notifies the workbench page when done.
182      *
183      * @param ref editor to add
184      * @param workbookId workbook that will contain the editor (or null if the editor
185      * should be added to the default workbook)
186      */

187     public void addEditor(EditorReference ref, String JavaDoc workbookId) {
188         IEditorReference refs[] = editorArea.getPage().getEditorReferences();
189         for (int i = 0; i < refs.length; i++) {
190             if (ref == refs[i]) {
191                 return;
192             }
193         }
194         
195         if (!(ref.getPane() instanceof MultiEditorInnerPane)) {
196             
197             EditorStack stack = null;
198             
199             if (workbookId != null) {
200                 stack = getWorkbookFromID(workbookId);
201             }
202             
203             if (stack == null) {
204                 stack = getActiveWorkbook();
205             }
206             
207             addToLayout((EditorPane)ref.getPane(), stack);
208         }
209         
210         editorArea.getPage().partAdded(ref);
211     }
212     
213     private void addToLayout(EditorPane pane, EditorStack stack) {
214         //EditorStack stack = editorArea.getActiveWorkbook();
215
pane.setWorkbook(stack);
216         
217         editorArea.addEditor(pane, stack);
218     }
219
220
221     /**
222      * @see IPersistablePart
223      */

224     public IStatus restoreState(IMemento memento) {
225         // Restore the editor area workbooks layout/relationship
226
return editorArea.restoreState(memento);
227     }
228
229     /**
230      * Restore the presentation
231      * @param areaMem
232      * @return
233      */

234     public IStatus restorePresentationState(IMemento areaMem) {
235         return editorArea.restorePresentationState(areaMem);
236     }
237
238     /**
239      * @see IPersistablePart
240      */

241     public IStatus saveState(IMemento memento) {
242         // Save the editor area workbooks layout/relationship
243
return editorArea.saveState(memento);
244     }
245
246     /**
247      * @see IEditorPresentation
248      */

249     public void setActiveEditorWorkbookFromID(String JavaDoc id) {
250         editorArea.setActiveWorkbookFromID(id);
251     }
252
253     /**
254      * Brings an editor to the front and optionally gives it focus.
255      *
256      * @param part the editor to make visible
257      * @param setFocus whether to give the editor focus
258      * @return true if the visible editor was changed, false if not.
259      */

260     public boolean setVisibleEditor(IEditorReference ref, boolean setFocus) {
261         IEditorReference visibleEditor = getVisibleEditor();
262         if (ref != visibleEditor) {
263             IEditorPart part = (IEditorPart) ref.getPart(true);
264             EditorPane pane = null;
265             if (part != null) {
266                 pane = (EditorPane) ((PartSite) part.getEditorSite()).getPane();
267             }
268             if (pane != null) {
269                 if (pane instanceof MultiEditorInnerPane) {
270                     EditorPane parentPane = ((MultiEditorInnerPane) pane)
271                             .getParentPane();
272                     EditorStack activeWorkbook = parentPane.getWorkbook();
273                     PartPane activePane = activeWorkbook.getSelection();
274                     if (activePane != parentPane) {
275                         parentPane.getWorkbook().setSelection(parentPane);
276                     } else {
277                         return false;
278                     }
279                 } else {
280                     pane.getWorkbook().setSelection(pane);
281                 }
282                 if (setFocus) {
283                     part.setFocus();
284                 }
285                 return true;
286             }
287         }
288         return false;
289     }
290
291     
292     
293     /**
294      * Method getWorkbooks.
295      * @return ArrayList
296      */

297     public ArrayList JavaDoc getWorkbooks() {
298         return editorArea.getEditorWorkbooks();
299     }
300     
301     public IEditorReference[] getEditors() {
302         List JavaDoc result = new ArrayList JavaDoc();
303         List JavaDoc workbooks = editorArea.getEditorWorkbooks();
304         
305         for (Iterator JavaDoc iter = workbooks.iterator(); iter.hasNext();) {
306             PartStack stack = (PartStack) iter.next();
307             
308             LayoutPart[] children = stack.getChildren();
309             
310             for (int i = 0; i < children.length; i++) {
311                 LayoutPart part = children[i];
312                 
313                 result.add(((PartPane)part).getPartReference());
314             }
315         }
316         
317         return (IEditorReference[]) result.toArray(new IEditorReference[result.size()]);
318     }
319
320     public EditorStack getWorkbookFromID(String JavaDoc workbookId) {
321         return editorArea.getWorkbookFromID(workbookId);
322     }
323
324     public void updateStackButtons() {
325         editorArea.updateStackButtons();
326     }
327 }
328
Popular Tags