001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.ui.IEditorPart;
018: import org.eclipse.ui.IEditorReference;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IPageLayout;
021: import org.eclipse.ui.part.MultiEditor;
022:
023: /**
024: * EditorAreaHelper is a wrapper for PartTabworkbook.
025: */
026: public class EditorAreaHelper {
027:
028: //private ArrayList editorTable = new ArrayList(4);
029:
030: private EditorSashContainer editorArea;
031:
032: /**
033: * Creates a new EditorAreaHelper.
034: */
035: public EditorAreaHelper(WorkbenchPage page) {
036: this .editorArea = new EditorSashContainer(
037: IPageLayout.ID_EDITOR_AREA, page, page
038: .getClientComposite());
039:
040: this .editorArea.createControl(page.getClientComposite());
041: this .editorArea.setActive(true);
042: }
043:
044: /**
045: * Displays a list of open editors
046: */
047: public void displayEditorList() {
048: EditorStack activeWorkbook = editorArea.getActiveWorkbook();
049: if (activeWorkbook != null) {
050: activeWorkbook.showPartList();
051: }
052: }
053:
054: /**
055: * Closes an editor.
056: *
057: * @param part the editor to close
058: */
059: public void closeEditor(IEditorReference ref) {
060: EditorPane pane = (EditorPane) ((WorkbenchPartReference) ref)
061: .getPane();
062: closeEditor(pane);
063: }
064:
065: /**
066: * Closes an editor.
067: *
068: * @param part the editor to close
069: */
070: public void closeEditor(IEditorPart part) {
071: EditorPane pane = (EditorPane) ((PartSite) part.getEditorSite())
072: .getPane();
073: closeEditor(pane);
074: }
075:
076: /**
077: * Closes an editor.
078: *
079: * @param part the editor to close
080: */
081: private void closeEditor(EditorPane pane) {
082: if (pane != null) {
083: if (!(pane instanceof MultiEditorInnerPane)) {
084: editorArea.removeEditor(pane);
085: }
086: }
087: }
088:
089: /**
090: * Deref a given part. Deconstruct its container as required.
091: * Do not remove drag listeners.
092: */
093: public static void derefPart(LayoutPart part) {
094:
095: // Get vital part stats before reparenting.
096: ILayoutContainer oldContainer = part.getContainer();
097:
098: // Reparent the part back to the main window
099: //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
111: .getContainer();
112: if (parentContainer != null) {
113: parentContainer.remove(parent);
114: parent.dispose();
115: }
116: }
117: }
118: }
119:
120: /**
121: * Dispose of the editor presentation.
122: */
123: public void dispose() {
124: if (editorArea != null) {
125: editorArea.setActive(false);
126: editorArea.dispose();
127: }
128: }
129:
130: /**
131: * @see IEditorPresentation
132: */
133: public String getActiveEditorWorkbookID() {
134: return editorArea.getActiveWorkbookID();
135: }
136:
137: public EditorStack getActiveWorkbook() {
138: return editorArea.getActiveWorkbook();
139: }
140:
141: /**
142: * Returns the editor area.
143: */
144: public LayoutPart getLayoutPart() {
145: return editorArea;
146: }
147:
148: /**
149: * Returns the active editor in this perspective. If the editors appear
150: * in a workbook this will be the visible editor. If the editors are
151: * scattered around the workbench this will be the most recent editor
152: * to hold focus.
153: *
154: * @return the active editor, or <code>null</code> if no editor is active
155: */
156: public IEditorReference getVisibleEditor() {
157: EditorStack activeWorkbook = editorArea.getActiveWorkbook();
158: EditorPane pane = (EditorPane) activeWorkbook.getSelection();
159: if (pane != null) {
160: IEditorReference result = pane.getEditorReference();
161: IEditorPart editorPart = (IEditorPart) result
162: .getPart(false);
163: if ((editorPart != null)
164: && (editorPart instanceof MultiEditor)) {
165: editorPart = ((MultiEditor) editorPart)
166: .getActiveEditor();
167: EditorSite site = (EditorSite) editorPart.getSite();
168: result = (IEditorReference) site.getPartReference();
169: }
170: return result;
171: }
172: return null;
173: }
174:
175: public void moveEditor(IEditorPart part, int position) {
176: /*EditorPane pane = (EditorPane)*/((EditorSite) part.getSite())
177: .getPane();
178: //TODO commented this out during presentations works
179: //pane.getWorkbook().reorderTab(pane, position);
180: }
181:
182: /**
183: * Main entry point for adding an editor. Adds the editor to the layout in the given
184: * stack, and notifies the workbench page when done.
185: *
186: * @param ref editor to add
187: * @param workbookId workbook that will contain the editor (or null if the editor
188: * should be added to the default workbook)
189: */
190: public void addEditor(EditorReference ref, String workbookId) {
191: IEditorReference refs[] = editorArea.getPage()
192: .getEditorReferences();
193: for (int i = 0; i < refs.length; i++) {
194: if (ref == refs[i]) {
195: return;
196: }
197: }
198:
199: if (!(ref.getPane() instanceof MultiEditorInnerPane)) {
200:
201: EditorStack stack = null;
202:
203: if (workbookId != null) {
204: stack = getWorkbookFromID(workbookId);
205: }
206:
207: if (stack == null) {
208: stack = getActiveWorkbook();
209: }
210:
211: addToLayout((EditorPane) ref.getPane(), stack);
212: }
213:
214: editorArea.getPage().partAdded(ref);
215: }
216:
217: private void addToLayout(EditorPane pane, EditorStack stack) {
218: //EditorStack stack = editorArea.getActiveWorkbook();
219: pane.setWorkbook(stack);
220:
221: editorArea.addEditor(pane, stack);
222: }
223:
224: /**
225: * @see IPersistablePart
226: */
227: public IStatus restoreState(IMemento memento) {
228: // Restore the editor area workbooks layout/relationship
229: return editorArea.restoreState(memento);
230: }
231:
232: /**
233: * Restore the presentation
234: * @param areaMem
235: * @return
236: */
237: public IStatus restorePresentationState(IMemento areaMem) {
238: return editorArea.restorePresentationState(areaMem);
239: }
240:
241: /**
242: * @see IPersistablePart
243: */
244: public IStatus saveState(IMemento memento) {
245: // Save the editor area workbooks layout/relationship
246: return editorArea.saveState(memento);
247: }
248:
249: /**
250: * @see IEditorPresentation
251: */
252: public void setActiveEditorWorkbookFromID(String id) {
253: editorArea.setActiveWorkbookFromID(id);
254: }
255:
256: /**
257: * Brings an editor to the front and optionally gives it focus.
258: *
259: * @param part the editor to make visible
260: * @param setFocus whether to give the editor focus
261: * @return true if the visible editor was changed, false if not.
262: */
263: public boolean setVisibleEditor(IEditorReference ref,
264: boolean setFocus) {
265: IEditorReference visibleEditor = getVisibleEditor();
266: if (ref != visibleEditor) {
267: IEditorPart part = (IEditorPart) ref.getPart(true);
268: EditorPane pane = null;
269: if (part != null) {
270: pane = (EditorPane) ((PartSite) part.getEditorSite())
271: .getPane();
272: }
273: if (pane != null) {
274: if (pane instanceof MultiEditorInnerPane) {
275: EditorPane parentPane = ((MultiEditorInnerPane) pane)
276: .getParentPane();
277: EditorStack activeWorkbook = parentPane
278: .getWorkbook();
279: PartPane activePane = activeWorkbook.getSelection();
280: if (activePane != parentPane) {
281: parentPane.getWorkbook().setSelection(
282: parentPane);
283: } else {
284: return false;
285: }
286: } else {
287: pane.getWorkbook().setSelection(pane);
288: }
289: if (setFocus) {
290: part.setFocus();
291: }
292: return true;
293: }
294: }
295: return false;
296: }
297:
298: /**
299: * Method getWorkbooks.
300: * @return ArrayList
301: */
302: public ArrayList getWorkbooks() {
303: return editorArea.getEditorWorkbooks();
304: }
305:
306: public IEditorReference[] getEditors() {
307: List result = new ArrayList();
308: List workbooks = editorArea.getEditorWorkbooks();
309:
310: for (Iterator iter = workbooks.iterator(); iter.hasNext();) {
311: PartStack stack = (PartStack) iter.next();
312:
313: LayoutPart[] children = stack.getChildren();
314:
315: for (int i = 0; i < children.length; i++) {
316: LayoutPart part = children[i];
317:
318: result.add(((PartPane) part).getPartReference());
319: }
320: }
321:
322: return (IEditorReference[]) result
323: .toArray(new IEditorReference[result.size()]);
324: }
325:
326: public EditorStack getWorkbookFromID(String workbookId) {
327: return editorArea.getWorkbookFromID(workbookId);
328: }
329:
330: public void updateStackButtons() {
331: editorArea.updateStackButtons();
332: }
333: }
|