]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/CompoundContributionItem.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / CompoundContributionItem.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.jface;
13
14 import org.eclipse.jface.action.ContributionItem;
15 import org.eclipse.jface.action.IContributionItem;
16 import org.eclipse.jface.action.IContributionManager;
17 import org.eclipse.jface.action.IMenuListener;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.CoolBar;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.MenuItem;
23 import org.eclipse.swt.widgets.ToolBar;
24
25 /**
26  * Modified version of org.eclipse.ui.actions.CompoundContributionItem
27  * This implementation adds ToolBar and CoolBar support.
28  * 
29  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
30  * 
31  */
32 public abstract class CompoundContributionItem extends ContributionItem {
33
34     private boolean dirty = true;
35
36     private IMenuListener menuListener = new IMenuListener() {
37         public void menuAboutToShow(IMenuManager manager) {
38             manager.markDirty();
39             dirty = true;
40         }
41     };
42     
43     private IContributionItem[] oldItems;
44     
45     /**
46      * Creates a compound contribution item with a <code>null</code> id.
47      */
48     protected CompoundContributionItem() {
49         super();
50     }
51
52     /**
53      * Creates a compound contribution item with the given (optional) id.
54      *
55      * @param id the contribution item identifier, or <code>null</code>
56      */
57     protected CompoundContributionItem(String id) {
58         super(id);
59     }
60     
61     /* (non-Javadoc)
62      * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
63      */
64     public void fill(Menu menu, int index) {
65         if (index == -1) {
66                         index = menu.getItemCount();
67                 }
68         if (!dirty && menu.getParentItem() != null) {
69             // insert a dummy item so that the parent item is not disabled
70             new MenuItem(menu, SWT.NONE, index);
71             return;
72         }
73         
74         IContributionItem[] items = getContributionItemsToFill();
75         for (int i = 0; i < items.length; i++) {
76             IContributionItem item = items[i];
77             int oldItemCount = menu.getItemCount();
78             if (item.isVisible()) {
79                 item.fill(menu, index);
80             }
81             int newItemCount = menu.getItemCount();
82             int numAdded = newItemCount - oldItemCount;
83             index += numAdded;
84         }
85         dirty = false;
86     }
87     
88     @Override
89     public void fill(CoolBar parent, int index) {
90         if (index == -1) {
91                         index = parent.getItemCount();
92                 }
93 //      if (!dirty ) {
94 //            return;
95 //        }
96         IContributionItem[] items = getContributionItemsToFill();
97         for (int i = 0; i < items.length; i++) {
98              IContributionItem item = items[i];
99              int oldItemCount = parent.getItemCount();
100              if (item.isVisible()) {
101                  item.fill(parent, index);
102              }
103              int newItemCount = parent.getItemCount();
104              int numAdded = newItemCount - oldItemCount;
105              index += numAdded;
106          }
107          dirty = false;
108     }
109     
110     @Override
111     public void fill(ToolBar parent, int index) {
112         if (index == -1) {
113                         index = parent.getItemCount();
114                 }
115 //      if (!dirty ) {
116 //            return;
117 //        }
118         IContributionItem[] items = getContributionItemsToFill();
119         for (int i = 0; i < items.length; i++) {
120             IContributionItem item = items[i];
121             int oldItemCount = parent.getItemCount();
122             if (item.isVisible()) {
123                 item.fill(parent, index);
124             }
125             int newItemCount = parent.getItemCount();
126             int numAdded = newItemCount - oldItemCount;
127             index += numAdded;
128         }
129         dirty = false;
130     }
131     
132     /**
133          * Return a list of contributions items that will replace this item in the
134          * parent manager. The list must contain new contribution items every call
135          * since the old ones will be disposed.
136          * 
137          * @return an array list of items to display. Must not be <code>null</code>.
138          */
139     protected abstract IContributionItem[] getContributionItems();
140     
141     private IContributionItem[] getContributionItemsToFill() {
142         if (oldItems != null) {
143             for (int i = 0; i < oldItems.length; i++) {
144                 IContributionItem oldItem = oldItems[i];
145                 oldItem.dispose();
146             }
147             oldItems = null;
148         }
149         oldItems = getContributionItems();
150         return oldItems;
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.jface.action.ContributionItem#isDirty()
155      */
156     public boolean isDirty() {
157         return dirty;
158     }
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.jface.action.ContributionItem#isDynamic()
162      */
163     public boolean isDynamic() {
164         return true;
165     }
166     
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.action.ContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
170      */
171     public void setParent(IContributionManager parent) {
172         if (getParent() instanceof IMenuManager) {
173             IMenuManager menuMgr = (IMenuManager) getParent();
174             menuMgr.removeMenuListener(menuListener);
175         }
176         if (parent instanceof IMenuManager) {
177             IMenuManager menuMgr = (IMenuManager) parent;
178             menuMgr.addMenuListener(menuListener);
179         }
180         super.setParent(parent);
181     }
182 }