KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.internal;
12
13 import org.eclipse.jface.action.ContributionItem;
14 import org.eclipse.jface.action.IContributionItem;
15 import org.eclipse.jface.action.IMenuListener;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.ToolBarManager;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.swt.widgets.MenuItem;
26 import org.eclipse.swt.widgets.ToolBar;
27 import org.eclipse.swt.widgets.ToolItem;
28 import org.eclipse.ui.IViewReference;
29 import org.eclipse.ui.internal.layout.TrimToolBarBase;
30
31 /**
32  * @since 3.3
33  *
34  */

35 public class ViewStackTrimToolBar extends TrimToolBarBase {
36     private boolean restoreOnUnzoom = false;
37     
38     // The orientation of the fast view pane when showing a view
39
private int paneOrientation;
40     
41     // The id of the part that was showing when we minimized
42
private String JavaDoc selectedTabId;
43
44     public ViewStackTrimToolBar(String JavaDoc id, int curSide, int paneOrientation, WorkbenchWindow wbw) {
45         super(id, curSide, wbw);
46         
47         this.paneOrientation = paneOrientation;
48         dock(curSide);
49     }
50     
51     /**
52      * Put the stack back into the presentation
53      */

54     protected void restoreToPresentation() {
55         Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective();
56         //FastViewManager fvMgr = persp.getFastViewManager();
57

58         LayoutPart part = persp.getPresentation().findPart(getId(), null);
59         if (part instanceof ContainerPlaceholder) {
60             ViewStack stack = (ViewStack) ((ContainerPlaceholder)part).getRealContainer();
61             stack.setMinimized(false);
62         }
63         //fvMgr.restoreToPresentation(getId());
64
}
65
66     public void initToolBarManager(final ToolBarManager mgr) {
67         // Set up the ToolBar with a restore button
68
IContributionItem restoreContrib = new ContributionItem() {
69             public void fill(ToolBar parent, int index) {
70                 ToolItem restoreItem = new ToolItem(mgr.getControl(), SWT.PUSH, index);
71                 Image tbImage = WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_ETOOL_RESTORE_TRIMPART);
72                 restoreItem.setImage(tbImage);
73                 String JavaDoc menuTip = WorkbenchMessages.StandardSystemToolbar_Restore;
74                 restoreItem.setToolTipText(menuTip);
75                 restoreItem.addSelectionListener(new SelectionListener() {
76                     public void widgetDefaultSelected(SelectionEvent e) {
77                         restoreToPresentation();
78                     }
79                     public void widgetSelected(SelectionEvent e) {
80                         restoreToPresentation();
81                     }
82                 });
83             }
84         };
85         mgr.add(restoreContrib);
86         
87         ShowFastViewContribution sfvc = new ShowFastViewContribution(wbw, getId());
88         mgr.add(sfvc);
89         
90         // Add context menu items
91
mgr.setContextMenuManager(new MenuManager());
92         MenuManager menuMgr = mgr.getContextMenuManager();
93         
94         final IContributionItem closeContrib = new ContributionItem() {
95             public void fill(Menu parent, int index) {
96                 MenuItem closeItem = new MenuItem(parent, SWT.NONE, index++);
97                 closeItem.setText(WorkbenchMessages.WorkbenchWindow_close);
98                 closeItem.addSelectionListener(new SelectionAdapter() {
99                     public void widgetSelected(SelectionEvent e) {
100                         IViewReference selectedView = null;
101                         if (contextToolItem != null) {
102                             selectedView = (IViewReference) contextToolItem.getData(ShowFastViewContribution.FAST_VIEW);
103                         }
104                         
105                         if (selectedView != null) {
106                             WorkbenchPage page = wbw.getActiveWorkbenchPage();
107                             if (page != null) {
108                                 page.hideView(selectedView);
109                             }
110                         }
111                     }
112                 });
113             }
114         };
115         
116         // We have to manage the visiblity this way...?
117
menuMgr.addMenuListener(new IMenuListener() {
118             public void menuAboutToShow(IMenuManager manager) {
119                 IViewReference selectedView = null;
120                 if (contextToolItem != null) {
121                     selectedView = (IViewReference) contextToolItem.getData(ShowFastViewContribution.FAST_VIEW);
122                 }
123                 
124                 // Only show the 'close' item if we've clicked on a view
125
Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective();
126                 closeContrib.setVisible(selectedView != null && persp.isCloseable(selectedView));
127                 manager.update(true);
128             }
129         });
130         
131         menuMgr.add(closeContrib);
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.ui.internal.layout.TrimToolBarBase#hookControl(org.eclipse.swt.widgets.ToolBar)
136      */

137     public void hookControl(ToolBarManager mgr) {
138         // Hook a drop Listener to the control
139
// NOTE: the drop target is self-managing...it
140
// both hooks the new target and removes it on dispose
141
new FastViewDnDHandler(id, mgr, wbw);
142     }
143     
144     /**
145      * Sets whether or not the stack gets restored on an unzoom
146      * operation.
147      *
148      * @param restoreOnUnzoom
149      */

150     public void setRestoreOnUnzoom(boolean restoreOnUnzoom) {
151         this.restoreOnUnzoom = restoreOnUnzoom;
152     }
153     
154     public boolean restoreOnUnzoom() {
155         return restoreOnUnzoom;
156     }
157
158     /**
159      * @param ref
160      * @param selected
161      */

162     public void setIconSelection(IViewReference ref, boolean selected) {
163         ToolItem item = ShowFastViewContribution.getItem(tbMgr.getControl(), ref);
164         if (item != null) {
165             item.setSelection(selected);
166             
167             if (selected) {
168                 selectedTabId = ref.getId();
169                 
170                 // Create a 'compound' id if this is a multi-instance part
171
if (ref.getSecondaryId() != null)
172                     selectedTabId = selectedTabId + ':' + ref.getSecondaryId();
173             }
174         }
175     }
176
177     /**
178      * @return Returns the paneOrientation.
179      */

180     public int getPaneOrientation() {
181         return paneOrientation;
182     }
183
184     /**
185      * Cache the tba that was on top when we were minimized
186      * @param selectedTab The id of the PartPane for the tab
187      */

188     public void setSelectedTabId(String JavaDoc id) {
189         selectedTabId = id;
190     }
191     
192     /**
193      * @return The id of the layout part representing the 'top' tab
194      */

195     public String JavaDoc getSelectedTabId() {
196         return selectedTabId;
197     }
198
199     /**
200      * @param newOrientation The new orientation for the fact view display
201      * @param wbw The currently active WorkbenchWindow
202      */

203     public void setOrientation(int newOrientation, WorkbenchWindow wbw) {
204         if (newOrientation == paneOrientation)
205             return;
206         
207         paneOrientation = newOrientation;
208         
209         // If there's a fast view showing, toggle it to pick up the change
210
if (wbw.getActivePage() instanceof WorkbenchPage) {
211             WorkbenchPage wbp = (WorkbenchPage) wbw.getActivePage();
212             Perspective persp = wbp.getActivePerspective();
213             if (persp != null) {
214                 IViewReference curRef = persp.getActiveFastView();
215                 if (curRef != null) {
216                     persp.setActiveFastView(null);
217                     persp.setActiveFastView(curRef);
218                 }
219             }
220             
221         }
222         
223     }
224 }
225
Popular Tags