KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Dan Rubel <dan_rubel@instantiations.com>
11  * - Fix for bug 11490 - define hidden view (placeholder for view) in plugin.xml
12  * Ted Stockwell <emorning@yahoo.com>
13  * - Fix for bug 63595 - IPageLayout.addFastView regression (3.0M8 to 3.0M9)
14  * Chris Gross <schtoo@schtoo.com>
15  * - Fix for 99155 - allow standalone view placeholders
16  * Chris Gross chris.gross@us.ibm.com Bug 107443
17  *******************************************************************************/

18 package org.eclipse.ui.internal;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.ui.IFolderLayout;
28 import org.eclipse.ui.IPageLayout;
29 import org.eclipse.ui.IPerspectiveDescriptor;
30 import org.eclipse.ui.IPlaceholderFolderLayout;
31 import org.eclipse.ui.IViewLayout;
32 import org.eclipse.ui.IViewReference;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.activities.WorkbenchActivityHelper;
35 import org.eclipse.ui.internal.presentations.PresentationFactoryUtil;
36 import org.eclipse.ui.internal.registry.ActionSetRegistry;
37 import org.eclipse.ui.internal.registry.IActionSetDescriptor;
38 import org.eclipse.ui.views.IViewDescriptor;
39 import org.eclipse.ui.views.IViewRegistry;
40
41 /**
42  * This factory is used to define the initial layout of a part sash container.
43  * <p>
44  * Design notes: The design of <code>IPageLayout</code> is a reflection of
45  * three requirements:
46  * <ol>
47  * <li>A mechanism is required to define the initial layout for a page. </li>
48  * <li>The views and editors within a page will be persisted between
49  * sessions.</li>
50  * <li>The view and editor lifecycle for (1) and (2) should be identical.</li>
51  * </ol>
52  * </p>
53  * <p>
54  * In reflection of these requirements, the following strategy has been
55  * implemented for layout definition.
56  * <ol>
57  * <li>A view extension is added to the workbench registry for the view.
58  * This extension defines the extension id and extension class. </li>
59  * <li>A view is added to a page by invoking one of the add methods
60  * in <code>IPageLayout</code>. The type of view is passed as an
61  * extension id, rather than a handle. The page layout will map
62  * the extension id to a view class, create an instance of the class,
63  * and then add the view to the page.</li>
64  * </ol>
65  * </p>
66  */

67 public class PageLayout implements IPageLayout {
68     private ArrayList JavaDoc actionSets = new ArrayList JavaDoc(3);
69
70     private IPerspectiveDescriptor descriptor;
71
72     private LayoutPart editorFolder;
73
74     private boolean editorVisible = true;
75
76     private boolean fixed;
77
78     private ArrayList JavaDoc fastViews = new ArrayList JavaDoc(3);
79
80     private Map JavaDoc mapIDtoFolder = new HashMap JavaDoc(10);
81
82     private Map JavaDoc mapIDtoPart = new HashMap JavaDoc(10);
83
84     private Map JavaDoc mapIDtoViewLayoutRec = new HashMap JavaDoc(10);
85     
86     private Map JavaDoc mapFolderToFolderLayout = new HashMap JavaDoc(10);
87
88     private ArrayList JavaDoc newWizardShortcuts = new ArrayList JavaDoc(3);
89
90     private ArrayList JavaDoc perspectiveShortcuts = new ArrayList JavaDoc(3);
91
92     private ViewSashContainer rootLayoutContainer;
93
94     private ArrayList JavaDoc showInPartIds = new ArrayList JavaDoc(3);
95
96     private ArrayList JavaDoc showViewShortcuts = new ArrayList JavaDoc(3);
97
98     private ViewFactory viewFactory;
99
100     private List JavaDoc minimizedStacks = new ArrayList JavaDoc();
101
102     /**
103      * Constructs a new PageLayout for other purposes.
104      */

105     public PageLayout() {
106         //no-op
107
}
108
109     /**
110      * Constructs a new PageLayout for the normal case of creating a new
111      * perspective.
112      */

113     public PageLayout(ViewSashContainer container, ViewFactory viewFactory,
114             LayoutPart editorFolder, IPerspectiveDescriptor descriptor) {
115         super();
116         this.viewFactory = viewFactory;
117         this.rootLayoutContainer = container;
118         this.editorFolder = editorFolder;
119         this.descriptor = descriptor;
120         prefill();
121     }
122
123     /**
124      * Adds the editor to a layout.
125      */

126     private void addEditorArea() {
127         try {
128             // Create the part.
129
LayoutPart newPart = createView(ID_EDITOR_AREA);
130             if (newPart == null) {
131                 // this should never happen as long as newID is the editor ID.
132
return;
133             }
134
135             setRefPart(ID_EDITOR_AREA, newPart);
136
137             // Add it to the layout.
138
rootLayoutContainer.add(newPart);
139         } catch (PartInitException e) {
140             WorkbenchPlugin.log(getClass(), "addEditorArea()", e); //$NON-NLS-1$
141
}
142     }
143
144     /**
145      * Adds an action set to the page.
146      *
147      * @param actionSetID Identifies the action set extension to use. It must
148      * exist within the workbench registry.
149      */

150     public void addActionSet(String JavaDoc actionSetID) {
151         if (!actionSets.contains(actionSetID)) {
152             actionSets.add(actionSetID);
153         }
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.ui.IPageLayout#addFastView(java.lang.String)
158      */

159     public void addFastView(String JavaDoc id) {
160         addFastView(id, INVALID_RATIO);
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.ui.IPageLayout#addFastView(java.lang.String, float)
165      */

166     public void addFastView(String JavaDoc id, float ratio) {
167         if (checkPartInLayout(id)) {
168             return;
169         }
170         if (id != null) {
171             try {
172                 IViewDescriptor viewDescriptor = viewFactory.getViewRegistry()
173                         .find(ViewFactory.extractPrimaryId(id));
174                 if (!WorkbenchActivityHelper.filterItem(viewDescriptor)) {
175                     IViewReference ref = viewFactory.createView(ViewFactory
176                             .extractPrimaryId(id), ViewFactory
177                             .extractSecondaryId(id));
178                     fastViews.add(ref);
179
180                     // force creation of the view layout rec
181
ViewLayoutRec rec = getViewLayoutRec(id, true);
182
183                     // remember the ratio, if valid
184
if (ratio >= IPageLayout.RATIO_MIN
185                             && ratio <= IPageLayout.RATIO_MAX) {
186                         rec.fastViewWidthRatio = ratio;
187                     }
188                 }
189             } catch (PartInitException e) {
190                 WorkbenchPlugin.log(getClass(), "addFastView", e); //$NON-NLS-1$
191
}
192         }
193     }
194
195     /**
196      * Check to see if the partId represents a fast view's id.
197      *
198      * @param partId
199      * The part's id.
200      * @return true if the partId is a fast view id.
201      */

202     private boolean isFastViewId(String JavaDoc partId) {
203         for (int i = 0; i < fastViews.size(); i++) {
204             IViewReference ref = (IViewReference) fastViews.get(i);
205             String JavaDoc secondaryId = ref.getSecondaryId();
206             String JavaDoc refId = (secondaryId == null ? ref.getId() : ref.getId()
207                     + ":" + secondaryId); //$NON-NLS-1$
208
if (refId.equals(partId)) {
209                 return true;
210             }
211         }
212         return false;
213     }
214
215     /**
216      * Returns the view layout record for the given view id, or null if not
217      * found. If create is true, the record is created if it doesn't already
218      * exist.
219      *
220      * @since 3.0
221      */

222     ViewLayoutRec getViewLayoutRec(String JavaDoc id, boolean create) {
223         ViewLayoutRec rec = (ViewLayoutRec) mapIDtoViewLayoutRec.get(id);
224         if (rec == null && create) {
225             rec = new ViewLayoutRec();
226             // set up the view layout appropriately if the page layout is fixed
227
if (isFixed()) {
228                 rec.isCloseable = false;
229                 rec.isMoveable = false;
230             }
231             mapIDtoViewLayoutRec.put(id, rec);
232         }
233         return rec;
234     }
235
236     /**
237      * Adds a creation wizard to the File New menu.
238      * The id must name a new wizard extension contributed to the
239      * workbench's extension point (named <code>"org.eclipse.ui.newWizards"</code>).
240      *
241      * @param id the wizard id
242      */

243     public void addNewWizardShortcut(String JavaDoc id) {
244         if (!newWizardShortcuts.contains(id)) {
245             newWizardShortcuts.add(id);
246         }
247     }
248
249     /**
250      * Add the layout part to the page's layout
251      */

252     private void addPart(LayoutPart newPart, String JavaDoc partId, int relationship,
253             float ratio, String JavaDoc refId) {
254
255         setRefPart(partId, newPart);
256
257         // If the referenced part is inside a folder,
258
// then use the folder as the reference part.
259
LayoutPart refPart = getFolderPart(refId);
260         if (refPart == null) {
261             refPart = getRefPart(refId);
262         }
263
264         // Add it to the layout.
265
if (refPart != null) {
266             ratio = normalizeRatio(ratio);
267             rootLayoutContainer.add(newPart, getPartSashConst(relationship),
268                     ratio, refPart);
269         } else {
270             WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_missingRefPart, refId ));
271             rootLayoutContainer.add(newPart);
272         }
273     }
274
275     /**
276      * Adds a perspective shortcut to the Perspective menu.
277      * The id must name a perspective extension contributed to the
278      * workbench's extension point (named <code>"org.eclipse.ui.perspectives"</code>).
279      *
280      * @param id the perspective id
281      */

282     public void addPerspectiveShortcut(String JavaDoc id) {
283         if (!perspectiveShortcuts.contains(id)) {
284             perspectiveShortcuts.add(id);
285         }
286     }
287
288     /* (non-Javadoc)
289      * @see org.eclipse.ui.IPageLayout#addPlaceholder(java.lang.String, int, float, java.lang.String)
290      */

291     public void addPlaceholder(String JavaDoc viewId, int relationship, float ratio,
292             String JavaDoc refId) {
293         if (!checkValidPlaceholderId(viewId)) {
294             return;
295         }
296
297         // Create the placeholder.
298
PartPlaceholder newPart = new PartPlaceholder(viewId);
299         addPart(newPart, viewId, relationship, ratio, refId);
300         // force creation of the view layout rec
301
getViewLayoutRec(viewId, true);
302     }
303
304     /**
305      * Checks whether the given id is a valid placeholder id.
306      * A placeholder id may be simple or compound, and can optionally contain a wildcard.
307      *
308      * @param id the placeholder id
309      * @return <code>true</code> if the given id is a valid placeholder id, <code>false</code> otherwise
310      */

311     boolean checkValidPlaceholderId(String JavaDoc id) {
312         // Check that view is not already in layout.
313
// This check is done even if the id has a wildcard, since it's incorrect to create
314
// multiple placeholders with the same id, wildcard or not.
315
if (checkPartInLayout(id)) {
316             return false;
317         }
318
319         // check that primary view id is valid, but only if it has no wildcard
320
String JavaDoc primaryId = ViewFactory.extractPrimaryId(id);
321         if (!ViewFactory.hasWildcard(primaryId)) {
322             IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry();
323             IViewDescriptor desc = reg.find(primaryId);
324             if (desc == null) {
325                 // cannot safely open the dialog so log the problem
326
WorkbenchPlugin.log("Unable to find view with id: " + primaryId + ", when creating perspective " + getDescriptor().getId()); //$NON-NLS-1$ //$NON-NLS-2$
327
return false;
328             }
329         }
330
331         return true;
332     }
333
334     /* (non-Javadoc)
335      * @see org.eclipse.ui.IPageLayout#addShowInPart(java.lang.String)
336      */

337     public void addShowInPart(String JavaDoc id) {
338         if (!showInPartIds.contains(id)) {
339             showInPartIds.add(id);
340         }
341     }
342
343     /**
344      * Adds a view to the Show View menu. The id must name a view extension
345      * contributed to the workbench's extension point (named <code>"org.eclipse.ui.views"</code>).
346      *
347      * @param id the view id
348      */

349     public void addShowViewShortcut(String JavaDoc id) {
350         if (!showViewShortcuts.contains(id)) {
351             showViewShortcuts.add(id);
352         }
353     }
354
355     /* (non-Javadoc)
356      * @see org.eclipse.ui.IPageLayout#addView(java.lang.String, int, float, java.lang.String)
357      */

358     public void addView(String JavaDoc viewId, int relationship, float ratio,
359             String JavaDoc refId) {
360         addView(viewId, relationship, ratio, refId, false, false, true);
361     }
362
363     /**
364      * Convenience method to allow setting the initial minimized
365      * state if a new stack is created. Used by the 'perspectiveExtension'
366      * reader.
367      *
368      * @since 3.3
369      */

370     public void addView(String JavaDoc viewId, int relationship, float ratio,
371             String JavaDoc refId, boolean minimized) {
372         addView(viewId, relationship, ratio, refId, minimized, false, true);
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.ui.IPageLayout#addView(java.lang.String, int, float, java.lang.String)
377      */

378     private void addView(String JavaDoc viewId, int relationship, float ratio,
379             String JavaDoc refId, boolean minimized, boolean standalone, boolean showTitle) {
380         if (checkPartInLayout(viewId)) {
381             return;
382         }
383
384         try {
385             // Create the part.
386
LayoutPart newPart = createView(viewId);
387             if (newPart == null) {
388                 addPlaceholder(viewId, relationship, ratio, refId);
389                 LayoutHelper.addViewActivator(this, viewId);
390             } else {
391                 int appearance = PresentationFactoryUtil.ROLE_VIEW;
392                 if (standalone) {
393                     if (showTitle) {
394                         appearance = PresentationFactoryUtil.ROLE_STANDALONE;
395                     } else {
396                         appearance = PresentationFactoryUtil.ROLE_STANDALONE_NOTITLE;
397                     }
398                 }
399
400                 ViewStack newFolder = new ViewStack(rootLayoutContainer.page,
401                         true, appearance, null);
402                 newFolder.add(newPart);
403                 setFolderPart(viewId, newFolder);
404                 addPart(newFolder, viewId, relationship, ratio, refId);
405                 // force creation of the view layout rec
406
getViewLayoutRec(viewId, true);
407                 
408                 // Capture any minimized stacks
409
if (minimized) {
410                     // Remember the minimized stacks so we can
411
// move them to the trim when the Perspective
412
// activates...
413
minimizedStacks.add(newFolder);
414                 }
415             }
416         } catch (PartInitException e) {
417             WorkbenchPlugin.log(getClass(), "addView", e); //$NON-NLS-1$
418
}
419     }
420
421     public List JavaDoc getMinimizedStacks() {
422         return minimizedStacks;
423     }
424     
425     /**
426      * Verify that the part is already present in the layout
427      * and cannot be added again. Log a warning message.
428      */

429     /*package*/
430     boolean checkPartInLayout(String JavaDoc partId) {
431         if (getRefPart(partId) != null || isFastViewId(partId)) {
432             WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_duplicateRefPart,partId ));
433             return true;
434         }
435
436         return false;
437     }
438
439     /* (non-Javadoc)
440      * @see org.eclipse.ui.IPageLayout#createFolder(java.lang.String, int, float, java.lang.String)
441      */

442     public IFolderLayout createFolder(String JavaDoc folderId, int relationship,
443             float ratio, String JavaDoc refId) {
444         if (checkPartInLayout(folderId)) {
445             ViewStack folder = (ViewStack) getRefPart(folderId);
446             
447             return (IFolderLayout) mapFolderToFolderLayout.get(folder);
448         }
449
450         // Create the folder.
451
ViewStack folder = new ViewStack(rootLayoutContainer.page);
452         folder.setID(folderId);
453         addPart(folder, folderId, relationship, ratio, refId);
454
455         // Create a wrapper.
456
FolderLayout layout = new FolderLayout(this, folder, viewFactory);
457         
458         mapFolderToFolderLayout.put(folder,layout);
459         
460         return layout;
461     }
462
463     /* (non-Javadoc)
464      * @see org.eclipse.ui.IPageLayout#createPlaceholderFolder(java.lang.String, int, float, java.lang.String)
465      */

466     public IPlaceholderFolderLayout createPlaceholderFolder(String JavaDoc folderId,
467             int relationship, float ratio, String JavaDoc refId) {
468         if (checkPartInLayout(folderId)) {
469             ContainerPlaceholder folder = (ContainerPlaceholder) getRefPart(folderId);
470             
471             return (IPlaceholderFolderLayout) mapFolderToFolderLayout.get(folder);
472         }
473
474         // Create the folder.
475
ContainerPlaceholder folder = new ContainerPlaceholder(null);
476         folder.setContainer(rootLayoutContainer);
477         folder.setRealContainer(new ViewStack(rootLayoutContainer.page));
478         folder.setID(folderId);
479         addPart(folder, folderId, relationship, ratio, refId);
480
481         // Create a wrapper.
482
IPlaceholderFolderLayout layout = new PlaceholderFolderLayout(this, folder);
483         
484         mapFolderToFolderLayout.put(folder,layout);
485         
486         return layout;
487     }
488
489     /**
490      * Create a new <code>LayoutPart</code>.
491      *
492      * @param partID the id of the part to create.
493      * @return the <code>LayoutPart</code>, or <code>null</code> if it should not be
494      * created because of activity filtering.
495      * @throws PartInitException thrown if there is a problem creating the part.
496      */

497     private LayoutPart createView(String JavaDoc partID) throws PartInitException {
498         if (partID.equals(ID_EDITOR_AREA)) {
499             return editorFolder;
500         }
501         IViewDescriptor viewDescriptor = viewFactory.getViewRegistry()
502                 .find(ViewFactory.extractPrimaryId(partID));
503         if (WorkbenchActivityHelper.filterItem(viewDescriptor)) {
504             return null;
505         }
506         return LayoutHelper.createView(getViewFactory(), partID);
507     }
508
509     /**
510      * @return the action set list for the page. This is <code>List</code> of
511      * <code>String</code>s.
512      */

513     public ArrayList JavaDoc getActionSets() {
514         return actionSets;
515     }
516
517     /* (non-Javadoc)
518      * @see org.eclipse.ui.IPageLayout#getDescriptor()
519      */

520     public IPerspectiveDescriptor getDescriptor() {
521         return descriptor;
522     }
523
524     /**
525      * @return an identifier for the editor area. The editor area is
526      * automatically added to each layout before any other part. It should be
527      * used as a reference part for other views.
528      */

529     public String JavaDoc getEditorArea() {
530         return ID_EDITOR_AREA;
531     }
532
533     /* (non-Javadoc)
534      * @see org.eclipse.ui.IPageLayout#getEditorReuseThreshold()
535      */

536     public int getEditorReuseThreshold() {
537         return -1;
538     }
539
540     /**
541      * @return <code>ArrayList</code>
542      */

543     public ArrayList JavaDoc getFastViews() {
544         return fastViews;
545     }
546
547     /**
548      * @return the folder part containing the given view ID or <code>null</code>
549      * if none (i.e. part of the page layout instead of a folder layout).
550      */

551     private ViewStack getFolderPart(String JavaDoc viewId) {
552         return (ViewStack) mapIDtoFolder.get(viewId);
553     }
554
555     /**
556      * @return the new wizard shortcuts associated with the page. This is a <code>List</code> of
557      * <code>String</code>s.
558      */

559     public ArrayList JavaDoc getNewWizardShortcuts() {
560         return newWizardShortcuts;
561     }
562
563     /**
564      * @return the part sash container const for a layout value.
565      */

566     private int getPartSashConst(int nRelationship) {
567         return nRelationship;
568     }
569
570     /**
571      * @return the perspective shortcuts associated with the page. This is a <code>List</code> of
572      * <code>String</code>s.
573      */

574     public ArrayList JavaDoc getPerspectiveShortcuts() {
575         return perspectiveShortcuts;
576     }
577
578     /**
579      * @return the part for a given ID.
580      */

581     /*package*/
582     LayoutPart getRefPart(String JavaDoc partID) {
583         return (LayoutPart) mapIDtoPart.get(partID);
584     }
585
586     /**
587      * @return the top level layout container.
588      */

589     public ViewSashContainer getRootLayoutContainer() {
590         return rootLayoutContainer;
591     }
592
593     /**
594      * @return the ids of the parts to list in the Show In... prompter. This is
595      * a <code>List</code> of <code>String</code>s.
596      */

597     public ArrayList JavaDoc getShowInPartIds() {
598         return showInPartIds;
599     }
600
601     /**
602      * @return the show view shortcuts associated with the page. This is a <code>List</code> of
603      * <code>String</code>s.
604      */

605     public ArrayList JavaDoc getShowViewShortcuts() {
606         return showViewShortcuts;
607     }
608
609     /**
610      * @return the <code>ViewFactory</code> for this <code>PageLayout</code>.
611      * @since 3.0
612      */

613     /* package */
614     ViewFactory getViewFactory() {
615         return viewFactory;
616     }
617
618     /* (non-Javadoc)
619      * @see org.eclipse.ui.IPageLayout#isEditorAreaVisible()
620      */

621     public boolean isEditorAreaVisible() {
622         return editorVisible;
623     }
624
625     /**
626      * Trim the ratio so that direct manipulation of parts is easy.
627      *
628      * @param in the initial ratio.
629      * @return the normalized ratio.
630      */

631     private float normalizeRatio(float in) {
632         if (in < RATIO_MIN) {
633             in = RATIO_MIN;
634         }
635         if (in > RATIO_MAX) {
636             in = RATIO_MAX;
637         }
638         return in;
639     }
640
641     /**
642      * Prefill the layout with required parts.
643      */

644     private void prefill() {
645         addEditorArea();
646
647         // Add default action sets.
648
ActionSetRegistry reg = WorkbenchPlugin.getDefault()
649                 .getActionSetRegistry();
650         IActionSetDescriptor[] array = reg.getActionSets();
651         int count = array.length;
652         for (int nX = 0; nX < count; nX++) {
653             IActionSetDescriptor desc = array[nX];
654             if (desc.isInitiallyVisible()) {
655                 addActionSet(desc.getId());
656             }
657         }
658     }
659
660     /* (non-Javadoc)
661      * @see org.eclipse.ui.IPageLayout#setEditorAreaVisible(boolean)
662      */

663     public void setEditorAreaVisible(boolean showEditorArea) {
664         editorVisible = showEditorArea;
665     }
666
667     /* (non-Javadoc)
668      * @see org.eclipse.ui.IPageLayout#setEditorReuseThreshold(int)
669      */

670     public void setEditorReuseThreshold(int openEditors) {
671         //no-op
672
}
673
674     /* (non-Javadoc)
675      * @see org.eclipse.ui.IPageLayout#setFixed(boolean)
676      */

677     public void setFixed(boolean fixed) {
678         this.fixed = fixed;
679     }
680
681     /* (non-Javadoc)
682      * @see org.eclipse.ui.IPageLayout#getFixed()
683      */

684     public boolean isFixed() {
685         return fixed;
686     }
687
688     /**
689      * Map the folder part containing the given view ID.
690      *
691      * @param viewId the part ID.
692      * @param container the <code>ContainerPlaceholder</code>.
693      */

694     /*package*/
695     void setFolderPart(String JavaDoc viewId, ContainerPlaceholder container) {
696         LayoutPart tabFolder = container.getRealContainer();
697         mapIDtoFolder.put(viewId, tabFolder);
698     }
699
700     /**
701      * Map the folder part containing the given view ID.
702      *
703      * @param viewId the part ID.
704      * @param folder the <code>ViewStack</code>.
705      */

706     /*package*/
707     void setFolderPart(String JavaDoc viewId, ViewStack folder) {
708         mapIDtoFolder.put(viewId, folder);
709     }
710
711     /**
712      * Map an ID to a part.
713      *
714      * @param partId the part ID.
715      * @param part the <code>LayoutPart</code>.
716      */

717     /*package*/
718     void setRefPart(String JavaDoc partID, LayoutPart part) {
719         mapIDtoPart.put(partID, part);
720     }
721
722     // stackPart(Layoutpart, String, String) added by dan_rubel@instantiations.com
723
/**
724      * Stack a part on top of another.
725      *
726      * @param newPart the new part.
727      * @param viewId the view ID.
728      * @param refId the reference ID.
729      */

730     private void stackPart(LayoutPart newPart, String JavaDoc viewId, String JavaDoc refId) {
731         setRefPart(viewId, newPart);
732         // force creation of the view layout rec
733
getViewLayoutRec(viewId, true);
734
735         // If ref part is in a folder than just add the
736
// new view to that folder.
737
ViewStack folder = getFolderPart(refId);
738         if (folder != null) {
739             folder.add(newPart);
740             setFolderPart(viewId, folder);
741             return;
742         }
743
744         // If the ref part is in the page layout then create
745
// a new folder and add the new view.
746
LayoutPart refPart = getRefPart(refId);
747         if (refPart != null && (refPart instanceof PartPane || refPart instanceof PartPlaceholder)) {
748             ViewStack newFolder = new ViewStack(rootLayoutContainer.page);
749             rootLayoutContainer.replace(refPart, newFolder);
750             newFolder.add(refPart);
751             newFolder.add(newPart);
752             setFolderPart(refId, newFolder);
753             setFolderPart(viewId, newFolder);
754             return;
755         }
756
757         // If ref part is not found then just do add.
758
WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.PageLayout_missingRefPart, refId ));
759         rootLayoutContainer.add(newPart);
760     }
761
762     // stackPlaceholder(String, String) added by dan_rubel@instantiations.com
763
/**
764      * Stack a placeholder on top of another.
765      *
766      * @param viewId the view ID.
767      * @param refId the reference ID.
768      */

769     public void stackPlaceholder(String JavaDoc viewId, String JavaDoc refId) {
770         if (checkPartInLayout(viewId)) {
771             return;
772         }
773
774         // Create the placeholder.
775
PartPlaceholder newPart = new PartPlaceholder(viewId);
776
777         LayoutPart refPart = getRefPart(refId);
778         if (refPart != null) {
779             newPart.setContainer(refPart.getContainer());
780         }
781
782         stackPart(newPart, viewId, refId);
783     }
784
785     // stackView(String, String) modified by dan_rubel@instantiations.com
786
/**
787      * Stack one view on top of another.
788      *
789      * @param viewId the view ID.
790      * @param refId the reference ID.
791      */

792     public void stackView(String JavaDoc viewId, String JavaDoc refId) {
793         if (checkPartInLayout(viewId)) {
794             return;
795         }
796
797         // Create the new part.
798
try {
799             LayoutPart newPart = createView(viewId);
800             if (newPart == null) {
801                 stackPlaceholder(viewId, refId);
802                 LayoutHelper.addViewActivator(this, viewId);
803             } else {
804                 stackPart(newPart, viewId, refId);
805             }
806         } catch (PartInitException e) {
807             WorkbenchPlugin.log(getClass(), "stackView", e); //$NON-NLS-1$
808
}
809     }
810
811     /**
812      * Converts SWT position constants into layout position constants.
813      *
814      * @param swtConstant one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT
815      * @return one of IPageLayout.TOP, IPageLayout.BOTTOM, IPageLayout.LEFT, IPageLayout.RIGHT, or -1 indicating an
816      * invalid input
817      *
818      * @since 3.0
819      */

820     public static int swtConstantToLayoutPosition(int swtConstant) {
821         switch (swtConstant) {
822         case SWT.TOP:
823             return IPageLayout.TOP;
824         case SWT.BOTTOM:
825             return IPageLayout.BOTTOM;
826         case SWT.RIGHT:
827             return IPageLayout.RIGHT;
828         case SWT.LEFT:
829             return IPageLayout.LEFT;
830         }
831
832         return -1;
833     }
834
835     /* (non-Javadoc)
836      * @see org.eclipse.ui.IPageLayout#addStandaloneView(java.lang.String, boolean, int, float, java.lang.String)
837      * @since 3.0
838      */

839     public void addStandaloneView(String JavaDoc viewId, boolean showTitle,
840             int relationship, float ratio, String JavaDoc refId) {
841         addView(viewId, relationship, ratio, refId, false, true, showTitle);
842         ViewLayoutRec rec = getViewLayoutRec(viewId, true);
843         rec.isStandalone = true;
844         rec.showTitle = showTitle;
845     }
846     
847     /* (non-Javadoc)
848      * @see org.eclipse.ui.IPageLayout#addStandaloneViewPlaceholder(java.lang.String, int, float, java.lang.String, boolean)
849      */

850     public void addStandaloneViewPlaceholder(String JavaDoc viewId, int relationship,
851             float ratio, String JavaDoc refId, boolean showTitle) {
852
853         String JavaDoc stackId = viewId + ".standalonefolder"; //$NON-NLS-1$
854

855         // Check to see if the view is already in the layout
856
if (!checkValidPlaceholderId(viewId)) {
857             return;
858         }
859
860         // Create the folder.
861
ContainerPlaceholder folder = new ContainerPlaceholder(null);
862         folder.setContainer(rootLayoutContainer);
863         int appearance = PresentationFactoryUtil.ROLE_STANDALONE;
864         if (!showTitle) {
865             appearance = PresentationFactoryUtil.ROLE_STANDALONE_NOTITLE;
866         }
867         folder.setRealContainer(new ViewStack(rootLayoutContainer.page, true,
868                 appearance, null));
869         folder.setID(stackId);
870         addPart(folder, stackId, relationship, ratio, refId);
871
872         // Create a wrapper.
873
PlaceholderFolderLayout placeHolder = new PlaceholderFolderLayout(this,
874                 folder);
875
876         // Add the standalone view immediately
877
placeHolder.addPlaceholder(viewId);
878
879         ViewLayoutRec rec = getViewLayoutRec(viewId, true);
880         rec.isStandalone = true;
881         rec.showTitle = showTitle;
882     }
883
884
885     /*
886      * (non-Javadoc)
887      *
888      * @see org.eclipse.ui.IPageLayout#getViewLayout(java.lang.String)
889      * @since 3.0
890      */

891     public IViewLayout getViewLayout(String JavaDoc viewId) {
892         ViewLayoutRec rec = getViewLayoutRec(viewId, true);
893         if (rec == null) {
894             return null;
895         }
896         return new ViewLayout(this, rec);
897     }
898
899     /**
900      * @since 3.0
901      */

902     public Map JavaDoc getIDtoViewLayoutRecMap() {
903         return mapIDtoViewLayoutRec;
904     }
905
906     /**
907      * Removes any existing placeholder with the given id.
908      *
909      * @param id the id for the placeholder
910      * @since 3.1
911      */

912     public void removePlaceholder(String JavaDoc id) {
913         LayoutPart part = getRefPart(id);
914         if (part instanceof PartPlaceholder) {
915             ViewStack stack = getFolderPart(id);
916             if (stack != null) {
917                 stack.remove(part);
918             }
919             else {
920                 rootLayoutContainer.remove(part);
921             }
922             mapIDtoPart.remove(id);
923             mapIDtoFolder.remove(id);
924             mapIDtoViewLayoutRec.remove(id);
925         }
926     }
927     
928     /* (non-Javadoc)
929      * @see org.eclipse.ui.IPageLayout#getFolderForView(java.lang.String)
930      */

931     public IPlaceholderFolderLayout getFolderForView(String JavaDoc viewId)
932     {
933         if (!mapIDtoFolder.containsKey(viewId))
934             return null;
935                 
936         ViewStack folder = (ViewStack) mapIDtoFolder.get(viewId);
937         IPlaceholderFolderLayout layout = null;
938         if (!mapFolderToFolderLayout.containsKey(folder))
939         {
940             layout = new FolderLayout(this, folder, viewFactory);
941             mapFolderToFolderLayout.put(folder, layout);
942         }
943         else
944         {
945             layout = (IPlaceholderFolderLayout)mapFolderToFolderLayout.get(folder);
946         }
947         return layout;
948     }
949 }
950
Popular Tags