001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Collapse all action (25826)
011: * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
012: *******************************************************************************/package org.eclipse.ui.views.navigator;
013:
014: import java.util.List;
015:
016: import org.eclipse.core.commands.operations.IUndoContext;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IResourceChangeEvent;
019: import org.eclipse.core.resources.IResourceChangeListener;
020: import org.eclipse.core.resources.IResourceDelta;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.jface.action.IMenuManager;
023: import org.eclipse.jface.action.IToolBarManager;
024: import org.eclipse.jface.action.MenuManager;
025: import org.eclipse.jface.action.Separator;
026: import org.eclipse.jface.util.IPropertyChangeListener;
027: import org.eclipse.jface.util.PropertyChangeEvent;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.TreeViewer;
030: import org.eclipse.swt.events.KeyEvent;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.ui.IActionBars;
033: import org.eclipse.ui.IWorkbenchActionConstants;
034: import org.eclipse.ui.IWorkingSet;
035: import org.eclipse.ui.actions.ActionContext;
036: import org.eclipse.ui.actions.ActionFactory;
037: import org.eclipse.ui.actions.AddBookmarkAction;
038: import org.eclipse.ui.actions.AddTaskAction;
039: import org.eclipse.ui.actions.ExportResourcesAction;
040: import org.eclipse.ui.actions.ImportResourcesAction;
041: import org.eclipse.ui.actions.NewWizardMenu;
042: import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
043: import org.eclipse.ui.dialogs.PropertyDialogAction;
044: import org.eclipse.ui.ide.IDEActionFactory;
045: import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
046: import org.eclipse.ui.operations.UndoRedoActionGroup;
047:
048: /**
049: * The main action group for the navigator.
050: * This contains a few actions and several subgroups.
051: */
052: public class MainActionGroup extends ResourceNavigatorActionGroup {
053:
054: protected AddBookmarkAction addBookmarkAction;
055:
056: protected AddTaskAction addTaskAction;
057:
058: protected PropertyDialogAction propertyDialogAction;
059:
060: protected ImportResourcesAction importAction;
061:
062: protected ExportResourcesAction exportAction;
063:
064: protected CollapseAllAction collapseAllAction;
065:
066: protected ToggleLinkingAction toggleLinkingAction;
067:
068: protected GotoActionGroup gotoGroup;
069:
070: protected OpenActionGroup openGroup;
071:
072: protected RefactorActionGroup refactorGroup;
073:
074: protected WorkingSetFilterActionGroup workingSetGroup;
075:
076: protected SortAndFilterActionGroup sortAndFilterGroup;
077:
078: protected UndoRedoActionGroup undoRedoGroup;
079:
080: protected WorkspaceActionGroup workspaceGroup;
081:
082: private IResourceChangeListener resourceChangeListener;
083:
084: private NewWizardMenu newWizardMenu;
085:
086: /**
087: * Constructs the main action group.
088: *
089: * @param navigator the navigator view
090: */
091: public MainActionGroup(IResourceNavigator navigator) {
092: super (navigator);
093: resourceChangeListener = new IResourceChangeListener() {
094: public void resourceChanged(IResourceChangeEvent event) {
095: handleResourceChanged(event);
096: }
097: };
098: ResourcesPlugin.getWorkspace().addResourceChangeListener(
099: resourceChangeListener,
100: 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 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()
133: .syncExec(new Runnable() {
134: public void run() {
135: addTaskAction
136: .selectionChanged(selection);
137: gotoGroup.updateActionBars();
138: refactorGroup.updateActionBars();
139: workspaceGroup.updateActionBars();
140: }
141: });
142: }
143: }
144: }
145: }
146:
147: /**
148: * Makes the actions contained directly in this action group.
149: */
150: protected void makeActions() {
151: Shell shell = navigator.getSite().getShell();
152:
153: newWizardMenu = new NewWizardMenu(navigator.getSite()
154: .getWorkbenchWindow());
155: addBookmarkAction = new AddBookmarkAction(shell);
156: addTaskAction = new AddTaskAction(shell);
157: propertyDialogAction = new PropertyDialogAction(shell,
158: navigator.getViewer());
159:
160: importAction = new ImportResourcesAction(navigator.getSite()
161: .getWorkbenchWindow());
162: importAction
163: .setDisabledImageDescriptor(getImageDescriptor("dtool16/import_wiz.gif")); //$NON-NLS-1$
164: importAction
165: .setImageDescriptor(getImageDescriptor("etool16/import_wiz.gif")); //$NON-NLS-1$
166:
167: exportAction = new ExportResourcesAction(navigator.getSite()
168: .getWorkbenchWindow());
169: exportAction
170: .setDisabledImageDescriptor(getImageDescriptor("dtool16/export_wiz.gif")); //$NON-NLS-1$
171: exportAction
172: .setImageDescriptor(getImageDescriptor("etool16/export_wiz.gif")); //$NON-NLS-1$
173:
174: collapseAllAction = new CollapseAllAction(navigator,
175: ResourceNavigatorMessages.CollapseAllAction_title);
176: collapseAllAction
177: .setToolTipText(ResourceNavigatorMessages.CollapseAllAction_toolTip);
178: collapseAllAction
179: .setImageDescriptor(getImageDescriptor("elcl16/collapseall.gif")); //$NON-NLS-1$
180:
181: toggleLinkingAction = new ToggleLinkingAction(navigator,
182: ResourceNavigatorMessages.ToggleLinkingAction_text);
183: toggleLinkingAction
184: .setToolTipText(ResourceNavigatorMessages.ToggleLinkingAction_toolTip);
185: toggleLinkingAction
186: .setImageDescriptor(getImageDescriptor("elcl16/synced.gif"));//$NON-NLS-1$
187: }
188:
189: /**
190: * Makes the sub action groups.
191: */
192: protected void makeSubGroups() {
193: gotoGroup = new GotoActionGroup(navigator);
194: openGroup = new OpenActionGroup(navigator);
195: refactorGroup = new RefactorActionGroup(navigator);
196: IPropertyChangeListener workingSetUpdater = new IPropertyChangeListener() {
197: public void propertyChange(PropertyChangeEvent event) {
198: String property = event.getProperty();
199:
200: if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET
201: .equals(property)) {
202: IResourceNavigator navigator = getNavigator();
203: Object newValue = event.getNewValue();
204:
205: if (newValue instanceof IWorkingSet) {
206: navigator.setWorkingSet((IWorkingSet) newValue);
207: } else if (newValue == null) {
208: navigator.setWorkingSet(null);
209: }
210: }
211: }
212: };
213: TreeViewer treeView = navigator.getViewer();
214: Shell shell = treeView.getControl().getShell();
215: workingSetGroup = new WorkingSetFilterActionGroup(shell,
216: workingSetUpdater);
217: workingSetGroup.setWorkingSet(navigator.getWorkingSet());
218: sortAndFilterGroup = new SortAndFilterActionGroup(navigator);
219: workspaceGroup = new WorkspaceActionGroup(navigator);
220: IUndoContext workspaceContext = (IUndoContext) ResourcesPlugin
221: .getWorkspace().getAdapter(IUndoContext.class);
222: undoRedoGroup = new UndoRedoActionGroup(getNavigator()
223: .getSite(), workspaceContext, true);
224:
225: }
226:
227: /**
228: * Extends the superclass implementation to set the context in the subgroups.
229: */
230: public void setContext(ActionContext context) {
231: super .setContext(context);
232: gotoGroup.setContext(context);
233: openGroup.setContext(context);
234: refactorGroup.setContext(context);
235: sortAndFilterGroup.setContext(context);
236: workspaceGroup.setContext(context);
237: undoRedoGroup.setContext(context);
238: }
239:
240: /**
241: * Fills the context menu with the actions contained in this group
242: * and its subgroups.
243: *
244: * @param menu the context menu
245: */
246: public void fillContextMenu(IMenuManager menu) {
247: IStructuredSelection selection = (IStructuredSelection) getContext()
248: .getSelection();
249:
250: MenuManager newMenu = new MenuManager(
251: ResourceNavigatorMessages.ResourceNavigator_new);
252: menu.add(newMenu);
253: newMenu.add(newWizardMenu);
254:
255: gotoGroup.fillContextMenu(menu);
256: openGroup.fillContextMenu(menu);
257: menu.add(new Separator());
258:
259: refactorGroup.fillContextMenu(menu);
260: menu.add(new Separator());
261:
262: menu.add(importAction);
263: menu.add(exportAction);
264: importAction.selectionChanged(selection);
265: exportAction.selectionChanged(selection);
266: menu.add(new Separator());
267:
268: workspaceGroup.fillContextMenu(menu);
269:
270: menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
271: menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS
272: + "-end")); //$NON-NLS-1$
273: menu.add(new Separator());
274:
275: if (selection.size() == 1) {
276: propertyDialogAction.selectionChanged(selection);
277: menu.add(propertyDialogAction);
278: }
279: }
280:
281: /**
282: * Adds the actions in this group and its subgroups to the action bars.
283: */
284: public void fillActionBars(IActionBars actionBars) {
285: actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES
286: .getId(), propertyDialogAction);
287: actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK
288: .getId(), addBookmarkAction);
289: actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK
290: .getId(), addTaskAction);
291:
292: gotoGroup.fillActionBars(actionBars);
293: openGroup.fillActionBars(actionBars);
294: refactorGroup.fillActionBars(actionBars);
295: workingSetGroup.fillActionBars(actionBars);
296: sortAndFilterGroup.fillActionBars(actionBars);
297: workspaceGroup.fillActionBars(actionBars);
298: undoRedoGroup.fillActionBars(actionBars);
299:
300: IMenuManager menu = actionBars.getMenuManager();
301: menu.add(toggleLinkingAction);
302:
303: IToolBarManager toolBar = actionBars.getToolBarManager();
304: toolBar.add(new Separator());
305: toolBar.add(collapseAllAction);
306: toolBar.add(toggleLinkingAction);
307: }
308:
309: /**
310: * Updates the actions which were added to the action bars,
311: * delegating to the subgroups as necessary.
312: */
313: public void updateActionBars() {
314: IStructuredSelection selection = (IStructuredSelection) getContext()
315: .getSelection();
316: propertyDialogAction.setEnabled(selection.size() == 1);
317: addBookmarkAction.selectionChanged(selection);
318: addTaskAction.selectionChanged(selection);
319:
320: gotoGroup.updateActionBars();
321: openGroup.updateActionBars();
322: refactorGroup.updateActionBars();
323: workingSetGroup.updateActionBars();
324: sortAndFilterGroup.updateActionBars();
325: workspaceGroup.updateActionBars();
326: undoRedoGroup.updateActionBars();
327: }
328:
329: /**
330: * Runs the default action (open file) by delegating the open group.
331: */
332: public void runDefaultAction(IStructuredSelection selection) {
333: openGroup.runDefaultAction(selection);
334: }
335:
336: /**
337: * Handles a key pressed event by invoking the appropriate action,
338: * delegating to the subgroups as necessary.
339: */
340: public void handleKeyPressed(KeyEvent event) {
341: refactorGroup.handleKeyPressed(event);
342: workspaceGroup.handleKeyPressed(event);
343: }
344:
345: /**
346: * Extends the superclass implementation to dispose the
347: * actions in this group and its subgroups.
348: */
349: public void dispose() {
350: ResourcesPlugin.getWorkspace().removeResourceChangeListener(
351: resourceChangeListener);
352:
353: newWizardMenu.dispose();
354: collapseAllAction.dispose();
355: importAction.dispose();
356: exportAction.dispose();
357: propertyDialogAction.dispose();
358: toggleLinkingAction.dispose();
359:
360: gotoGroup.dispose();
361: openGroup.dispose();
362: refactorGroup.dispose();
363: sortAndFilterGroup.dispose();
364: workingSetGroup.dispose();
365: workspaceGroup.dispose();
366: undoRedoGroup.dispose();
367: super.dispose();
368: }
369: }
|