KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > CompoundContributionItem


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.actions;
12
13 import org.eclipse.jface.action.ContributionItem;
14 import org.eclipse.jface.action.IContributionItem;
15 import org.eclipse.jface.action.IContributionManager;
16 import org.eclipse.jface.action.IMenuListener;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.MenuItem;
21
22 /**
23  * A compound contribution is a contribution item consisting of a
24  * dynamic list of contribution items.
25  *
26  * @since 3.1
27  */

28 public abstract class CompoundContributionItem extends ContributionItem {
29
30     private boolean dirty = true;
31
32     private IMenuListener menuListener = new IMenuListener() {
33         public void menuAboutToShow(IMenuManager manager) {
34             manager.markDirty();
35             dirty = true;
36         }
37     };
38     
39     private IContributionItem[] oldItems;
40     
41     /**
42      * Creates a compound contribution item with a <code>null</code> id.
43      */

44     protected CompoundContributionItem() {
45         super();
46     }
47
48     /**
49      * Creates a compound contribution item with the given (optional) id.
50      *
51      * @param id the contribution item identifier, or <code>null</code>
52      */

53     protected CompoundContributionItem(String JavaDoc id) {
54         super(id);
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
59      */

60     public void fill(Menu menu, int index) {
61         if (index == -1) {
62             index = menu.getItemCount();
63         }
64         if (!dirty && menu.getParentItem() != null) {
65             // insert a dummy item so that the parent item is not disabled
66
new MenuItem(menu, SWT.NONE, index);
67             return;
68         }
69         
70         IContributionItem[] items = getContributionItemsToFill();
71         for (int i = 0; i < items.length; i++) {
72             IContributionItem item = items[i];
73             int oldItemCount = menu.getItemCount();
74             if (item.isVisible()) {
75                 item.fill(menu, index);
76             }
77             int newItemCount = menu.getItemCount();
78             int numAdded = newItemCount - oldItemCount;
79             index += numAdded;
80         }
81         dirty = false;
82     }
83     
84     /**
85      * Return a list of contributions items that will replace this item in the
86      * parent manager. The list must contain new contribution items every call
87      * since the old ones will be disposed.
88      *
89      * @return an array list of items to display. Must not be <code>null</code>.
90      */

91     protected abstract IContributionItem[] getContributionItems();
92     
93     private IContributionItem[] getContributionItemsToFill() {
94         if (oldItems != null) {
95             for (int i = 0; i < oldItems.length; i++) {
96                 IContributionItem oldItem = oldItems[i];
97                 oldItem.dispose();
98             }
99             oldItems = null;
100         }
101         oldItems = getContributionItems();
102         return oldItems;
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.jface.action.ContributionItem#isDirty()
107      */

108     public boolean isDirty() {
109         return dirty;
110     }
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.jface.action.ContributionItem#isDynamic()
114      */

115     public boolean isDynamic() {
116         return true;
117     }
118     
119     
120     /* (non-Javadoc)
121      * @see org.eclipse.jface.action.ContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
122      */

123     public void setParent(IContributionManager parent) {
124         if (getParent() instanceof IMenuManager) {
125             IMenuManager menuMgr = (IMenuManager) getParent();
126             menuMgr.removeMenuListener(menuListener);
127         }
128         if (parent instanceof IMenuManager) {
129             IMenuManager menuMgr = (IMenuManager) parent;
130             menuMgr.addMenuListener(menuListener);
131         }
132         super.setParent(parent);
133     }
134 }
135
Popular Tags