1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.ui.jface;
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;
26 * Modified version of org.eclipse.ui.actions.CompoundContributionItem
27 * This implementation adds ToolBar and CoolBar support.
29 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32 public abstract class CompoundContributionItem extends ContributionItem {
34 private boolean dirty = true;
36 private IMenuListener menuListener = new IMenuListener() {
37 public void menuAboutToShow(IMenuManager manager) {
43 private IContributionItem[] oldItems;
46 * Creates a compound contribution item with a <code>null</code> id.
48 protected CompoundContributionItem() {
53 * Creates a compound contribution item with the given (optional) id.
55 * @param id the contribution item identifier, or <code>null</code>
57 protected CompoundContributionItem(String id) {
62 * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
64 public void fill(Menu menu, int index) {
66 index = menu.getItemCount();
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);
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);
81 int newItemCount = menu.getItemCount();
82 int numAdded = newItemCount - oldItemCount;
89 public void fill(CoolBar parent, int index) {
91 index = parent.getItemCount();
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);
103 int newItemCount = parent.getItemCount();
104 int numAdded = newItemCount - oldItemCount;
111 public void fill(ToolBar parent, int index) {
113 index = parent.getItemCount();
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);
125 int newItemCount = parent.getItemCount();
126 int numAdded = newItemCount - oldItemCount;
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.
137 * @return an array list of items to display. Must not be <code>null</code>.
139 protected abstract IContributionItem[] getContributionItems();
141 private IContributionItem[] getContributionItemsToFill() {
142 if (oldItems != null) {
143 for (int i = 0; i < oldItems.length; i++) {
144 IContributionItem oldItem = oldItems[i];
149 oldItems = getContributionItems();
154 * @see org.eclipse.jface.action.ContributionItem#isDirty()
156 public boolean isDirty() {
161 * @see org.eclipse.jface.action.ContributionItem#isDynamic()
163 public boolean isDynamic() {
169 * @see org.eclipse.jface.action.ContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
171 public void setParent(IContributionManager parent) {
172 if (getParent() instanceof IMenuManager) {
173 IMenuManager menuMgr = (IMenuManager) getParent();
174 menuMgr.removeMenuListener(menuListener);
176 if (parent instanceof IMenuManager) {
177 IMenuManager menuMgr = (IMenuManager) parent;
178 menuMgr.addMenuListener(menuListener);
180 super.setParent(parent);