001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Kiryl Kazakevich, Intel - bug 88359
011: *******************************************************************************/package org.eclipse.ui.internal;
012:
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018:
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.jface.action.IContributionItem;
023: import org.eclipse.jface.action.MenuManager;
024: import org.eclipse.jface.action.ToolBarManager;
025: import org.eclipse.jface.util.Geometry;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.events.SelectionListener;
029: import org.eclipse.swt.graphics.Cursor;
030: import org.eclipse.swt.graphics.Image;
031: import org.eclipse.swt.graphics.Point;
032: import org.eclipse.swt.graphics.Rectangle;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Event;
036: import org.eclipse.swt.widgets.Listener;
037: import org.eclipse.swt.widgets.Menu;
038: import org.eclipse.swt.widgets.ToolBar;
039: import org.eclipse.swt.widgets.ToolItem;
040: import org.eclipse.ui.IMemento;
041: import org.eclipse.ui.IViewReference;
042: import org.eclipse.ui.IWorkbenchPart;
043: import org.eclipse.ui.IWorkbenchPreferenceConstants;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.internal.dnd.AbstractDropTarget;
046: import org.eclipse.ui.internal.dnd.DragUtil;
047: import org.eclipse.ui.internal.dnd.IDragOverListener;
048: import org.eclipse.ui.internal.dnd.IDropTarget;
049: import org.eclipse.ui.internal.layout.CellData;
050: import org.eclipse.ui.internal.layout.CellLayout;
051: import org.eclipse.ui.internal.layout.IWindowTrim;
052: import org.eclipse.ui.internal.layout.LayoutUtil;
053: import org.eclipse.ui.internal.layout.Row;
054: import org.eclipse.ui.internal.util.PrefUtil;
055: import org.eclipse.ui.presentations.PresentationUtil;
056: import org.osgi.framework.Bundle;
057:
058: /**
059: * Represents the fast view bar.
060: *
061: * <p>The set of fastviews are obtained from the WorkbenchWindow that
062: * is passed into the constructor. The set of fastviews may be refreshed to
063: * match the state of the perspective by calling the update(...) method.</p>
064: *
065: * @see org.eclipse.ui.internal.FastViewPane
066: */
067: public class FastViewBar implements IWindowTrim {
068: public static String FASTVIEWBAR_ID = "org.eclise.ui.internal.FastViewBar"; //$NON-NLS-1$
069:
070: private ToolBarManager fastViewBar;
071: private MenuManager fastViewBarMenuManager;
072: private MenuManager showViewMenuMgr;
073: private FastViewBarContextMenuContribution contextContributionItem;
074:
075: private WorkbenchWindow window;
076: private IViewReference selection;
077:
078: // "New Fast View" 'Button' fields
079: private MenuManager newFastViewMenuMgr;
080: private Composite fvbComposite;
081: private ToolBar menuTB;
082: private ToolItem menuItem;
083: private CellData toolBarData;
084:
085: private static final int HIDDEN_WIDTH = 5;
086:
087: private int oldLength = 0;
088:
089: private ViewDropTarget dropTarget;
090:
091: private Listener dragListener = new Listener() {
092: public void handleEvent(Event event) {
093: Point position = DragUtil.getEventLoc(event);
094:
095: IViewReference ref = getViewAt(position);
096:
097: if (ref == null) {
098: startDraggingFastViewBar(position, false);
099: } else {
100: startDraggingFastView(ref, position, false);
101: }
102: }
103: };
104:
105: // Map of string view IDs onto Booleans (true iff horizontally aligned)
106: private Map viewOrientation = new HashMap();
107:
108: private Listener addMenuListener = new Listener() {
109: public void handleEvent(Event event) {
110: Point loc = new Point(event.x, event.y);
111: if (event.type == SWT.MenuDetect) {
112: showAddFastViewPopup(loc);
113: }
114: }
115: };
116:
117: private Listener menuListener = new Listener() {
118: public void handleEvent(Event event) {
119: Point loc = new Point(event.x, event.y);
120: if (event.type == SWT.MenuDetect) {
121: showFastViewBarPopup(loc);
122: }
123: }
124: };
125: private int fCurrentSide = SWT.DEFAULT;
126:
127: private boolean hasNewFastViewDisabled = false;
128:
129: class ViewDropTarget extends AbstractDropTarget {
130: List panes;
131:
132: ToolItem position;
133:
134: /**
135: * @param panesToDrop the list of ViewPanes to drop at the given position
136: */
137: public ViewDropTarget(List panesToDrop, ToolItem position) {
138: setTarget(panesToDrop, position);
139: }
140:
141: public void setTarget(List panesToDrop, ToolItem position) {
142: panes = panesToDrop;
143: this .position = position;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.eclipse.ui.internal.dnd.IDropTarget#drop()
148: */
149: public void drop() {
150: IViewReference view = getViewFor(position);
151:
152: Iterator iter = panes.iterator();
153: while (iter.hasNext()) {
154: ViewPane pane = (ViewPane) iter.next();
155: IViewReference ref = pane.getViewReference();
156: getPerspective().getFastViewManager().addViewReference(
157: FASTVIEWBAR_ID, getIndex(view), ref, true);
158: // getPage().addFastView(pane.getViewReference());
159: // getPage().getActivePerspective().moveFastView(
160: // pane.getViewReference(), view);
161: }
162: update(true);
163: }
164:
165: /* (non-Javadoc)
166: * @see org.eclipse.ui.internal.dnd.IDropTarget#getCursor()
167: */
168: public Cursor getCursor() {
169: return DragCursors.getCursor(DragCursors.FASTVIEW);
170: }
171:
172: public Rectangle getSnapRectangle() {
173: if (position == null) {
174: // As long as the toolbar is not empty, highlight the place
175: // where this view will appear (we
176: // may have compressed it to save space when empty, so the actual
177: // icon location may not be over the toolbar when it is empty)
178: if (getToolBar().getItemCount() > 0) {
179: return getLocationOfNextIcon();
180: }
181: // If the toolbar is empty, highlight the entire toolbar
182: return DragUtil.getDisplayBounds(getControl());
183: }
184:
185: return Geometry.toDisplay(getToolBar(), position
186: .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
199: .getAPIPreferenceStore()
200: .getBoolean(
201: IWorkbenchPreferenceConstants.DISABLE_NEW_FAST_VIEW);
202:
203: // Construct the context menu for the fast view bar area
204: fastViewBarMenuManager = new MenuManager();
205: contextContributionItem = new FastViewBarContextMenuContribution(
206: this );
207: fastViewBarMenuManager.add(contextContributionItem);
208:
209: if (!hasNewFastViewDisabled) {
210: // Add "New Fast View" submenu
211: showViewMenuMgr = new MenuManager(
212: WorkbenchMessages.FastViewBar_show_view, "showView"); //$NON-NLS-1$
213: IContributionItem showViewMenu = new ShowViewMenu(window,
214: ShowViewMenu.class.getName(), true);
215: showViewMenuMgr.add(showViewMenu);
216: fastViewBarMenuManager.add(showViewMenuMgr);
217:
218: // Construct the context menu for the "New Fast View" 'button'
219: newFastViewMenuMgr = new MenuManager(
220: WorkbenchMessages.FastViewBar_show_view, "showView"); //$NON-NLS-1$
221: showViewMenu = new ShowViewMenu(window, ShowViewMenu.class
222: .getName(), true);
223: newFastViewMenuMgr.add(showViewMenu);
224: }
225: }
226:
227: /**
228: * Returns the platform's idea of where the fast view bar should be docked in a fresh
229: * workspace. This value is meaningless after a workspace has been setup, since the
230: * fast view bar state is then persisted in the workbench. This preference is just
231: * used for applications that want the initial docking location to be somewhere other
232: * than bottom.
233: * @return the initial side to dock on
234: */
235: public static int getInitialSide() {
236: String loc = PrefUtil
237: .getAPIPreferenceStore()
238: .getString(
239: IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION);
240:
241: if (IWorkbenchPreferenceConstants.BOTTOM.equals(loc)) {
242: return SWT.BOTTOM;
243: }
244: if (IWorkbenchPreferenceConstants.LEFT.equals(loc)) {
245: return SWT.LEFT;
246: }
247: if (IWorkbenchPreferenceConstants.RIGHT.equals(loc)) {
248: return SWT.RIGHT;
249: }
250:
251: Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
252: if (bundle != null) {
253: IStatus status = new Status(
254: IStatus.WARNING,
255: PlatformUI.PLUGIN_ID,
256: IStatus.WARNING,
257: "Invalid value for " //$NON-NLS-1$
258: + PlatformUI.PLUGIN_ID
259: + "/" //$NON-NLS-1$
260: + IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION
261: + " preference. Value \"" + loc //$NON-NLS-1$
262: + "\" should be one of \"" //$NON-NLS-1$
263: + IWorkbenchPreferenceConstants.LEFT
264: + "\", \"" //$NON-NLS-1$
265: + IWorkbenchPreferenceConstants.BOTTOM
266: + "\", or \"" //$NON-NLS-1$
267: + IWorkbenchPreferenceConstants.RIGHT
268: + "\".", null); //$NON-NLS-1$
269: Platform.getLog(bundle).log(status);
270: }
271:
272: // use bottom as the default-default
273: return SWT.BOTTOM;
274: }
275:
276: public void setOrientation(IViewReference refToSet, int newState) {
277: if (newState == getOrientation(refToSet)) {
278: return;
279: }
280:
281: viewOrientation.put(refToSet.getId(), new Integer(newState));
282: Perspective persp = getPerspective();
283:
284: if (persp != null) {
285: IViewReference ref = persp.getActiveFastView();
286: if (ref != null) {
287: persp.setActiveFastView(null);
288: }
289: persp.setActiveFastView(refToSet);
290: }
291: }
292:
293: /**
294: * Returns the active workbench page or null if none
295: */
296: private WorkbenchPage getPage() {
297: if (window == null) {
298: return null;
299: }
300:
301: return window.getActiveWorkbenchPage();
302: }
303:
304: /**
305: * Returns the current perspective or null if none
306: */
307: private Perspective getPerspective() {
308:
309: WorkbenchPage page = getPage();
310:
311: if (page == null) {
312: return null;
313: }
314:
315: return page.getActivePerspective();
316: }
317:
318: /**
319: * Creates the underlying SWT fvbComposite for the fast view bar. Will add exactly
320: * one new fvbComposite to the given composite. Makes no assumptions about the layout
321: * being used in the parent composite.
322: *
323: * @param parent enclosing SWT composite
324: */
325: public void createControl(Composite parent) {
326: fvbComposite = new Composite(parent, SWT.NONE) {
327: public Point computeSize(int wHint, int hHint,
328: boolean changed) {
329: Point size = super .computeSize(wHint, hHint, changed);
330: if (Geometry.isHorizontal(getSide())) {
331: if (size.y < TrimUtil.TRIM_DEFAULT_HEIGHT) {
332: size.y = TrimUtil.TRIM_DEFAULT_HEIGHT;
333: }
334: } else {
335: if (size.x < TrimUtil.TRIM_DEFAULT_HEIGHT) {
336: size.x = TrimUtil.TRIM_DEFAULT_HEIGHT;
337: }
338: }
339: return size;
340: }
341: };
342: String tip = WorkbenchMessages.FastViewBar_0;
343: fvbComposite.setToolTipText(tip);
344:
345: fvbComposite.addListener(SWT.MenuDetect, menuListener);
346: PresentationUtil.addDragListener(fvbComposite, dragListener);
347:
348: createChildControls();
349: }
350:
351: /**
352: * Create the contents of the fast view bar. The top-level fvbComposite (created by createControl) is a
353: * composite that is created once over the lifetime of the fast view bar. This method creates the
354: * rest of the widgetry inside that composite. The controls created by this method will be
355: * destroyed and recreated if the fast view bar is docked to a different side of the window.
356: */
357: protected void createChildControls() {
358: int newSide = getSide();
359: int orientation = Geometry.isHorizontal(newSide) ? SWT.HORIZONTAL
360: : SWT.VERTICAL;
361:
362: // Create a ControlLayout apropriate for the new orientation
363: CellLayout controlLayout;
364: if (Geometry.isHorizontal(newSide)) {
365: controlLayout = new CellLayout(0).setMargins(0, 0)
366: .setDefaultRow(Row.growing()).setDefaultColumn(
367: Row.fixed()).setColumn(1, Row.growing());
368: } else {
369: controlLayout = new CellLayout(1).setMargins(0, 3)
370: .setDefaultColumn(Row.growing()).setDefaultRow(
371: Row.fixed()).setRow(1, Row.growing());
372: }
373:
374: // Set up the composite for the new orientation
375: fvbComposite.setLayout(controlLayout);
376:
377: if (!hasNewFastViewDisabled) {
378: // Create a toolbar to show an 'Add FastView' menu 'button'
379: menuTB = new ToolBar(fvbComposite, SWT.FLAT | orientation);
380:
381: // Construct an item to act as a 'menu button' (a la the PerspectiveSwitcher)
382: menuItem = new ToolItem(menuTB, SWT.PUSH, 0);
383:
384: Image tbImage = WorkbenchImages
385: .getImage(IWorkbenchGraphicConstants.IMG_ETOOL_NEW_FASTVIEW);
386: menuItem.setImage(tbImage);
387:
388: String menuTip = WorkbenchMessages.FastViewBar_0;
389: menuItem.setToolTipText(menuTip);
390: //new ToolItem(menuTB, SWT.SEPARATOR, 1);
391:
392: // Now that the ToolBar is populated calculate its size...
393: Point size = menuTB.computeSize(SWT.DEFAULT, SWT.DEFAULT,
394: true);
395: menuTB.setBounds(0, 0, size.x, size.y);
396:
397: // Bring up the 'Add Fast View' menu on a left -or- right button click
398: // Right click (context menu)
399: menuItem.addListener(SWT.MenuDetect, addMenuListener);
400: menuTB.addListener(SWT.MenuDetect, addMenuListener);
401:
402: // Left Click...
403: menuItem.addSelectionListener(new SelectionListener() {
404: public void widgetSelected(SelectionEvent e) {
405: Rectangle bb = DragUtil.getDisplayBounds(menuTB);
406: showAddFastViewPopup(new Point(bb.x, bb.y
407: + bb.height));
408: }
409:
410: public void widgetDefaultSelected(SelectionEvent e) {
411: }
412:
413: });
414:
415: // try to get the layout correct...
416: toolBarData = new CellData();
417: toolBarData.align(SWT.FILL, SWT.FILL);
418: menuTB.setLayoutData(toolBarData);
419: }
420:
421: // Construct the ToolBar containing the 'Fast' views
422: fastViewBar = new ToolBarManager(SWT.FLAT | SWT.WRAP
423: | orientation);
424: fastViewBar.add(new ShowFastViewContribution(window));
425:
426: fastViewBar.createControl(fvbComposite);
427:
428: getToolBar().addListener(SWT.MenuDetect, menuListener);
429:
430: IDragOverListener fastViewDragTarget = new IDragOverListener() {
431:
432: public IDropTarget drag(Control currentControl,
433: Object draggedObject, Point position,
434: Rectangle dragRectangle) {
435: ToolItem targetItem = getToolItem(position);
436: if (draggedObject instanceof ViewPane) {
437: ViewPane pane = (ViewPane) draggedObject;
438:
439: // Can't drag views between windows
440: if (pane.getWorkbenchWindow() != window) {
441: return null;
442: }
443:
444: List newList = new ArrayList(1);
445: newList.add(draggedObject);
446:
447: return createDropTarget(newList, targetItem);
448: }
449: if (draggedObject instanceof ViewStack) {
450: ViewStack folder = (ViewStack) draggedObject;
451:
452: if (folder.getWorkbenchWindow() != window) {
453: return null;
454: }
455:
456: List viewList = new ArrayList(folder.getItemCount());
457: LayoutPart[] children = folder.getChildren();
458:
459: for (int idx = 0; idx < children.length; idx++) {
460: if (!(children[idx] instanceof PartPlaceholder)) {
461: viewList.add(children[idx]);
462: }
463: }
464:
465: return createDropTarget(viewList, targetItem);
466: }
467:
468: return null;
469: }
470:
471: };
472:
473: toolBarData = new CellData();
474: toolBarData.align(SWT.FILL, SWT.FILL);
475:
476: getToolBar().setLayoutData(toolBarData);
477: PresentationUtil.addDragListener(getToolBar(), dragListener);
478: DragUtil.addDragTarget(getControl(), fastViewDragTarget);
479:
480: update(true);
481: }
482:
483: /**
484: * Creates and returns a drop target with the given properties. To save object allocation,
485: * the same instance is saved and reused wherever possible.
486: *
487: * @param targetItem
488: * @param viewList
489: * @since 3.1
490: */
491: private IDropTarget createDropTarget(List viewList,
492: ToolItem targetItem) {
493: if (dropTarget == null) {
494: dropTarget = new ViewDropTarget(viewList, targetItem);
495: } else {
496: dropTarget.setTarget(viewList, targetItem);
497: }
498: return dropTarget;
499: }
500:
501: /**
502: * Begins dragging a particular fast view
503: *
504: * @param ref
505: * @param position
506: */
507: protected void startDraggingFastView(IViewReference ref,
508: Point position, boolean usingKeyboard) {
509: ViewPane pane = (ViewPane) ((WorkbenchPartReference) ref)
510: .getPane();
511:
512: ToolItem item = itemFor(pane.getViewReference());
513:
514: Rectangle dragRect = Geometry.toDisplay(getToolBar(), item
515: .getBounds());
516:
517: startDrag(((WorkbenchPartReference) ref).getPane(), dragRect,
518: position, usingKeyboard);
519: }
520:
521: private void startDrag(Object toDrag, Rectangle dragRect,
522: Point position, boolean usingKeyboard) {
523:
524: Perspective persp = getPerspective();
525:
526: WorkbenchPage page = getPage();
527:
528: IViewReference oldFastView = null;
529: if (persp != null) {
530: oldFastView = persp.getActiveFastView();
531:
532: if (page != null) {
533: page.hideFastView();
534: }
535: }
536:
537: if (page.isZoomed()) {
538: page.zoomOut();
539: }
540:
541: boolean success = DragUtil.performDrag(toDrag, dragRect,
542: position, !usingKeyboard);
543:
544: // If the drag was cancelled, reopen the old fast view
545: if (!success && oldFastView != null && page != null) {
546: page.toggleFastView(oldFastView);
547: }
548: }
549:
550: /**
551: * Begins dragging the fast view bar
552: *
553: * @param position initial mouse position
554: * @param usingKeyboard true iff the bar is being dragged using the keyboard
555: */
556: protected void startDraggingFastViewBar(Point position,
557: boolean usingKeyboard) {
558: Rectangle dragRect = DragUtil.getDisplayBounds(fvbComposite);
559:
560: startDrag(this , dragRect, position, usingKeyboard);
561: }
562:
563: /**
564: * Returns the toolbar for the fastview bar.
565: */
566: private ToolBar getToolBar() {
567: return fastViewBar.getControl();
568: }
569:
570: private IViewReference getViewFor(ToolItem item) {
571: if (item == null) {
572: return null;
573: }
574:
575: return (IViewReference) item
576: .getData(ShowFastViewContribution.FAST_VIEW);
577: }
578:
579: /**
580: * Returns the view at the given position, or null if none
581: *
582: * @param position to test, in display coordinates
583: * @return the view at the given position or null if none
584: */
585: private IViewReference getViewAt(Point position) {
586: return getViewFor(getToolItem(position));
587: }
588:
589: /**
590: * Returns the toolbar item at the given position, in display coordinates
591: * @param position
592: */
593: private ToolItem getToolItem(Point position) {
594: ToolBar toolbar = getToolBar();
595: Point local = toolbar.toControl(position);
596: return toolbar.getItem(local);
597: }
598:
599: /**
600: * Shows the popup menu for an item in the fast view bar.
601: */
602: private void showFastViewBarPopup(Point pt) {
603: // Get the tool item under the mouse.
604:
605: ToolBar toolBar = getToolBar();
606:
607: Menu menu = fastViewBarMenuManager.createContextMenu(toolBar);
608:
609: IViewReference selectedView = getViewAt(pt);
610: contextContributionItem.setTarget(selectedView);
611:
612: menu.setLocation(pt.x, pt.y);
613: menu.setVisible(true);
614: }
615:
616: /**
617: * Shows the popup menu for an item in the fast view bar.
618: */
619: private void showAddFastViewPopup(Point pt) {
620: Menu menu = newFastViewMenuMgr.createContextMenu(menuTB);
621: menu.setLocation(pt.x, pt.y);
622: menu.setVisible(true);
623: }
624:
625: public int getOrientation(IViewReference ref) {
626: return isHorizontal(ref) ? SWT.HORIZONTAL : SWT.VERTICAL;
627: }
628:
629: /**
630: * Returns the underlying SWT fvbComposite for the fast view bar, or null if
631: * createControl has not yet been invoked. The caller must not make any
632: * assumptions about the type of Control that is returned.
633: *
634: * @return the underlying SWT fvbComposite for the fast view bar
635: */
636: public Control getControl() {
637: return fvbComposite;
638: }
639:
640: public void dispose() {
641: fastViewBarMenuManager.dispose();
642:
643: disposeChildControls();
644: }
645:
646: protected void disposeChildControls() {
647: fastViewBar.dispose();
648: fastViewBar = null;
649:
650: if (menuItem != null) {
651: menuItem.dispose();
652: menuTB.dispose();
653: }
654:
655: oldLength = 0;
656: }
657:
658: /**
659: * Refreshes the contents to match the fast views in the window's
660: * current perspective.
661: *
662: * @param force
663: */
664: public void update(boolean force) {
665: fastViewBar.update(force);
666: ToolItem[] items = fastViewBar.getControl().getItems();
667:
668: updateLayoutData();
669:
670: for (int idx = 0; idx < items.length; idx++) {
671: IViewReference view = getViewFor(items[idx]);
672:
673: viewOrientation
674: .put(view.getId(), new Integer(
675: isHorizontal(view) ? SWT.HORIZONTAL
676: : SWT.VERTICAL));
677: }
678: }
679:
680: private void updateLayoutData() {
681: ToolItem[] items = fastViewBar.getControl().getItems();
682: boolean isHorizontal = Geometry.isHorizontal(getSide());
683: boolean shouldExpand = items.length > 0;
684:
685: Point hint = new Point(32, shouldExpand ? SWT.DEFAULT
686: : HIDDEN_WIDTH);
687:
688: if (!isHorizontal) {
689: Geometry.flipXY(hint);
690: }
691:
692: if (shouldExpand) {
693: toolBarData.setHint(CellData.MINIMUM, hint);
694: } else {
695: toolBarData.setHint(CellData.OVERRIDE, hint);
696: }
697:
698: if (items.length != oldLength) {
699: LayoutUtil.resize(fvbComposite);
700: oldLength = items.length;
701: }
702: }
703:
704: /**
705: * Returns the currently selected fastview
706: *
707: * @return the currently selected fastview or null if none
708: */
709: public IViewReference getSelection() {
710: return selection;
711: }
712:
713: /**
714: * Sets the currently selected fastview.
715: *
716: * @param selected the currently selected fastview, or null if none
717: */
718: public void setSelection(IViewReference selected) {
719:
720: ToolItem[] items = fastViewBar.getControl().getItems();
721: for (int i = 0; i < items.length; i++) {
722: ToolItem item = items[i];
723: item.setSelection(getView(item) == selected);
724: }
725:
726: selection = selected;
727: }
728:
729: /**
730: * Returns the view associated with the given toolbar item
731: *
732: * @param item
733: */
734: private IViewReference getView(ToolItem item) {
735: return (IViewReference) item
736: .getData(ShowFastViewContribution.FAST_VIEW);
737: }
738:
739: private int getIndex(IViewReference toFind) {
740: ToolItem[] items = fastViewBar.getControl().getItems();
741: for (int i = 0; i < items.length; i++) {
742: if (items[i].getData(ShowFastViewContribution.FAST_VIEW) == toFind) {
743: return i;
744: }
745: }
746:
747: return items.length;
748: }
749:
750: private ToolItem getItem(int idx) {
751: ToolItem[] items = fastViewBar.getControl().getItems();
752: if (idx >= items.length) {
753: return null;
754: }
755:
756: return items[idx];
757: }
758:
759: /**
760: * Returns the toolbar item associated with the given view
761: *
762: * @param toFind
763: */
764: private ToolItem itemFor(IViewReference toFind) {
765: return getItem(getIndex(toFind));
766: }
767:
768: /* (non-Javadoc)
769: * @see org.eclipse.ui.internal.IWindowTrim#getValidSides()
770: */
771: public int getValidSides() {
772: return SWT.TOP | SWT.LEFT | SWT.RIGHT | SWT.BOTTOM;
773: }
774:
775: /* (non-Javadoc)
776: * @see org.eclipse.ui.internal.IWindowTrim#docked(int)
777: */
778: public void dock(int side) {
779: fCurrentSide = side;
780: disposeChildControls();
781: createChildControls();
782: }
783:
784: /**
785: * Get the current side.
786: * @return SWT.BOTTOM or SWT.RIGHT or SWT.LEFT
787: */
788: public int getSide() {
789: if (fCurrentSide == SWT.DEFAULT) {
790: fCurrentSide = getInitialSide();
791: }
792: return fCurrentSide;
793: }
794:
795: private boolean isHorizontal(IViewReference ref) {
796: Integer orientation = (Integer) viewOrientation
797: .get(ref.getId());
798: boolean horizontalBar = Geometry.isHorizontal(getSide());
799: boolean horizontal = horizontalBar;
800: if (orientation != null) {
801: horizontal = orientation.intValue() == SWT.HORIZONTAL;
802: } else {
803: horizontal = false;
804: }
805:
806: return horizontal;
807: }
808:
809: /**
810: * @param ref
811: */
812: public int getViewSide(IViewReference ref) {
813: boolean horizontal = isHorizontal(ref);
814:
815: if (horizontal) {
816: return (getSide() == SWT.BOTTOM) ? SWT.BOTTOM : SWT.TOP;
817: }
818:
819: return (getSide() == SWT.RIGHT) ? SWT.RIGHT : SWT.LEFT;
820: }
821:
822: public void saveState(IMemento memento) {
823: memento.putInteger(IWorkbenchConstants.TAG_FAST_VIEW_SIDE,
824: getSide());
825:
826: Iterator iter = viewOrientation.keySet().iterator();
827: while (iter.hasNext()) {
828: String next = (String) iter.next();
829: IMemento orientation = memento
830: .createChild(IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION);
831:
832: orientation.putString(IWorkbenchConstants.TAG_VIEW, next);
833: orientation.putInteger(IWorkbenchConstants.TAG_POSITION,
834: ((Integer) viewOrientation.get(next)).intValue());
835: }
836:
837: }
838:
839: /**
840: * Returns the approximate location where the next fastview icon
841: * will be drawn (display coordinates)
842: */
843: public Rectangle getLocationOfNextIcon() {
844: ToolBar control = getToolBar();
845:
846: Rectangle result = control.getBounds();
847: Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT,
848: false);
849: result.height = size.y;
850: result.width = size.x;
851:
852: boolean horizontal = Geometry.isHorizontal(getSide());
853: if (control.getItemCount() == 0) {
854: Geometry.setDimension(result, horizontal, 0);
855: }
856:
857: int hoverSide = horizontal ? SWT.RIGHT : SWT.BOTTOM;
858:
859: result = Geometry.getExtrudedEdge(result, -Geometry
860: .getDimension(result, !horizontal), hoverSide);
861:
862: return Geometry.toDisplay(control.getParent(), result);
863: }
864:
865: public void restoreState(IMemento memento) {
866: Integer bigInt;
867: bigInt = memento
868: .getInteger(IWorkbenchConstants.TAG_FAST_VIEW_SIDE);
869: if (bigInt != null) {
870: dock(bigInt.intValue());
871: }
872:
873: IMemento[] orientations = memento
874: .getChildren(IWorkbenchConstants.TAG_FAST_VIEW_ORIENTATION);
875: for (int i = 0; i < orientations.length; i++) {
876: IMemento next = orientations[i];
877:
878: viewOrientation.put(next
879: .getString(IWorkbenchConstants.TAG_VIEW), next
880: .getInteger(IWorkbenchConstants.TAG_POSITION));
881: }
882: }
883:
884: public WorkbenchWindow getWindow() {
885: return window;
886: }
887:
888: public void restoreView(IViewReference selectedView) {
889: if (selectedView != null) {
890: WorkbenchPage page = window.getActiveWorkbenchPage();
891: if (page != null) {
892: int idx = getIndex(selectedView);
893: ToolItem item = getItem(idx);
894: Rectangle bounds = item.getBounds();
895: Rectangle startBounds = Geometry.toDisplay(item
896: .getParent(), bounds);
897:
898: Perspective persp = getPerspective();
899: if (persp != null) {
900: persp.getFastViewManager().removeViewReference(
901: selectedView, true, true);
902: }
903:
904: IWorkbenchPart toActivate = selectedView.getPart(true);
905: if (toActivate != null) {
906: page.activate(toActivate);
907: }
908:
909: ViewPane pane = (ViewPane) ((WorkbenchPartReference) selectedView)
910: .getPane();
911:
912: RectangleAnimation animation = new RectangleAnimation(
913: window.getShell(), startBounds, pane
914: .getParentBounds());
915:
916: animation.schedule();
917: }
918: }
919: }
920:
921: /**
922: * @return The list of all view references in the stack
923: */
924: public List getViewRefs() {
925: List refs = new ArrayList(fastViewBar.getControl()
926: .getItemCount());
927: ToolItem[] items = fastViewBar.getControl().getItems();
928: for (int i = 0; i < items.length; i++) {
929: Object data = items[i]
930: .getData(ShowFastViewContribution.FAST_VIEW);
931: if (data != null)
932: refs.add(data);
933: }
934:
935: return refs;
936: }
937:
938: /* (non-Javadoc)
939: * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
940: */
941: public boolean isCloseable() {
942: return false;
943: }
944:
945: /* (non-Javadoc)
946: * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
947: */
948: public void handleClose() {
949: // nothing to do...
950: }
951:
952: /* (non-Javadoc)
953: * @see org.eclipse.ui.internal.IWindowTrim#getId()
954: */
955: public String getId() {
956: return "org.eclise.ui.internal.FastViewBar"; //$NON-NLS-1$
957: }
958:
959: /* (non-Javadoc)
960: * @see org.eclipse.ui.internal.IWindowTrim#getDisplayName()
961: */
962: public String getDisplayName() {
963: return WorkbenchMessages.TrimCommon_FastView_TrimName;
964: }
965:
966: /**
967: * Returns the context menu contribution item. This is for
968: * internal UI testing only.
969: *
970: * @return the context menu contribution item
971: * @since 3.1.1
972: */
973: public FastViewBarContextMenuContribution testContextMenu() {
974: return contextContributionItem;
975: }
976:
977: /* (non-Javadoc)
978: * @see org.eclipse.ui.IWindowTrim#getWidthHint()
979: */
980: public int getWidthHint() {
981: return SWT.DEFAULT;
982: }
983:
984: /* (non-Javadoc)
985: * @see org.eclipse.ui.IWindowTrim#getHeightHint()
986: */
987: public int getHeightHint() {
988: return SWT.DEFAULT;
989: }
990:
991: /* (non-Javadoc)
992: * @see org.eclipse.ui.IWindowTrim#isResizeable()
993: */
994: public boolean isResizeable() {
995: return false;
996: }
997: }
|