KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.internal;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.MultiStatus;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
23
24 /**
25  * Represents the top level container.
26  */

27 public class ViewSashContainer extends PartSashContainer {
28     public ViewSashContainer(WorkbenchPage page, Composite parent) {
29         super("root layout container", page, parent);//$NON-NLS-1$
30
}
31
32     /**
33      * Gets root container for this part.
34      */

35     public ViewSashContainer getRootContainer() {
36         return this;
37     }
38
39     /**
40      * Subclasses override this method to specify
41      * the composite to use to parent all children
42      * layout parts it contains.
43      */

44     protected Composite createParent(Composite parentWidget) {
45         return parentWidget;
46     }
47
48     /**
49      * Subclasses override this method to dispose
50      * of any swt resources created during createParent.
51      */

52     protected void disposeParent() {
53         // do nothing
54
}
55
56     /**
57      * Get the part control. This method may return null.
58      */

59     public Control getControl() {
60         return this.parent;
61     }
62
63     /**
64      * @see IPersistablePart
65      */

66     public IStatus restoreState(IMemento memento) {
67         MultiStatus result = new MultiStatus(
68                 PlatformUI.PLUGIN_ID,
69                 IStatus.OK,
70                 WorkbenchMessages.RootLayoutContainer_problemsRestoringPerspective, null);
71
72         // Read the info elements.
73
IMemento[] children = memento.getChildren(IWorkbenchConstants.TAG_INFO);
74
75         // Create a part ID to part hashtable.
76
final Map JavaDoc mapIDtoPart = new HashMap JavaDoc(children.length);
77
78         // Loop through the info elements.
79
for (int i = 0; i < children.length; i++) {
80             // Get the info details.
81
IMemento childMem = children[i];
82             String JavaDoc partID = childMem.getString(IWorkbenchConstants.TAG_PART);
83             final String JavaDoc relativeID = childMem
84                     .getString(IWorkbenchConstants.TAG_RELATIVE);
85             int relationship = 0;
86             float ratio = 0.0f;
87             int left = 0, right = 0;
88             if (relativeID != null) {
89                 relationship = childMem.getInteger(
90                         IWorkbenchConstants.TAG_RELATIONSHIP).intValue();
91
92                 // Note: the ratio is used for reading pre-3.0 eclipse workspaces. It should be ignored
93
// if "left" and "right" are available.
94
Float JavaDoc ratioFloat = childMem
95                         .getFloat(IWorkbenchConstants.TAG_RATIO);
96                 Integer JavaDoc leftInt = childMem
97                         .getInteger(IWorkbenchConstants.TAG_RATIO_LEFT);
98                 Integer JavaDoc rightInt = childMem
99                         .getInteger(IWorkbenchConstants.TAG_RATIO_RIGHT);
100                 if (leftInt != null && rightInt != null) {
101                     left = leftInt.intValue();
102                     right = rightInt.intValue();
103                 } else {
104                     if (ratioFloat != null) {
105                         ratio = ratioFloat.floatValue();
106                     }
107                 }
108             }
109             String JavaDoc strFolder = childMem
110                     .getString(IWorkbenchConstants.TAG_FOLDER);
111
112             // Create the part.
113
LayoutPart part = null;
114             if (strFolder == null) {
115                 part = new PartPlaceholder(partID);
116             } else {
117                 ViewStack folder = new ViewStack(page);
118                 folder.setID(partID);
119                 result.add(folder.restoreState(childMem
120                         .getChild(IWorkbenchConstants.TAG_FOLDER)));
121                 ContainerPlaceholder placeholder = new ContainerPlaceholder(
122                         partID);
123                 placeholder.setRealContainer(folder);
124                 part = placeholder;
125             }
126             // 1FUN70C: ITPUI:WIN - Shouldn't set Container when not active
127
part.setContainer(this);
128
129             final int myLeft = left, myRight= right, myRelationship = relationship;
130             final float myRatio = ratio;
131             final LayoutPart myPart = part;
132             
133             StartupThreading.runWithoutExceptions(new StartupRunnable() {
134
135                 public void runWithException() throws Throwable JavaDoc {
136                     // Add the part to the layout
137
if (relativeID == null) {
138                         add(myPart);
139                     } else {
140                         LayoutPart refPart = (LayoutPart) mapIDtoPart.get(relativeID);
141                         if (refPart != null) {
142                             if (myLeft != 0) {
143                                 add(myPart, myRelationship, myLeft, myRight, refPart);
144                             } else {
145                                 add(myPart, myRelationship, myRatio, refPart);
146                             }
147                         } else {
148                             WorkbenchPlugin
149                                     .log("Unable to find part for ID: " + relativeID);//$NON-NLS-1$
150
}
151                     }
152                 }});
153             
154             mapIDtoPart.put(partID, part);
155         }
156         return result;
157     }
158
159     /**
160      * @see IPersistablePart
161      */

162     public IStatus saveState(IMemento memento) {
163         RelationshipInfo[] relationships = computeRelation();
164
165         MultiStatus result = new MultiStatus(
166                 PlatformUI.PLUGIN_ID,
167                 IStatus.OK,
168                 WorkbenchMessages.RootLayoutContainer_problemsSavingPerspective, null);
169
170         // Loop through the relationship array.
171
for (int i = 0; i < relationships.length; i++) {
172             // Save the relationship info ..
173
// private LayoutPart part;
174
// private int relationship;
175
// private float ratio;
176
// private LayoutPart relative;
177
RelationshipInfo info = relationships[i];
178             IMemento childMem = memento
179                     .createChild(IWorkbenchConstants.TAG_INFO);
180             childMem.putString(IWorkbenchConstants.TAG_PART, info.part.getID());
181             if (info.relative != null) {
182                 childMem.putString(IWorkbenchConstants.TAG_RELATIVE,
183                         info.relative.getID());
184                 childMem.putInteger(IWorkbenchConstants.TAG_RELATIONSHIP,
185                         info.relationship);
186                 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_LEFT,
187                         info.left);
188                 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_RIGHT,
189                         info.right);
190
191                 // The ratio is only needed for saving workspaces that can be read by old versions
192
// of Eclipse. It is not used in newer versions of Eclipse, which use the "left"
193
// and "right" attributes instead.
194
childMem.putFloat(IWorkbenchConstants.TAG_RATIO, info
195                         .getRatio());
196             }
197
198             // Is this part a folder or a placeholder for one?
199
ViewStack folder = null;
200             if (info.part instanceof ViewStack) {
201                 folder = (ViewStack) info.part;
202             } else if (info.part instanceof ContainerPlaceholder) {
203                 LayoutPart part = ((ContainerPlaceholder) info.part)
204                         .getRealContainer();
205                 if (part instanceof ViewStack) {
206                     folder = (ViewStack) part;
207                 }
208             }
209
210             // If this is a folder (ViewStack) save the contents.
211
if (folder != null) {
212                 childMem.putString(IWorkbenchConstants.TAG_FOLDER, "true");//$NON-NLS-1$
213

214                 IMemento folderMem = childMem
215                         .createChild(IWorkbenchConstants.TAG_FOLDER);
216                 result.add(folder.saveState(folderMem));
217             }
218         }
219         return result;
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.ui.internal.PartSashContainer#getDockingRatio(org.eclipse.ui.internal.LayoutPart, org.eclipse.ui.internal.LayoutPart)
224      */

225     protected float getDockingRatio(LayoutPart dragged, LayoutPart target) {
226         if (isStackType(target)) {
227             return super.getDockingRatio(dragged, target);
228         } else {
229             return 0.25f;
230         }
231     }
232
233     /* (non-Javadoc)
234      * @see org.eclipse.ui.internal.PartSashContainer#isStackType(org.eclipse.ui.internal.LayoutPart)
235      */

236     public boolean isStackType(LayoutPart toTest) {
237         return (toTest instanceof ViewStack);
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.ui.internal.PartSashContainer#isPaneType(org.eclipse.ui.internal.LayoutPart)
242      */

243     public boolean isPaneType(LayoutPart toTest) {
244         return (toTest instanceof ViewPane);
245     }
246
247     /* (non-Javadoc)
248      * @see org.eclipse.ui.internal.PartSashContainer#createStack(org.eclipse.ui.internal.LayoutPart)
249      */

250     protected PartStack createStack() {
251         ViewStack result = new ViewStack(page);
252         return result;
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.ui.internal.PartSashContainer#setVisiblePart(org.eclipse.ui.internal.ILayoutContainer, org.eclipse.ui.internal.LayoutPart)
257      */

258     protected void setVisiblePart(ILayoutContainer container,
259             LayoutPart visiblePart) {
260         if (container instanceof ViewStack) {
261             ViewStack tabFolder = (ViewStack) container;
262
263             tabFolder.setSelection(visiblePart);
264         }
265     }
266
267     /* (non-Javadoc)
268      * @see org.eclipse.ui.internal.PartSashContainer#getVisiblePart(org.eclipse.ui.internal.ILayoutContainer)
269      */

270     protected LayoutPart getVisiblePart(ILayoutContainer container) {
271         return ((ViewStack) container).getSelection();
272     }
273
274     /* (non-Javadoc)
275      * @see org.eclipse.ui.internal.PartSashContainer#derefPart(org.eclipse.ui.internal.LayoutPart)
276      */

277     protected void derefPart(LayoutPart sourcePart) {
278         page.getActivePerspective().getPresentation().derefPart(sourcePart);
279     }
280
281     /* (non-Javadoc)
282      * @see org.eclipse.ui.internal.PartSashContainer#addChild(org.eclipse.ui.internal.PartSashContainer.RelationshipInfo)
283      */

284     protected void addChild(RelationshipInfo info) {
285         LayoutPart child = info.part;
286
287         // Nasty hack: ensure that all views end up inside a tab folder.
288
// Since the view title is provided by the tab folder, this ensures
289
// that views don't get created without a title tab.
290
if (child instanceof ViewPane) {
291             ViewStack folder = new ViewStack(page);
292             folder.add(child);
293             info.part = folder;
294         }
295
296         super.addChild(info);
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.ui.internal.ILayoutContainer#replace(org.eclipse.ui.internal.LayoutPart, org.eclipse.ui.internal.LayoutPart)
301      */

302     public void replace(LayoutPart oldChild, LayoutPart newChild) {
303         if (!isChild(oldChild)) {
304             return;
305         }
306
307         // Nasty hack: ensure that all views end up inside a tab folder.
308
// Since the view title is provided by the tab folder, this ensures
309
// that views don't get created without a title tab.
310
if (newChild instanceof ViewPane) {
311             ViewStack folder = new ViewStack(page);
312             folder.add(newChild);
313             newChild = folder;
314         }
315
316         super.replace(oldChild, newChild);
317     }
318 }
319
Popular Tags