KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > MultiActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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                 public void fill(Menu menu, int index) {
95                     
96                     int style= SWT.CHECK;
97                     if ((fActions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0)
98                         style= SWT.RADIO;
99                     
100                     MenuItem mi= new MenuItem(menu, style, index);
101                     ImageDescriptor d= fActions[j].getImageDescriptor();
102                     mi.setImage(JavaPlugin.getImageDescriptorRegistry().get(d));
103                     fItems[j]= mi;
104                     mi.setText(fActions[j].getText());
105                     mi.setSelection(fCurrentSelection == j);
106                     mi.addSelectionListener(new SelectionAdapter() {
107
108                         public void widgetSelected(SelectionEvent e) {
109                             if (fCurrentSelection == j) {
110                                 fItems[fCurrentSelection].setSelection(true);
111                                 return;
112                             }
113                             fActions[j].run();
114
115                             // Update checked state
116
fItems[fCurrentSelection].setSelection(false);
117                             fCurrentSelection= j;
118                             fItems[fCurrentSelection].setSelection(true);
119                         }
120
121                     });
122                 }
123                 public boolean isDynamic() {
124                     return false;
125                 }
126             });
127         }
128     }
129 }
130
Popular Tags