KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Cagatay Kavukcuoglu <cagatayk@acm.org>
11  * - Fix for bug 10025 - Resizing views should not use height ratios
12  *******************************************************************************/

13 package org.eclipse.ui.internal;
14
15
16 import org.eclipse.jface.action.MenuManager;
17 import org.eclipse.jface.internal.provisional.action.IToolBarManager2;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.MouseAdapter;
22 import org.eclipse.swt.events.MouseEvent;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Menu;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.ToolBar;
30 import org.eclipse.ui.IViewReference;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.internal.dnd.DragUtil;
33 import org.eclipse.ui.internal.provisional.presentations.IActionBarPresentationFactory;
34 import org.eclipse.ui.presentations.IPresentablePart;
35 import org.eclipse.ui.presentations.StackPresentation;
36
37 /**
38  * Provides a wrapper for the view's widgetry.
39  *
40  * TODO: Delete ViewPane and EditorPane, and make PartPane non-abstract.
41  */

42 public class ViewPane extends PartPane {
43
44     // create initially toolbarless bar manager so that actions may be added in the
45
// init method of the view.
46
private IToolBarManager2 isvToolBarMgr = null;
47
48     private MenuManager isvMenuMgr;
49
50     boolean hasFocus;
51
52     /**
53      * Indicates whether a toolbar button is shown for the view local menu.
54      */

55     private boolean hadViewMenu = false;
56
57     /**
58      * Toolbar manager for the ISV toolbar.
59      */

60     private class ISVPropListener implements IPropertyChangeListener {
61         private Control toolBar;
62         
63         /**
64          * Constructor
65          * @param toolBar
66          */

67         public ISVPropListener (Control toolBar) {
68             this.toolBar = toolBar;
69         }
70         
71         /* (non-Javadoc)
72          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
73          */

74         public void propertyChange(PropertyChangeEvent event) {
75             String JavaDoc property = event.getProperty();
76             Integer JavaDoc newValue = (Integer JavaDoc)event.getNewValue();
77             if (IToolBarManager2.PROP_LAYOUT.equals(property)) {
78                 toolBarResized(toolBar, newValue != null ? newValue.intValue() : 0);
79                 if (toolBar instanceof Composite) {
80                     ((Composite)toolBar).layout();
81                 } else {
82                     toolBar.getParent().layout();
83                 }
84             }
85         }
86     }
87
88     /**
89      * Menu manager for view local menu.
90      */

91     class PaneMenuManager extends MenuManager {
92         public PaneMenuManager() {
93             super("View Local Menu"); //$NON-NLS-1$
94
}
95
96         protected void update(boolean force, boolean recursive) {
97             super.update(force, recursive);
98
99             boolean hasMenu = !isEmpty();
100             if (hasMenu != hadViewMenu) {
101                 hadViewMenu = hasMenu;
102                 firePropertyChange(IPresentablePart.PROP_PANE_MENU);
103             }
104         }
105     }
106
107     /**
108      * Constructs a view pane for a view part.
109      */

110     public ViewPane(IViewReference ref, WorkbenchPage page) {
111         super(ref, page);
112         IActionBarPresentationFactory actionBarPresentation = ((WorkbenchWindow) page
113                 .getWorkbenchWindow()).getActionBarPresentationFactory();
114         
115         isvToolBarMgr = actionBarPresentation.createViewToolBarManager();
116     }
117
118     /**
119      * Create control. Add the title bar.
120      */

121     public void createControl(Composite parent) {
122         // Only do this once.
123
if (getControl() != null && !getControl().isDisposed()) {
124             return;
125         }
126
127         super.createControl(parent);
128     }
129
130     /**
131      * Create a title bar for the pane.
132      * - the view icon and title to the far left
133      * - the view toolbar appears in the middle.
134      * - the view pulldown menu, pin button, and close button to the far right.
135      */

136     protected void createTitleBar() {
137         // Only do this once.
138

139         updateTitles();
140
141         // Listen to title changes.
142
getPartReference().addPropertyListener(this);
143
144         createToolBars();
145
146     }
147
148     private void toolBarResized(Control toolBar, int newSize) {
149         
150         Control toolbar = isvToolBarMgr.getControl2();
151         if (toolbar != null) {
152             Control ctrl = getControl();
153
154             boolean visible = ctrl != null && ctrl.isVisible()
155                     && toolbarIsVisible();
156
157             toolbar.setVisible(visible);
158         }
159
160         firePropertyChange(IPresentablePart.PROP_TOOLBAR);
161     }
162
163     /**
164      *
165      */

166     private void createToolBars() {
167         Composite parentControl = control;
168         
169         // ISV toolbar.
170
// // 1GD0ISU: ITPUI:ALL - Dbl click on view tool cause zoom
171
final Control isvToolBar = isvToolBarMgr.createControl2(parentControl.getParent());
172         
173         isvToolBarMgr.addPropertyChangeListener(new ISVPropListener(isvToolBar));
174         
175         isvToolBar.addMouseListener(new MouseAdapter() {
176             public void mouseDoubleClick(MouseEvent event) {
177                 if (event.widget instanceof ToolBar) {
178                 
179                     if (((ToolBar)event.widget).getItem(new Point(event.x, event.y)) == null) {
180                         doZoom();
181                     }
182                 }
183             }
184         });
185
186         
187         isvToolBar.addListener(SWT.Activate, this);
188         isvToolBar.moveAbove(control);
189     }
190
191     public void dispose() {
192         super.dispose();
193
194         /* Bug 42684. The ViewPane instance has been disposed, but an attempt is
195          * then made to remove focus from it. This happens because the ViewPane is
196          * still viewed as the active part. In general, when disposed, the control
197          * containing the titleLabel will also disappear (disposing of the
198          * titleLabel). As a result, the reference to titleLabel should be dropped.
199          */

200         if (isvMenuMgr != null) {
201             isvMenuMgr.dispose();
202         }
203         if (isvToolBarMgr != null) {
204             isvToolBarMgr.dispose();
205         }
206     }
207
208     /**
209      * @see PartPane#doHide
210      */

211     public void doHide() {
212         getPage().hideView(getViewReference());
213     }
214
215     /*package*/Rectangle getParentBounds() {
216         Control ctrl = getControl();
217
218         if (getContainer() != null && getContainer() instanceof LayoutPart) {
219             LayoutPart part = (LayoutPart) getContainer();
220
221             if (part.getControl() != null) {
222                 ctrl = part.getControl();
223             }
224         }
225
226         return DragUtil.getDisplayBounds(ctrl);
227     }
228
229     /**
230      * Make this view pane a fast view
231      */

232     public void doMakeFast() {
233         WorkbenchWindow window = (WorkbenchWindow) getPage()
234                 .getWorkbenchWindow();
235
236         FastViewBar fastViewBar = window.getFastViewBar();
237         if (fastViewBar == null || getPage().getActivePerspective() == null)
238             return;
239
240         Shell shell = window.getShell();
241
242         RectangleAnimation animation = new RectangleAnimation(shell,
243                 getParentBounds(), fastViewBar.getLocationOfNextIcon());
244
245         animation.schedule();
246
247         FastViewManager fvm = getPage().getActivePerspective().getFastViewManager();
248         fvm.addViewReference(FastViewBar.FASTVIEWBAR_ID, -1, getViewReference(), true);
249     }
250
251     public void doRemoveFast() {
252         if (getPage().getActivePerspective() == null)
253             return;
254
255         Shell shell = getControl().getShell();
256
257         Rectangle initialBounds = getParentBounds();
258
259         FastViewManager fvm = getPage().getActivePerspective().getFastViewManager();
260         fvm.removeViewReference(getViewReference(), true, true);
261
262         IWorkbenchPart toActivate = getViewReference().getPart(true);
263         if (toActivate != null) {
264             getPage().activate(toActivate);
265         }
266
267         Rectangle finalBounds = getParentBounds();
268
269         RectangleAnimation animation = new RectangleAnimation(shell,
270                 initialBounds, finalBounds);
271
272         animation.schedule();
273     }
274
275     /**
276      * Pin the view.
277      */

278     protected void doDock() {
279         Perspective persp = getPage().getActivePerspective();
280         if (persp != null) {
281             persp.getFastViewManager().removeViewReference(getViewReference(), true, true);
282         }
283     }
284     
285     public void doDetach() {
286         getPage().detachView(getViewReference());
287     }
288             
289     public void doAttach() {
290         getPage().attachView(getViewReference());
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.ui.internal.LayoutPart#getCompoundId()
295      */

296     public String JavaDoc getCompoundId() {
297         IViewReference ref = getViewReference();
298         if (ref != null) {
299             return ViewFactory.getKey(ref);
300         }
301         
302         return super.getCompoundId();
303     }
304
305     /**
306      * Returns the drag control.
307      */

308     public Control getDragHandle() {
309         return control;
310     }
311
312     /**
313      * @see ViewActionBars
314      */

315     public MenuManager getMenuManager() {
316         if (isvMenuMgr == null) {
317             isvMenuMgr = new PaneMenuManager();
318         }
319         return isvMenuMgr;
320     }
321
322     /**
323      * Returns the tab list to use when this part is active.
324      * Includes the view and its tab (if applicable), in the appropriate order.
325      */

326     public Control[] getTabList() {
327         Control c = getControl();
328         if (getContainer() instanceof ViewStack) {
329             ViewStack tf = (ViewStack) getContainer();
330             return tf.getTabList(this);
331         }
332         return new Control[] { c };
333     }
334
335     /**
336      * @see ViewActionBars
337      */

338     public IToolBarManager2 getToolBarManager() {
339         return isvToolBarMgr;
340     }
341
342     /**
343      * Answer the view part child.
344      */

345     public IViewReference getViewReference() {
346         return (IViewReference) getPartReference();
347     }
348
349     /**
350      * Sets the fast view state. If this view is a fast view then
351      * various controls like pin and minimize are added to the
352      * system bar.
353      */

354     public void setFast(boolean b) {
355     }
356
357     /* (non-Javadoc)
358      * Method declared on PartPane.
359      */

360     /* package */
361     void shellActivated() {
362     }
363
364     /* (non-Javadoc)
365      * Method declared on PartPane.
366      */

367     /* package */
368     void shellDeactivated() {
369     }
370
371     /**
372      * Set the active border.
373      * @param active
374      */

375     void setActive(boolean active) {
376         hasFocus = active;
377
378         if (getContainer() instanceof PartStack) {
379             ((PartStack) getContainer())
380                     .setActive(active ? StackPresentation.AS_ACTIVE_FOCUS
381                             : StackPresentation.AS_INACTIVE);
382         }
383     }
384
385     /**
386      * Indicate focus in part.
387      */

388     public void showFocus(boolean inFocus) {
389         setActive(inFocus);
390     }
391
392     /**
393      * Return true if this view is a fast view.
394      */

395     private boolean isFastView() {
396         return page.isFastView(getViewReference());
397     }
398
399     /**
400      * Return true if the view may be moved.
401      */

402     boolean isMoveable() {
403         return !page.isFixedLayout();
404     }
405
406     /**
407      * Return if there should be a view menu at all.
408      * There is no view menu if there is no menu manager,
409      * no pull down button or if the receiver is an
410      * inactive fast view.
411      */

412     public boolean hasViewMenu() {
413
414         if (isvMenuMgr != null) {
415             return !isvMenuMgr.isEmpty();
416         }
417
418         return false;
419     }
420
421     public void showViewMenu(Point location) {
422         if (!hasViewMenu()) {
423             return;
424         }
425
426         // If this is a fast view, it may have been minimized. Do nothing in this case.
427
if (isFastView() && (page.getActiveFastView() != getViewReference())) {
428             return;
429         }
430
431         Menu aMenu = isvMenuMgr.createContextMenu(getControl().getParent());
432         aMenu.setLocation(location.x, location.y);
433         aMenu.setVisible(true);
434     }
435
436     public String JavaDoc toString() {
437
438         return getClass().getName() + "@" + Integer.toHexString(hashCode()); //$NON-NLS-1$
439
}
440
441     /**
442      * @see ViewActionBars
443      */

444     public void updateActionBars() {
445         if (isvMenuMgr != null) {
446             isvMenuMgr.updateAll(false);
447         }
448         if (isvToolBarMgr != null) {
449             isvToolBarMgr.update(false);
450         }
451
452     }
453
454     /**
455      * Update the title attributes.
456      */

457     public void updateTitles() {
458         firePropertyChange(IPresentablePart.PROP_TITLE);
459     }
460
461     /* (non-Javadoc)
462      * @see org.eclipse.ui.internal.PartPane#addSizeMenuItem(org.eclipse.swt.widgets.Menu)
463      */

464     public void addSizeMenuItem(Menu menu, int index) {
465         if (isMoveable()) {
466             super.addSizeMenuItem(menu, index);
467         }
468     }
469
470     /* (non-Javadoc)
471      * @see org.eclipse.ui.internal.PartPane#doZoom()
472      */

473     protected void doZoom() {
474         if (isMoveable()) {
475             super.doZoom();
476         }
477     }
478
479     /* (non-Javadoc)
480      * @see org.eclipse.ui.internal.LayoutPart#setContainer(org.eclipse.ui.internal.ILayoutContainer)
481      */

482     public void setContainer(ILayoutContainer container) {
483         ILayoutContainer oldContainer = getContainer();
484         if (hasFocus) {
485             if (oldContainer != null && oldContainer instanceof PartStack) {
486                 ((PartStack) oldContainer)
487                         .setActive(StackPresentation.AS_INACTIVE);
488             }
489
490             if (container != null && container instanceof PartStack) {
491                 ((PartStack) container)
492                         .setActive(StackPresentation.AS_ACTIVE_FOCUS);
493             }
494         }
495
496         super.setContainer(container);
497     }
498
499     /* (non-Javadoc)
500      * @see org.eclipse.ui.internal.LayoutPart#reparent(org.eclipse.swt.widgets.Composite)
501      */

502     public void reparent(Composite newParent) {
503         super.reparent(newParent);
504
505         if (isvToolBarMgr != null) {
506             Control bar = isvToolBarMgr.getControl2();
507             if (bar != null) {
508                 bar.setParent(newParent);
509                 bar.moveAbove(control);
510             }
511         }
512     }
513
514     /* (non-Javadoc)
515      * @see org.eclipse.ui.internal.LayoutPart#moveAbove(org.eclipse.swt.widgets.Control)
516      */

517     public void moveAbove(Control refControl) {
518         super.moveAbove(refControl);
519
520         Control toolbar = internalGetToolbar();
521         
522         if (toolbar != null) {
523             toolbar.moveAbove(control);
524         }
525     }
526
527     /* (non-Javadoc)
528      * @see org.eclipse.ui.internal.LayoutPart#setVisible(boolean)
529      */

530     public void setVisible(boolean makeVisible) {
531         super.setVisible(makeVisible);
532
533         Control toolbar = internalGetToolbar();
534         
535         if (toolbar != null) {
536             boolean visible = makeVisible && toolbarIsVisible();
537             toolbar.setVisible(visible);
538         }
539     }
540
541     public boolean toolbarIsVisible() {
542         IToolBarManager2 toolbarManager = getToolBarManager();
543
544         if (toolbarManager == null) {
545             return false;
546         }
547
548         Control control = toolbarManager.getControl2();
549
550         if (control == null || control.isDisposed()) {
551             return false;
552         }
553
554         return toolbarManager.getItemCount() > 0;
555     }
556
557     /* (non-Javadoc)
558      * @see org.eclipse.ui.internal.PartPane#showHighlight()
559      */

560     public void showHighlight() {
561         firePropertyChange(IPresentablePart.PROP_HIGHLIGHT_IF_BACK);
562     }
563
564     /* (non-Javadoc)
565      * @see org.eclipse.ui.internal.LayoutPart#getPlaceHolderId()
566      */

567     public String JavaDoc getPlaceHolderId() {
568         return ViewFactory.getKey(getViewReference());
569     }
570
571     /* (non-Javadoc)
572      * @see org.eclipse.ui.internal.PartPane#getToolBar()
573      */

574     public Control getToolBar() {
575
576         if (!toolbarIsVisible()) {
577             return null;
578         }
579         
580         return internalGetToolbar();
581     }
582     
583     private Control internalGetToolbar() {
584         if (isvToolBarMgr == null) {
585             return null;
586         }
587         
588         return isvToolBarMgr.getControl2();
589     }
590
591     /* (non-Javadoc)
592      * @see org.eclipse.ui.internal.PartPane#isCloseable()
593      */

594     public boolean isCloseable() {
595         Perspective perspective = page.getActivePerspective();
596         if (perspective == null) {
597             // Shouldn't happen -- can't have a ViewStack without a
598
// perspective
599
return true;
600         }
601         return perspective.isCloseable(getViewReference());
602     }
603     
604     public void showSystemMenu() {
605         if (isFastView()) {
606             Perspective perspective = page.getActivePerspective();
607             if (perspective != null) {
608                 perspective.getFastViewPane().showSystemMenu();
609             }
610         } else {
611             super.showSystemMenu();
612         }
613     }
614     
615     public void showPaneMenu() {
616         if (isFastView()) {
617             Perspective perspective = page.getActivePerspective();
618             if (perspective != null) {
619                 perspective.getFastViewPane().showPaneMenu();
620             }
621         } else {
622             super.showPaneMenu();
623         }
624     }
625     
626     public void removeContributions() {
627         super.removeContributions();
628         
629         if (isvMenuMgr != null) {
630             isvMenuMgr.removeAll();
631         }
632         if (isvToolBarMgr != null) {
633             isvToolBarMgr.removeAll();
634         }
635     }
636 }
637
Popular Tags