KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > WorkingSetRootModeActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.ui.internal.navigator.resources.actions;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.ContributionItem;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.jface.action.Separator;
20 import org.eclipse.jface.viewers.StructuredViewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.swt.widgets.MenuItem;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.ISharedImages;
28 import org.eclipse.ui.IWorkbenchActionConstants;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.ActionGroup;
31 import org.eclipse.ui.ide.IDE;
32 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages;
33 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorPlugin;
34 import org.eclipse.ui.internal.navigator.workingsets.WorkingSetsContentProvider;
35 import org.eclipse.ui.navigator.IExtensionStateModel;
36
37 /**
38  * @since 3.2
39  *
40  */

41 public class WorkingSetRootModeActionGroup extends ActionGroup {
42
43     private IExtensionStateModel stateModel;
44     private StructuredViewer structuredViewer;
45
46     private boolean hasContributedToViewMenu = false;
47     private IAction workingSetsAction = null;
48     private IAction projectsAction = null;
49     private IAction[] actions;
50     private int currentSelection;
51     private MenuItem[] items;
52
53     private class TopLevelContentAction extends Action implements IAction {
54
55         private final boolean groupWorkingSets;
56
57         /**
58          * Construct an Action that represents a toggle-able state between
59          * Showing top level Working Sets and Projects.
60          *
61          * @param toGroupWorkingSets
62          */

63         public TopLevelContentAction(boolean toGroupWorkingSets) {
64             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
65
groupWorkingSets = toGroupWorkingSets;
66         }
67
68         /*
69          * @see org.eclipse.jface.action.IAction#run()
70          */

71         public void run() {
72             if (stateModel
73                     .getBooleanProperty(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS) != groupWorkingSets) {
74                 stateModel.setBooleanProperty(
75                         WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS,
76                         groupWorkingSets);
77
78                 structuredViewer.getControl().setRedraw(false);
79                 try {
80                     structuredViewer.refresh();
81                 } finally {
82                     structuredViewer.getControl().setRedraw(true);
83                 }
84             }
85         }
86     }
87
88     /**
89      * Create an action group that will listen to the stateModel and update the
90      * structuredViewer when necessary.
91      *
92      * @param structuredViewer
93      * @param stateModel
94      */

95     public WorkingSetRootModeActionGroup(StructuredViewer aStructuredViewer,
96             IExtensionStateModel aStateModel) {
97         super();
98         structuredViewer = aStructuredViewer;
99         stateModel = aStateModel;
100     }
101
102     /*
103      * (non-Javadoc)
104      *
105      * @see ActionGroup#fillActionBars(IActionBars)
106      */

107     public void fillActionBars(IActionBars actionBars) {
108         synchronized (this) {
109             if (!hasContributedToViewMenu) {
110                 contributeToViewMenu(actionBars.getMenuManager());
111                 hasContributedToViewMenu = true;
112             }
113         }
114     }
115
116     private void contributeToViewMenu(IMenuManager viewMenu) {
117
118         IMenuManager topLevelSubMenu = new MenuManager(
119                 WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Top_Level_Element_);
120         addActions(topLevelSubMenu);
121         viewMenu.insertBefore(IWorkbenchActionConstants.MB_ADDITIONS,
122                 topLevelSubMenu);
123     }
124
125     /**
126      * Adds the actions to the given menu manager.
127      */

128     protected void addActions(IMenuManager viewMenu) {
129
130         viewMenu.add(new Separator());
131         items = new MenuItem[actions.length];
132
133         for (int i = 0; i < actions.length; i++) {
134             final int j = i;
135
136             viewMenu.add(new ContributionItem() {
137
138                 public void fill(Menu menu, int index) {
139
140                     int style = SWT.CHECK;
141                     if ((actions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0)
142                         style = SWT.RADIO;
143
144                     final MenuItem mi = new MenuItem(menu, style, index);
145                     items[j] = mi;
146                     mi.setText(actions[j].getText());
147                     mi.setSelection(currentSelection == j);
148                     mi.addSelectionListener(new SelectionAdapter() {
149
150                         public void widgetSelected(SelectionEvent e) {
151                             if (currentSelection == j) {
152                                 items[currentSelection].setSelection(true);
153                                 return;
154                             }
155                             actions[j].run();
156
157                             // Update checked state
158
items[currentSelection].setSelection(false);
159                             currentSelection = j;
160                             items[currentSelection].setSelection(true);
161                         }
162
163                     });
164
165                 }
166
167                 public boolean isDynamic() {
168                     return false;
169                 }
170             });
171         }
172     }
173
174     private IAction[] createActions() {
175
176         ISharedImages sharedImages = PlatformUI.getWorkbench()
177                 .getSharedImages();
178
179         projectsAction = new TopLevelContentAction(false);
180         projectsAction
181                 .setText(WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Project_);
182         projectsAction.setImageDescriptor(sharedImages
183                 .getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT));
184
185         workingSetsAction = new TopLevelContentAction(true);
186         workingSetsAction
187                 .setText(WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Working_Set_);
188         workingSetsAction.setImageDescriptor(WorkbenchNavigatorPlugin
189                 .getDefault().getImageRegistry().getDescriptor(
190                         "full/obj16/workingsets.gif")); //$NON-NLS-1$
191

192         return new IAction[] { projectsAction, workingSetsAction };
193     }
194
195     /**
196      * Toggle whether top level working sets should be displayed as a group or
197      * collapse to just show their contents.
198      *
199      * @param showTopLevelWorkingSets
200      */

201     public void setShowTopLevelWorkingSets(boolean showTopLevelWorkingSets) {
202         if (actions == null) {
203             actions = createActions();
204             setActions(actions, showTopLevelWorkingSets ? 1 /*
205                                                              * Show Top Level
206                                                              * Working Sets
207                                                              */

208                     : 0);
209         }
210         workingSetsAction.setChecked(showTopLevelWorkingSets);
211         projectsAction.setChecked(!showTopLevelWorkingSets);
212
213         if (items != null) {
214             for (int i = 0; i < items.length; i++) {
215                 if(items[i] != null && actions[i] != null)
216                     items[i].setSelection(actions[i].isChecked());
217             }
218         }
219         if (stateModel != null) {
220             stateModel.setBooleanProperty(
221                     WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS,
222                     showTopLevelWorkingSets);
223         }
224
225     }
226
227     /**
228      * Configure the actions that are displayed in the menu by this ActionGroup.
229      *
230      * @param theActions
231      * An array of possible actions.
232      * @param selected
233      * The index of the "enabled" action.
234      */

235     private void setActions(IAction[] theActions, int selected) {
236         actions = theActions;
237         currentSelection = selected;
238
239     }
240
241     public void setStateModel(IExtensionStateModel sStateModel) {
242         stateModel = sStateModel;
243     }
244 }
245
Popular Tags