001: package org.drools.eclipse.flow.common.editor;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.eclipse.jface.action.Action;
008: import org.eclipse.jface.action.ActionContributionItem;
009: import org.eclipse.jface.action.IAction;
010: import org.eclipse.jface.action.IContributionItem;
011: import org.eclipse.jface.action.IMenuCreator;
012: import org.eclipse.jface.util.IPropertyChangeListener;
013: import org.eclipse.jface.util.PropertyChangeEvent;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.events.SelectionListener;
016: import org.eclipse.swt.widgets.Control;
017: import org.eclipse.swt.widgets.Menu;
018: import org.eclipse.swt.widgets.MenuItem;
019:
020: public class DropDownMenuWithDefaultAction extends Action implements
021: IMenuCreator {
022:
023: private Menu dropDownMenu;
024:
025: private IAction delegate;
026:
027: private List list;
028:
029: private IPropertyChangeListener enabledListener;
030: private SelectionListener selectionListener;
031:
032: public DropDownMenuWithDefaultAction(final IAction action) {
033: this .selectionListener = new ItemSelectionListener(this );
034: setMenuCreator(this );
035: this .dropDownMenu = null;
036: setAction(action);
037: this .list = new ArrayList();
038: }
039:
040: public void dispose() {
041: if (this .dropDownMenu != null) {
042: this .dropDownMenu.dispose();
043: this .dropDownMenu = null;
044: }
045: }
046:
047: public void add(final IContributionItem item) {
048: this .list.add(item);
049: }
050:
051: public void add(final IAction action) {
052: this .list.add(action);
053: }
054:
055: public Menu getMenu(final Control parent) {
056: if (this .dropDownMenu == null) {
057: this .dropDownMenu = new Menu(parent);
058: populateMenu();
059: }
060: return this .dropDownMenu;
061: }
062:
063: public Menu getMenu(final Menu parent) {
064: if (this .dropDownMenu == null) {
065: this .dropDownMenu = new Menu(parent);
066: populateMenu();
067: }
068: return this .dropDownMenu;
069: }
070:
071: private void populateMenu() {
072: for (final Iterator it = this .list.iterator(); it.hasNext();) {
073: final Object object = it.next();
074: if (object instanceof IContributionItem) {
075: final IContributionItem item = (IContributionItem) object;
076: item.fill(this .dropDownMenu, -1);
077: } else {
078: final IAction action = (IAction) object;
079: final ActionContributionItem item = new ActionContributionItem(
080: action);
081: item.fill(this .dropDownMenu, -1);
082: }
083: }
084: final MenuItem[] items = this .dropDownMenu.getItems();
085: for (int i = 0; i < items.length; i++) {
086: items[i].addSelectionListener(this .selectionListener);
087: }
088: }
089:
090: public void setAction(final IAction action) {
091: if (this .enabledListener == null) {
092: this .enabledListener = new EnabledPropertyChangeListener(
093: this );
094: }
095: setText(action.getText());
096: setToolTipText(action.getToolTipText());
097: setImageDescriptor(action.getImageDescriptor());
098: setDisabledImageDescriptor(action.getDisabledImageDescriptor());
099: setEnabled(action.isEnabled());
100: setDescription(action.getDescription());
101: setHelpListener(action.getHelpListener());
102: setHoverImageDescriptor(action.getHoverImageDescriptor());
103: if (this .delegate != null) {
104: this .delegate
105: .removePropertyChangeListener(this .enabledListener);
106: }
107: this .delegate = action;
108: this .delegate.addPropertyChangeListener(this .enabledListener);
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
113: */
114: public void run() {
115: this .delegate.run();
116: }
117:
118: public static class EnabledPropertyChangeListener implements
119: IPropertyChangeListener {
120: private IAction action;
121:
122: public EnabledPropertyChangeListener(final IAction action) {
123: this .action = action;
124: }
125:
126: public void propertyChange(final PropertyChangeEvent event) {
127: if (event.getProperty().equals(IAction.ENABLED)) {
128: this .action.setEnabled(((Boolean) event.getNewValue())
129: .booleanValue());
130: }
131: }
132: }
133:
134: public static class ItemSelectionListener implements
135: SelectionListener {
136: private DropDownMenuWithDefaultAction dropDownMenu;
137:
138: public ItemSelectionListener(
139: final DropDownMenuWithDefaultAction dropDownMenu) {
140: this .dropDownMenu = dropDownMenu;
141: }
142:
143: public void widgetDefaultSelected(final SelectionEvent e) {
144: final MenuItem menuItem = (MenuItem) e.getSource();
145: if (menuItem.getData() instanceof ActionContributionItem) {
146: final ActionContributionItem item = (ActionContributionItem) menuItem
147: .getData();
148: this .dropDownMenu.setAction(item.getAction());
149: }
150: }
151:
152: public void widgetSelected(final SelectionEvent e) {
153: final MenuItem menuItem = (MenuItem) e.getSource();
154: if (menuItem.getData() instanceof ActionContributionItem) {
155: final ActionContributionItem item = (ActionContributionItem) menuItem
156: .getData();
157: this.dropDownMenu.setAction(item.getAction());
158: }
159: }
160: }
161: }
|