]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/actions/MultiActionGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / actions / MultiActionGroup.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
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;
18
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;
24
25 import org.eclipse.ui.actions.ActionGroup;
26
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28
29 /**
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.
35  *
36  * @since 2.1
37  */
38 public class MultiActionGroup extends ActionGroup {
39
40         public IAction[] NO_ACTIONS = new IAction[0];
41
42         private IAction[] fActions;
43
44         private int fCurrentSelection;
45         private MenuItem[] fItems;
46
47         /**
48          * Creates a new action group with a given set of actions.
49          *
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.
54          */
55         public MultiActionGroup(IAction[] actions, int currentSelection) {
56                 super();
57                 setActions(actions, currentSelection);
58         }
59
60         /**
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)}.
63          */
64         protected MultiActionGroup() {
65                 super();
66         }
67
68         /**
69          * Sets the given actions.
70          *
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.
75          */
76         protected final void setActions(IAction[] actions, int currentSelection) {
77                 fCurrentSelection= currentSelection;
78                 fActions = actions;
79         }
80
81         /**
82          * Adds the actions to the given menu manager.
83          */
84         protected void addActions(IMenuManager viewMenu) {
85
86                 viewMenu.add(new Separator());
87                 fItems= new MenuItem[fActions.length];
88
89                 for (int i= 0; i < fActions.length; i++) {
90                         final int j= i;
91
92                         viewMenu.add(new ContributionItem() {
93
94                                 @Override
95                                 public void fill(Menu menu, int index) {
96
97                                         int style= SWT.CHECK;
98                                         if ((fActions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0)
99                                                 style= SWT.RADIO;
100
101                                         MenuItem mi= new MenuItem(menu, style, index);
102                                         ImageDescriptor d= fActions[j].getImageDescriptor();
103                                         mi.setImage(JavaPlugin.getImageDescriptorRegistry().get(d));
104                                         fItems[j]= mi;
105                                         mi.setText(fActions[j].getText());
106                                         mi.setSelection(fCurrentSelection == j);
107                                         mi.addSelectionListener(new SelectionAdapter() {
108
109                                                 @Override
110                                                 public void widgetSelected(SelectionEvent e) {
111                                                         if (fCurrentSelection == j) {
112                                                                 fItems[fCurrentSelection].setSelection(true);
113                                                                 return;
114                                                         }
115                                                         fActions[j].run();
116
117                                                         // Update checked state
118                                                         fItems[fCurrentSelection].setSelection(false);
119                                                         fCurrentSelection= j;
120                                                         fItems[fCurrentSelection].setSelection(true);
121                                                 }
122
123                                         });
124                                 }
125                                 @Override
126                                 public boolean isDynamic() {
127                                         return false;
128                                 }
129                         });
130                 }
131         }
132 }