KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Cagatay Kavukcuoglu <cagatayk@acm.org> - Fix for bug 10025 - Resizing views
11  * should not use height ratios
12  *******************************************************************************/

13
14 package org.eclipse.ui.internal;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.ui.IEditorReference;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.presentations.PresentablePart;
25 import org.eclipse.ui.internal.presentations.PresentationFactoryUtil;
26 import org.eclipse.ui.internal.presentations.SystemMenuPinEditor;
27 import org.eclipse.ui.internal.presentations.SystemMenuSize;
28 import org.eclipse.ui.internal.presentations.UpdatingActionContributionItem;
29 import org.eclipse.ui.internal.presentations.util.TabbedStackPresentation;
30 import org.eclipse.ui.internal.util.Util;
31 import org.eclipse.ui.presentations.IPresentablePart;
32 import org.eclipse.ui.presentations.IStackPresentationSite;
33 import org.eclipse.ui.presentations.StackPresentation;
34
35 /**
36  * Represents a tab folder of editors. This layout part container only accepts
37  * EditorPane parts.
38  *
39  * TODO: Make PartStack non-abstract and delete this class. The differences between
40  * editors and views should be handled by the presentation or the editors/views themselves.
41  */

42 public class EditorStack extends PartStack {
43
44     private EditorSashContainer editorArea;
45
46     private WorkbenchPage page;
47
48     private SystemMenuSize sizeItem = new SystemMenuSize(null);
49
50     private SystemMenuPinEditor pinEditorItem = new SystemMenuPinEditor(null);
51
52     public EditorStack(EditorSashContainer editorArea, WorkbenchPage page) {
53         super(PresentationFactoryUtil.ROLE_EDITOR);
54         this.editorArea = editorArea;
55         setID(this.toString());
56         // Each folder has a unique ID so relative positioning is unambiguous.
57
// save off a ref to the page
58
//@issue is it okay to do this??
59
//I think so since a ViewStack is
60
//not used on more than one page.
61
this.page = page;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.ui.internal.PartStack#getPage()
66      */

67     protected WorkbenchPage getPage() {
68         return page;
69     }
70
71     public void addSystemActions(IMenuManager menuManager) {
72         pinEditorItem = new SystemMenuPinEditor((EditorPane) getSelection());
73         appendToGroupIfPossible(menuManager,
74                 "misc", new UpdatingActionContributionItem(pinEditorItem)); //$NON-NLS-1$
75
sizeItem = new SystemMenuSize(getSelection());
76         appendToGroupIfPossible(menuManager, "size", sizeItem); //$NON-NLS-1$
77
}
78
79     public boolean isMoveable(IPresentablePart part) {
80         return true;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.presentations.IStackPresentationSite#supportsState(int)
85      */

86     public boolean supportsState(int state) {
87         if (page.isFixedLayout()) {
88             return false;
89         }
90
91         return true;
92     }
93
94     /**
95      * Factory method for editor workbooks.
96      */

97     public static EditorStack newEditorWorkbook(EditorSashContainer editorArea,
98             WorkbenchPage page) {
99         return new EditorStack(editorArea, page);
100     }
101
102     protected void add(LayoutPart newChild, Object JavaDoc cookie) {
103         super.add(newChild, cookie);
104
105         ((EditorPane) newChild).setWorkbook(this);
106     }
107
108     /**
109      * See IVisualContainer#add
110      */

111     public void add(LayoutPart child) {
112         super.add(child);
113
114         if (child instanceof EditorPane) {
115             ((EditorPane) child).setWorkbook(this);
116         }
117     }
118
119     protected void updateActions(PresentablePart current) {
120         EditorPane pane = null;
121         if (current != null && current.getPane() instanceof EditorPane) {
122             pane = (EditorPane) current.getPane();
123         }
124
125         sizeItem.setPane(pane);
126         pinEditorItem.setPane(pane);
127     }
128
129     public Control[] getTabList() {
130         return getTabList(getSelection());
131     }
132
133     public void removeAll() {
134         LayoutPart[] children = getChildren();
135
136         for (int i = 0; i < children.length; i++) {
137             remove(children[i]);
138         }
139     }
140
141     public boolean isActiveWorkbook() {
142         EditorSashContainer area = getEditorArea();
143
144         if (area != null) {
145             return area.isActiveWorkbook(this);
146         } else {
147             return false;
148         }
149     }
150
151     public void becomeActiveWorkbook(boolean hasFocus) {
152         EditorSashContainer area = getEditorArea();
153
154         if (area != null) {
155             area.setActiveWorkbook(this, hasFocus);
156         }
157     }
158
159     public EditorPane[] getEditors() {
160         LayoutPart[] children = getChildren();
161
162         EditorPane[] panes = new EditorPane[children.length];
163         for (int idx = 0; idx < children.length; idx++) {
164             panes[idx] = (EditorPane) children[idx];
165         }
166
167         return panes;
168     }
169
170     public EditorSashContainer getEditorArea() {
171         return editorArea;
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.ui.internal.PartStack#canMoveFolder()
176      */

177     protected boolean canMoveFolder() {
178         return true;
179     }
180
181     /* (non-Javadoc)
182      * @see org.eclipse.ui.internal.PartStack#derefPart(org.eclipse.ui.internal.LayoutPart)
183      */

184     protected void derefPart(LayoutPart toDeref) {
185         EditorAreaHelper.derefPart(toDeref);
186     }
187
188     /* (non-Javadoc)
189      * @see org.eclipse.ui.internal.PartStack#allowsDrop(org.eclipse.ui.internal.PartPane)
190      */

191     protected boolean allowsDrop(PartPane part) {
192         return part instanceof EditorPane;
193     }
194
195     public void setFocus() {
196         super.setFocus();
197         becomeActiveWorkbook(true);
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.ui.internal.PartStack#close(org.eclipse.ui.presentations.IPresentablePart[])
202      */

203     protected void close(IPresentablePart[] parts) {
204
205         if (parts.length == 1) {
206             close(parts[0]);
207             return;
208         }
209
210         IEditorReference[] toClose = new IEditorReference[parts.length];
211         for (int idx = 0; idx < parts.length; idx++) {
212             EditorPane part = (EditorPane) getPaneFor(parts[idx]);
213             toClose[idx] = part.getEditorReference();
214         }
215
216         WorkbenchPage page = getPage();
217
218         if (page != null) {
219             page.closeEditors(toClose, true);
220         }
221     }
222
223     /* (non-Javadoc)
224      * @see org.eclipse.ui.internal.LayoutPart#testInvariants()
225      */

226     public void testInvariants() {
227         super.testInvariants();
228
229         int active = getActive();
230
231         if (active == StackPresentation.AS_ACTIVE_FOCUS) {
232             Assert.isTrue(isActiveWorkbook());
233         } else if (active == StackPresentation.AS_ACTIVE_NOFOCUS) {
234             Assert.isTrue(isActiveWorkbook());
235         } else if (active == StackPresentation.AS_INACTIVE) {
236             Assert.isTrue(!isActiveWorkbook());
237         }
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.ui.internal.PartStack#restoreState(org.eclipse.ui.IMemento)
242      */

243     public IStatus restoreState(IMemento memento) {
244         Integer JavaDoc expanded = memento.getInteger(IWorkbenchConstants.TAG_EXPANDED);
245         setState((expanded == null || expanded.intValue() != IStackPresentationSite.STATE_MINIMIZED) ? IStackPresentationSite.STATE_RESTORED
246                 : IStackPresentationSite.STATE_MINIMIZED);
247
248         Integer JavaDoc appearance = memento
249                 .getInteger(IWorkbenchConstants.TAG_APPEARANCE);
250         if (appearance != null) {
251             this.appearance = appearance.intValue();
252         }
253
254         // Determine if the presentation has saved any info here
255
savedPresentationState = null;
256         IMemento[] presentationMementos = memento
257                 .getChildren(IWorkbenchConstants.TAG_PRESENTATION);
258
259         for (int idx = 0; idx < presentationMementos.length; idx++) {
260             IMemento child = presentationMementos[idx];
261
262             String JavaDoc id = child.getString(IWorkbenchConstants.TAG_ID);
263
264             if (Util.equals(id, getFactory().getId())) {
265                 savedPresentationState = child;
266                 break;
267             }
268         }
269
270         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
271
}
272
273     /* (non-Javadoc)
274      * @see org.eclipse.ui.internal.PartStack#saveState(org.eclipse.ui.IMemento)
275      */

276     public IStatus saveState(IMemento memento) {
277         memento
278                 .putInteger(
279                         IWorkbenchConstants.TAG_EXPANDED,
280                         (getPresentationSite().getState() == IStackPresentationSite.STATE_MINIMIZED) ? IStackPresentationSite.STATE_MINIMIZED
281                                 : IStackPresentationSite.STATE_RESTORED);
282
283         memento.putInteger(IWorkbenchConstants.TAG_APPEARANCE, appearance);
284
285         savePresentationState();
286
287         if (savedPresentationState != null) {
288             IMemento presentationState = memento
289                     .createChild(IWorkbenchConstants.TAG_PRESENTATION);
290             presentationState.putMemento(savedPresentationState);
291         }
292
293         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
294
}
295
296     /* (non-Javadoc)
297      * @see org.eclipse.ui.internal.PartStack#setMinimized(boolean)
298      */

299     public void setMinimized(boolean minimized) {
300         // 'Smart' minimize; move the editor area to the trim
301
Perspective persp = getPage().getActivePerspective();
302         if (Perspective.useNewMinMax(persp)) {
303             if (minimized) {
304                 persp.setEditorAreaState(IStackPresentationSite.STATE_MINIMIZED);
305             }
306             else {
307                 // First, if we're maximized then revert
308
if (persp.getPresentation().getMaximizedStack() != null) {
309                     PartStack maxStack = persp.getPresentation().getMaximizedStack();
310                     if (maxStack instanceof ViewStack) {
311                         maxStack.setState(IStackPresentationSite.STATE_RESTORED);
312                     }
313                     else if (maxStack instanceof EditorStack) {
314                         // We handle editor max through the perspective since it's
315
// shared between pages...
316
persp.setEditorAreaState(IStackPresentationSite.STATE_RESTORED);
317                     }
318                 }
319                 
320                 int curState = persp.getEditorAreaState();
321                 if (curState == IStackPresentationSite.STATE_MINIMIZED)
322                     curState = IStackPresentationSite.STATE_RESTORED;
323                 
324                 persp.setEditorAreaState(curState);
325             }
326             
327             refreshPresentationState();
328             //return;
329
}
330         
331         super.setMinimized(minimized);
332     }
333
334     /**
335      * Changes the editor stack's state to the given one -without-
336      * side-effects. This is used when switching perspectives because
337      * the Editor Area is perspective based but is shared between all
338      * perspectives...
339      *
340      * @param newState The new state to set the editor stack to
341      */

342     public void setStateLocal(int newState) {
343         if (newState == getState())
344             return;
345         
346         //isMinimized = getState() == IStackPresentationSite.STATE_MINIMIZED;
347
super.setMinimized(newState == IStackPresentationSite.STATE_MINIMIZED);
348         presentationSite.setPresentationState(newState);
349     }
350     
351     /**
352      * Cause the folder to hide or show its
353      * Minimize and Maximize affordances.
354      *
355      * @param show
356      * <code>true</code> - the min/max buttons are visible.
357      * @since 3.3
358      */

359     public void showMinMax(boolean show) {
360         StackPresentation pres = getPresentation();
361         if (pres == null)
362             return;
363         
364         if (pres instanceof TabbedStackPresentation)
365             ((TabbedStackPresentation)pres).showMinMax(show);
366     }
367 }
368
Popular Tags