KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.action.ContributionItem;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.action.IContributionManager;
19 import org.eclipse.jface.action.ICoolBarManager;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.IToolBarManager;
22 import org.eclipse.jface.action.SubMenuManager;
23 import org.eclipse.jface.action.SubToolBarManager;
24 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem;
25 import org.eclipse.ui.IActionBars2;
26 import org.eclipse.ui.IWorkbenchActionConstants;
27 import org.eclipse.ui.SubActionBars2;
28 import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
29 import org.eclipse.ui.services.IServiceLocator;
30
31 /**
32  * This class represents the action bars for an action set.
33  */

34 public class ActionSetActionBars extends SubActionBars2 {
35     
36     private IActionBarConfigurer2 actionBarConfigurer = null;
37
38     private String JavaDoc actionSetId;
39
40     private ArrayList JavaDoc adjunctContributions = new ArrayList JavaDoc();
41
42     private IToolBarManager coolItemToolBarMgr = null;
43
44     private IToolBarContributionItem toolBarContributionItem = null;
45
46     /**
47      * Constructs a new action bars object
48      */

49     public ActionSetActionBars(IActionBars2 parent, IServiceLocator serviceLocator, IActionBarConfigurer2 actionBarConfigurer, String JavaDoc actionSetId) {
50         super(parent, serviceLocator);
51         this.actionSetId = actionSetId;
52         this.actionBarConfigurer = actionBarConfigurer;
53     }
54
55     /**
56      * Adds to the list all the actions that are part of this action set but
57      * belong to different cool bar items.
58      *
59      * @param item
60      * the item defined in this actionset but in a different tool Bar
61      * contribution item
62      */

63     /* package */void addAdjunctContribution(IContributionItem item) {
64         adjunctContributions.add(item);
65     }
66
67     /*
68      * (non-Javadoc) Inherited from SubActionBars.
69      */

70     protected SubMenuManager createSubMenuManager(IMenuManager parent) {
71         return new ActionSetMenuManager(parent, actionSetId);
72     }
73
74     /*
75      * (non-Javadoc) Inherited from SubActionBars.
76      */

77     protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) {
78         // return null, action sets are managed by CoolItemToolBarManagers
79
return null;
80     }
81
82     /**
83      * Dispose the contributions.
84      */

85     public void dispose() {
86         super.dispose();
87         if (coolItemToolBarMgr == null) {
88             return;
89         }
90         IContributionItem[] items = coolItemToolBarMgr.getItems();
91         // remove the action set's items from its action bar, don't use
92
// removeAll since other items from other actions sets may be in
93
// the action bar's cool item
94
for (int i = 0; i < items.length; i++) {
95             IContributionItem item = items[i];
96             if (item instanceof PluginActionCoolBarContributionItem) {
97                 PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item;
98                 if (actionSetItem.getActionSetId().equals(actionSetId)) {
99                     coolItemToolBarMgr.remove(item);
100                     item.dispose();
101                 }
102             } else {
103                 // leave separators and group markers intact, doing
104
// so allows ordering to be maintained when action sets
105
// are removed then added back
106
}
107         }
108
109         // remove items from this action set that are in other action bars
110
for (int i = 0; i < adjunctContributions.size(); i++) {
111             ContributionItem item = (ContributionItem) adjunctContributions
112                     .get(i);
113             IContributionManager parent = item.getParent();
114             if (parent != null) {
115                 parent.remove(item);
116                 item.dispose();
117             }
118         }
119         toolBarContributionItem = null;
120         coolItemToolBarMgr = null;
121         adjunctContributions = new ArrayList JavaDoc();
122     }
123
124     /**
125      * Returns the contribution item that the given contribution item should be
126      * inserted after.
127      *
128      * @param startId
129      * the location to start looking alphabetically.
130      * @param itemId
131      * the target item id.
132      * @param mgr
133      * the contribution manager.
134      * @return the contribution item that the given items should be returned
135      * after.
136      *
137      * @since 3.0
138      */

139     private IContributionItem findAlphabeticalOrder(String JavaDoc startId,
140             String JavaDoc itemId, IContributionManager mgr) {
141         IContributionItem[] items = mgr.getItems();
142         int insertIndex = 0;
143
144         // look for starting point
145
while (insertIndex < items.length) {
146             IContributionItem item = items[insertIndex];
147             if (item.getId() != null && item.getId().equals(startId)) {
148                 break;
149             }
150             ++insertIndex;
151         }
152
153         // Find the index that this item should be inserted in
154
for (int i = insertIndex + 1; i < items.length; i++) {
155             IContributionItem item = items[i];
156             String JavaDoc testId = item.getId();
157
158             if (item.isGroupMarker()) {
159                 break;
160             }
161
162             if (itemId != null && testId != null) {
163                 if (itemId.compareTo(testId) < 1) {
164                     break;
165                 }
166             }
167             insertIndex = i;
168         }
169         // Should be inserted at the end
170
if (insertIndex >= items.length) {
171             return null;
172         }
173         return items[insertIndex];
174     }
175
176     /* package */String JavaDoc getActionSetId() {
177         return actionSetId;
178     }
179
180     /**
181      * Returns a tool bar manager for this Item.
182      *
183      * @return the tool bar manager
184      */

185     public IToolBarManager getToolBarManager() {
186         ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
187         if (coolBarManager == null) {
188             return null;
189         }
190         return actionBarConfigurer.createToolBarManager();
191     }
192
193     /**
194      * Returns the correct tool bar for the given action id. If this action is
195      * an adjunct type the it returns the toolbar manager from the cool bar
196      * manager.
197      *
198      * @param id
199      * the id of the action
200      * @return the tool bar manager
201      */

202     public IToolBarManager getToolBarManager(String JavaDoc actionId) {
203         // Check if a tool bar manager for an adjunct type is being requested
204
String JavaDoc toolBarId = actionSetId;
205         boolean isAdjunctType = false;
206         if (!actionId.equals(actionSetId)) {
207             // Adjunct type
208
toolBarId = actionId;
209             isAdjunctType = true;
210         }
211
212         // Rereive the cool bar manager
213
ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
214         if (coolBarManager == null) {
215             return null;
216         }
217
218         // Check to see that there isn't already a tool bar created
219
// and the tool bar being requested is not for an adjunct action
220
if ((coolItemToolBarMgr != null) && (!isAdjunctType)) {
221             return coolItemToolBarMgr;
222         }
223
224         // Search for toolBarId in the cool Bar manager
225
IContributionItem cbItem = coolBarManager.find(toolBarId);
226         // If there hasn't been a tool bar contribution item created for this
227
// tool bar
228
// id then create one. Otherwise retrieve the tool bar contribution
229
// item
230
if (cbItem instanceof IToolBarContributionItem) {
231
232             IToolBarContributionItem tbcbItem = (IToolBarContributionItem) cbItem;
233             coolItemToolBarMgr = tbcbItem.getToolBarManager();
234             // If this not an adjuct type then we can cashe the tool bar
235
// contribution type
236
if (!isAdjunctType) {
237                 toolBarContributionItem = tbcbItem;
238             }
239         } else {
240             
241             coolItemToolBarMgr = actionBarConfigurer.createToolBarManager();
242            
243             // If this is not an adjunct type then create a tool bar
244
// contribution item
245
// we don't create one for an adjunct type because another action
246
// set action bars contains one
247

248             IContributionItem toolBarContributionItem = actionBarConfigurer
249                     .createToolBarContributionItem(coolItemToolBarMgr,
250                             toolBarId);
251
252             toolBarContributionItem.setParent(coolItemToolBarMgr);
253             toolBarContributionItem.setVisible(getActive());
254             coolItemToolBarMgr.markDirty();
255
256             // Now add the tool bar contribution Item to the cool bar manager
257
IContributionItem refItem = findAlphabeticalOrder(
258                     IWorkbenchActionConstants.MB_ADDITIONS, toolBarId,
259                     coolBarManager);
260             if (refItem != null) {
261                 coolBarManager.insertAfter(refItem.getId(),
262                         toolBarContributionItem);
263             } else {
264                 coolBarManager.add(toolBarContributionItem);
265             }
266         }
267         return coolItemToolBarMgr;
268     }
269
270     // for dynamic UI
271
/* package */void removeAdjunctContribution(ContributionItem item) {
272         adjunctContributions.remove(item);
273     }
274
275     /**
276      * Activate / Deactivate the contributions.
277      */

278     protected void setActive(boolean set) {
279         super.setActive(set);
280
281         ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
282         if (coolBarManager == null) {
283             return;
284         }
285
286         // 1. Need to set visibility for all non-adjunct actions
287
if (coolItemToolBarMgr != null) {
288             IContributionItem[] items = coolItemToolBarMgr.getItems();
289             for (int i = 0; i < items.length; i++) {
290                 IContributionItem item = items[i];
291                 if (item instanceof PluginActionCoolBarContributionItem) {
292                     PluginActionCoolBarContributionItem actionSetItem = (PluginActionCoolBarContributionItem) item;
293                     // Only if the action set id for this contribution item is
294
// the same
295
// as this object
296
if (actionSetItem.getActionSetId().equals(actionSetId)) {
297                         item.setVisible(set);
298                         coolItemToolBarMgr.markDirty();
299                         if (!coolBarManager.isDirty()) {
300                             coolBarManager.markDirty();
301                         }
302                     }
303                 }
304             }
305             // Update the manager
306
coolItemToolBarMgr.update(false);
307             if (toolBarContributionItem != null) {
308                 toolBarContributionItem.update(ICoolBarManager.SIZE);
309             }
310         }
311
312         // 2. Need to set visibility for all adjunct actions
313
if (adjunctContributions.size() > 0) {
314             for (Iterator JavaDoc i = adjunctContributions.iterator(); i.hasNext();) {
315                 IContributionItem item = (IContributionItem) i.next();
316                 if (item instanceof ContributionItem) {
317                     item.setVisible(set);
318                     IContributionManager manager = ((ContributionItem) item)
319                             .getParent();
320                     manager.markDirty();
321                     manager.update(false);
322                     if (!coolBarManager.isDirty()) {
323                         coolBarManager.markDirty();
324                     }
325                     item.update(ICoolBarManager.SIZE);
326                 }
327
328             }
329
330         }
331     }
332
333 }
334
Popular Tags