KickJava   Java API By Example, From Geeks To Geeks.

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


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> - Images for menu items (27481)
11  *******************************************************************************/

12 package org.eclipse.ui.views.navigator;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.ICommand;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IncrementalProjectBuilder;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.actions.ActionFactory;
29 import org.eclipse.ui.actions.BuildAction;
30 import org.eclipse.ui.actions.CloseResourceAction;
31 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
32 import org.eclipse.ui.actions.OpenResourceAction;
33 import org.eclipse.ui.actions.RefreshAction;
34 import org.eclipse.ui.ide.IDEActionFactory;
35
36 /**
37  * This is the action group for workspace actions such as Build, Refresh Local,
38  * and Open/Close Project.
39  */

40 public class WorkspaceActionGroup extends ResourceNavigatorActionGroup {
41
42     private BuildAction buildAction;
43
44     private OpenResourceAction openProjectAction;
45
46     private CloseResourceAction closeProjectAction;
47     
48     private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction;
49
50     private RefreshAction refreshAction;
51
52     public WorkspaceActionGroup(IResourceNavigator navigator) {
53         super(navigator);
54     }
55
56     public void fillActionBars(IActionBars actionBars) {
57         actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(),
58                 refreshAction);
59         actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT
60                 .getId(), buildAction);
61         actionBars.setGlobalActionHandler(
62                 IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction);
63         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT
64                 .getId(), closeProjectAction);
65         actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS
66                 .getId(), closeUnrelatedProjectsAction);
67     }
68
69     /**
70      * Adds the build, open project, close project and refresh resource
71      * actions to the context menu.
72      * <p>
73      * The following conditions apply:
74      * build-only projects selected, auto build disabled, at least one
75      * builder present
76      * open project-only projects selected, at least one closed project
77      * close project-only projects selected, at least one open project
78      * refresh-no closed project selected
79      * </p>
80      * <p>
81      * Both the open project and close project action may be on the menu
82      * at the same time.
83      * </p>
84      * <p>
85      * No disabled action should be on the context menu.
86      * </p>
87      *
88      * @param menu context menu to add actions to
89      */

90     public void fillContextMenu(IMenuManager menu) {
91         IStructuredSelection selection = (IStructuredSelection) getContext()
92                 .getSelection();
93         boolean isProjectSelection = true;
94         boolean hasOpenProjects = false;
95         boolean hasClosedProjects = false;
96         boolean hasBuilder = true; // false if any project is closed or does not have builder
97
Iterator JavaDoc resources = selection.iterator();
98
99         while (resources.hasNext()
100                 && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) {
101             Object JavaDoc next = resources.next();
102             IProject project = null;
103
104             if (next instanceof IProject) {
105                 project = (IProject) next;
106             } else if (next instanceof IAdaptable) {
107                 project = (IProject) ((IAdaptable) next)
108                         .getAdapter(IProject.class);
109             }
110
111             if (project == null) {
112                 isProjectSelection = false;
113                 continue;
114             }
115             if (project.isOpen()) {
116                 hasOpenProjects = true;
117                 if (hasBuilder && !hasBuilder(project)) {
118                     hasBuilder = false;
119                 }
120             } else {
121                 hasClosedProjects = true;
122                 hasBuilder = false;
123             }
124         }
125         if (!selection.isEmpty() && isProjectSelection
126                 && !ResourcesPlugin.getWorkspace().isAutoBuilding()
127                 && hasBuilder) {
128             // Allow manual incremental build only if auto build is off.
129
buildAction.selectionChanged(selection);
130             menu.add(buildAction);
131         }
132         if (!hasClosedProjects) {
133             refreshAction.selectionChanged(selection);
134             menu.add(refreshAction);
135         }
136         if (isProjectSelection) {
137             if (hasClosedProjects) {
138                 openProjectAction.selectionChanged(selection);
139                 menu.add(openProjectAction);
140             }
141             if (hasOpenProjects) {
142                 closeProjectAction.selectionChanged(selection);
143                 menu.add(closeProjectAction);
144                 closeUnrelatedProjectsAction.selectionChanged(selection);
145                 menu.add(closeUnrelatedProjectsAction);
146             }
147         }
148     }
149
150     /**
151      * Handles a key pressed event by invoking the appropriate action.
152      */

153     public void handleKeyPressed(KeyEvent event) {
154         if (event.keyCode == SWT.F5 && event.stateMask == 0) {
155             if (refreshAction.isEnabled()) {
156                 refreshAction.refreshAll();
157             }
158
159             // Swallow the event
160
event.doit = false;
161         }
162     }
163
164     /**
165      * Returns whether there are builders configured on the given project.
166      *
167      * @return <code>true</code> if it has builders,
168      * <code>false</code> if not, or if this could not be determined
169      */

170     boolean hasBuilder(IProject project) {
171         try {
172             ICommand[] commands = project.getDescription().getBuildSpec();
173             if (commands.length > 0) {
174                 return true;
175             }
176         } catch (CoreException e) {
177             // Cannot determine if project has builders. Project is closed
178
// or does not exist. Fall through to return false.
179
}
180         return false;
181     }
182
183     protected void makeActions() {
184         Shell shell = navigator.getSite().getShell();
185         openProjectAction = new OpenResourceAction(shell);
186         closeProjectAction = new CloseResourceAction(shell);
187         closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(shell);
188         refreshAction = new RefreshAction(shell);
189         refreshAction
190                 .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
191
refreshAction
192                 .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
193
buildAction = new BuildAction(shell,
194                 IncrementalProjectBuilder.INCREMENTAL_BUILD);
195     }
196
197     public void updateActionBars() {
198         IStructuredSelection selection = (IStructuredSelection) getContext()
199                 .getSelection();
200         refreshAction.selectionChanged(selection);
201         buildAction.selectionChanged(selection);
202         openProjectAction.selectionChanged(selection);
203         closeUnrelatedProjectsAction.selectionChanged(selection);
204         closeProjectAction.selectionChanged(selection);
205     }
206 }
207
Popular Tags