Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dbb9dcf69cd04ea6a65944400e91ccf874e6847d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * A <code>SubContributionManager</code> is used to define a set of contribution
 * items within a parent manager.  Once defined, the visibility of the entire set can 
 * be changed as a unit.
 */
public abstract class SubContributionManager implements IContributionManager {
    /**
     * The parent contribution manager.
     */
    private IContributionManager parentMgr;

    /**
     * Maps each item in the manager to a wrapper.  The wrapper is used to 
     * control the visibility of each item.
     */
    private Map<IContributionItem, SubContributionItem> mapItemToWrapper = new HashMap<IContributionItem, SubContributionItem>();

    /**
     * The visibility of the manager,
     */
    private boolean visible = false;

    /**
     * Constructs a new <code>SubContributionManager</code>
     *
     * @param mgr the parent contribution manager.  All contributions made to the
     *      <code>SubContributionManager</code> are forwarded and appear in the
     *      parent manager.
     */
    public SubContributionManager(IContributionManager mgr) {
        super();
        parentMgr = mgr;
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void add(IAction action) {
        add(new ActionContributionItem(action));
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void add(IContributionItem item) {
        SubContributionItem wrap = wrap(item);
        wrap.setVisible(visible);
        parentMgr.add(wrap);
        itemAdded(item, wrap);
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void appendToGroup(String groupName, IAction action) {
        appendToGroup(groupName, new ActionContributionItem(action));
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void appendToGroup(String groupName, IContributionItem item) {
        SubContributionItem wrap = wrap(item);
        wrap.setVisible(visible);
        parentMgr.appendToGroup(groupName, wrap);
        itemAdded(item, wrap);
    }

    /**
     * Disposes this sub contribution manager, removing all its items
     * and cleaning up any other resources allocated by it.
     * This must leave no trace of this sub contribution manager
     * in the parent manager.  Subclasses may extend.
     * 
     * @since 3.0
     */
    public void disposeManager() {
        Iterator<SubContributionItem> it = mapItemToWrapper.values().iterator();
        // Dispose items in addition to removing them.
        // See bugs 64024 and 73715 for details.
	    // Do not use getItems() here as subclasses can override that in bad ways.
        while (it.hasNext()) {
            IContributionItem item = it.next();
            item.dispose();
        }
        removeAll();
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     *
     * Returns the item passed to us, not the wrapper.
     */
    public IContributionItem find(String id) {
        IContributionItem item = parentMgr.find(id);
        // Return the item passed to us, not the wrapper.
        item = unwrap(item);
        return item;
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     *
     * Returns the items passed to us, not the wrappers.
     */
    public IContributionItem[] getItems() {
        IContributionItem[] result = new IContributionItem[mapItemToWrapper
                .size()];
        mapItemToWrapper.keySet().toArray(result);
        return result;
    }

    /**
     * Returns the parent manager.
     *
     * @return the parent manager
     */
    public IContributionManager getParent() {
        return parentMgr;
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public IContributionManagerOverrides getOverrides() {
        return parentMgr.getOverrides();
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void insertAfter(String id, IAction action) {
        insertAfter(id, new ActionContributionItem(action));
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void insertAfter(String id, IContributionItem item) {
        SubContributionItem wrap = wrap(item);
        wrap.setVisible(visible);
        parentMgr.insertAfter(id, wrap);
        itemAdded(item, wrap);
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void insertBefore(String id, IAction action) {
        insertBefore(id, new ActionContributionItem(action));
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void insertBefore(String id, IContributionItem item) {
        SubContributionItem wrap = wrap(item);
        wrap.setVisible(visible);
        parentMgr.insertBefore(id, wrap);
        itemAdded(item, wrap);
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public boolean isDirty() {
        return parentMgr.isDirty();
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public boolean isEmpty() {
        return parentMgr.isEmpty();
    }

    /**
     * Returns whether the contribution list is visible.
     * If the visibility is <code>true</code> then each item within the manager 
     * appears within the parent manager.  Otherwise, the items are not visible.
     *
     * @return <code>true</code> if the manager is visible
     */
    public boolean isVisible() {
        return visible;
    }

    /**
     * Notifies that an item has been added.
     * <p>
     * Subclasses are not expected to override this method.
     * </p>
     *
     * @param item the item contributed by the client
     * @param wrap the item contributed to the parent manager as a proxy for the item
     *      contributed by the client
     */
    protected void itemAdded(IContributionItem item, SubContributionItem wrap) {
    	item.setParent(this);
        mapItemToWrapper.put(item, wrap);
    }

    /**
     * Notifies that an item has been removed.
     * <p>
     * Subclasses are not expected to override this method.
     * </p>
     *
     * @param item the item contributed by the client
     */
    protected void itemRemoved(IContributionItem item) {
        mapItemToWrapper.remove(item);
        item.setParent(null);
    }

    /**
     * @return fetch all enumeration of wrappers for the item
     * @deprecated Use getItems(String value) instead.
     */
    public Enumeration<SubContributionItem> items() {
        final Iterator<SubContributionItem> i = mapItemToWrapper.values().iterator();
        return new Enumeration<SubContributionItem>() {
            public boolean hasMoreElements() {
                return i.hasNext();
            }

            public SubContributionItem nextElement() {
                return i.next();
            }
        };
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void markDirty() {
        parentMgr.markDirty();
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void prependToGroup(String groupName, IAction action) {
        prependToGroup(groupName, new ActionContributionItem(action));
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void prependToGroup(String groupName, IContributionItem item) {
        SubContributionItem wrap = wrap(item);
        wrap.setVisible(visible);
        parentMgr.prependToGroup(groupName, wrap);
        itemAdded(item, wrap);
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public IContributionItem remove(String id) {
        IContributionItem result = parentMgr.remove(id);
        // result is the wrapped item
        if (result != null) {
        	IContributionItem item = unwrap(result);
			itemRemoved(item);
		}
        return result;
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public IContributionItem remove(IContributionItem item) {
        SubContributionItem wrap = mapItemToWrapper
                .get(item);
        if (wrap == null) {
			return null;
		}
        IContributionItem result = parentMgr.remove(wrap);
        if (result == null) {
			return null;
		}
        itemRemoved(item);
        return item;
    }

    /* (non-Javadoc)
     * Method declared on IContributionManager.
     */
    public void removeAll() {
    	Object[] array = mapItemToWrapper.keySet().toArray();
    	for (int i = 0; i < array.length; i++) {
			IContributionItem item = (IContributionItem) array[i];
			remove(item);
		}
        mapItemToWrapper.clear();
    }

    /**
     * Sets the visibility of the manager.  If the visibility is <code>true</code>
     * then each item within the manager appears within the parent manager.
     * Otherwise, the items are not visible.
     *
     * @param visible the new visibility
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
        if (mapItemToWrapper.size() > 0) {
            Iterator<SubContributionItem> it = mapItemToWrapper.values().iterator();
            while (it.hasNext()) {
                IContributionItem item = it.next();
                item.setVisible(visible);
            }
            parentMgr.markDirty();
        }
    }

    /**
     * Wraps a contribution item in a sub contribution item, and returns the new wrapper.
     * @param item the contribution item to be wrapped
     * @return the wrapped item
     */
    protected SubContributionItem wrap(IContributionItem item) {
        return new SubContributionItem(item);
    }

    /**
     * Unwraps a nested contribution item. If the contribution item is an
     * instance of <code>SubContributionItem</code>, then its inner item is
     * returned. Otherwise, the item itself is returned.
     * 
     * @param item
     *            The item to unwrap; may be <code>null</code>.
     * @return The inner item of <code>item</code>, if <code>item</code> is
     *         a <code>SubContributionItem</code>;<code>item</code>
     *         otherwise.
     */
    protected IContributionItem unwrap(IContributionItem item) {
        if (item instanceof SubContributionItem) {
            return ((SubContributionItem) item).getInnerItem();
        }

        return item;
    }
}

Back to the top