KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.jface.util.Geometry;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.IPerspectiveDescriptor;
27 import org.eclipse.ui.IPerspectiveListener2;
28 import org.eclipse.ui.IViewReference;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchPartReference;
32 import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
33 import org.eclipse.ui.internal.layout.IWindowTrim;
34 import org.eclipse.ui.internal.layout.LayoutUtil;
35 import org.eclipse.ui.internal.layout.TrimLayout;
36 import org.eclipse.ui.internal.presentations.PresentablePart;
37 import org.eclipse.ui.internal.presentations.util.TabbedStackPresentation;
38 import org.eclipse.ui.internal.tweaklets.Animations;
39 import org.eclipse.ui.internal.tweaklets.Tweaklets;
40 import org.eclipse.ui.internal.util.Util;
41 import org.eclipse.ui.presentations.IPresentablePart;
42 import org.eclipse.ui.presentations.IStackPresentationSite;
43
44 /**
45  * Manage all Fast views for a particular perspective. As of 3.3 fast views
46  * appear in more than one manner (legacy FVB and Trim Stacks). The manager is
47  * responsible for providing a single implementation for the methods relating to
48  * fast views regardless of their UI presentation.
49  *
50  * @since 3.3
51  *
52  */

53 public class FastViewManager {
54     private Perspective perspective;
55     private WorkbenchPage page;
56     private WorkbenchWindow wbw;
57     private TrimLayout tbm;
58
59     /**
60      * Maps a String to a list of IViewReferences. The string represents the
61      * 'id' of either the legacy FBV or the ViewStack id of some stack which may
62      * have elements in the trim.
63      * <p>
64      * NOTE: For TrimStacks, the order of the view ref's in the contained list
65      * is the order in which they will appear in the tab folder when the stack
66      * un-minimizes.
67      * </p>
68      */

69     private Map JavaDoc idToFastViewsMap = new HashMap JavaDoc();
70
71     /**
72      * Batch update management
73      */

74     private boolean deferringUpdates = false;
75
76     /**
77      * animation whose life-cycle spans a
78      * 'deferUpdates' cycle.
79      */

80     private AnimationEngine batchAnimation = null;
81     
82     /**
83      * Used for non-deferred animations
84      */

85     private AnimationEngine oneShotAnimation = null;
86     //private RectangleAnimation oneShotAnimation = null;
87

88     private IPerspectiveListener2 perspListener = new IPerspectiveListener2() {
89         public void perspectiveActivated(IWorkbenchPage page,
90                 IPerspectiveDescriptor perspective) {
91             // Only listen for changes in -this- perspective
92
if (FastViewManager.this.perspective.getDesc() == perspective)
93                 handlePerspectiveActivation(page, perspective);
94         }
95
96         public void perspectiveChanged(IWorkbenchPage changedPage,
97                 IPerspectiveDescriptor perspective,
98                 IWorkbenchPartReference partRef, String JavaDoc changeId) {
99             // Only listen for changes in -this- perspective
100
if (FastViewManager.this.perspective.getDesc() == perspective)
101                 handlePerspectiveChange(changedPage, perspective, partRef,
102                     changeId);
103         }
104
105         public void perspectiveChanged(IWorkbenchPage changedPage,
106                 IPerspectiveDescriptor perspective, String JavaDoc changeId) {
107             // Only listen for changes in -this- perspective
108
if (FastViewManager.this.perspective.getDesc() == perspective)
109                 handlePerspectiveChange(changedPage, perspective, changeId);
110         }
111     };
112     
113     /**
114      * Creates a new manager for a particular perspective
115      *
116      * @param perspective
117      * @param page
118      */

119     public FastViewManager(Perspective perspective, WorkbenchPage page) {
120         this.perspective = perspective;
121         this.page = page;
122
123         // Access the trim manager for this window
124
wbw = (WorkbenchWindow) page.getWorkbenchWindow();
125         tbm = (TrimLayout) wbw.getTrimManager();
126     }
127
128     protected void handlePerspectiveActivation(IWorkbenchPage activatingPage,
129             IPerspectiveDescriptor activatingPerspective) {
130         // If this perspective is activating then update the
131
// legacy FVB to show this perspective's refs
132
if (activatingPage == page
133                 && perspective.getDesc() == activatingPerspective)
134             updateTrim(FastViewBar.FASTVIEWBAR_ID);
135     }
136
137     protected void handlePerspectiveChange(IWorkbenchPage changedPage,
138             IPerspectiveDescriptor changedPerspective,
139             IWorkbenchPartReference partRef, String JavaDoc changeId) {
140         // Only handle changes for our perspective
141
if (changedPage != page && perspective.getDesc() != changedPerspective)
142             return;
143
144         if (changeId.equals(IWorkbenchPage.CHANGE_VIEW_HIDE)) {
145             if (partRef instanceof IViewReference) {
146                 ViewReference ref = (ViewReference) partRef;
147                 if (ref.getPane().getContainer() instanceof ViewStack) {
148                     int viewCount = 0;
149                     LayoutPart[] children = ref.getPane().getContainer().getChildren();
150                     for (int i = 0; i < children.length; i++) {
151                         if (children[i] instanceof ViewPane && children[i] != ref.getPane())
152                             viewCount++;
153                     }
154                     
155                     if (viewCount == 0)
156                         ref.getPane().getStack().setState(IStackPresentationSite.STATE_RESTORED);
157                 }
158             }
159             // Remove the view from any FV list that it may be in
160
// Do this here since the view's controls may be about
161
// to be disposed...
162
removeViewReference((IViewReference) partRef, false, true);
163         }
164
165         if (changeId.equals(IWorkbenchPage.CHANGE_FAST_VIEW_REMOVE)) {
166             // Remove the view from any FV list that it may be in
167
// Do this here since the view's controls may be about
168
// to be disposed...
169
removeViewReference((IViewReference) partRef, false, true);
170         }
171     }
172
173     protected void handlePerspectiveChange(IWorkbenchPage changedPage,
174             IPerspectiveDescriptor changedPerspective, String JavaDoc changeId) {
175     }
176
177     /**
178      * @return The list of current fast views associated with the given id or
179      * the complete list of fastviews if the id == null.
180      */

181     public List JavaDoc getFastViews(String JavaDoc forId) {
182         List JavaDoc fvs = new ArrayList JavaDoc();
183
184         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
185         while (mapIter.hasNext()) {
186             String JavaDoc id = (String JavaDoc) mapIter.next();
187             if (forId == null || forId.equals(id)) {
188                 List JavaDoc fvList = (List JavaDoc) idToFastViewsMap.get(id);
189                 for (Iterator JavaDoc fvIter = fvList.iterator(); fvIter.hasNext();) {
190                     fvs.add(fvIter.next());
191                 }
192             }
193         }
194
195         return fvs;
196     }
197
198     /**
199      * Casues the given {@link IViewReference} to be added to the list
200      * identified by the 'id' parameter. The reference is added at the specified
201      * index or at the end if the index is -1. If there was a previous entry for
202      * this ref it will be removed so that only the ref will only ever be in one
203      * list at a time.
204      * <p>
205      * NOTE: The trim life-cycle is managed at the stack level so there -must-
206      * be an entry in the map and a corresponding trim element before calling
207      * this method,
208      * </p>
209      * <p>
210      * The page/perspective are updated to make the view a fastview if
211      * necessary.
212      * </p>
213      *
214      * @param id
215      * The id of the {@link IWindowTrim} that is to show the ref
216      * @param index
217      * The index to insert the ref at
218      * @param ref
219      * The {@link IViewReference} to add
220      * @param update
221      * cause the trim to update if <code>true</code>
222      */

223     public void addViewReference(String JavaDoc id, int index, IViewReference ref,
224             boolean update) {
225         if (id == null || ref == null)
226             return;
227
228         List JavaDoc fvList = (List JavaDoc) idToFastViewsMap.get(id);
229         if (fvList == null) {
230             // Not in the model yet, add it
231
fvList = new ArrayList JavaDoc();
232             idToFastViewsMap.put(id, fvList);
233         }
234
235         // bounds checking
236
if (index < 0 || index > fvList.size())
237             index = fvList.size();
238
239         // Is it already in a list?
240
String JavaDoc curLocation = getIdForRef(ref);
241         if (curLocation != null) {
242             // is it the same list that it's being added to?
243
if (id.equals(curLocation)) {
244                 int curIndex = fvList.indexOf(ref);
245                 if (index == curIndex)
246                     return; // No-Op
247

248                 // If we're inserting after where we
249
// were then decrement the index to
250
// account for the removal of the old ref
251
if (index > curIndex)
252                     index--;
253             }
254
255             // Remove it...
256
removeViewReference(ref, false, true);
257         } else {
258             // It's not a fastview, make it one
259
makeFast(ref, true, false);
260         }
261
262         fvList.add(index, ref);
263
264         // Note that the update call will create and show the ViewStackTrimToolbar
265
// if necessary
266
if (update)
267             updateTrim(id);
268     }
269
270     /**
271      * Create the Trim element for the stack containing the given reference
272      *
273      * @param suggestedSide
274      * @param paneOrientation
275      * @param ref
276      * The {@link IViewReference} whose stack needs trim creation.
277      */

278     private ViewStackTrimToolBar getTrimForViewStack(String JavaDoc id,
279             int suggestedSide, int paneOrientation) {
280         // Do we already have one??
281
ViewStackTrimToolBar trim = (ViewStackTrimToolBar) tbm.getTrim(id);
282         if (trim == null) {
283             int cachedSide = tbm.getPreferredArea(id);
284             if (cachedSide != -1)
285                 suggestedSide = cachedSide;
286             
287             IWindowTrim beforeMe = tbm.getPreferredLocation(id);
288             
289             trim = new ViewStackTrimToolBar(id, suggestedSide,
290                     paneOrientation, wbw);
291             tbm.addTrim(suggestedSide, trim, beforeMe);
292             updateTrim(trim.getId());
293         }
294
295         return trim;
296     }
297
298     /**
299      * Causes the trim element associated with the id to synch itself with the
300      * current list of views. This method will create a new ViewStackTrimToolbar
301      * if necessary (i.e. on the first call after views have been added to the map)
302      * and will also hide the trim element when the number of views in the mapped
303      * list goes to zero.
304      *
305      * @param id
306      * The id of the {@link IWindowTrim} to update
307      */

308     private void updateTrim(String JavaDoc id) {
309         // Get the trim part from the trim manager
310
IWindowTrim trim = tbm.getTrim(id);
311
312         // If it's not there there's not much we can do
313
if (trim == null)
314             return;
315
316         // If there are no fast views for the bar then hide it
317
List JavaDoc fvs = (List JavaDoc) idToFastViewsMap.get(id);
318         if (fvs != null && fvs.size() == 0
319                 && !FastViewBar.FASTVIEWBAR_ID.equals(id)) {
320             if (trim.getControl().getVisible()) {
321                 tbm.setTrimVisible(trim, false);
322                 tbm.forceLayout();
323             }
324             return;
325         }
326
327         // Ensure that the trim is displayed
328
if (!trim.getControl().getVisible()) {
329             tbm.setTrimVisible(trim, true);
330         }
331
332         if (trim instanceof FastViewBar) {
333             FastViewBar fvb = (FastViewBar) trim;
334             fvb.update(true);
335         } else if (trim instanceof ViewStackTrimToolBar) {
336             ViewStackTrimToolBar vstb = (ViewStackTrimToolBar) trim;
337             vstb.update(true);
338             vstb.getControl().pack();
339             LayoutUtil.resize(trim.getControl());
340         }
341
342         tbm.forceLayout();
343     }
344
345     /**
346      * Remove the view reference from its existing location
347      *
348      * @param ref
349      * The {@link IViewReference} to remove
350      */

351     public void removeViewReference(IViewReference ref, boolean makeUnfast, boolean update) {
352         String JavaDoc id = getIdForRef(ref);
353
354         if (id != null) {
355             // Remove the ref
356
List JavaDoc fvList = (List JavaDoc) idToFastViewsMap.get(id);
357             fvList.remove(ref);
358
359             if (makeUnfast)
360                 makeFast(ref, false, false);
361             
362             if (update)
363                 updateTrim(id);
364         }
365     }
366
367     /**
368      *
369      * @param ref
370      * @param makeFast
371      * @param activate
372      */

373     private void makeFast(IViewReference ref, boolean makeFast, boolean activate) {
374         if (ref == null || page == null)
375             return;
376
377         if (makeFast) {
378             page.makeFastView(ref);
379         } else {
380             page.removeFastView(ref);
381
382             if (activate) {
383                 IWorkbenchPart toActivate = ref.getPart(true);
384                 if (toActivate != null) {
385                     page.activate(toActivate);
386                 }
387             }
388         }
389     }
390
391     /**
392      * @param ref
393      * The IViewRference to check
394      * @return true iff the ref is in -any- list
395      */

396     boolean isFastView(IViewReference ref) {
397         return getIdForRef(ref) != null;
398     }
399
400     /**
401      * @param ref
402      * The IViewRference to check
403      * @return The id of the trim bar currently showing the reference or
404      * <code>null</code> if it's not in any list
405      */

406     public String JavaDoc getIdForRef(IViewReference ref) {
407         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
408         while (mapIter.hasNext()) {
409             String JavaDoc id = (String JavaDoc) mapIter.next();
410             List JavaDoc fvList = (List JavaDoc) idToFastViewsMap.get(id);
411             if (fvList.contains(ref))
412                 return id;
413         }
414
415         return null;
416     }
417
418     /**
419      * @return The side that the fast view pane should be attached to based on
420      * the position of the trim element containing the ref.
421      */

422     public int getViewSide(IViewReference ref) {
423         IWindowTrim trim = getTrimForRef(ref);
424         if (trim == null)
425             return SWT.BOTTOM;
426
427         int curSide = SWT.BOTTOM;
428         int paneOrientation = SWT.BOTTOM;
429
430         if (trim instanceof ViewStackTrimToolBar) {
431             curSide = ((ViewStackTrimToolBar) trim).getCurrentSide();
432             paneOrientation = ((ViewStackTrimToolBar) trim)
433                     .getPaneOrientation();
434         } else if (trim instanceof FastViewBar) {
435             curSide = ((FastViewBar) trim).getSide();
436             paneOrientation = ((FastViewBar) trim).getOrientation(ref);
437         }
438
439         // Get trim layout info
440
Point trimCenter = Geometry.centerPoint(trim.getControl().getBounds());
441         Point shellCenter = Geometry.centerPoint(trim.getControl().getShell()
442                 .getClientArea());
443
444         // Horizontal has to snap to either TOP or BOTTOM...
445
if (paneOrientation == SWT.HORIZONTAL) {
446             if (curSide == SWT.TOP || curSide == SWT.BOTTOM)
447                 return curSide;
448
449             // Are we on the top or bottom 'end' of the trim area?
450
return (trimCenter.y < shellCenter.y) ? SWT.TOP : SWT.BOTTOM;
451         }
452
453         if (paneOrientation == SWT.VERTICAL) {
454             if (curSide == SWT.LEFT || curSide == SWT.RIGHT)
455                 return curSide;
456
457             // Are we on the left or right 'end' of the trim area?
458
return (trimCenter.x < shellCenter.x) ? SWT.LEFT : SWT.RIGHT;
459         }
460
461         return SWT.BOTTOM;
462     }
463
464     /**
465      * Return the trim element showing the given reference
466      *
467      * @param ref
468      * The reference to find
469      * @return the IWindowTrim showing the ref
470      */

471     private IWindowTrim getTrimForRef(IViewReference ref) {
472         String JavaDoc id = getIdForRef(ref);
473         if (id == null)
474             return null; // Not in trim
475

476         return tbm.getTrim(id);
477     }
478
479     /**
480      * @return a List of <code>IViewReference</code> sorted into the order in
481      * which they appear in the visual stack.
482      */

483     private List JavaDoc getTrueViewOrder(ViewStack stack) {
484         List JavaDoc orderedViews = new ArrayList JavaDoc();
485         IPresentablePart[] parts = null;
486         if (stack.getPresentation() instanceof TabbedStackPresentation) {
487             TabbedStackPresentation tsp = (TabbedStackPresentation) stack
488                     .getPresentation();
489             // KLUDGE!! uses a 'testing only' API to get the parts in their 'visible' order
490
parts = tsp.getPartList();
491         }
492
493         // If we didn't get the parts from the tab list then try the presentable part API
494
// ViewStack's declared 'no title' fail the call above, returning an empty array
495
if (parts == null || parts.length == 0){
496             // We'll have to process the parts in the order given...
497
// This is certain to fail on drag re-ordering of the
498
// icons in the trim since we have no API to inform the
499
// custom presentation
500
List JavaDoc partList = stack.getPresentableParts();
501             parts = (IPresentablePart[]) partList.toArray(new IPresentablePart[partList.size()]);
502         }
503
504         // Now, process the parts...
505
for (int i = 0; i < parts.length; i++) {
506             if (parts[i] instanceof PresentablePart) {
507                 PresentablePart part = (PresentablePart) parts[i];
508                 IWorkbenchPartReference ref = part.getPane()
509                         .getPartReference();
510                 if (ref instanceof IViewReference)
511                     orderedViews.add(ref);
512             }
513         }
514
515         return orderedViews;
516     }
517
518     public void moveToTrim(ViewStack vs, boolean restoreOnUnzoom) {
519         // Don't do anything when initializing...
520
if (vs.getBounds().width == 0)
521             return; // indicates that we're startin up
522

523         // If we're part of a 'maximize' operation then use the cached
524
// bounds...
525
Rectangle stackBounds = perspective.getPresentation().getCachedBoundsFor(vs.getID());
526
527         // OK, no cache means that we use the current stack position
528
if (stackBounds == null)
529             stackBounds = vs.getBounds();
530         
531         int paneOrientation = (stackBounds.width > stackBounds.height) ? SWT.HORIZONTAL
532                 : SWT.VERTICAL;
533
534         // Remember the tab that was selected when we minimized
535
String JavaDoc selId = ""; //$NON-NLS-1$
536
PartPane selectedTab = vs.getSelection();
537         if (selectedTab != null)
538             selId = selectedTab.getCompoundId();
539         
540         vs.deferUpdates(true);
541         
542         // animate the minimize
543
RectangleAnimationFeedbackBase animation = (RectangleAnimationFeedbackBase) getDeferrableAnimation().getFeedback();
544         animation.addStartRect(vs.getControl());
545
546         //long startTick = System.currentTimeMillis();
547
// Update the model first
548
List JavaDoc toMove = getTrueViewOrder(vs);
549         for (Iterator JavaDoc viewIter = toMove.iterator(); viewIter.hasNext();) {
550             IViewReference ref = (IViewReference) viewIter.next();
551             addViewReference(vs.getID(), -1, ref, false);
552         }
553
554         vs.deferUpdates(false);
555         
556         // Find (or create) the trim stack to move to
557
ViewStackTrimToolBar vstb = getTrimForViewStack(vs.getID(), perspective
558                 .calcStackSide(stackBounds), paneOrientation);
559         vstb.setRestoreOnUnzoom(restoreOnUnzoom);
560         vstb.setSelectedTabId(selId);
561         updateTrim(vstb.getId());
562         
563         //System.out.println("minimize time: " + (System.currentTimeMillis()-startTick)); //$NON-NLS-1$
564
if (vstb != null) {
565             animation.addEndRect(vstb.getControl());
566             scheduleDeferrableAnimation();
567         }
568     }
569
570     /**
571      * Restore the trim element representing a ViewStack back into the
572      * presentation.
573      *
574      * @param viewStackTrimToolBar
575      * The trim version to restore
576      */

577     public void restoreToPresentation(String JavaDoc id) {
578         ViewStackTrimToolBar vstb = getViewStackTrimToolbar(id);
579         
580         // The IntroPart uses the old min/max behavior; ensure that
581
// we were really a minimized trim stack
582
if (vstb == null)
583             return;
584         
585         // remove any showing fast view
586
page.hideFastView();
587
588         // The stored id may be 'compound' if it's a multi-instance
589
// view; split out the secondary id (if any)
590
String JavaDoc selectedTabId = vstb.getSelectedTabId();
591         String JavaDoc[] idParts = Util.split(selectedTabId, ':');
592         if (idParts[0].length() == selectedTabId.length())
593             idParts[1] = null;
594         
595         List JavaDoc fvs = getFastViews(id);
596         for (Iterator JavaDoc fvIter = fvs.iterator(); fvIter.hasNext();) {
597             IViewReference ref = (IViewReference) fvIter.next();
598             removeViewReference(ref, true, !fvIter.hasNext());
599         }
600
601         // Restore the correct tab to the 'top'
602
LayoutPart stack = perspective.getPresentation().findPart(id, null);
603         if (stack instanceof PartStack) {
604             LayoutPart selTab = perspective.getPresentation().findPart(idParts[0], idParts[1]);
605             if (selTab instanceof PartPane && selTab instanceof ViewPane) {
606                 ((PartStack)stack).setSelection(selTab);
607                 
608                 // activate the view if we're not doing a compound operation
609
if (!deferringUpdates)
610                     ((ViewPane)selTab).requestActivation();
611             }
612         }
613         
614         // Hide the Trim
615
updateTrim(id);
616     }
617
618     /**
619      * Restore all fact view stacks created as part of a zoom
620      */

621     public void restoreZoomedViewStacks() {
622         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
623         while (mapIter.hasNext()) {
624             String JavaDoc id = (String JavaDoc) mapIter.next();
625             IWindowTrim trim = tbm.getTrim(id);
626             if (trim != null && trim instanceof ViewStackTrimToolBar) {
627                 ViewStackTrimToolBar vstb = (ViewStackTrimToolBar) trim;
628                 if (vstb.restoreOnUnzoom())
629                     restoreToPresentation(vstb.getId());
630             }
631         }
632     }
633
634     /**
635      * @param ref
636      * Sets the ref of the icon
637      * @param selected
638      * the selection state of the icon
639      */

640     public void setFastViewIconSelection(IViewReference ref, boolean selected) {
641         String JavaDoc id = getIdForRef(ref);
642         IWindowTrim trim = tbm.getTrim(id);
643         if (trim instanceof ViewStackTrimToolBar) {
644             ViewStackTrimToolBar vstb = (ViewStackTrimToolBar) trim;
645             vstb.setIconSelection(ref, selected);
646         } else if (trim instanceof FastViewBar) {
647             FastViewBar fvb = (FastViewBar) trim;
648             if (selected) {
649                 fvb.setSelection(ref);
650             } else {
651                 if (ref == fvb.getSelection()) {
652                     fvb.setSelection(null);
653                 }
654             }
655         }
656
657     }
658
659     /**
660      * Activate the manager. Called from the Perspecive's 'onActivate'
661      */

662     public void activate() {
663         wbw.addPerspectiveListener(perspListener);
664         setTrimStackVisibility(true);
665     }
666
667     /**
668      * Activate the manager. Called from the Perspecive's 'onActivate'
669      */

670     public void deActivate() {
671         wbw.removePerspectiveListener(perspListener);
672         setTrimStackVisibility(false);
673     }
674
675     /**
676      * Restore any trim stacks. This method is used when the presentation
677      * is switched back to 3.0; if we aren't using the new min/max story
678      * then we shouldn't -have- any trim stacks.
679      */

680     public boolean restoreAllTrimStacks() {
681         boolean stacksWereRestored = false;
682         
683         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
684         while (mapIter.hasNext()) {
685             String JavaDoc id = (String JavaDoc) mapIter.next();
686             
687             // Skip the legacy FstViewBar
688
if (id.equals(FastViewBar.FASTVIEWBAR_ID))
689                 continue;
690             
691             // Restore the views
692
List JavaDoc fvs = getFastViews(id);
693             for (Iterator JavaDoc fvIter = fvs.iterator(); fvIter.hasNext();) {
694                 IViewReference ref = (IViewReference) fvIter.next();
695                 removeViewReference(ref, true, !fvIter.hasNext());
696             }
697
698             // Blow the trim away
699
IWindowTrim trim = tbm.getTrim(id);
700             if (trim != null && trim instanceof ViewStackTrimToolBar) {
701                 tbm.removeTrim(trim);
702                 trim.getControl().dispose();
703                 
704                 stacksWereRestored = true;
705             }
706         }
707         
708         tbm.forceLayout();
709         
710         return stacksWereRestored;
711     }
712
713     /**
714      * Show all non-empty trim stacks. Create the stack if necessary
715      */

716     private void setTrimStackVisibility(boolean visible) {
717         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
718         while (mapIter.hasNext()) {
719             String JavaDoc id = (String JavaDoc) mapIter.next();
720             List JavaDoc fvs = getFastViews(id);
721             
722             // Never show 'empty' stacks
723
if (visible && fvs.size() == 0)
724                 continue;
725
726             IWindowTrim trim = tbm.getTrim(id);
727             if (trim != null && trim instanceof ViewStackTrimToolBar) {
728                 ((ViewStackTrimToolBar)trim).update(true);
729                 tbm.setTrimVisible(trim, visible);
730             }
731         }
732     }
733
734     public void saveState(IMemento memento) {
735         // Output legacy fastviews
736
FastViewBar fvb = wbw.getFastViewBar();
737         if (fvb != null) {
738             List JavaDoc fvRefs = getFastViews(FastViewBar.FASTVIEWBAR_ID);
739             if (fvRefs.size() > 0) {
740                 IMemento childMem = memento
741                         .createChild(IWorkbenchConstants.TAG_FAST_VIEWS);
742                 Iterator JavaDoc itr = fvRefs.iterator();
743                 while (itr.hasNext()) {
744                     IViewReference ref = (IViewReference) itr.next();
745                     IMemento viewMemento = childMem
746                             .createChild(IWorkbenchConstants.TAG_VIEW);
747                     String JavaDoc id = ViewFactory.getKey(ref);
748                     viewMemento.putString(IWorkbenchConstants.TAG_ID, id);
749                     float ratio = perspective.getFastViewWidthRatio(ref);
750                     viewMemento.putFloat(IWorkbenchConstants.TAG_RATIO, ratio);
751                 }
752             }
753         }
754
755         // Write all the current (non-empty) bars
756
IMemento barsMemento = memento
757                 .createChild(IWorkbenchConstants.TAG_FAST_VIEW_BARS);
758
759         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
760         while (mapIter.hasNext()) {
761             String JavaDoc id = (String JavaDoc) mapIter.next();
762
763             // Legacy FV's are stored above...
764
if (FastViewBar.FASTVIEWBAR_ID.equals(id))
765                 continue;
766
767             List JavaDoc fvs = getFastViews(id);
768             if (fvs.size() > 0) {
769                 IMemento barMemento = barsMemento
770                         .createChild(IWorkbenchConstants.TAG_FAST_VIEW_BAR);
771                 barMemento.putString(IWorkbenchConstants.TAG_ID, id);
772
773                 // Orientation / restore
774
ViewStackTrimToolBar vstb = (ViewStackTrimToolBar) tbm
775                         .getTrim(id);
776                 if (vstb != null) {
777                     barMemento.putInteger(
778                             IWorkbenchConstants.TAG_FAST_VIEW_SIDE,
779                             vstb.getCurrentSide());
780                     
781                     barMemento.putInteger(
782                             IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION,
783                             vstb.getPaneOrientation());
784                     
785                     int boolVal = vstb.restoreOnUnzoom() ? 1 : 0;
786                     barMemento.putInteger(IWorkbenchConstants.TAG_FAST_VIEW_STYLE, boolVal);
787                     
788                     barMemento.putString(IWorkbenchConstants.TAG_FAST_VIEW_SEL_ID, vstb.getSelectedTabId());
789                 }
790
791                 IMemento viewsMem = barMemento
792                         .createChild(IWorkbenchConstants.TAG_FAST_VIEWS);
793                 Iterator JavaDoc itr = fvs.iterator();
794                 while (itr.hasNext()) {
795                     IMemento refMem = viewsMem
796                             .createChild(IWorkbenchConstants.TAG_VIEW);
797                     IViewReference ref = (IViewReference) itr.next();
798
799                     // id
800
String JavaDoc viewId = ViewFactory.getKey(ref);
801                     refMem.putString(IWorkbenchConstants.TAG_ID, viewId);
802                     
803                     // width ratio
804
float ratio = perspective.getFastViewWidthRatio(ref);
805                     refMem.putFloat(IWorkbenchConstants.TAG_RATIO, ratio);
806                 }
807             }
808         }
809     }
810
811     public void restoreState(IMemento memento, MultiStatus result) {
812         // Load the fast views
813
IMemento fastViewsMem = memento
814                 .getChild(IWorkbenchConstants.TAG_FAST_VIEWS);
815
816         // -Replace- the current list with the one from the store
817
List JavaDoc refsList = new ArrayList JavaDoc();
818         idToFastViewsMap.put(FastViewBar.FASTVIEWBAR_ID, refsList);
819
820         if (fastViewsMem != null) {
821             IMemento[] views = fastViewsMem
822                     .getChildren(IWorkbenchConstants.TAG_VIEW);
823             for (int x = 0; x < views.length; x++) {
824                 // Get the view details.
825
IMemento childMem = views[x];
826                 IViewReference ref = perspective.restoreFastView(childMem,
827                         result);
828                 if (ref != null)
829                     refsList.add(ref);
830             }
831         }
832
833         // Load the Trim Stack info
834
IMemento barsMem = memento
835                 .getChild(IWorkbenchConstants.TAG_FAST_VIEW_BARS);
836         
837         // It's not there for old workspaces
838
if (barsMem == null)
839             return;
840         
841         IMemento[] bars = barsMem
842                 .getChildren(IWorkbenchConstants.TAG_FAST_VIEW_BAR);
843         for (int i = 0; i < bars.length; i++) {
844             final String JavaDoc id = bars[i].getString(IWorkbenchConstants.TAG_ID);
845             fastViewsMem = bars[i].getChild(IWorkbenchConstants.TAG_FAST_VIEWS);
846
847             // -Replace- the current list with the one from the store
848
refsList = new ArrayList JavaDoc();
849             idToFastViewsMap.put(id, refsList);
850
851             if (fastViewsMem != null) {
852                 IMemento[] views = fastViewsMem
853                         .getChildren(IWorkbenchConstants.TAG_VIEW);
854                 result.merge(perspective.createReferences(views));
855                 
856                 // Create the trim area for the trim stack
857
// Only create the TB if there are views in it
858
if (views.length > 0) {
859                     final int side = bars[i].getInteger(
860                             IWorkbenchConstants.TAG_FAST_VIEW_SIDE)
861                             .intValue();
862                     final int orientation = bars[i].getInteger(
863                             IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION)
864                             .intValue();
865                     int boolVal = bars[i].getInteger(
866                             IWorkbenchConstants.TAG_FAST_VIEW_STYLE).intValue();
867                     final boolean restoreOnUnzoom = (boolVal > 0);
868                     
869                     final String JavaDoc selId = bars[i].getString(IWorkbenchConstants.TAG_FAST_VIEW_SEL_ID);
870                     
871                     // Create the stack
872
StartupThreading.runWithoutExceptions(new StartupRunnable() {
873                         public void runWithException() throws Throwable JavaDoc {
874                             ViewStackTrimToolBar vstb = getTrimForViewStack(id, side, orientation);
875                             vstb.setRestoreOnUnzoom(restoreOnUnzoom);
876                             if (selId != null)
877                                 vstb.setSelectedTabId(selId);
878                         }
879                     });
880                 }
881                 
882                 for (int x = 0; x < views.length; x++) {
883                     // Get the view details.
884
IMemento childMem = views[x];
885                     IViewReference ref = perspective.restoreFastView(childMem,
886                             result);
887                     if (ref != null)
888                         refsList.add(ref);
889                 }
890             }
891         }
892     }
893
894     /**
895      * Returns the trim element for the given id if it exists. This
896      * will not be <code>null</code> if there are entries in the
897      * 'idToFastViewsMap' for this id.
898      *
899      * @param id The id of the view stack to get the trim toolbar for.
900      */

901     public ViewStackTrimToolBar getViewStackTrimToolbar(String JavaDoc id) {
902         return (ViewStackTrimToolBar) tbm.getTrim(id);
903     }
904     
905     public void printFVModel() {
906         Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
907         while (mapIter.hasNext()) {
908             String JavaDoc id = (String JavaDoc) mapIter.next();
909             List JavaDoc fvList = (List JavaDoc) idToFastViewsMap.get(id);
910             System.out.println("FastView: " + id + " count = " + fvList.size()); //$NON-NLS-1$//$NON-NLS-2$
911
for (Iterator JavaDoc fvIter = fvList.iterator(); fvIter.hasNext();) {
912                 IViewReference ref = (IViewReference) fvIter.next();
913                 System.out.println(" Ref: " + ref.getId()); //$NON-NLS-1$
914
}
915         }
916     }
917
918     /**
919      * Informs the manager that a batch operation has started
920      * (say 'maximize', where many stacks will change state).
921      *
922      * @param defer
923      * true when starting a batch operation
924      * false when ending the operation
925      */

926     public void deferUpdates(boolean defer) {
927         if (defer && !deferringUpdates) {
928             deferringUpdates = defer;
929             
930             deferAnimations(true);
931             
932             return;
933         }
934         
935         // 'false': reset and run any necessary updates
936
deferringUpdates = false;
937         deferAnimations(false);
938     }
939
940     /**
941      * When 'defer' is true we create a RectangleAnimation object
942      * to be used for any desired feedback. When ending it
943      * schedules the animation and resets.
944      *
945      * @param defer
946      * true when starting a batch operation
947      * false when ending the operation
948      */

949     private void deferAnimations(boolean defer) {
950         if (defer) {
951             RectangleAnimationFeedbackBase feedback = ((Animations) Tweaklets
952                     .get(Animations.KEY)).createFeedback(wbw.getShell());
953             batchAnimation = new AnimationEngine(feedback, 400);
954             return;
955         }
956
957         if (batchAnimation != null)
958             batchAnimation.schedule();
959         batchAnimation = null;
960     }
961
962     /**
963      * Returns the animation object appropriate for the deferred state
964      * @return Either a 'one-shot' or a 'batch' animation object
965      */

966     private AnimationEngine getDeferrableAnimation() {
967         if (deferringUpdates)
968             return batchAnimation;
969         
970         // Create a 'one-shot' animation
971
RectangleAnimationFeedbackBase feedback = ((Animations) Tweaklets
972                 .get(Animations.KEY)).createFeedback(wbw.getShell());
973         oneShotAnimation = new AnimationEngine(feedback, 400);
974         return oneShotAnimation;
975     }
976     
977     private void scheduleDeferrableAnimation() {
978         if (deferringUpdates)
979             return;
980         
981         // We can only schedule the 'one-shot' animations
982
// the batch ones are sheduled at batch end
983
if (oneShotAnimation != null)
984             oneShotAnimation.schedule();
985         oneShotAnimation = null;
986     }
987
988     /**
989      * Returns the 'bottom/right' trim stack. This is used to
990      * match the old behavior when opening a new view that has no placeholder
991      * in the case where there WB is maximized.
992      *
993      * @return The 'bottom/right' trim stack or null if there are no
994      * defined trim stacks
995      */

996     public ViewStackTrimToolBar getBottomRightTrimStack() {
997         ViewStackTrimToolBar blTrimStack = null;
998         Point blPt = new Point(0,0);
999         
1000        Iterator JavaDoc mapIter = idToFastViewsMap.keySet().iterator();
1001        while (mapIter.hasNext()) {
1002            String JavaDoc id = (String JavaDoc) mapIter.next();
1003            
1004            // Skip the legacy FstViewBar
1005
if (id.equals(FastViewBar.FASTVIEWBAR_ID))
1006                continue;
1007
1008            if (getFastViews(id).size() > 0) {
1009                // if we have views in the model then 'vstt' will not be null
1010
ViewStackTrimToolBar vstt = getViewStackTrimToolbar(id);
1011                Point loc = vstt.getControl().getLocation();
1012                if (loc.y > blPt.y || (loc.y == blPt.y && loc.x > blPt.x)) {
1013                    blPt = loc;
1014                    blTrimStack = vstt;
1015                }
1016            }
1017        }
1018
1019        return blTrimStack;
1020    }
1021}
1022
Popular Tags