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.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.MultiStatus;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
022:
023: /**
024: * Represents the top level container.
025: */
026: public class ViewSashContainer extends PartSashContainer {
027: public ViewSashContainer(WorkbenchPage page, Composite parent) {
028: super ("root layout container", page, parent);//$NON-NLS-1$
029: }
030:
031: /**
032: * Gets root container for this part.
033: */
034: public ViewSashContainer getRootContainer() {
035: return this ;
036: }
037:
038: /**
039: * Subclasses override this method to specify
040: * the composite to use to parent all children
041: * layout parts it contains.
042: */
043: protected Composite createParent(Composite parentWidget) {
044: return parentWidget;
045: }
046:
047: /**
048: * Subclasses override this method to dispose
049: * of any swt resources created during createParent.
050: */
051: protected void disposeParent() {
052: // do nothing
053: }
054:
055: /**
056: * Get the part control. This method may return null.
057: */
058: public Control getControl() {
059: return this .parent;
060: }
061:
062: /**
063: * @see IPersistablePart
064: */
065: public IStatus restoreState(IMemento memento) {
066: MultiStatus result = new MultiStatus(
067: PlatformUI.PLUGIN_ID,
068: IStatus.OK,
069: WorkbenchMessages.RootLayoutContainer_problemsRestoringPerspective,
070: null);
071:
072: // Read the info elements.
073: IMemento[] children = memento
074: .getChildren(IWorkbenchConstants.TAG_INFO);
075:
076: // Create a part ID to part hashtable.
077: final Map mapIDtoPart = new HashMap(children.length);
078:
079: // Loop through the info elements.
080: for (int i = 0; i < children.length; i++) {
081: // Get the info details.
082: IMemento childMem = children[i];
083: String partID = childMem
084: .getString(IWorkbenchConstants.TAG_PART);
085: final String relativeID = childMem
086: .getString(IWorkbenchConstants.TAG_RELATIVE);
087: int relationship = 0;
088: float ratio = 0.0f;
089: int left = 0, right = 0;
090: if (relativeID != null) {
091: relationship = childMem.getInteger(
092: IWorkbenchConstants.TAG_RELATIONSHIP)
093: .intValue();
094:
095: // Note: the ratio is used for reading pre-3.0 eclipse workspaces. It should be ignored
096: // if "left" and "right" are available.
097: Float ratioFloat = childMem
098: .getFloat(IWorkbenchConstants.TAG_RATIO);
099: Integer leftInt = childMem
100: .getInteger(IWorkbenchConstants.TAG_RATIO_LEFT);
101: Integer rightInt = childMem
102: .getInteger(IWorkbenchConstants.TAG_RATIO_RIGHT);
103: if (leftInt != null && rightInt != null) {
104: left = leftInt.intValue();
105: right = rightInt.intValue();
106: } else {
107: if (ratioFloat != null) {
108: ratio = ratioFloat.floatValue();
109: }
110: }
111: }
112: String strFolder = childMem
113: .getString(IWorkbenchConstants.TAG_FOLDER);
114:
115: // Create the part.
116: LayoutPart part = null;
117: if (strFolder == null) {
118: part = new PartPlaceholder(partID);
119: } else {
120: ViewStack folder = new ViewStack(page);
121: folder.setID(partID);
122: result.add(folder.restoreState(childMem
123: .getChild(IWorkbenchConstants.TAG_FOLDER)));
124: ContainerPlaceholder placeholder = new ContainerPlaceholder(
125: partID);
126: placeholder.setRealContainer(folder);
127: part = placeholder;
128: }
129: // 1FUN70C: ITPUI:WIN - Shouldn't set Container when not active
130: part.setContainer(this );
131:
132: final int myLeft = left, myRight = right, myRelationship = relationship;
133: final float myRatio = ratio;
134: final LayoutPart myPart = part;
135:
136: StartupThreading
137: .runWithoutExceptions(new StartupRunnable() {
138:
139: public void runWithException() throws Throwable {
140: // Add the part to the layout
141: if (relativeID == null) {
142: add(myPart);
143: } else {
144: LayoutPart refPart = (LayoutPart) mapIDtoPart
145: .get(relativeID);
146: if (refPart != null) {
147: if (myLeft != 0) {
148: add(myPart, myRelationship,
149: myLeft, myRight,
150: refPart);
151: } else {
152: add(myPart, myRelationship,
153: myRatio, refPart);
154: }
155: } else {
156: WorkbenchPlugin
157: .log("Unable to find part for ID: " + relativeID);//$NON-NLS-1$
158: }
159: }
160: }
161: });
162:
163: mapIDtoPart.put(partID, part);
164: }
165: return result;
166: }
167:
168: /**
169: * @see IPersistablePart
170: */
171: public IStatus saveState(IMemento memento) {
172: RelationshipInfo[] relationships = computeRelation();
173:
174: MultiStatus result = new MultiStatus(
175: PlatformUI.PLUGIN_ID,
176: IStatus.OK,
177: WorkbenchMessages.RootLayoutContainer_problemsSavingPerspective,
178: null);
179:
180: // Loop through the relationship array.
181: for (int i = 0; i < relationships.length; i++) {
182: // Save the relationship info ..
183: // private LayoutPart part;
184: // private int relationship;
185: // private float ratio;
186: // private LayoutPart relative;
187: RelationshipInfo info = relationships[i];
188: IMemento childMem = memento
189: .createChild(IWorkbenchConstants.TAG_INFO);
190: childMem.putString(IWorkbenchConstants.TAG_PART, info.part
191: .getID());
192: if (info.relative != null) {
193: childMem.putString(IWorkbenchConstants.TAG_RELATIVE,
194: info.relative.getID());
195: childMem.putInteger(
196: IWorkbenchConstants.TAG_RELATIONSHIP,
197: info.relationship);
198: childMem.putInteger(IWorkbenchConstants.TAG_RATIO_LEFT,
199: info.left);
200: childMem
201: .putInteger(
202: IWorkbenchConstants.TAG_RATIO_RIGHT,
203: info.right);
204:
205: // The ratio is only needed for saving workspaces that can be read by old versions
206: // of Eclipse. It is not used in newer versions of Eclipse, which use the "left"
207: // and "right" attributes instead.
208: childMem.putFloat(IWorkbenchConstants.TAG_RATIO, info
209: .getRatio());
210: }
211:
212: // Is this part a folder or a placeholder for one?
213: ViewStack folder = null;
214: if (info.part instanceof ViewStack) {
215: folder = (ViewStack) info.part;
216: } else if (info.part instanceof ContainerPlaceholder) {
217: LayoutPart part = ((ContainerPlaceholder) info.part)
218: .getRealContainer();
219: if (part instanceof ViewStack) {
220: folder = (ViewStack) part;
221: }
222: }
223:
224: // If this is a folder (ViewStack) save the contents.
225: if (folder != null) {
226: childMem.putString(IWorkbenchConstants.TAG_FOLDER,
227: "true");//$NON-NLS-1$
228:
229: IMemento folderMem = childMem
230: .createChild(IWorkbenchConstants.TAG_FOLDER);
231: result.add(folder.saveState(folderMem));
232: }
233: }
234: return result;
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.ui.internal.PartSashContainer#getDockingRatio(org.eclipse.ui.internal.LayoutPart, org.eclipse.ui.internal.LayoutPart)
239: */
240: protected float getDockingRatio(LayoutPart dragged,
241: LayoutPart target) {
242: if (isStackType(target)) {
243: return super .getDockingRatio(dragged, target);
244: } else {
245: return 0.25f;
246: }
247: }
248:
249: /* (non-Javadoc)
250: * @see org.eclipse.ui.internal.PartSashContainer#isStackType(org.eclipse.ui.internal.LayoutPart)
251: */
252: public boolean isStackType(LayoutPart toTest) {
253: return (toTest instanceof ViewStack);
254: }
255:
256: /* (non-Javadoc)
257: * @see org.eclipse.ui.internal.PartSashContainer#isPaneType(org.eclipse.ui.internal.LayoutPart)
258: */
259: public boolean isPaneType(LayoutPart toTest) {
260: return (toTest instanceof ViewPane);
261: }
262:
263: /* (non-Javadoc)
264: * @see org.eclipse.ui.internal.PartSashContainer#createStack(org.eclipse.ui.internal.LayoutPart)
265: */
266: protected PartStack createStack() {
267: ViewStack result = new ViewStack(page);
268: return result;
269: }
270:
271: /* (non-Javadoc)
272: * @see org.eclipse.ui.internal.PartSashContainer#setVisiblePart(org.eclipse.ui.internal.ILayoutContainer, org.eclipse.ui.internal.LayoutPart)
273: */
274: protected void setVisiblePart(ILayoutContainer container,
275: LayoutPart visiblePart) {
276: if (container instanceof ViewStack) {
277: ViewStack tabFolder = (ViewStack) container;
278:
279: tabFolder.setSelection(visiblePart);
280: }
281: }
282:
283: /* (non-Javadoc)
284: * @see org.eclipse.ui.internal.PartSashContainer#getVisiblePart(org.eclipse.ui.internal.ILayoutContainer)
285: */
286: protected LayoutPart getVisiblePart(ILayoutContainer container) {
287: return ((ViewStack) container).getSelection();
288: }
289:
290: /* (non-Javadoc)
291: * @see org.eclipse.ui.internal.PartSashContainer#derefPart(org.eclipse.ui.internal.LayoutPart)
292: */
293: protected void derefPart(LayoutPart sourcePart) {
294: page.getActivePerspective().getPresentation().derefPart(
295: sourcePart);
296: }
297:
298: /* (non-Javadoc)
299: * @see org.eclipse.ui.internal.PartSashContainer#addChild(org.eclipse.ui.internal.PartSashContainer.RelationshipInfo)
300: */
301: protected void addChild(RelationshipInfo info) {
302: LayoutPart child = info.part;
303:
304: // Nasty hack: ensure that all views end up inside a tab folder.
305: // Since the view title is provided by the tab folder, this ensures
306: // that views don't get created without a title tab.
307: if (child instanceof ViewPane) {
308: ViewStack folder = new ViewStack(page);
309: folder.add(child);
310: info.part = folder;
311: }
312:
313: super .addChild(info);
314: }
315:
316: /* (non-Javadoc)
317: * @see org.eclipse.ui.internal.ILayoutContainer#replace(org.eclipse.ui.internal.LayoutPart, org.eclipse.ui.internal.LayoutPart)
318: */
319: public void replace(LayoutPart oldChild, LayoutPart newChild) {
320: if (!isChild(oldChild)) {
321: return;
322: }
323:
324: // Nasty hack: ensure that all views end up inside a tab folder.
325: // Since the view title is provided by the tab folder, this ensures
326: // that views don't get created without a title tab.
327: if (newChild instanceof ViewPane) {
328: ViewStack folder = new ViewStack(page);
329: folder.add(newChild);
330: newChild = folder;
331: }
332:
333: super.replace(oldChild, newChild);
334: }
335: }
|