1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.widgets.Menu;
17 import org.eclipse.swt.widgets.MenuItem;
19 import org.eclipse.jface.action.ContributionItem;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.actions.ActionGroup;
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 * A MultiActionGroup will display a list of IActions in a menu by transforming them
31 * into MenuItems. The list of labels given will be what is displayed in the ViewMenu for
32 * the corresponding action (the action at the same position in the action array).
33 * The actions are currently implemented as state based
34 * so that after an action is executed the label will have a selection check.
38 public class MultiActionGroup extends ActionGroup {
40 public IAction[] NO_ACTIONS = new IAction[0];
42 private IAction[] fActions;
44 private int fCurrentSelection;
45 private MenuItem[] fItems;
48 * Creates a new action group with a given set of actions.
50 * @param actions the actions for this multi group
51 * @param currentSelection decides which action is selected in the menu on start up.
52 * Denotes the location in the actions array of the current
53 * selected state. It cannot be null.
55 public MultiActionGroup(IAction[] actions, int currentSelection) {
57 setActions(actions, currentSelection);
61 * Creates a new action group. Clients using this constructor must set the actions
62 * immediately after creating the multi action group by calling {@link #setActions(IAction[], int)}.
64 protected MultiActionGroup() {
69 * Sets the given actions.
71 * @param actions the actions for this multi group, at least one
72 * @param currentSelection decides which action is selected in the menu on start up.
73 * Denotes the location in the actions array of the current
74 * selected state. It cannot be null.
76 protected final void setActions(IAction[] actions, int currentSelection) {
77 fCurrentSelection= currentSelection;
82 * Adds the actions to the given menu manager.
84 protected void addActions(IMenuManager viewMenu) {
86 viewMenu.add(new Separator());
87 fItems= new MenuItem[fActions.length];
89 for (int i= 0; i < fActions.length; i++) {
92 viewMenu.add(new ContributionItem() {
95 public void fill(Menu menu, int index) {
98 if ((fActions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0)
101 MenuItem mi= new MenuItem(menu, style, index);
102 ImageDescriptor d= fActions[j].getImageDescriptor();
103 mi.setImage(JavaPlugin.getImageDescriptorRegistry().get(d));
105 mi.setText(fActions[j].getText());
106 mi.setSelection(fCurrentSelection == j);
107 mi.addSelectionListener(new SelectionAdapter() {
110 public void widgetSelected(SelectionEvent e) {
111 if (fCurrentSelection == j) {
112 fItems[fCurrentSelection].setSelection(true);
117 // Update checked state
118 fItems[fCurrentSelection].setSelection(false);
119 fCurrentSelection= j;
120 fItems[fCurrentSelection].setSelection(true);
126 public boolean isDynamic() {