KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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  * Kiryl Kazakevich, Intel - bug 88359
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.IStatus;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.action.IContributionItem;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.action.ToolBarManager;
26 import org.eclipse.jface.util.Geometry;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.graphics.Cursor;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.Listener;
38 import org.eclipse.swt.widgets.Menu;
39 import org.eclipse.swt.widgets.ToolBar;
40 import org.eclipse.swt.widgets.ToolItem;
41 import org.eclipse.ui.IMemento;
42 import org.eclipse.ui.IViewReference;
43 import org.eclipse.ui.IWorkbenchPart;
44 import org.eclipse.ui.IWorkbenchPreferenceConstants;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.internal.dnd.AbstractDropTarget;
47 import org.eclipse.ui.internal.dnd.DragUtil;
48 import org.eclipse.ui.internal.dnd.IDragOverListener;
49 import org.eclipse.ui.internal.dnd.IDropTarget;
50 import org.eclipse.ui.internal.layout.CellData;
51 import org.eclipse.ui.internal.layout.CellLayout;
52 import org.eclipse.ui.internal.layout.IWindowTrim;
53 import org.eclipse.ui.internal.layout.LayoutUtil;
54 import org.eclipse.ui.internal.layout.Row;
55 import org.eclipse.ui.internal.util.PrefUtil;
56 import org.eclipse.ui.presentations.PresentationUtil;
57 import org.osgi.framework.Bundle;
58
59 /**
60  * Represents the fast view bar.
61  *
62  * <p>The set of fastviews are obtained from the WorkbenchWindow that
63  * is passed into the constructor. The set of fastviews may be refreshed to
64  * match the state of the perspective by calling the update(...) method.</p>
65  *
66  * @see org.eclipse.ui.internal.FastViewPane
67  */

68 public class FastViewBar implements IWindowTrim {
69     public static String JavaDoc FASTVIEWBAR_ID = "org.eclise.ui.internal.FastViewBar"; //$NON-NLS-1$
70

71     private ToolBarManager fastViewBar;
72     private MenuManager fastViewBarMenuManager;
73     private MenuManager showViewMenuMgr;
74     private FastViewBarContextMenuContribution contextContributionItem;
75
76     private WorkbenchWindow window;
77     private IViewReference selection;
78     
79     // "New Fast View" 'Button' fields
80
private MenuManager newFastViewMenuMgr;
81     private Composite fvbComposite;
82     private ToolBar menuTB;
83     private ToolItem menuItem;
84     private CellData toolBarData;
85
86     private static final int HIDDEN_WIDTH = 5;
87
88
89     private int oldLength = 0;
90     
91     private ViewDropTarget dropTarget;
92
93     private Listener dragListener = new Listener() {
94         public void handleEvent(Event event) {
95             Point position = DragUtil.getEventLoc(event);
96
97             IViewReference ref = getViewAt(position);
98
99             if (ref == null) {
100                 startDraggingFastViewBar(position, false);
101             } else {
102                 startDraggingFastView(ref, position, false);
103             }
104         }
105     };
106
107     // Map of string view IDs onto Booleans (true iff horizontally aligned)
108
private Map JavaDoc viewOrientation = new HashMap JavaDoc();
109
110     private Listener addMenuListener = new Listener() {
111         public void handleEvent(Event event) {
112             Point loc = new Point(event.x, event.y);
113             if (event.type == SWT.MenuDetect) {
114                 showAddFastViewPopup(loc);
115             }
116         }
117     };
118
119     private Listener menuListener = new Listener() {
120         public void handleEvent(Event event) {
121             Point loc = new Point(event.x, event.y);
122             if (event.type == SWT.MenuDetect) {
123                 showFastViewBarPopup(loc);
124             }
125         }
126     };
127     private int fCurrentSide = SWT.DEFAULT;
128
129     private boolean hasNewFastViewDisabled = false;
130
131     class ViewDropTarget extends AbstractDropTarget {
132         List JavaDoc panes;
133
134         ToolItem position;
135
136         /**
137          * @param panesToDrop the list of ViewPanes to drop at the given position
138          */

139         public ViewDropTarget(List JavaDoc panesToDrop, ToolItem position) {
140             setTarget(panesToDrop, position);
141         }
142         
143         public void setTarget(List JavaDoc panesToDrop, ToolItem position) {
144             panes = panesToDrop;
145             this.position = position;
146         }
147
148         /* (non-Javadoc)
149          * @see org.eclipse.ui.internal.dnd.IDropTarget#drop()
150          */

151         public void drop() {
152             IViewReference view = getViewFor(position);
153
154             Iterator JavaDoc iter = panes.iterator();
155             while (iter.hasNext()) {
156                 ViewPane pane = (ViewPane) iter.next();
157                 IViewReference ref = pane.getViewReference();
158                 getPerspective().getFastViewManager().addViewReference(FASTVIEWBAR_ID, getIndex(view), ref, true);
159 // getPage().addFastView(pane.getViewReference());
160
// getPage().getActivePerspective().moveFastView(
161
// pane.getViewReference(), view);
162
}
163             update(true);
164         }
165
166         /* (non-Javadoc)
167          * @see org.eclipse.ui.internal.dnd.IDropTarget#getCursor()
168          */

169         public Cursor getCursor() {
170             return DragCursors.getCursor(DragCursors.FASTVIEW);
171         }
172
173         public Rectangle getSnapRectangle() {
174             if (position == null) {
175                 // As long as the toolbar is not empty, highlight the place
176
// where this view will appear (we
177
// may have compressed it to save space when empty, so the actual
178
// icon location may not be over the toolbar when it is empty)
179
if (getToolBar().getItemCount() > 0) {
180                     return getLocationOfNextIcon();
181                 }
182                 // If the toolbar is empty, highlight the entire toolbar
183
return DragUtil.getDisplayBounds(getControl());
184             }
185
186             return Geometry.toDisplay(getToolBar(), position.getBounds());
187         }
188     }
189     
190     /**
191      * Constructs a new fast view bar for the given workbench window.
192      *
193      * @param theWindow
194      */

195     public FastViewBar(WorkbenchWindow theWindow) {
196         window = theWindow;
197
198         hasNewFastViewDisabled = PrefUtil.getAPIPreferenceStore().getBoolean(
199                 IWorkbenchPreferenceConstants.DISABLE_NEW_FAST_VIEW);
200
201         // Construct the context menu for the fast view bar area
202
fastViewBarMenuManager = new MenuManager();
203         contextContributionItem = new FastViewBarContextMenuContribution(this);
204         fastViewBarMenuManager.add(contextContributionItem);
205
206         if (!hasNewFastViewDisabled) {
207             // Add "New Fast View" submenu
208
showViewMenuMgr = new MenuManager(WorkbenchMessages.FastViewBar_show_view, "showView"); //$NON-NLS-1$
209
IContributionItem showViewMenu = new ShowViewMenu(window, ShowViewMenu.class.getName(), true);
210             showViewMenuMgr.add(showViewMenu);
211             fastViewBarMenuManager.add(showViewMenuMgr);
212
213             // Construct the context menu for the "New Fast View" 'button'
214
newFastViewMenuMgr = new MenuManager(WorkbenchMessages.FastViewBar_show_view, "showView"); //$NON-NLS-1$
215
showViewMenu = new ShowViewMenu(window, ShowViewMenu.class.getName(), true);
216             newFastViewMenuMgr.add(showViewMenu);
217         }
218     }
219
220     /**
221      * Returns the platform's idea of where the fast view bar should be docked in a fresh
222      * workspace. This value is meaningless after a workspace has been setup, since the
223      * fast view bar state is then persisted in the workbench. This preference is just
224      * used for applications that want the initial docking location to be somewhere other
225      * than bottom.
226      * @return the initial side to dock on
227      */

228     public static int getInitialSide() {
229         String JavaDoc loc = PrefUtil.getAPIPreferenceStore().getString(
230                 IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION);
231
232         if (IWorkbenchPreferenceConstants.BOTTOM.equals(loc)) {
233             return SWT.BOTTOM;
234         }
235         if (IWorkbenchPreferenceConstants.LEFT.equals(loc)) {
236             return SWT.LEFT;
237         }
238         if (IWorkbenchPreferenceConstants.RIGHT.equals(loc)) {
239             return SWT.RIGHT;
240         }
241
242         Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
243         if (bundle != null) {
244             IStatus status = new Status(
245                     IStatus.WARNING,
246                     PlatformUI.PLUGIN_ID,
247                     IStatus.WARNING,
248                     "Invalid value for " //$NON-NLS-1$
249
+ PlatformUI.PLUGIN_ID
250                             + "/" //$NON-NLS-1$
251
+ IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION
252                             + " preference. Value \"" + loc //$NON-NLS-1$
253
+ "\" should be one of \"" //$NON-NLS-1$
254
+ IWorkbenchPreferenceConstants.LEFT + "\", \"" //$NON-NLS-1$
255
+ IWorkbenchPreferenceConstants.BOTTOM
256                             + "\", or \"" //$NON-NLS-1$
257
+ IWorkbenchPreferenceConstants.RIGHT + "\".", null); //$NON-NLS-1$
258
Platform.getLog(bundle).log(status);
259         }
260
261         // use bottom as the default-default
262
return SWT.BOTTOM;
263     }
264
265     public void setOrientation(IViewReference refToSet, int newState) {
266         if (newState == getOrientation(refToSet)) {
267             return;
268         }
269
270         viewOrientation.put(refToSet.getId(), new Integer JavaDoc(newState));
271         Perspective persp = getPerspective();
272
273         if (persp != null) {
274             IViewReference ref = persp.getActiveFastView();
275             if (ref != null) {
276                 persp.setActiveFastView(null);
277             }
278             persp.setActiveFastView(refToSet);
279         }
280     }
281
282     /**
283      * Returns the active workbench page or null if none
284      */

285     private WorkbenchPage getPage() {
286         if (window == null) {
287             return null;
288         }
289
290         return window.getActiveWorkbenchPage();
291     }
292
293     /**
294      * Returns the current perspective or null if none
295      */

296     private Perspective getPerspective() {
297
298         WorkbenchPage page = getPage();
299
300         if (page == null) {
301             return null;
302         }
303
304         return page.getActivePerspective();
305     }
306
307     /**
308      * Creates the underlying SWT fvbComposite for the fast view bar. Will add exactly
309      * one new fvbComposite to the given composite. Makes no assumptions about the layout
310      * being used in the parent composite.
311      *
312      * @param parent enclosing SWT composite
313      */

314     public void createControl(Composite parent) {
315         fvbComposite = new Composite(parent, SWT.NONE) {
316             public Point computeSize(int wHint, int hHint, boolean changed) {
317                 Point size = super.computeSize(wHint, hHint, changed);
318                 if (Geometry.isHorizontal(getSide())) {
319                     if (size.y < TrimUtil.TRIM_DEFAULT_HEIGHT) {
320                         size.y = TrimUtil.TRIM_DEFAULT_HEIGHT;
321                     }
322                 } else {
323                     if (size.x < TrimUtil.TRIM_DEFAULT_HEIGHT) {
324                         size.x = TrimUtil.TRIM_DEFAULT_HEIGHT;
325                     }
326                 }
327                 return size;
328             }};
329         String JavaDoc tip = WorkbenchMessages.FastViewBar_0;
330         fvbComposite.setToolTipText(tip);
331
332         fvbComposite.addListener(SWT.MenuDetect, menuListener);
333         PresentationUtil.addDragListener(fvbComposite, dragListener);
334
335         createChildControls();
336     }
337
338     /**
339      * Create the contents of the fast view bar. The top-level fvbComposite (created by createControl) is a
340      * composite that is created once over the lifetime of the fast view bar. This method creates the
341      * rest of the widgetry inside that composite. The controls created by this method will be
342      * destroyed and recreated if the fast view bar is docked to a different side of the window.
343      */

344     protected void createChildControls() {
345         int newSide = getSide();
346         int orientation = Geometry.isHorizontal(newSide) ? SWT.HORIZONTAL
347                 : SWT.VERTICAL;
348         
349         // Create a ControlLayout apropriate for the new orientation
350
CellLayout controlLayout;
351         if (Geometry.isHorizontal(newSide)) {
352             controlLayout = new CellLayout(0)
353                 .setMargins(0, 0)
354                 .setDefaultRow(Row.growing())
355                 .setDefaultColumn(Row.fixed())
356                 .setColumn(1, Row.growing());
357         } else {
358             controlLayout = new CellLayout(1)
359                 .setMargins(0, 3)
360                 .setDefaultColumn(Row.growing())
361                 .setDefaultRow(Row.fixed())
362                 .setRow(1, Row.growing());
363         }
364         
365         // Set up the composite for the new orientation
366
fvbComposite.setLayout(controlLayout);
367
368         if (!hasNewFastViewDisabled) {
369             // Create a toolbar to show an 'Add FastView' menu 'button'
370
menuTB = new ToolBar(fvbComposite, SWT.FLAT | orientation);
371
372             // Construct an item to act as a 'menu button' (a la the PerspectiveSwitcher)
373
menuItem = new ToolItem(menuTB, SWT.PUSH, 0);
374
375             Image tbImage = WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_ETOOL_NEW_FASTVIEW);
376             menuItem.setImage(tbImage);
377
378             String JavaDoc menuTip = WorkbenchMessages.FastViewBar_0;
379             menuItem.setToolTipText(menuTip);
380             //new ToolItem(menuTB, SWT.SEPARATOR, 1);
381

382             // Now that the ToolBar is populated calculate its size...
383
Point size = menuTB.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
384             menuTB.setBounds(0, 0, size.x, size.y);
385
386             // Bring up the 'Add Fast View' menu on a left -or- right button click
387
// Right click (context menu)
388
menuItem.addListener(SWT.MenuDetect, addMenuListener);
389             menuTB.addListener(SWT.MenuDetect, addMenuListener);
390
391             // Left Click...
392
menuItem.addSelectionListener(new SelectionListener() {
393                 public void widgetSelected(SelectionEvent e) {
394                     Rectangle bb = DragUtil.getDisplayBounds(menuTB);
395                     showAddFastViewPopup(new Point(bb.x,bb.y+bb.height));
396                 }
397
398                 public void widgetDefaultSelected(SelectionEvent e) {
399                 }
400
401             });
402
403             // try to get the layout correct...
404
toolBarData = new CellData();
405             toolBarData.align(SWT.FILL, SWT.FILL);
406             menuTB.setLayoutData(toolBarData);
407         }
408
409         // Construct the ToolBar containing the 'Fast' views
410
fastViewBar = new ToolBarManager(SWT.FLAT | SWT.WRAP | orientation);
411         fastViewBar.add(new ShowFastViewContribution(window));
412
413         fastViewBar.createControl(fvbComposite);
414
415         getToolBar().addListener(SWT.MenuDetect, menuListener);
416
417         IDragOverListener fastViewDragTarget = new IDragOverListener() {
418
419             public IDropTarget drag(Control currentControl,
420                     Object JavaDoc draggedObject, Point position,
421                     Rectangle dragRectangle) {
422                 ToolItem targetItem = getToolItem(position);
423                 if (draggedObject instanceof ViewPane) {
424                     ViewPane pane = (ViewPane) draggedObject;
425
426                     // Can't drag views between windows
427
if (pane.getWorkbenchWindow() != window) {
428                         return null;
429                     }
430
431                     List JavaDoc newList = new ArrayList JavaDoc(1);
432                     newList.add(draggedObject);
433
434                     return createDropTarget(newList, targetItem);
435                 }
436                 if (draggedObject instanceof ViewStack) {
437                     ViewStack folder = (ViewStack) draggedObject;
438
439                     if (folder.getWorkbenchWindow() != window) {
440                         return null;
441                     }
442
443                     List JavaDoc viewList = new ArrayList JavaDoc(folder.getItemCount());
444                     LayoutPart[] children = folder.getChildren();
445
446                     for (int idx = 0; idx < children.length; idx++) {
447                         if (!(children[idx] instanceof PartPlaceholder)) {
448                             viewList.add(children[idx]);
449                         }
450                     }
451
452                     return createDropTarget(viewList, targetItem);
453                 }
454
455                 return null;
456             }
457
458         };
459
460         toolBarData = new CellData();
461         toolBarData.align(SWT.FILL, SWT.FILL);
462
463         getToolBar().setLayoutData(toolBarData);
464         PresentationUtil.addDragListener(getToolBar(), dragListener);
465         DragUtil.addDragTarget(getControl(), fastViewDragTarget);
466
467         update(true);
468     }
469
470     /**
471      * Creates and returns a drop target with the given properties. To save object allocation,
472      * the same instance is saved and reused wherever possible.
473      *
474      * @param targetItem
475      * @param viewList
476      * @since 3.1
477      */

478     private IDropTarget createDropTarget(List JavaDoc viewList, ToolItem targetItem) {
479         if (dropTarget == null) {
480             dropTarget = new ViewDropTarget(viewList, targetItem);
481         } else {
482             dropTarget.setTarget(viewList, targetItem);
483         }
484         return dropTarget;
485     }
486     
487     /**
488      * Begins dragging a particular fast view
489      *
490      * @param ref
491      * @param position
492      */

493     protected void startDraggingFastView(IViewReference ref, Point position,
494             boolean usingKeyboard) {
495         ViewPane pane = (ViewPane) ((WorkbenchPartReference) ref).getPane();
496
497         ToolItem item = itemFor(pane.getViewReference());
498
499         Rectangle dragRect = Geometry.toDisplay(getToolBar(), item.getBounds());
500
501         startDrag(((WorkbenchPartReference) ref).getPane(), dragRect, position,
502                 usingKeyboard);
503     }
504
505     private void startDrag(Object JavaDoc toDrag, Rectangle dragRect, Point position,
506             boolean usingKeyboard) {
507
508         Perspective persp = getPerspective();
509
510         WorkbenchPage page = getPage();
511
512         IViewReference oldFastView = null;
513         if (persp != null) {
514             oldFastView = persp.getActiveFastView();
515
516             if (page != null) {
517                 page.hideFastView();
518             }
519         }
520
521         if (page.isZoomed()) {
522             page.zoomOut();
523         }
524
525         boolean success = DragUtil.performDrag(toDrag, dragRect, position,
526                 !usingKeyboard);
527
528         // If the drag was cancelled, reopen the old fast view
529
if (!success && oldFastView != null && page != null) {
530             page.toggleFastView(oldFastView);
531         }
532     }
533
534     /**
535      * Begins dragging the fast view bar
536      *
537      * @param position initial mouse position
538      * @param usingKeyboard true iff the bar is being dragged using the keyboard
539      */

540     protected void startDraggingFastViewBar(Point position,
541             boolean usingKeyboard) {
542         Rectangle dragRect = DragUtil.getDisplayBounds(fvbComposite);
543
544         startDrag(this, dragRect, position, usingKeyboard);
545     }
546
547     /**
548      * Returns the toolbar for the fastview bar.
549      */

550     private ToolBar getToolBar() {
551         return fastViewBar.getControl();
552     }
553
554     private IViewReference getViewFor(ToolItem item) {
555         if (item == null) {
556             return null;
557         }
558
559         return (IViewReference) item
560                 .getData(ShowFastViewContribution.FAST_VIEW);
561     }
562
563     /**
564      * Returns the view at the given position, or null if none
565      *
566      * @param position to test, in display coordinates
567      * @return the view at the given position or null if none
568      */

569     private IViewReference getViewAt(Point position) {
570         return getViewFor(getToolItem(position));
571     }
572
573     /**
574      * Returns the toolbar item at the given position, in display coordinates
575      * @param position
576      */

577     private ToolItem getToolItem(Point position) {
578         ToolBar toolbar = getToolBar();
579         Point local = toolbar.toControl(position);
580         return toolbar.getItem(local);
581     }
582
583     /**
584      * Shows the popup menu for an item in the fast view bar.
585      */

586     private void showFastViewBarPopup(Point pt) {
587         // Get the tool item under the mouse.
588

589         ToolBar toolBar = getToolBar();
590
591         Menu menu = fastViewBarMenuManager.createContextMenu(toolBar);
592
593         IViewReference selectedView = getViewAt(pt);
594         contextContributionItem.setTarget(selectedView);
595
596         menu.setLocation(pt.x, pt.y);
597         menu.setVisible(true);
598     }
599
600     /**
601      * Shows the popup menu for an item in the fast view bar.
602      */

603     private void showAddFastViewPopup(Point pt) {
604         Menu menu = newFastViewMenuMgr.createContextMenu(menuTB);
605         menu.setLocation(pt.x, pt.y);
606         menu.setVisible(true);
607     }
608
609     public int getOrientation(IViewReference ref) {
610         return isHorizontal(ref) ? SWT.HORIZONTAL : SWT.VERTICAL;
611     }
612
613     /**
614      * Returns the underlying SWT fvbComposite for the fast view bar, or null if
615      * createControl has not yet been invoked. The caller must not make any
616      * assumptions about the type of Control that is returned.
617      *
618      * @return the underlying SWT fvbComposite for the fast view bar
619      */

620     public Control getControl() {
621         return fvbComposite;
622     }
623
624     public void dispose() {
625         fastViewBarMenuManager.dispose();
626
627         disposeChildControls();
628     }
629
630     protected void disposeChildControls() {
631         fastViewBar.dispose();
632         fastViewBar = null;
633
634         if (menuItem != null) {
635             menuItem.dispose();
636             menuTB.dispose();
637         }
638         
639         oldLength = 0;
640     }
641
642     
643     /**
644      * Refreshes the contents to match the fast views in the window's
645      * current perspective.
646      *
647      * @param force
648      */

649     public void update(boolean force) {
650         fastViewBar.update(force);
651         ToolItem[] items = fastViewBar.getControl().getItems();
652
653         updateLayoutData();
654
655         for (int idx = 0; idx < items.length; idx++) {
656             IViewReference view = getViewFor(items[idx]);
657
658             viewOrientation.put(view.getId(), new Integer JavaDoc(
659                     isHorizontal(view) ? SWT.HORIZONTAL : SWT.VERTICAL));
660         }
661     }
662     
663     private void updateLayoutData() {
664         ToolItem[] items = fastViewBar.getControl().getItems();
665         boolean isHorizontal = Geometry.isHorizontal(getSide());
666         boolean shouldExpand = items.length > 0;
667
668         Point hint = new Point(32, shouldExpand ? SWT.DEFAULT : HIDDEN_WIDTH);
669         
670         if (!isHorizontal) {
671             Geometry.flipXY(hint);
672         }
673         
674         if (shouldExpand) {
675             toolBarData.setHint(CellData.MINIMUM, hint);
676         } else {
677             toolBarData.setHint(CellData.OVERRIDE, hint);
678         }
679    
680         if (items.length != oldLength) {
681             LayoutUtil.resize(fvbComposite);
682             oldLength = items.length;
683         }
684     }
685
686     /**
687      * Returns the currently selected fastview
688      *
689      * @return the currently selected fastview or null if none
690      */

691     public IViewReference getSelection() {
692         return selection;
693     }
694
695     /**
696      * Sets the currently selected fastview.
697      *
698      * @param selected the currently selected fastview, or null if none
699      */

700     public void setSelection(IViewReference selected) {
701
702         ToolItem[] items = fastViewBar.getControl().getItems();
703         for (int i = 0; i < items.length; i++) {
704             ToolItem item = items[i];
705             item.setSelection(getView(item) == selected);
706         }
707
708         selection = selected;
709     }
710
711     /**
712      * Returns the view associated with the given toolbar item
713      *
714      * @param item
715      */

716     private IViewReference getView(ToolItem item) {
717         return (IViewReference) item
718                 .getData(ShowFastViewContribution.FAST_VIEW);
719     }
720
721     private int getIndex(IViewReference toFind) {
722         ToolItem[] items = fastViewBar.getControl().getItems();
723         for (int i = 0; i < items.length; i++) {
724             if (items[i].getData(ShowFastViewContribution.FAST_VIEW) == toFind) {
725                 return i;
726             }
727         }
728
729         return items.length;
730     }
731
732     private ToolItem getItem(int idx) {
733         ToolItem[] items = fastViewBar.getControl().getItems();
734         if (idx >= items.length) {
735             return null;
736         }
737
738         return items[idx];
739     }
740
741     /**
742      * Returns the toolbar item associated with the given view
743      *
744      * @param toFind
745      */

746     private ToolItem itemFor(IViewReference toFind) {
747         return getItem(getIndex(toFind));
748     }
749
750     /* (non-Javadoc)
751      * @see org.eclipse.ui.internal.IWindowTrim#getValidSides()
752      */

753     public int getValidSides() {
754         return SWT.TOP | SWT.LEFT | SWT.RIGHT | SWT.BOTTOM;
755     }
756
757     /* (non-Javadoc)
758      * @see org.eclipse.ui.internal.IWindowTrim#docked(int)
759      */

760     public void dock(int side) {
761         fCurrentSide = side;
762         disposeChildControls();
763         createChildControls();
764     }
765
766     /**
767      * Get the current side.
768      * @return SWT.BOTTOM or SWT.RIGHT or SWT.LEFT
769      */

770     public int getSide() {
771         if (fCurrentSide==SWT.DEFAULT) {
772             fCurrentSide = getInitialSide();
773         }
774         return fCurrentSide;
775     }
776
777
778     private boolean isHorizontal(IViewReference ref) {
779         Integer JavaDoc orientation = (Integer JavaDoc) viewOrientation.get(ref.getId());
780         boolean horizontalBar = Geometry.isHorizontal(getSide());
781         boolean horizontal = horizontalBar;
782         if (orientation != null) {
783             horizontal = orientation.intValue() == SWT.HORIZONTAL;
784         } else {
785             horizontal = false;
786         }
787
788         return horizontal;
789     }
790
791     /**
792      * @param ref
793      */

794     public int getViewSide(IViewReference ref) {
795         boolean horizontal = isHorizontal(ref);
796
797         if (horizontal) {
798             return (getSide() == SWT.BOTTOM) ? SWT.BOTTOM : SWT.TOP;
799         }
800         
801         return (getSide() == SWT.RIGHT) ? SWT.RIGHT : SWT.LEFT;
802     }
803
804     public void saveState(IMemento memento) {
805         memento.putInteger(IWorkbenchConstants.TAG_FAST_VIEW_SIDE, getSide());
806
807         Iterator JavaDoc iter = viewOrientation.keySet().iterator();
808         while (iter.hasNext()) {
809             String JavaDoc next = (String JavaDoc) iter.next();
810             IMemento orientation = memento
811                     .createChild(IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION);
812
813             orientation.putString(IWorkbenchConstants.TAG_VIEW, next);
814             orientation.putInteger(IWorkbenchConstants.TAG_POSITION,
815                     ((Integer JavaDoc) viewOrientation.get(next)).intValue());
816         }
817
818     }
819
820     /**
821      * Returns the approximate location where the next fastview icon
822      * will be drawn (display coordinates)
823      */

824     public Rectangle getLocationOfNextIcon() {
825         ToolBar control = getToolBar();
826
827         Rectangle result = control.getBounds();
828         Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
829         result.height = size.y;
830         result.width = size.x;
831         
832         boolean horizontal = Geometry.isHorizontal(getSide());
833         if (control.getItemCount() == 0) {
834             Geometry.setDimension(result, horizontal, 0);
835         }
836         
837         int hoverSide = horizontal ? SWT.RIGHT : SWT.BOTTOM;
838
839         result = Geometry.getExtrudedEdge(result, -Geometry.getDimension(
840                 result, !horizontal), hoverSide);
841
842         return Geometry.toDisplay(control.getParent(), result);
843     }
844
845     public void restoreState(IMemento memento) {
846         Integer JavaDoc bigInt;
847         bigInt = memento.getInteger(IWorkbenchConstants.TAG_FAST_VIEW_SIDE);
848         if (bigInt != null) {
849             dock(bigInt.intValue());
850         }
851
852         IMemento[] orientations = memento
853                 .getChildren(IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION);
854         for (int i = 0; i < orientations.length; i++) {
855             IMemento next = orientations[i];
856
857             viewOrientation.put(next.getString(IWorkbenchConstants.TAG_VIEW),
858                     next.getInteger(IWorkbenchConstants.TAG_POSITION));
859         }
860     }
861     
862     public WorkbenchWindow getWindow() {
863         return window;
864     }
865     
866     public void restoreView(IViewReference selectedView) {
867         if (selectedView != null) {
868             WorkbenchPage page = window.getActiveWorkbenchPage();
869             if (page != null) {
870                 int idx = getIndex(selectedView);
871                 ToolItem item = getItem(idx);
872                 Rectangle bounds = item.getBounds();
873                 Rectangle startBounds = Geometry.toDisplay(item
874                         .getParent(), bounds);
875
876                 Perspective persp = getPerspective();
877                 if (persp != null) {
878                     persp.getFastViewManager().removeViewReference(selectedView, true, true);
879                 }
880
881                 IWorkbenchPart toActivate = selectedView
882                         .getPart(true);
883                 if (toActivate != null) {
884                     page.activate(toActivate);
885                 }
886
887                 ViewPane pane = (ViewPane) ((WorkbenchPartReference) selectedView)
888                         .getPane();
889
890                 RectangleAnimation animation = new RectangleAnimation(
891                         window.getShell(), startBounds, pane
892                                 .getParentBounds());
893
894                 animation.schedule();
895             }
896         }
897     }
898
899     /**
900      * @return The list of all view references in the stack
901      */

902     public List JavaDoc getViewRefs() {
903         List JavaDoc refs = new ArrayList JavaDoc(fastViewBar.getControl().getItemCount());
904         ToolItem[] items = fastViewBar.getControl().getItems();
905         for (int i = 0; i < items.length; i++) {
906             Object JavaDoc data = items[i].getData(ShowFastViewContribution.FAST_VIEW);
907             if (data != null)
908                 refs.add(data);
909         }
910         
911         return refs;
912     }
913
914     /* (non-Javadoc)
915      * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
916      */

917     public boolean isCloseable() {
918         return false;
919     }
920
921     /* (non-Javadoc)
922      * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
923      */

924     public void handleClose() {
925         // nothing to do...
926
}
927     
928     /* (non-Javadoc)
929      * @see org.eclipse.ui.internal.IWindowTrim#getId()
930      */

931     public String JavaDoc getId() {
932         return "org.eclise.ui.internal.FastViewBar"; //$NON-NLS-1$
933
}
934
935     /* (non-Javadoc)
936      * @see org.eclipse.ui.internal.IWindowTrim#getDisplayName()
937      */

938     public String JavaDoc getDisplayName() {
939         return WorkbenchMessages.TrimCommon_FastView_TrimName;
940     }
941
942     /**
943      * Returns the context menu contribution item. This is for
944      * internal UI testing only.
945      *
946      * @return the context menu contribution item
947      * @since 3.1.1
948      */

949     public FastViewBarContextMenuContribution testContextMenu() {
950         return contextContributionItem;
951     }
952
953     /* (non-Javadoc)
954      * @see org.eclipse.ui.IWindowTrim#getWidthHint()
955      */

956     public int getWidthHint() {
957         return SWT.DEFAULT;
958     }
959
960     /* (non-Javadoc)
961      * @see org.eclipse.ui.IWindowTrim#getHeightHint()
962      */

963     public int getHeightHint() {
964         return SWT.DEFAULT;
965     }
966
967     /* (non-Javadoc)
968      * @see org.eclipse.ui.IWindowTrim#isResizeable()
969      */

970     public boolean isResizeable() {
971         return false;
972     }
973 }
974
Popular Tags