001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.widgets.Menu;
016: import org.eclipse.swt.widgets.MenuItem;
017:
018: import org.eclipse.jface.action.ContributionItem;
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.Separator;
022: import org.eclipse.jface.resource.ImageDescriptor;
023:
024: import org.eclipse.ui.actions.ActionGroup;
025:
026: import org.eclipse.jdt.internal.ui.JavaPlugin;
027:
028: /**
029: * A MultiActionGroup will display a list of IActions in a menu by transforming them
030: * into MenuItems. The list of labels given will be what is displayed in the ViewMenu for
031: * the corresponding action (the action at the same position in the action array).
032: * The actions are currently implemented as state based
033: * so that after an action is executed the label will have a selection check.
034: *
035: * @since 2.1
036: */
037: public class MultiActionGroup extends ActionGroup {
038:
039: public IAction[] NO_ACTIONS = new IAction[0];
040:
041: private IAction[] fActions;
042:
043: private int fCurrentSelection;
044: private MenuItem[] fItems;
045:
046: /**
047: * Creates a new action group with a given set of actions.
048: *
049: * @param actions the actions for this multi group
050: * @param currentSelection decides which action is selected in the menu on start up.
051: * Denotes the location in the actions array of the current
052: * selected state. It cannot be null.
053: */
054: public MultiActionGroup(IAction[] actions, int currentSelection) {
055: super ();
056: setActions(actions, currentSelection);
057: }
058:
059: /**
060: * Creates a new action group. Clients using this constructor must set the actions
061: * immediately after creating the multi action group by calling {@link #setActions(IAction[], int)}.
062: */
063: protected MultiActionGroup() {
064: super ();
065: }
066:
067: /**
068: * Sets the given actions.
069: *
070: * @param actions the actions for this multi group, at least one
071: * @param currentSelection decides which action is selected in the menu on start up.
072: * Denotes the location in the actions array of the current
073: * selected state. It cannot be null.
074: */
075: protected final void setActions(IAction[] actions,
076: int currentSelection) {
077: fCurrentSelection = currentSelection;
078: fActions = actions;
079: }
080:
081: /**
082: * Adds the actions to the given menu manager.
083: */
084: protected void addActions(IMenuManager viewMenu) {
085:
086: viewMenu.add(new Separator());
087: fItems = new MenuItem[fActions.length];
088:
089: for (int i = 0; i < fActions.length; i++) {
090: final int j = i;
091:
092: viewMenu.add(new ContributionItem() {
093:
094: public void fill(Menu menu, int index) {
095:
096: int style = SWT.CHECK;
097: if ((fActions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0)
098: style = SWT.RADIO;
099:
100: MenuItem mi = new MenuItem(menu, style, index);
101: ImageDescriptor d = fActions[j]
102: .getImageDescriptor();
103: mi.setImage(JavaPlugin.getImageDescriptorRegistry()
104: .get(d));
105: fItems[j] = mi;
106: mi.setText(fActions[j].getText());
107: mi.setSelection(fCurrentSelection == j);
108: mi.addSelectionListener(new SelectionAdapter() {
109:
110: public void widgetSelected(SelectionEvent e) {
111: if (fCurrentSelection == j) {
112: fItems[fCurrentSelection]
113: .setSelection(true);
114: return;
115: }
116: fActions[j].run();
117:
118: // Update checked state
119: fItems[fCurrentSelection]
120: .setSelection(false);
121: fCurrentSelection = j;
122: fItems[fCurrentSelection]
123: .setSelection(true);
124: }
125:
126: });
127: }
128:
129: public boolean isDynamic() {
130: return false;
131: }
132: });
133: }
134: }
135: }
|