KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > MainActionGroup


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  * Sebastian Davids <sdavids@gmx.de> - Collapse all action (25826)
11  * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
12  *******************************************************************************/

13 package org.eclipse.ui.views.navigator;
14
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IResourceChangeEvent;
20 import org.eclipse.core.resources.IResourceChangeListener;
21 import org.eclipse.core.resources.IResourceDelta;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.TreeViewer;
31 import org.eclipse.swt.events.KeyEvent;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IActionBars;
34 import org.eclipse.ui.IWorkbenchActionConstants;
35 import org.eclipse.ui.IWorkingSet;
36 import org.eclipse.ui.actions.ActionContext;
37 import org.eclipse.ui.actions.ActionFactory;
38 import org.eclipse.ui.actions.AddBookmarkAction;
39 import org.eclipse.ui.actions.AddTaskAction;
40 import org.eclipse.ui.actions.ExportResourcesAction;
41 import org.eclipse.ui.actions.ImportResourcesAction;
42 import org.eclipse.ui.actions.NewWizardMenu;
43 import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
44 import org.eclipse.ui.dialogs.PropertyDialogAction;
45 import org.eclipse.ui.ide.IDEActionFactory;
46 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
47 import org.eclipse.ui.operations.UndoRedoActionGroup;
48
49 /**
50  * The main action group for the navigator.
51  * This contains a few actions and several subgroups.
52  */

53 public class MainActionGroup extends ResourceNavigatorActionGroup {
54
55     protected AddBookmarkAction addBookmarkAction;
56
57     protected AddTaskAction addTaskAction;
58
59     protected PropertyDialogAction propertyDialogAction;
60
61     protected ImportResourcesAction importAction;
62     
63     protected ExportResourcesAction exportAction;
64
65     protected CollapseAllAction collapseAllAction;
66
67     protected ToggleLinkingAction toggleLinkingAction;
68
69     protected GotoActionGroup gotoGroup;
70
71     protected OpenActionGroup openGroup;
72
73     protected RefactorActionGroup refactorGroup;
74
75     protected WorkingSetFilterActionGroup workingSetGroup;
76
77     protected SortAndFilterActionGroup sortAndFilterGroup;
78     
79     protected UndoRedoActionGroup undoRedoGroup;
80
81     protected WorkspaceActionGroup workspaceGroup;
82
83     private IResourceChangeListener resourceChangeListener;
84
85     private NewWizardMenu newWizardMenu;
86
87     /**
88      * Constructs the main action group.
89      *
90      * @param navigator the navigator view
91      */

92     public MainActionGroup(IResourceNavigator navigator) {
93         super(navigator);
94         resourceChangeListener = new IResourceChangeListener() {
95             public void resourceChanged(IResourceChangeEvent event) {
96                 handleResourceChanged(event);
97             }
98         };
99         ResourcesPlugin.getWorkspace().addResourceChangeListener(
100                 resourceChangeListener, IResourceChangeEvent.POST_CHANGE);
101         makeSubGroups();
102     }
103
104     /**
105      * Handles a resource changed event by updating the enablement
106      * if one of the selected projects is opened or closed.
107      */

108     protected void handleResourceChanged(IResourceChangeEvent event) {
109         ActionContext context = getContext();
110         if (context == null) {
111             return;
112         }
113
114         final IStructuredSelection selection = (IStructuredSelection) context
115                 .getSelection();
116         if (ResourceSelectionUtil.allResourcesAreOfType(selection,
117                 IResource.PROJECT) == false) {
118             return;
119         }
120         List JavaDoc sel = selection.toList();
121         IResourceDelta delta = event.getDelta();
122         if (delta == null) {
123             return;
124         }
125         IResourceDelta[] projDeltas = delta
126                 .getAffectedChildren(IResourceDelta.CHANGED);
127         for (int i = 0; i < projDeltas.length; ++i) {
128             IResourceDelta projDelta = projDeltas[i];
129             //changing the project open state or description will effect open/close/build action enablement
130
if ((projDelta.getFlags() & (IResourceDelta.OPEN | IResourceDelta.DESCRIPTION)) != 0) {
131                 if (sel.contains(projDelta.getResource())) {
132                     getNavigator().getSite().getShell().getDisplay().syncExec(
133                             new Runnable JavaDoc() {
134                                 public void run() {
135                                     addTaskAction.selectionChanged(selection);
136                                     gotoGroup.updateActionBars();
137                                     refactorGroup.updateActionBars();
138                                     workspaceGroup.updateActionBars();
139                                 }
140                             });
141                 }
142             }
143         }
144     }
145
146     /**
147      * Makes the actions contained directly in this action group.
148      */

149     protected void makeActions() {
150         Shell shell = navigator.getSite().getShell();
151
152         newWizardMenu = new NewWizardMenu(navigator.getSite().getWorkbenchWindow());
153         addBookmarkAction = new AddBookmarkAction(shell);
154         addTaskAction = new AddTaskAction(shell);
155         propertyDialogAction = new PropertyDialogAction(shell, navigator
156                 .getViewer());
157
158         importAction = new ImportResourcesAction(navigator.getSite()
159                 .getWorkbenchWindow());
160         importAction
161                 .setDisabledImageDescriptor(getImageDescriptor("dtool16/import_wiz.gif")); //$NON-NLS-1$
162
importAction
163                 .setImageDescriptor(getImageDescriptor("etool16/import_wiz.gif")); //$NON-NLS-1$
164

165         exportAction = new ExportResourcesAction(navigator.getSite()
166                 .getWorkbenchWindow());
167         exportAction
168                 .setDisabledImageDescriptor(getImageDescriptor("dtool16/export_wiz.gif")); //$NON-NLS-1$
169
exportAction
170                 .setImageDescriptor(getImageDescriptor("etool16/export_wiz.gif")); //$NON-NLS-1$
171

172         
173         collapseAllAction = new CollapseAllAction(navigator,
174                 ResourceNavigatorMessages.CollapseAllAction_title);
175         collapseAllAction.setToolTipText(ResourceNavigatorMessages.CollapseAllAction_toolTip);
176         collapseAllAction
177                 .setImageDescriptor(getImageDescriptor("elcl16/collapseall.gif")); //$NON-NLS-1$
178

179         toggleLinkingAction = new ToggleLinkingAction(navigator,
180                 ResourceNavigatorMessages.ToggleLinkingAction_text);
181         toggleLinkingAction.setToolTipText(ResourceNavigatorMessages.ToggleLinkingAction_toolTip);
182         toggleLinkingAction
183                 .setImageDescriptor(getImageDescriptor("elcl16/synced.gif"));//$NON-NLS-1$
184
}
185
186     /**
187      * Makes the sub action groups.
188      */

189     protected void makeSubGroups() {
190         gotoGroup = new GotoActionGroup(navigator);
191         openGroup = new OpenActionGroup(navigator);
192         refactorGroup = new RefactorActionGroup(navigator);
193         IPropertyChangeListener workingSetUpdater = new IPropertyChangeListener() {
194             public void propertyChange(PropertyChangeEvent event) {
195                 String JavaDoc property = event.getProperty();
196
197                 if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET
198                         .equals(property)) {
199                     IResourceNavigator navigator = getNavigator();
200                     Object JavaDoc newValue = event.getNewValue();
201
202                     if (newValue instanceof IWorkingSet) {
203                         navigator.setWorkingSet((IWorkingSet) newValue);
204                     } else if (newValue == null) {
205                         navigator.setWorkingSet(null);
206                     }
207                 }
208             }
209         };
210         TreeViewer treeView = navigator.getViewer();
211         Shell shell = treeView.getControl().getShell();
212         workingSetGroup = new WorkingSetFilterActionGroup(shell,
213                 workingSetUpdater);
214         workingSetGroup.setWorkingSet(navigator.getWorkingSet());
215         sortAndFilterGroup = new SortAndFilterActionGroup(navigator);
216         workspaceGroup = new WorkspaceActionGroup(navigator);
217         IUndoContext workspaceContext= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
218         undoRedoGroup= new UndoRedoActionGroup(getNavigator().getSite(), workspaceContext, true);
219
220     }
221     
222     /**
223      * Extends the superclass implementation to set the context in the subgroups.
224      */

225     public void setContext(ActionContext context) {
226         super.setContext(context);
227         gotoGroup.setContext(context);
228         openGroup.setContext(context);
229         refactorGroup.setContext(context);
230         sortAndFilterGroup.setContext(context);
231         workspaceGroup.setContext(context);
232         undoRedoGroup.setContext(context);
233     }
234
235     /**
236      * Fills the context menu with the actions contained in this group
237      * and its subgroups.
238      *
239      * @param menu the context menu
240      */

241     public void fillContextMenu(IMenuManager menu) {
242         IStructuredSelection selection = (IStructuredSelection) getContext()
243                 .getSelection();
244
245         MenuManager newMenu = new MenuManager(ResourceNavigatorMessages.ResourceNavigator_new);
246         menu.add(newMenu);
247         newMenu.add(newWizardMenu);
248
249         gotoGroup.fillContextMenu(menu);
250         openGroup.fillContextMenu(menu);
251         menu.add(new Separator());
252
253         refactorGroup.fillContextMenu(menu);
254         menu.add(new Separator());
255
256         menu.add(importAction);
257         menu.add(exportAction);
258         importAction.selectionChanged(selection);
259         exportAction.selectionChanged(selection);
260         menu.add(new Separator());
261
262         workspaceGroup.fillContextMenu(menu);
263
264         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
265         menu
266                 .add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS
267                         + "-end")); //$NON-NLS-1$
268
menu.add(new Separator());
269
270         if (selection.size() == 1) {
271             propertyDialogAction.selectionChanged(selection);
272             menu.add(propertyDialogAction);
273         }
274     }
275
276     /**
277      * Adds the actions in this group and its subgroups to the action bars.
278      */

279     public void fillActionBars(IActionBars actionBars) {
280         actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(),
281                 propertyDialogAction);
282         actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(),
283                 addBookmarkAction);
284         actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(),
285                 addTaskAction);
286
287         gotoGroup.fillActionBars(actionBars);
288         openGroup.fillActionBars(actionBars);
289         refactorGroup.fillActionBars(actionBars);
290         workingSetGroup.fillActionBars(actionBars);
291         sortAndFilterGroup.fillActionBars(actionBars);
292         workspaceGroup.fillActionBars(actionBars);
293         undoRedoGroup.fillActionBars(actionBars);
294
295         IMenuManager menu = actionBars.getMenuManager();
296         menu.add(toggleLinkingAction);
297
298         IToolBarManager toolBar = actionBars.getToolBarManager();
299         toolBar.add(new Separator());
300         toolBar.add(collapseAllAction);
301         toolBar.add(toggleLinkingAction);
302     }
303
304     /**
305      * Updates the actions which were added to the action bars,
306      * delegating to the subgroups as necessary.
307      */

308     public void updateActionBars() {
309         IStructuredSelection selection = (IStructuredSelection) getContext()
310                 .getSelection();
311         propertyDialogAction.setEnabled(selection.size() == 1);
312         addBookmarkAction.selectionChanged(selection);
313         addTaskAction.selectionChanged(selection);
314
315         gotoGroup.updateActionBars();
316         openGroup.updateActionBars();
317         refactorGroup.updateActionBars();
318         workingSetGroup.updateActionBars();
319         sortAndFilterGroup.updateActionBars();
320         workspaceGroup.updateActionBars();
321         undoRedoGroup.updateActionBars();
322     }
323
324     /**
325      * Runs the default action (open file) by delegating the open group.
326      */

327     public void runDefaultAction(IStructuredSelection selection) {
328         openGroup.runDefaultAction(selection);
329     }
330
331     /**
332      * Handles a key pressed event by invoking the appropriate action,
333      * delegating to the subgroups as necessary.
334      */

335     public void handleKeyPressed(KeyEvent event) {
336         refactorGroup.handleKeyPressed(event);
337         workspaceGroup.handleKeyPressed(event);
338     }
339
340     /**
341      * Extends the superclass implementation to dispose the
342      * actions in this group and its subgroups.
343      */

344     public void dispose() {
345         ResourcesPlugin.getWorkspace().removeResourceChangeListener(
346                 resourceChangeListener);
347
348         newWizardMenu.dispose();
349         collapseAllAction.dispose();
350         importAction.dispose();
351         exportAction.dispose();
352         propertyDialogAction.dispose();
353         toggleLinkingAction.dispose();
354
355         gotoGroup.dispose();
356         openGroup.dispose();
357         refactorGroup.dispose();
358         sortAndFilterGroup.dispose();
359         workingSetGroup.dispose();
360         workspaceGroup.dispose();
361         undoRedoGroup.dispose();
362         super.dispose();
363     }
364 }
365
Popular Tags