001: /*******************************************************************************
002: * Copyright (c) 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: ******************************************************************************/package org.eclipse.ui.internal.menus;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.expressions.Expression;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.jface.action.ContributionManager;
020: import org.eclipse.jface.action.IContributionItem;
021: import org.eclipse.jface.action.ToolBarContributionItem;
022: import org.eclipse.jface.action.ToolBarManager;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.ToolBar;
026: import org.eclipse.ui.internal.WorkbenchWindow;
027: import org.eclipse.ui.internal.expressions.WorkbenchWindowExpression;
028: import org.eclipse.ui.internal.layout.IWindowTrim;
029: import org.eclipse.ui.internal.layout.TrimLayout;
030: import org.eclipse.ui.internal.misc.StatusUtil;
031: import org.eclipse.ui.menus.AbstractContributionFactory;
032: import org.eclipse.ui.menus.IMenuService;
033: import org.eclipse.ui.menus.MenuUtil;
034: import org.eclipse.ui.statushandlers.StatusManager;
035:
036: /**
037: * Manage trim contributions added through the 'org.eclipse.ui.menus'
038: * extension point.
039: *
040: * @since 3.3
041: *
042: */
043: public class TrimContributionManager extends ContributionManager {
044: private class ToolBarTrimProxy implements IWindowTrim {
045: private String id;
046: private String uriSpec;
047: private WorkbenchMenuService menuService;
048: private WorkbenchWindow wbw;
049: private ToolBar tb = null;
050: private ToolBarManager tbm = null;
051:
052: ToolBarTrimProxy(String id, WorkbenchWindow wbw) {
053: this .id = id;
054: uriSpec = "toolbar:" + id; //$NON-NLS-1$
055: this .wbw = wbw;
056:
057: this .menuService = (WorkbenchMenuService) wbw
058: .getWorkbench().getService(IMenuService.class);
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.ui.internal.layout.IWindowTrim#dock(int)
063: */
064: public void dock(int dropSide) {
065: dispose();
066:
067: int orientation = SWT.HORIZONTAL;
068: if (dropSide == SWT.LEFT || dropSide == SWT.RIGHT)
069: orientation = SWT.VERTICAL;
070:
071: // Create the new control, manager...
072: tbm = new ToolBarManager(SWT.FLAT | orientation);
073: menuService.populateContributionManager(tbm, uriSpec);
074:
075: // Set the state for any Control entries
076: IContributionItem[] items = tbm.getItems();
077: for (int i = 0; i < items.length; i++) {
078: if (items[i] instanceof InternalControlContribution) {
079: InternalControlContribution wbwcc = (InternalControlContribution) items[i];
080: wbwcc.setWorkbenchWindow(wbw);
081: wbwcc.setCurSide(dropSide);
082: }
083: }
084:
085: // OK, create the ToolBar (causes an 'update(true)'
086: tb = tbm.createControl(wbw.getShell());
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.ui.internal.layout.IWindowTrim#getControl()
091: */
092: public Control getControl() {
093: return tb;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.ui.internal.layout.IWindowTrim#getDisplayName()
098: */
099: public String getDisplayName() {
100: return getId();
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.ui.internal.layout.IWindowTrim#getHeightHint()
105: */
106: public int getHeightHint() {
107: return SWT.DEFAULT;
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.ui.internal.layout.IWindowTrim#getId()
112: */
113: public String getId() {
114: return id;
115: }
116:
117: /* (non-Javadoc)
118: * @see org.eclipse.ui.internal.layout.IWindowTrim#getValidSides()
119: */
120: public int getValidSides() {
121: return SWT.TOP | SWT.BOTTOM | SWT.LEFT | SWT.RIGHT;
122: }
123:
124: /* (non-Javadoc)
125: * @see org.eclipse.ui.internal.layout.IWindowTrim#getWidthHint()
126: */
127: public int getWidthHint() {
128: return SWT.DEFAULT;
129: }
130:
131: /* (non-Javadoc)
132: * @see org.eclipse.ui.internal.layout.IWindowTrim#handleClose()
133: */
134: public void handleClose() {
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.ui.internal.layout.IWindowTrim#isCloseable()
139: */
140: public boolean isCloseable() {
141: return false;
142: }
143:
144: /* (non-Javadoc)
145: * @see org.eclipse.ui.internal.layout.IWindowTrim#isResizeable()
146: */
147: public boolean isResizeable() {
148: return false;
149: }
150:
151: /**
152: * Dispose any trim element resources
153: */
154: public void dispose() {
155: if (tbm != null) {
156: tbm.removeAll();
157: tbm.dispose();
158: }
159: }
160: }
161:
162: /**
163: * A List of the URI's representing the trim areas
164: */
165: private String[] trimAreaURIs = { MenuUtil.TRIM_COMMAND1,
166: MenuUtil.TRIM_COMMAND2, MenuUtil.TRIM_VERTICAL1,
167: MenuUtil.TRIM_VERTICAL2, MenuUtil.TRIM_STATUS };
168:
169: /**
170: * The SWT 'side' corresponding to a URI
171: */
172: private int[] swtSides = { SWT.TOP, SWT.TOP, SWT.LEFT, SWT.RIGHT,
173: SWT.BOTTOM };
174:
175: private WorkbenchWindow wbWindow;
176: TrimLayout layout;
177: private InternalMenuService menuService;
178:
179: List contributedTrim = new ArrayList();
180:
181: List contributedLists = new ArrayList();
182:
183: private Expression restrictionExpression;
184:
185: /**
186: * Construct a contribution manager for the given window
187: */
188: public TrimContributionManager(WorkbenchWindow window) {
189: wbWindow = window;
190: layout = (TrimLayout) wbWindow.getShell().getLayout();
191: menuService = (InternalMenuService) window
192: .getService(IMenuService.class);
193: restrictionExpression = new WorkbenchWindowExpression(wbWindow);
194: }
195:
196: /* (non-Javadoc)
197: * @see org.eclipse.jface.action.IContributionManager#update(boolean)
198: */
199: public void update(boolean force) {
200: update(force, false);
201: }
202:
203: public void update(boolean force, boolean hideTopTrim) {
204: // Remove any contributed trim
205: teardown();
206:
207: // Process the additions for each 'area'
208: for (int i = 0; i < trimAreaURIs.length; i++) {
209: // IntroBar want to hide the top trim
210: if (hideTopTrim && swtSides[i] == SWT.TOP)
211: continue;
212:
213: List contribs = menuService
214: .getAdditionsForURI(new MenuLocationURI(
215: trimAreaURIs[i]));
216:
217: for (Iterator cacheIter = contribs.iterator(); cacheIter
218: .hasNext();) {
219: AbstractContributionFactory cache = (AbstractContributionFactory) cacheIter
220: .next();
221: ContributionRoot ciList = new ContributionRoot(
222: menuService, restrictionExpression, cache
223: .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 ciIter = ciList.getItems().iterator(); ciIter
228: .hasNext();) {
229: IContributionItem ci = (IContributionItem) ciIter
230: .next();
231: if (ci instanceof ToolBarContributionItem) {
232: // HACK!! Fake this
233: ToolBarTrimProxy tbProxy = new ToolBarTrimProxy(
234: ci.getId(), wbWindow);
235: tbProxy.dock(swtSides[i]);
236:
237: // If we're adding to the 'command1' area then we're -before- the CoolBar
238: IWindowTrim insertBefore = null;
239: if (i == 0) {
240: insertBefore = layout
241: .getTrim("org.eclipse.ui.internal.WorkbenchWindow.topBar"); //$NON-NLS-1$
242: }
243: layout.addTrim(swtSides[i], tbProxy,
244: insertBefore);
245: contributedTrim.add(tbProxy);
246: }
247: }
248: }
249: }
250: }
251:
252: private void teardown() {
253: // First, remove all trim
254: for (Iterator iter = contributedTrim.iterator(); iter.hasNext();) {
255: ToolBarTrimProxy proxy = (ToolBarTrimProxy) iter.next();
256: layout.removeTrim(proxy);
257:
258: try {
259: proxy.dispose();
260: } catch (Throwable e) {
261: IStatus status = null;
262: if (e instanceof CoreException) {
263: status = ((CoreException) e).getStatus();
264: } else {
265: status = StatusUtil
266: .newStatus(
267: IStatus.ERROR,
268: "Internal plug-in widget delegate error on dispose.", e); //$NON-NLS-1$
269: }
270: StatusUtil
271: .handleStatus(
272: status,
273: "widget delegate failed on dispose: id = " + proxy.getId(), StatusManager.LOG); //$NON-NLS-1$
274: }
275: }
276:
277: // Clear out the old list
278: contributedTrim.clear();
279:
280: // clean up the list of ContributionLists
281: for (Iterator iter = contributedLists.iterator(); iter
282: .hasNext();) {
283: ContributionRoot list = (ContributionRoot) iter.next();
284: list.release();
285: }
286:
287: contributedLists.clear();
288: }
289:
290: /**
291: *
292: */
293: public void dispose() {
294: teardown();
295: }
296:
297: /**
298: * @param knownIds
299: */
300: public void updateLocations(List knownIds) {
301: // TODO Auto-generated method stub
302:
303: }
304: }
|