Source code: org/eclipse/ui/internal/EditorStack.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 * 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.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.util.Assert;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.ui.IEditorReference;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.PlatformUI;
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.util.Util;
30 import org.eclipse.ui.presentations.IPresentablePart;
31 import org.eclipse.ui.presentations.IStackPresentationSite;
32 import org.eclipse.ui.presentations.StackPresentation;
33
34 /**
35 * Represents a tab folder of editors. This layout part container only accepts
36 * EditorPane parts.
37 *
38 * TODO: Make PartStack non-abstract and delete this class. The differences between
39 * editors and views should be handled by the presentation or the editors/views themselves.
40 */
41 public class EditorStack extends PartStack {
42
43 private EditorSashContainer editorArea;
44
45 private WorkbenchPage page;
46
47 private SystemMenuSize sizeItem = new SystemMenuSize(null);
48 private SystemMenuPinEditor pinEditorItem = new SystemMenuPinEditor(null);
49
50 public EditorStack(EditorSashContainer editorArea, WorkbenchPage page) {
51 super(PresentationFactoryUtil.ROLE_EDITOR); //$NON-NLS-1$
52 this.editorArea = editorArea;
53 setID(this.toString());
54 // Each folder has a unique ID so relative positioning is unambiguous.
55 // save off a ref to the page
56 //@issue is it okay to do this??
57 //I think so since a ViewStack is
58 //not used on more than one page.
59 this.page = page;
60 }
61
62 /* (non-Javadoc)
63 * @see org.eclipse.ui.internal.PartStack#createControl(org.eclipse.swt.widgets.Composite, org.eclipse.ui.presentations.StackPresentation)
64 */
65 public void createControl(Composite parent, StackPresentation presentation) {
66 super.createControl(parent, presentation);
67
68 // Hack from Eclipse 2.1.3 to force initialization of the active editor
69 setSelection(getVisiblePart());
70 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.ui.internal.PartStack#getPage()
74 */
75 protected WorkbenchPage getPage() {
76 return page;
77 }
78
79 public void addSystemActions(IMenuManager menuManager) {
80 pinEditorItem = new SystemMenuPinEditor((EditorPane)getVisiblePart());
81 appendToGroupIfPossible(menuManager, "misc", new UpdatingActionContributionItem(pinEditorItem)); //$NON-NLS-1$
82 sizeItem = new SystemMenuSize((PartPane)getVisiblePart());
83 appendToGroupIfPossible(menuManager, "size", sizeItem); //$NON-NLS-1$
84 }
85
86 public boolean isMoveable(IPresentablePart part) {
87 return true;
88 }
89
90 public boolean isCloseable(IPresentablePart part) {
91 return true;
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.ui.presentations.IStackPresentationSite#supportsState(int)
96 */
97 public boolean supportsState(int state) {
98 if (page.isFixedLayout())
99 return false;
100 return state == IStackPresentationSite.STATE_MAXIMIZED || state == IStackPresentationSite.STATE_RESTORED;
101 }
102
103 /**
104 * Factory method for editor workbooks.
105 */
106 public static EditorStack newEditorWorkbook(EditorSashContainer editorArea,
107 WorkbenchPage page) {
108 return new EditorStack(editorArea, page);
109 }
110
111 protected void add(LayoutPart newChild, Object cookie) {
112 super.add(newChild, cookie);
113
114 ((EditorPane) newChild).setWorkbook(this);
115 }
116
117 /**
118 * See IVisualContainer#add
119 */
120 public void add(LayoutPart child) {
121 super.add(child);
122
123 if (child instanceof EditorPane) {
124 ((EditorPane) child).setWorkbook(this);
125 }
126 }
127
128 protected void updateActions() {
129 EditorPane pane = (EditorPane)getVisiblePart();
130
131 sizeItem.setPane(pane);
132 pinEditorItem.setPane(pane);
133 }
134
135 public Control[] getTabList() {
136 return getTabList(getVisiblePart());
137 }
138
139 public void removeAll() {
140 LayoutPart[] children = getChildren();
141
142 for (int i = 0; i < children.length; i++)
143 remove((EditorPane) children[i]);
144 }
145
146 public boolean isActiveWorkbook() {
147 EditorSashContainer area = getEditorArea();
148
149 if (area != null)
150 return area.isActiveWorkbook(this);
151 else
152 return false;
153 }
154
155 public void becomeActiveWorkbook(boolean hasFocus) {
156 EditorSashContainer area = getEditorArea();
157
158 if (area != null) area.setActiveWorkbook(this, hasFocus);
159 }
160
161 public EditorPane[] getEditors() {
162 LayoutPart[] children = getChildren();
163
164 EditorPane[] panes = new EditorPane[children.length];
165 for (int idx = 0; idx < children.length; idx++) {
166 panes[idx] = (EditorPane)children[idx];
167 }
168
169 return panes;
170 }
171
172 public EditorSashContainer getEditorArea() {
173 return editorArea;
174 }
175
176 public EditorPane getVisibleEditor() {
177 return (EditorPane) getVisiblePart();
178 }
179
180 /* (non-Javadoc)
181 * @see org.eclipse.ui.internal.PartStack#setSelection(org.eclipse.ui.internal.LayoutPart)
182 */
183 public void setSelection(LayoutPart part) {
184 // Hack from Eclipse 2.1.3 to force initialization of the active editor
185 if (!isDisposed() && part instanceof PartPane) {
186 Object result = ((PartPane)part).getPartReference().getPart(true);
187 if (result == null) {
188 part = null;
189 }
190 }
191
192 if (getVisiblePart() == part) {
193 return;
194 }
195
196 super.setSelection(part);
197 }
198
199 public void setVisibleEditor(EditorPane editorPane) {
200 setSelection(editorPane);
201 }
202
203 public void showVisibleEditor() {
204 }
205
206 /* (non-Javadoc)
207 * @see org.eclipse.ui.internal.PartStack#canMoveFolder()
208 */
209 protected boolean canMoveFolder() {
210 return true;
211 }
212
213 /* (non-Javadoc)
214 * @see org.eclipse.ui.internal.PartStack#derefPart(org.eclipse.ui.internal.LayoutPart)
215 */
216 protected void derefPart(LayoutPart toDeref) {
217 EditorAreaHelper.derefPart(toDeref);
218 }
219
220 /* (non-Javadoc)
221 * @see org.eclipse.ui.internal.PartStack#allowsDrop(org.eclipse.ui.internal.PartPane)
222 */
223 protected boolean allowsDrop(PartPane part) {
224 return part instanceof EditorPane;
225 }
226
227 public void setFocus() {
228 super.setFocus();
229 becomeActiveWorkbook(true);
230 }
231
232 /* (non-Javadoc)
233 * @see org.eclipse.ui.internal.PartStack#close(org.eclipse.ui.presentations.IPresentablePart[])
234 */
235 protected void close(IPresentablePart[] parts) {
236
237 if (parts.length == 1) {
238 close(parts[0]);
239 return;
240 }
241
242 IEditorReference[] toClose = new IEditorReference[parts.length];
243 for (int idx = 0; idx < parts.length; idx++) {
244 EditorPane part = (EditorPane)getPaneFor(parts[idx]);
245 toClose[idx] = part.getEditorReference();
246 }
247
248 WorkbenchPage page = getPage();
249
250 if (page != null) {
251 page.closeEditors(toClose, true);
252 }
253 }
254
255 /* (non-Javadoc)
256 * @see org.eclipse.ui.internal.LayoutPart#testInvariants()
257 */
258 public void testInvariants() {
259 super.testInvariants();
260
261 int active = getActive();
262
263 if (active == StackPresentation.AS_ACTIVE_FOCUS) {
264 Assert.isTrue(isActiveWorkbook());
265 } else if (active == StackPresentation.AS_ACTIVE_NOFOCUS) {
266 Assert.isTrue(isActiveWorkbook());
267 } else if (active == StackPresentation.AS_INACTIVE) {
268 Assert.isTrue(!isActiveWorkbook());
269 }
270 }
271
272 /* (non-Javadoc)
273 * @see org.eclipse.ui.internal.PartStack#restoreState(org.eclipse.ui.IMemento)
274 */
275 public IStatus restoreState(IMemento memento) {
276 Integer expanded = memento.getInteger(IWorkbenchConstants.TAG_EXPANDED);
277 setState((expanded == null || expanded.intValue() != IStackPresentationSite.STATE_MINIMIZED) ? IStackPresentationSite.STATE_RESTORED
278 : IStackPresentationSite.STATE_MINIMIZED);
279
280 Integer appearance = memento.getInteger(IWorkbenchConstants.TAG_APPEARANCE);
281 if (appearance != null) {
282 this.appearance = appearance.intValue();
283 }
284
285 // Determine if the presentation has saved any info here
286 savedPresentationState = null;
287 IMemento[] presentationMementos = memento.getChildren(IWorkbenchConstants.TAG_PRESENTATION);
288
289 for (int idx = 0; idx < presentationMementos.length; idx++) {
290 IMemento child = presentationMementos[idx];
291
292 String id = child.getString(IWorkbenchConstants.TAG_ID);
293
294 if (Util.equals(id, getFactory().getId())) {
295 savedPresentationState = child;
296 break;
297 }
298 }
299
300 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
301 }
302
303 /* (non-Javadoc)
304 * @see org.eclipse.ui.internal.PartStack#saveState(org.eclipse.ui.IMemento)
305 */
306 public IStatus saveState(IMemento memento) {
307 memento
308 .putInteger(
309 IWorkbenchConstants.TAG_EXPANDED,
310 (getPresentationSite().getState() == IStackPresentationSite.STATE_MINIMIZED) ? IStackPresentationSite.STATE_MINIMIZED
311 : IStackPresentationSite.STATE_RESTORED);
312
313 memento.putInteger(IWorkbenchConstants.TAG_APPEARANCE, appearance);
314
315 savePresentationState();
316
317 if (savedPresentationState != null) {
318 IMemento presentationState = memento.createChild(IWorkbenchConstants.TAG_PRESENTATION);
319 presentationState.putMemento(savedPresentationState);
320 }
321
322 return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
323 }
324 }