KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > action > SubContributionManager


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.jface.action;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * A <code>SubContributionManager</code> is used to define a set of contribution
20  * items within a parent manager. Once defined, the visibility of the entire set can
21  * be changed as a unit.
22  */

23 public abstract class SubContributionManager implements IContributionManager {
24     /**
25      * The parent contribution manager.
26      */

27     private IContributionManager parentMgr;
28
29     /**
30      * Maps each item in the manager to a wrapper. The wrapper is used to
31      * control the visibility of each item.
32      */

33     private Map JavaDoc mapItemToWrapper = new HashMap JavaDoc();
34
35     /**
36      * The visibility of the manager,
37      */

38     private boolean visible = false;
39
40     /**
41      * Constructs a new <code>SubContributionManager</code>
42      *
43      * @param mgr the parent contribution manager. All contributions made to the
44      * <code>SubContributionManager</code> are forwarded and appear in the
45      * parent manager.
46      */

47     public SubContributionManager(IContributionManager mgr) {
48         super();
49         parentMgr = mgr;
50     }
51
52     /* (non-Javadoc)
53      * Method declared on IContributionManager.
54      */

55     public void add(IAction action) {
56         add(new ActionContributionItem(action));
57     }
58
59     /* (non-Javadoc)
60      * Method declared on IContributionManager.
61      */

62     public void add(IContributionItem item) {
63         item.setParent(this);
64         SubContributionItem wrap = wrap(item);
65         wrap.setVisible(visible);
66         parentMgr.add(wrap);
67         itemAdded(item, wrap);
68     }
69
70     /* (non-Javadoc)
71      * Method declared on IContributionManager.
72      */

73     public void appendToGroup(String JavaDoc groupName, IAction action) {
74         appendToGroup(groupName, new ActionContributionItem(action));
75     }
76
77     /* (non-Javadoc)
78      * Method declared on IContributionManager.
79      */

80     public void appendToGroup(String JavaDoc groupName, IContributionItem item) {
81         item.setParent(this);
82         SubContributionItem wrap = wrap(item);
83         wrap.setVisible(visible);
84         parentMgr.appendToGroup(groupName, wrap);
85         itemAdded(item, wrap);
86     }
87
88     /**
89      * Disposes this sub contribution manager, removing all its items
90      * and cleaning up any other resources allocated by it.
91      * This must leave no trace of this sub contribution manager
92      * in the parent manager. Subclasses may extend.
93      *
94      * @since 3.0
95      */

96     public void disposeManager() {
97         Iterator JavaDoc it = mapItemToWrapper.values().iterator();
98         // Dispose items in addition to removing them.
99
// See bugs 64024 and 73715 for details.
100
// Do not use getItems() here as subclasses can override that in bad ways.
101
while (it.hasNext()) {
102             IContributionItem item = (IContributionItem) it.next();
103             item.dispose();
104         }
105         removeAll();
106     }
107
108     /* (non-Javadoc)
109      * Method declared on IContributionManager.
110      *
111      * Returns the item passed to us, not the wrapper.
112      */

113     public IContributionItem find(String JavaDoc id) {
114         IContributionItem item = parentMgr.find(id);
115         // Return the item passed to us, not the wrapper.
116
item = unwrap(item);
117         return item;
118     }
119
120     /* (non-Javadoc)
121      * Method declared on IContributionManager.
122      *
123      * Returns the items passed to us, not the wrappers.
124      */

125     public IContributionItem[] getItems() {
126         IContributionItem[] result = new IContributionItem[mapItemToWrapper
127                 .size()];
128         mapItemToWrapper.keySet().toArray(result);
129         return result;
130     }
131
132     /**
133      * Returns the parent manager.
134      *
135      * @return the parent manager
136      */

137     public IContributionManager getParent() {
138         return parentMgr;
139     }
140
141     /* (non-Javadoc)
142      * Method declared on IContributionManager.
143      */

144     public IContributionManagerOverrides getOverrides() {
145         return parentMgr.getOverrides();
146     }
147
148     /* (non-Javadoc)
149      * Method declared on IContributionManager.
150      */

151     public void insertAfter(String JavaDoc id, IAction action) {
152         insertAfter(id, new ActionContributionItem(action));
153     }
154
155     /* (non-Javadoc)
156      * Method declared on IContributionManager.
157      */

158     public void insertAfter(String JavaDoc id, IContributionItem item) {
159         item.setParent(this);
160         SubContributionItem wrap = wrap(item);
161         wrap.setVisible(visible);
162         parentMgr.insertAfter(id, wrap);
163         itemAdded(item, wrap);
164     }
165
166     /* (non-Javadoc)
167      * Method declared on IContributionManager.
168      */

169     public void insertBefore(String JavaDoc id, IAction action) {
170         insertBefore(id, new ActionContributionItem(action));
171     }
172
173     /* (non-Javadoc)
174      * Method declared on IContributionManager.
175      */

176     public void insertBefore(String JavaDoc id, IContributionItem item) {
177         item.setParent(this);
178         SubContributionItem wrap = wrap(item);
179         wrap.setVisible(visible);
180         parentMgr.insertBefore(id, wrap);
181         itemAdded(item, wrap);
182     }
183
184     /* (non-Javadoc)
185      * Method declared on IContributionManager.
186      */

187     public boolean isDirty() {
188         return parentMgr.isDirty();
189     }
190
191     /* (non-Javadoc)
192      * Method declared on IContributionManager.
193      */

194     public boolean isEmpty() {
195         return parentMgr.isEmpty();
196     }
197
198     /**
199      * Returns whether the contribution list is visible.
200      * If the visibility is <code>true</code> then each item within the manager
201      * appears within the parent manager. Otherwise, the items are not visible.
202      *
203      * @return <code>true</code> if the manager is visible
204      */

205     public boolean isVisible() {
206         return visible;
207     }
208
209     /**
210      * Notifies that an item has been added.
211      * <p>
212      * Subclasses are not expected to override this method.
213      * </p>
214      *
215      * @param item the item contributed by the client
216      * @param wrap the item contributed to the parent manager as a proxy for the item
217      * contributed by the client
218      */

219     protected void itemAdded(IContributionItem item, SubContributionItem wrap) {
220         mapItemToWrapper.put(item, wrap);
221     }
222
223     /**
224      * Notifies that an item has been removed.
225      * <p>
226      * Subclasses are not expected to override this method.
227      * </p>
228      *
229      * @param item the item contributed by the client
230      */

231     protected void itemRemoved(IContributionItem item) {
232         mapItemToWrapper.remove(item);
233     }
234
235     /**
236      * @return fetch all enumeration of wrappers for the item
237      * @deprecated Use getItems(String value) instead.
238      */

239     public Enumeration JavaDoc items() {
240         final Iterator JavaDoc i = mapItemToWrapper.values().iterator();
241         return new Enumeration JavaDoc() {
242             public boolean hasMoreElements() {
243                 return i.hasNext();
244             }
245
246             public Object JavaDoc nextElement() {
247                 return i.next();
248             }
249         };
250     }
251
252     /* (non-Javadoc)
253      * Method declared on IContributionManager.
254      */

255     public void markDirty() {
256         parentMgr.markDirty();
257     }
258
259     /* (non-Javadoc)
260      * Method declared on IContributionManager.
261      */

262     public void prependToGroup(String JavaDoc groupName, IAction action) {
263         prependToGroup(groupName, new ActionContributionItem(action));
264     }
265
266     /* (non-Javadoc)
267      * Method declared on IContributionManager.
268      */

269     public void prependToGroup(String JavaDoc groupName, IContributionItem item) {
270         item.setParent(this);
271         SubContributionItem wrap = wrap(item);
272         wrap.setVisible(visible);
273         parentMgr.prependToGroup(groupName, wrap);
274         itemAdded(item, wrap);
275     }
276
277     /* (non-Javadoc)
278      * Method declared on IContributionManager.
279      */

280     public IContributionItem remove(String JavaDoc id) {
281         IContributionItem result = parentMgr.remove(id);
282         if (result != null) {
283             itemRemoved(result);
284         }
285         return result;
286     }
287
288     /* (non-Javadoc)
289      * Method declared on IContributionManager.
290      */

291     public IContributionItem remove(IContributionItem item) {
292         SubContributionItem wrap = (SubContributionItem) mapItemToWrapper
293                 .get(item);
294         if (wrap == null) {
295             return null;
296         }
297         IContributionItem result = parentMgr.remove(wrap);
298         if (result == null) {
299             return null;
300         }
301         itemRemoved(item);
302         return item;
303     }
304
305     /* (non-Javadoc)
306      * Method declared on IContributionManager.
307      */

308     public void removeAll() {
309         Iterator JavaDoc it = mapItemToWrapper.values().iterator();
310         while (it.hasNext()) {
311             IContributionItem item = (IContributionItem) it.next();
312             parentMgr.remove(item);
313         }
314         mapItemToWrapper.clear();
315     }
316
317     /**
318      * Sets the visibility of the manager. If the visibility is <code>true</code>
319      * then each item within the manager appears within the parent manager.
320      * Otherwise, the items are not visible.
321      *
322      * @param visible the new visibility
323      */

324     public void setVisible(boolean visible) {
325         this.visible = visible;
326         if (mapItemToWrapper.size() > 0) {
327             Iterator JavaDoc it = mapItemToWrapper.values().iterator();
328             while (it.hasNext()) {
329                 IContributionItem item = (IContributionItem) it.next();
330                 item.setVisible(visible);
331             }
332             parentMgr.markDirty();
333         }
334     }
335
336     /**
337      * Wraps a contribution item in a sub contribution item, and returns the new wrapper.
338      * @param item the contribution item to be wrapped
339      * @return the wrapped item
340      */

341     protected SubContributionItem wrap(IContributionItem item) {
342         return new SubContributionItem(item);
343     }
344
345     /**
346      * Unwraps a nested contribution item. If the contribution item is an
347      * instance of <code>SubContributionItem</code>, then its inner item is
348      * returned. Otherwise, the item itself is returned.
349      *
350      * @param item
351      * The item to unwrap; may be <code>null</code>.
352      * @return The inner item of <code>item</code>, if <code>item</code> is
353      * a <code>SubContributionItem</code>;<code>item</code>
354      * otherwise.
355      */

356     protected IContributionItem unwrap(IContributionItem item) {
357         if (item instanceof SubContributionItem) {
358             return ((SubContributionItem) item).getInnerItem();
359         }
360
361         return item;
362     }
363 }
364
Popular Tags