KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > TrimContributionManager


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.menus;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.expressions.Expression;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.action.ContributionManager;
22 import org.eclipse.jface.action.IContributionItem;
23 import org.eclipse.jface.action.ToolBarContributionItem;
24 import org.eclipse.jface.action.ToolBarManager;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.ToolBar;
28 import org.eclipse.ui.internal.WorkbenchWindow;
29 import org.eclipse.ui.internal.expressions.WorkbenchWindowExpression;
30 import org.eclipse.ui.internal.layout.IWindowTrim;
31 import org.eclipse.ui.internal.layout.TrimLayout;
32 import org.eclipse.ui.internal.misc.StatusUtil;
33 import org.eclipse.ui.menus.AbstractContributionFactory;
34 import org.eclipse.ui.menus.IMenuService;
35 import org.eclipse.ui.menus.MenuUtil;
36 import org.eclipse.ui.statushandlers.StatusManager;
37
38 /**
39  * Manage trim contributions added through the 'org.eclipse.ui.menus'
40  * extension point.
41  *
42  * @since 3.3
43  *
44  */

45 public class TrimContributionManager extends ContributionManager {
46     private class ToolBarTrimProxy implements IWindowTrim {
47         private String JavaDoc id;
48         private String JavaDoc uriSpec;
49         private WorkbenchMenuService menuService;
50         private WorkbenchWindow wbw;
51         private ToolBar tb = null;
52         private ToolBarManager tbm = null;
53
54         ToolBarTrimProxy(String JavaDoc id, WorkbenchWindow wbw) {
55             this.id = id;
56             uriSpec = "toolbar:" + id; //$NON-NLS-1$
57
this.wbw = wbw;
58             
59             this.menuService = (WorkbenchMenuService) wbw.getWorkbench().getService(
60                     IMenuService.class);
61         }
62         
63         /* (non-Javadoc)
64          * @see org.eclipse.ui.internal.layout.IWindowTrim#dock(int)
65          */

66         public void dock(int dropSide) {
67             dispose();
68             
69             int orientation = SWT.HORIZONTAL;
70             if (dropSide == SWT.LEFT || dropSide == SWT.RIGHT)
71                 orientation = SWT.VERTICAL;
72             
73             // Create the new control, manager...
74
tbm = new ToolBarManager(SWT.FLAT | orientation);
75             menuService.populateContributionManager(tbm, uriSpec);
76             
77             // Set the state for any Control entries
78
IContributionItem[] items = tbm.getItems();
79             for (int i = 0; i < items.length; i++) {
80                 if (items[i] instanceof InternalControlContribution) {
81                     InternalControlContribution wbwcc = (InternalControlContribution) items[i];
82                     wbwcc.setWorkbenchWindow(wbw);
83                     wbwcc.setCurSide(dropSide);
84                 }
85             }
86             
87             // OK, create the ToolBar (causes an 'update(true)'
88
tb = tbm.createControl(wbw.getShell());
89         }
90
91         /* (non-Javadoc)
92          * @see org.eclipse.ui.internal.layout.IWindowTrim#getControl()
93          */

94         public Control getControl() {
95             return tb;
96         }
97
98         /* (non-Javadoc)
99          * @see org.eclipse.ui.internal.layout.IWindowTrim#getDisplayName()
100          */

101         public String JavaDoc getDisplayName() {
102             return getId();
103         }
104
105         /* (non-Javadoc)
106          * @see org.eclipse.ui.internal.layout.IWindowTrim#getHeightHint()
107          */

108         public int getHeightHint() {
109             return SWT.DEFAULT;
110         }
111
112         /* (non-Javadoc)
113          * @see org.eclipse.ui.internal.layout.IWindowTrim#getId()
114          */

115         public String JavaDoc getId() {
116             return id;
117         }
118
119         /* (non-Javadoc)
120          * @see org.eclipse.ui.internal.layout.IWindowTrim#getValidSides()
121          */

122         public int getValidSides() {
123             return SWT.TOP | SWT.BOTTOM | SWT.LEFT | SWT.RIGHT;
124         }
125
126         /* (non-Javadoc)
127          * @see org.eclipse.ui.internal.layout.IWindowTrim#getWidthHint()
128          */

129         public int getWidthHint() {
130             return SWT.DEFAULT;
131         }
132
133         /* (non-Javadoc)
134          * @see org.eclipse.ui.internal.layout.IWindowTrim#handleClose()
135          */

136         public void handleClose() {
137         }
138
139         /* (non-Javadoc)
140          * @see org.eclipse.ui.internal.layout.IWindowTrim#isCloseable()
141          */

142         public boolean isCloseable() {
143             return false;
144         }
145
146         /* (non-Javadoc)
147          * @see org.eclipse.ui.internal.layout.IWindowTrim#isResizeable()
148          */

149         public boolean isResizeable() {
150             return false;
151         }
152         
153         /**
154          * Dispose any trim element resources
155          */

156         public void dispose() {
157             if (tbm != null) {
158                 tbm.removeAll();
159                 tbm.dispose();
160             }
161         }
162     }
163     
164     /**
165      * A List of the URI's representing the trim areas
166      */

167     private String JavaDoc[] trimAreaURIs = {
168             MenuUtil.TRIM_COMMAND1,
169             MenuUtil.TRIM_COMMAND2,
170             MenuUtil.TRIM_VERTICAL1,
171             MenuUtil.TRIM_VERTICAL2,
172             MenuUtil.TRIM_STATUS
173     };
174
175     /**
176      * The SWT 'side' corresponding to a URI
177      */

178     private int[] swtSides = { SWT.TOP, SWT.TOP, SWT.LEFT, SWT.RIGHT, SWT.BOTTOM };
179
180     private WorkbenchWindow wbWindow;
181     TrimLayout layout;
182     private InternalMenuService menuService;
183     
184     List JavaDoc contributedTrim = new ArrayList JavaDoc();
185
186     List JavaDoc contributedLists = new ArrayList JavaDoc();
187
188     private Expression restrictionExpression;
189
190     /**
191      * Construct a contribution manager for the given window
192      */

193     public TrimContributionManager(WorkbenchWindow window) {
194         wbWindow = window;
195         layout = (TrimLayout) wbWindow.getShell().getLayout();
196         menuService = (InternalMenuService) window.getService(
197                 IMenuService.class);
198         restrictionExpression = new WorkbenchWindowExpression(wbWindow);
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.jface.action.IContributionManager#update(boolean)
203      */

204     public void update(boolean force) {
205         update(force, false);
206     }
207     
208     public void update(boolean force, boolean hideTopTrim) {
209         // Remove any contributed trim
210
teardown();
211         
212         // Process the additions for each 'area'
213
for (int i = 0; i < trimAreaURIs.length; i++) {
214             // IntroBar want to hide the top trim
215
if (hideTopTrim && swtSides[i] == SWT.TOP)
216                 continue;
217             
218             List JavaDoc contribs = menuService.getAdditionsForURI(new MenuLocationURI(trimAreaURIs[i]));
219             
220             for (Iterator JavaDoc cacheIter = contribs.iterator(); cacheIter.hasNext();) {
221                 AbstractContributionFactory cache = (AbstractContributionFactory) cacheIter.next();
222                 ContributionRoot ciList = new ContributionRoot(menuService,
223                         restrictionExpression, cache.getNamespace());
224                 cache.createContributionItems(wbWindow, ciList);
225                 // save the list for later cleanup of any visibility expressions that were added.
226
contributedLists.add(ciList);
227                 for (Iterator JavaDoc ciIter = ciList.getItems().iterator(); ciIter.hasNext();) {
228                     IContributionItem ci = (IContributionItem) ciIter.next();
229                     if (ci instanceof ToolBarContributionItem) {
230                         // HACK!! Fake this
231
ToolBarTrimProxy tbProxy = new ToolBarTrimProxy(ci.getId(), wbWindow);
232                         tbProxy.dock(swtSides[i]);
233                         
234                         // If we're adding to the 'command1' area then we're -before- the CoolBar
235
IWindowTrim insertBefore = null;
236                         if (i == 0) {
237                             insertBefore = layout.getTrim("org.eclipse.ui.internal.WorkbenchWindow.topBar"); //$NON-NLS-1$
238
}
239                         layout.addTrim(swtSides[i], tbProxy, insertBefore);
240                         contributedTrim.add(tbProxy);
241                     }
242                 }
243             }
244         }
245     }
246
247     private void teardown() {
248         // First, remove all trim
249
for (Iterator JavaDoc iter = contributedTrim.iterator(); iter.hasNext();) {
250             ToolBarTrimProxy proxy = (ToolBarTrimProxy) iter.next();
251             layout.removeTrim(proxy);
252
253             try {
254                 proxy.dispose();
255             } catch (Throwable JavaDoc e) {
256                 IStatus status = null;
257                 if (e instanceof CoreException) {
258                     status = ((CoreException) e).getStatus();
259                 } else {
260                     status = StatusUtil
261                             .newStatus(
262                                     IStatus.ERROR,
263                                     "Internal plug-in widget delegate error on dispose.", e); //$NON-NLS-1$
264
}
265                 StatusUtil
266                         .handleStatus(
267                                 status,
268                                 "widget delegate failed on dispose: id = " + proxy.getId(), StatusManager.LOG); //$NON-NLS-1$
269
}
270         }
271
272         // Clear out the old list
273
contributedTrim.clear();
274         
275         // clean up the list of ContributionLists
276
for (Iterator JavaDoc iter = contributedLists.iterator(); iter.hasNext();) {
277             ContributionRoot list = (ContributionRoot) iter.next();
278             list.release();
279         }
280         
281         contributedLists.clear();
282     }
283     
284     /**
285      *
286      */

287     public void dispose() {
288         teardown();
289     }
290
291     /**
292      * @param knownIds
293      */

294     public void updateLocations(List JavaDoc knownIds) {
295         // TODO Auto-generated method stub
296

297     }
298 }
299
Popular Tags