KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.MultiStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.DropTarget;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
30 import org.eclipse.ui.internal.presentations.PresentationSerializer;
31 import org.eclipse.ui.presentations.IStackPresentationSite;
32 import org.eclipse.ui.presentations.StackPresentation;
33
34 /**
35  * Represents the area set aside for editor workbooks.
36  * This container only accepts EditorStack and PartSash
37  * as layout parts.
38  *
39  * Note no views are allowed within this container.
40  */

41 public class EditorSashContainer extends PartSashContainer {
42
43     static final String JavaDoc DEFAULT_WORKBOOK_ID = "DefaultEditorWorkbook";//$NON-NLS-1$
44

45     private ArrayList JavaDoc editorWorkbooks = new ArrayList JavaDoc(3);
46
47     private EditorStack activeEditorWorkbook;
48
49     private DropTarget dropTarget;
50
51     public EditorSashContainer(String JavaDoc editorId, WorkbenchPage page, Composite parent) {
52         super(editorId, page, parent);
53
54         createDefaultWorkbook();
55     }
56
57     
58     /**
59      * Add an editor to the active workbook.
60      */

61     public void addEditor(EditorPane pane, EditorStack stack) {
62         //EditorStack workbook = getActiveWorkbook();
63
stack.add(pane);
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.ui.internal.PartSashContainer#addChild(org.eclipse.ui.internal.PartSashContainer.RelationshipInfo)
68      */

69     protected void addChild(RelationshipInfo info) {
70         super.addChild(info);
71         
72         updateStackButtons();
73     }
74     
75     /**
76      * Hides the min/max buttons for all editor stacks
77      * -except- for the upper/left one.
78      */

79     public void updateStackButtons() {
80          // This is applicable only when the new
81
// min/max behaviour is being used
82
Perspective persp = getPage().getActivePerspective();
83         if (!Perspective.useNewMinMax(persp))
84             return;
85         
86         // Find the upper Right editor stack
87
LayoutPart[] stacks = getChildren();
88         EditorStack winner = getUpperRightEditorStack(stacks);
89         
90         // Now hide the buttons for all but the upper right stack
91
for (int i = 0; i < stacks.length; i++) {
92             if (!(stacks[i] instanceof EditorStack))
93                 continue;
94             ((EditorStack)stacks[i]).showMinMax(stacks[i] == winner);
95         }
96         
97         // Force the stack's presentation state to match its perspective
98
persp.refreshEditorAreaVisibility();
99     }
100
101     /**
102      * @param stacks
103      * @return the EditorStack in the upper right position
104      */

105     public EditorStack getUpperRightEditorStack(LayoutPart[] stacks) {
106         if (stacks == null)
107             stacks = getChildren();
108         
109         // Find the upper Right editor stack
110
EditorStack winner = null;
111         Rectangle winnerRect = null;
112
113         for (int i = 0; i < stacks.length; i++) {
114             if (!(stacks[i] instanceof EditorStack))
115                 continue;
116             
117             EditorStack stack = (EditorStack) stacks[i];
118             Rectangle bb = stack.getBounds();
119             if (winnerRect == null ||
120                 bb.y < winnerRect.y ||
121                 (bb.y == winnerRect.y && bb.x > winnerRect.x)) {
122                 winner = stack;
123                 winnerRect = bb;
124             }
125         }
126         
127         return winner;
128     }
129
130     /**
131      * Notification that a child layout part has been
132      * added to the container. Subclasses may override
133      * this method to perform any container specific
134      * work.
135      */

136     protected void childAdded(LayoutPart child) {
137         super.childAdded(child);
138         
139         if (child instanceof EditorStack) {
140             editorWorkbooks.add(child);
141         }
142     }
143
144     /**
145      * Notification that a child layout part has been
146      * removed from the container. Subclasses may override
147      * this method to perform any container specific
148      * work.
149      */

150     protected void childRemoved(LayoutPart child) {
151         super.childRemoved(child);
152         
153         if (child instanceof EditorStack) {
154             editorWorkbooks.remove(child);
155             if (activeEditorWorkbook == child) {
156                 setActiveWorkbook(null, false);
157             }
158             
159             updateStackButtons();
160         }
161     }
162
163     protected EditorStack createDefaultWorkbook() {
164         EditorStack newWorkbook = EditorStack.newEditorWorkbook(this, page);
165         newWorkbook.setID(DEFAULT_WORKBOOK_ID);
166         add(newWorkbook);
167         return newWorkbook;
168     }
169
170     /**
171      * Subclasses override this method to specify
172      * the composite to use to parent all children
173      * layout parts it contains.
174      */

175     protected Composite createParent(Composite parentWidget) {
176         return new Composite(parentWidget, SWT.NONE);
177     }
178
179     /**
180      * Dispose of the editor area.
181      */

182     public void dispose() {
183         // Free editor workbooks.
184
editorWorkbooks.clear();
185
186         // Free rest.
187
super.dispose();
188     }
189
190     /**
191      * Subclasses override this method to dispose
192      * of any swt resources created during createParent.
193      */

194     protected void disposeParent() {
195         this.parent.dispose();
196     }
197
198     /**
199      * Return the editor workbook which is active.
200      */

201     public EditorStack getActiveWorkbook() {
202         if (activeEditorWorkbook == null) {
203             if (editorWorkbooks.size() < 1) {
204                 setActiveWorkbook(createDefaultWorkbook(), false);
205             } else {
206                 setActiveWorkbook((EditorStack) editorWorkbooks.get(0), false);
207             }
208         }
209
210         return activeEditorWorkbook;
211     }
212
213     /**
214      * Return the editor workbook id which is active.
215      */

216     public String JavaDoc getActiveWorkbookID() {
217         return getActiveWorkbook().getID();
218     }
219
220     /**
221      * Return the all the editor workbooks.
222      */

223     public ArrayList JavaDoc getEditorWorkbooks() {
224         return (ArrayList JavaDoc) editorWorkbooks.clone();
225     }
226
227     /**
228      * Return the all the editor workbooks.
229      */

230     public int getEditorWorkbookCount() {
231         return editorWorkbooks.size();
232     }
233
234     /**
235      * Return true is the workbook specified
236      * is the active one.
237      */

238     protected boolean isActiveWorkbook(EditorStack workbook) {
239         return activeEditorWorkbook == workbook;
240     }
241
242     /**
243      * Find the sashs around the specified part.
244      */

245     public void findSashes(LayoutPart pane, PartPane.Sashes sashes) {
246         //Find the sashes around the current editor and
247
//then the sashes around the editor area.
248
super.findSashes(pane, sashes);
249
250         ILayoutContainer container = getContainer();
251         if (container != null) {
252             container.findSashes(this, sashes);
253         }
254     }
255
256     /**
257      * Remove all the editors
258      */

259     public void removeAllEditors() {
260         EditorStack currentWorkbook = getActiveWorkbook();
261
262         // Iterate over a copy so the original can be modified.
263
Iterator JavaDoc workbooks = ((ArrayList JavaDoc) editorWorkbooks.clone()).iterator();
264         while (workbooks.hasNext()) {
265             EditorStack workbook = (EditorStack) workbooks.next();
266             workbook.removeAll();
267             if (workbook != currentWorkbook) {
268                 remove(workbook);
269                 workbook.dispose();
270             }
271         }
272     }
273
274     /**
275      * Remove an editor from its' workbook.
276      */

277     public void removeEditor(EditorPane pane) {
278         EditorStack workbook = pane.getWorkbook();
279         if (workbook == null) {
280             return;
281         }
282         workbook.remove(pane);
283
284         // remove the editor workbook if empty
285
if (workbook.getItemCount() < 1 /* && editorWorkbooks.size() > 1*/) {
286             // If the user closes the last editor and the editor area
287
// is maximized, restore it
288
Perspective persp = getPage().getActivePerspective();
289             if (Perspective.useNewMinMax(persp)) {
290                 if (persp.getPresentation().getMaximizedStack() instanceof EditorStack)
291                     persp.getPresentation().getMaximizedStack().
292                         setState(IStackPresentationSite.STATE_RESTORED);
293             }
294
295             remove(workbook);
296             workbook.dispose();
297         }
298     }
299
300     /**
301      * @see IPersistablePart
302      */

303     public IStatus restoreState(IMemento memento) {
304         MultiStatus result = new MultiStatus(
305                 PlatformUI.PLUGIN_ID,
306                 IStatus.OK,
307                 WorkbenchMessages.RootLayoutContainer_problemsRestoringPerspective, null);
308
309         // Remove the default editor workbook that is
310
// initialy created with the editor area.
311
if (children != null) {
312             StartupThreading.runWithoutExceptions(new StartupRunnable() {
313
314                 public void runWithException() throws Throwable JavaDoc {
315                     EditorStack defaultWorkbook = null;
316                     for (int i = 0; i < children.size(); i++) {
317                         LayoutPart child = (LayoutPart) children.get(i);
318                         if (child.getID() == DEFAULT_WORKBOOK_ID) {
319                             defaultWorkbook = (EditorStack) child;
320                             if (defaultWorkbook.getItemCount() > 0) {
321                                 defaultWorkbook = null;
322                             }
323                         }
324                     }
325                     if (defaultWorkbook != null) {
326                         remove(defaultWorkbook);
327                     }
328                 }});
329             
330         }
331
332         // Restore the relationship/layout
333
IMemento[] infos = memento.getChildren(IWorkbenchConstants.TAG_INFO);
334         final Map JavaDoc mapIDtoPart = new HashMap JavaDoc(infos.length);
335
336         for (int i = 0; i < infos.length; i++) {
337             // Get the info details.
338
IMemento childMem = infos[i];
339             final String JavaDoc partID = childMem.getString(IWorkbenchConstants.TAG_PART);
340             final String JavaDoc relativeID = childMem
341                     .getString(IWorkbenchConstants.TAG_RELATIVE);
342             int relationship = 0;
343             int left = 0, right = 0;
344             float ratio = 0.5f;
345             if (relativeID != null) {
346                 relationship = childMem.getInteger(
347                         IWorkbenchConstants.TAG_RELATIONSHIP).intValue();
348                 Float JavaDoc ratioFloat = childMem
349                         .getFloat(IWorkbenchConstants.TAG_RATIO);
350                 Integer JavaDoc leftInt = childMem
351                         .getInteger(IWorkbenchConstants.TAG_RATIO_LEFT);
352                 Integer JavaDoc rightInt = childMem
353                         .getInteger(IWorkbenchConstants.TAG_RATIO_RIGHT);
354                 if (leftInt != null && rightInt != null) {
355                     left = leftInt.intValue();
356                     right = rightInt.intValue();
357                 } else if (ratioFloat != null) {
358                     ratio = ratioFloat.floatValue();
359                 }
360             }
361
362             final EditorStack workbook [] = new EditorStack[1];
363             StartupThreading.runWithoutExceptions(new StartupRunnable() {
364
365                 public void runWithException() throws Throwable JavaDoc {
366                     // Create the part.
367
workbook[0] = EditorStack.newEditorWorkbook(EditorSashContainer.this, page);
368                     workbook[0].setID(partID);
369                     // 1FUN70C: ITPUI:WIN - Shouldn't set Container when not active
370
workbook[0].setContainer(EditorSashContainer.this);
371                 }});
372             
373
374             IMemento workbookMemento = childMem
375                     .getChild(IWorkbenchConstants.TAG_FOLDER);
376             if (workbookMemento != null) {
377                 result.add(workbook[0].restoreState(workbookMemento));
378             }
379
380             final int myLeft = left, myRight = right, myRelationship = relationship;
381             final float myRatio = ratio;
382             StartupThreading.runWithoutExceptions(new StartupRunnable() {
383
384                 public void runWithException() throws Throwable JavaDoc {
385                     // Add the part to the layout
386
if (relativeID == null) {
387                         add(workbook[0]);
388                     } else {
389                         LayoutPart refPart = (LayoutPart) mapIDtoPart.get(relativeID);
390                         if (refPart != null) {
391                             //$TODO pass in left and right
392
if (myLeft == 0 || myRight == 0) {
393                                 add(workbook[0], myRelationship, myRatio, refPart);
394                             } else {
395                                 add(workbook[0], myRelationship, myLeft, myRight, refPart);
396                             }
397                         } else {
398                             WorkbenchPlugin
399                                     .log("Unable to find part for ID: " + relativeID);//$NON-NLS-1$
400
}
401                     }
402                 }});
403             
404             mapIDtoPart.put(partID, workbook[0]);
405         }
406
407         return result;
408     }
409
410     /**
411      * @see IPersistablePart
412      */

413     public IStatus saveState(IMemento memento) {
414         RelationshipInfo[] relationships = computeRelation();
415         MultiStatus result = new MultiStatus(
416                 PlatformUI.PLUGIN_ID,
417                 IStatus.OK,
418                 WorkbenchMessages.RootLayoutContainer_problemsSavingPerspective, null);
419
420         for (int i = 0; i < relationships.length; i++) {
421             // Save the relationship info ..
422
// private LayoutPart part;
423
// private int relationship;
424
// private float ratio;
425
// private LayoutPart relative;
426
RelationshipInfo info = relationships[i];
427             IMemento childMem = memento
428                     .createChild(IWorkbenchConstants.TAG_INFO);
429             childMem.putString(IWorkbenchConstants.TAG_PART, info.part.getID());
430
431             EditorStack stack = (EditorStack) info.part;
432             if (stack != null) {
433                 IMemento folderMem = childMem
434                         .createChild(IWorkbenchConstants.TAG_FOLDER);
435                 result.add(stack.saveState(folderMem));
436             }
437
438             if (info.relative != null) {
439                 childMem.putString(IWorkbenchConstants.TAG_RELATIVE,
440                         info.relative.getID());
441                 childMem.putInteger(IWorkbenchConstants.TAG_RELATIONSHIP,
442                         info.relationship);
443                 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_LEFT,
444                         info.left);
445                 childMem.putInteger(IWorkbenchConstants.TAG_RATIO_RIGHT,
446                         info.right);
447                 // Note: "ratio" is not used in newer versions of Eclipse, which use "left"
448
// and "right" (above) instead
449
childMem.putFloat(IWorkbenchConstants.TAG_RATIO, info
450                         .getRatio());
451             }
452         }
453         
454         return result;
455     }
456
457     /**
458      * Set the editor workbook which is active.
459      */

460     public void setActiveWorkbook(EditorStack newWorkbook, boolean hasFocus) {
461         if (newWorkbook != null) {
462             if (newWorkbook.isDisposed()) {
463                 return;
464             }
465             if (!editorWorkbooks.contains(newWorkbook)) {
466                 return;
467             }
468         }
469         EditorStack oldWorkbook = activeEditorWorkbook;
470         activeEditorWorkbook = newWorkbook;
471
472         if (oldWorkbook != null && oldWorkbook != newWorkbook) {
473             oldWorkbook.setActive(StackPresentation.AS_INACTIVE);
474         }
475
476         if (newWorkbook != null) {
477             if (hasFocus) {
478                 newWorkbook.setActive(StackPresentation.AS_ACTIVE_FOCUS);
479             } else {
480                 newWorkbook.setActive(StackPresentation.AS_ACTIVE_NOFOCUS);
481             }
482         }
483
484         updateTabList();
485     }
486
487     /**
488      * Set the editor workbook which is active.
489      */

490     public void setActiveWorkbookFromID(String JavaDoc id) {
491         for (int i = 0; i < editorWorkbooks.size(); i++) {
492             EditorStack workbook = (EditorStack) editorWorkbooks.get(i);
493             if (workbook.getID().equals(id)) {
494                 setActiveWorkbook(workbook, false);
495             }
496         }
497     }
498     
499     public EditorStack getWorkbookFromID(String JavaDoc id) {
500         for (int i = 0; i < editorWorkbooks.size(); i++) {
501             EditorStack workbook = (EditorStack) editorWorkbooks.get(i);
502             if (workbook.getID().equals(id)) {
503                 return workbook;
504             }
505         }
506         
507         return null;
508     }
509
510     /**
511      * Updates the editor area's tab list to include the active
512      * editor and its tab.
513      */

514     public void updateTabList() {
515         Composite parent = getParent();
516         if (parent != null) { // parent may be null on startup
517
EditorStack wb = getActiveWorkbook();
518             if (wb == null) {
519                 parent.setTabList(new Control[0]);
520             } else {
521                 parent.setTabList(wb.getTabList());
522             }
523         }
524     }
525
526     /**
527      * @see org.eclipse.ui.internal.LayoutPart#createControl(org.eclipse.swt.widgets.Composite)
528      */

529     public void createControl(Composite parent) {
530         super.createControl(parent);
531
532         //let the user drop files/editor input on the editor area
533
addDropSupport();
534     }
535
536     private void addDropSupport() {
537         if (dropTarget == null) {
538             WorkbenchWindowConfigurer winConfigurer = ((WorkbenchWindow) page
539                     .getWorkbenchWindow()).getWindowConfigurer();
540
541             dropTarget = new DropTarget(getControl(), DND.DROP_DEFAULT
542                     | DND.DROP_COPY | DND.DROP_LINK);
543             dropTarget.setTransfer(winConfigurer.getTransfers());
544             if (winConfigurer.getDropTargetListener() != null) {
545                 dropTarget.addDropListener(winConfigurer
546                         .getDropTargetListener());
547             }
548         }
549     }
550
551     /* package */DropTarget getDropTarget() {
552         return dropTarget;
553     }
554
555     /**
556      * @see org.eclipse.ui.internal.LayoutPart#getImportance()
557      */

558     public boolean isCompressible() {
559         //Added for bug 19524
560
return true;
561     }
562
563     /* (non-Javadoc)
564      * @see org.eclipse.ui.internal.PartSashContainer#isStackType(org.eclipse.ui.internal.LayoutPart)
565      */

566     public boolean isStackType(LayoutPart toTest) {
567         return (toTest instanceof EditorStack);
568     }
569
570     /* (non-Javadoc)
571      * @see org.eclipse.ui.internal.PartSashContainer#isPaneType(org.eclipse.ui.internal.LayoutPart)
572      */

573     public boolean isPaneType(LayoutPart toTest) {
574         return (toTest instanceof EditorPane);
575     }
576
577     /* (non-Javadoc)
578      * @see org.eclipse.ui.internal.PartSashContainer#createStack(org.eclipse.ui.internal.LayoutPart)
579      */

580     protected PartStack createStack() {
581         EditorStack newWorkbook = EditorStack.newEditorWorkbook(this, page);
582
583         return newWorkbook;
584     }
585
586     /* (non-Javadoc)
587      * @see org.eclipse.ui.internal.PartSashContainer#setVisiblePart(org.eclipse.ui.internal.ILayoutContainer, org.eclipse.ui.internal.LayoutPart)
588      */

589     protected void setVisiblePart(ILayoutContainer container,
590             LayoutPart visiblePart) {
591         EditorStack refPart = (EditorStack) container;
592
593         refPart.becomeActiveWorkbook(true);
594         refPart.setSelection(visiblePart);
595     }
596
597     /* (non-Javadoc)
598      * @see org.eclipse.ui.internal.PartSashContainer#getVisiblePart(org.eclipse.ui.internal.ILayoutContainer)
599      */

600     protected LayoutPart getVisiblePart(ILayoutContainer container) {
601         EditorStack refPart = (EditorStack) container;
602
603         return refPart.getSelection();
604     }
605
606     /* (non-Javadoc)
607      * @see org.eclipse.ui.internal.PartSashContainer#pickPartToZoom()
608      */

609     public LayoutPart pickPartToZoom() {
610         return getActiveWorkbook();
611     }
612     
613     /**
614      * Restore the presentation state. Loop over the workbooks, create the appropriate serializer and pass to the presentation.
615      *
616      * @param areaMem the memento containing presentation
617      * @return the restoration status
618      */

619     public IStatus restorePresentationState(IMemento areaMem) {
620         for (Iterator JavaDoc i = getEditorWorkbooks().iterator(); i.hasNext();) {
621             final EditorStack workbook = (EditorStack) i.next();
622             final IMemento memento = workbook.getSavedPresentationState();
623             if (memento == null) {
624                 continue;
625             }
626             final PresentationSerializer serializer = new PresentationSerializer(
627                     workbook.getPresentableParts());
628             StartupThreading.runWithoutExceptions(new StartupRunnable(){
629
630                 public void runWithException() throws Throwable JavaDoc {
631                      workbook.getPresentation().restoreState(serializer, memento);
632                 }});
633            
634         }
635         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
636
}
637 }
638
Popular Tags