001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.action.ContributionItem;
016: import org.eclipse.jface.action.IContributionItem;
017: import org.eclipse.jface.action.IContributionManager;
018: import org.eclipse.jface.action.ICoolBarManager;
019: import org.eclipse.jface.action.IMenuManager;
020: import org.eclipse.jface.action.IToolBarManager;
021: import org.eclipse.jface.action.SubMenuManager;
022: import org.eclipse.jface.action.SubToolBarManager;
023: import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem;
024: import org.eclipse.ui.IActionBars2;
025: import org.eclipse.ui.IWorkbenchActionConstants;
026: import org.eclipse.ui.SubActionBars2;
027: import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
028: import org.eclipse.ui.services.IServiceLocator;
029:
030: /**
031: * This class represents the action bars for an action set.
032: */
033: public class ActionSetActionBars extends SubActionBars2 {
034:
035: private IActionBarConfigurer2 actionBarConfigurer = null;
036:
037: private String actionSetId;
038:
039: private ArrayList adjunctContributions = new ArrayList();
040:
041: private IToolBarManager coolItemToolBarMgr = null;
042:
043: private IToolBarContributionItem toolBarContributionItem = null;
044:
045: /**
046: * Constructs a new action bars object
047: */
048: public ActionSetActionBars(IActionBars2 parent,
049: IServiceLocator serviceLocator,
050: IActionBarConfigurer2 actionBarConfigurer,
051: String actionSetId) {
052: super (parent, serviceLocator);
053: this .actionSetId = actionSetId;
054: this .actionBarConfigurer = actionBarConfigurer;
055: }
056:
057: /**
058: * Adds to the list all the actions that are part of this action set but
059: * belong to different cool bar items.
060: *
061: * @param item
062: * the item defined in this actionset but in a different tool Bar
063: * contribution item
064: */
065: /* package */void addAdjunctContribution(IContributionItem item) {
066: adjunctContributions.add(item);
067: }
068:
069: /*
070: * (non-Javadoc) Inherited from SubActionBars.
071: */
072: protected SubMenuManager createSubMenuManager(IMenuManager parent) {
073: return new ActionSetMenuManager(parent, actionSetId);
074: }
075:
076: /*
077: * (non-Javadoc) Inherited from SubActionBars.
078: */
079: protected SubToolBarManager createSubToolBarManager(
080: IToolBarManager parent) {
081: // return null, action sets are managed by CoolItemToolBarManagers
082: return null;
083: }
084:
085: /**
086: * Dispose the contributions.
087: */
088: public void dispose() {
089: super .dispose();
090: if (coolItemToolBarMgr == null) {
091: return;
092: }
093: IContributionItem[] items = coolItemToolBarMgr.getItems();
094: // remove the action set's items from its action bar, don't use
095: // removeAll since other items from other actions sets may be in
096: // the action bar's cool item
097: for (int i = 0; i < items.length; i++) {
098: IContributionItem item = items[i];
099: if (item instanceof PluginActionCoolBarContributionItem) {
100: PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item;
101: if (actionSetItem.getActionSetId().equals(actionSetId)) {
102: coolItemToolBarMgr.remove(item);
103: item.dispose();
104: }
105: } else {
106: // leave separators and group markers intact, doing
107: // so allows ordering to be maintained when action sets
108: // are removed then added back
109: }
110: }
111:
112: // remove items from this action set that are in other action bars
113: for (int i = 0; i < adjunctContributions.size(); i++) {
114: ContributionItem item = (ContributionItem) adjunctContributions
115: .get(i);
116: IContributionManager parent = item.getParent();
117: if (parent != null) {
118: parent.remove(item);
119: item.dispose();
120: }
121: }
122: toolBarContributionItem = null;
123: coolItemToolBarMgr = null;
124: adjunctContributions = new ArrayList();
125: }
126:
127: /**
128: * Returns the contribution item that the given contribution item should be
129: * inserted after.
130: *
131: * @param startId
132: * the location to start looking alphabetically.
133: * @param itemId
134: * the target item id.
135: * @param mgr
136: * the contribution manager.
137: * @return the contribution item that the given items should be returned
138: * after.
139: *
140: * @since 3.0
141: */
142: private IContributionItem findAlphabeticalOrder(String startId,
143: String itemId, IContributionManager mgr) {
144: IContributionItem[] items = mgr.getItems();
145: int insertIndex = 0;
146:
147: // look for starting point
148: while (insertIndex < items.length) {
149: IContributionItem item = items[insertIndex];
150: if (item.getId() != null && item.getId().equals(startId)) {
151: break;
152: }
153: ++insertIndex;
154: }
155:
156: // Find the index that this item should be inserted in
157: for (int i = insertIndex + 1; i < items.length; i++) {
158: IContributionItem item = items[i];
159: String testId = item.getId();
160:
161: if (item.isGroupMarker()) {
162: break;
163: }
164:
165: if (itemId != null && testId != null) {
166: if (itemId.compareTo(testId) < 1) {
167: break;
168: }
169: }
170: insertIndex = i;
171: }
172: // Should be inserted at the end
173: if (insertIndex >= items.length) {
174: return null;
175: }
176: return items[insertIndex];
177: }
178:
179: /* package */String getActionSetId() {
180: return actionSetId;
181: }
182:
183: /**
184: * Returns a tool bar manager for this Item.
185: *
186: * @return the tool bar manager
187: */
188: public IToolBarManager getToolBarManager() {
189: ICoolBarManager coolBarManager = getCastedParent()
190: .getCoolBarManager();
191: if (coolBarManager == null) {
192: return null;
193: }
194: return actionBarConfigurer.createToolBarManager();
195: }
196:
197: /**
198: * Returns the correct tool bar for the given action id. If this action is
199: * an adjunct type the it returns the toolbar manager from the cool bar
200: * manager.
201: *
202: * @param id
203: * the id of the action
204: * @return the tool bar manager
205: */
206: public IToolBarManager getToolBarManager(String actionId) {
207: // Check if a tool bar manager for an adjunct type is being requested
208: String toolBarId = actionSetId;
209: boolean isAdjunctType = false;
210: if (!actionId.equals(actionSetId)) {
211: // Adjunct type
212: toolBarId = actionId;
213: isAdjunctType = true;
214: }
215:
216: // Rereive the cool bar manager
217: ICoolBarManager coolBarManager = getCastedParent()
218: .getCoolBarManager();
219: if (coolBarManager == null) {
220: return null;
221: }
222:
223: // Check to see that there isn't already a tool bar created
224: // and the tool bar being requested is not for an adjunct action
225: if ((coolItemToolBarMgr != null) && (!isAdjunctType)) {
226: return coolItemToolBarMgr;
227: }
228:
229: // Search for toolBarId in the cool Bar manager
230: IContributionItem cbItem = coolBarManager.find(toolBarId);
231: // If there hasn't been a tool bar contribution item created for this
232: // tool bar
233: // id then create one. Otherwise retrieve the tool bar contribution
234: // item
235: if (cbItem instanceof IToolBarContributionItem) {
236:
237: IToolBarContributionItem tbcbItem = (IToolBarContributionItem) cbItem;
238: coolItemToolBarMgr = tbcbItem.getToolBarManager();
239: // If this not an adjuct type then we can cashe the tool bar
240: // contribution type
241: if (!isAdjunctType) {
242: toolBarContributionItem = tbcbItem;
243: }
244: } else {
245:
246: coolItemToolBarMgr = actionBarConfigurer
247: .createToolBarManager();
248:
249: // If this is not an adjunct type then create a tool bar
250: // contribution item
251: // we don't create one for an adjunct type because another action
252: // set action bars contains one
253:
254: IContributionItem toolBarContributionItem = actionBarConfigurer
255: .createToolBarContributionItem(coolItemToolBarMgr,
256: toolBarId);
257:
258: toolBarContributionItem.setParent(coolItemToolBarMgr);
259: toolBarContributionItem.setVisible(getActive());
260: coolItemToolBarMgr.markDirty();
261:
262: // Now add the tool bar contribution Item to the cool bar manager
263: IContributionItem refItem = findAlphabeticalOrder(
264: IWorkbenchActionConstants.MB_ADDITIONS, toolBarId,
265: coolBarManager);
266: if (refItem != null) {
267: coolBarManager.insertAfter(refItem.getId(),
268: toolBarContributionItem);
269: } else {
270: coolBarManager.add(toolBarContributionItem);
271: }
272: }
273: return coolItemToolBarMgr;
274: }
275:
276: // for dynamic UI
277: /* package */void removeAdjunctContribution(ContributionItem item) {
278: adjunctContributions.remove(item);
279: }
280:
281: /**
282: * Activate / Deactivate the contributions.
283: */
284: protected void setActive(boolean set) {
285: super .setActive(set);
286:
287: ICoolBarManager coolBarManager = getCastedParent()
288: .getCoolBarManager();
289: if (coolBarManager == null) {
290: return;
291: }
292:
293: // 1. Need to set visibility for all non-adjunct actions
294: if (coolItemToolBarMgr != null) {
295: IContributionItem[] items = coolItemToolBarMgr.getItems();
296: for (int i = 0; i < items.length; i++) {
297: IContributionItem item = items[i];
298: if (item instanceof PluginActionCoolBarContributionItem) {
299: PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item;
300: // Only if the action set id for this contribution item is
301: // the same
302: // as this object
303: if (actionSetItem.getActionSetId().equals(
304: actionSetId)) {
305: item.setVisible(set);
306: coolItemToolBarMgr.markDirty();
307: if (!coolBarManager.isDirty()) {
308: coolBarManager.markDirty();
309: }
310: }
311: }
312: }
313: // Update the manager
314: coolItemToolBarMgr.update(false);
315: if (toolBarContributionItem != null) {
316: toolBarContributionItem.update(ICoolBarManager.SIZE);
317: }
318: }
319:
320: // 2. Need to set visibility for all adjunct actions
321: if (adjunctContributions.size() > 0) {
322: for (Iterator i = adjunctContributions.iterator(); i
323: .hasNext();) {
324: IContributionItem item = (IContributionItem) i.next();
325: if (item instanceof ContributionItem) {
326: item.setVisible(set);
327: IContributionManager manager = ((ContributionItem) item)
328: .getParent();
329: manager.markDirty();
330: manager.update(false);
331: if (!coolBarManager.isDirty()) {
332: coolBarManager.markDirty();
333: }
334: item.update(ICoolBarManager.SIZE);
335: }
336:
337: }
338:
339: }
340: }
341:
342: }
|