KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > GlobalBuildAction


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.ui.actions;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IncrementalProjectBuilder;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.MultiStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
32 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
33 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
34 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
35 import org.eclipse.ui.internal.ide.actions.BuildUtilities;
36
37 /**
38  * Standard action for full and incremental builds of all projects within the
39  * workspace.
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  */

44 public class GlobalBuildAction extends Action implements
45         ActionFactory.IWorkbenchAction {
46     /**
47      * The type of build performed by this action. Can be either
48      * <code>IncrementalProjectBuilder.INCREMENTAL_BUILD</code> or
49      * <code>IncrementalProjectBuilder.FULL_BUILD</code>.
50      */

51     private int buildType;
52
53     /**
54      * The workbench window; or <code>null</code> if this action has been
55      * <code>dispose</code>d.
56      */

57     private IWorkbenchWindow workbenchWindow;
58
59     /**
60      * Creates a new action of the appropriate type. The action id is
61      * <code>IWorkbenchActionConstants.BUILD</code> for incremental builds and
62      * <code>IWorkbenchActionConstants.REBUILD_ALL</code> for full builds.
63      *
64      * @param workbench
65      * the active workbench
66      * @param shell
67      * the shell for any dialogs
68      * @param type
69      * the type of build; one of
70      * <code>IncrementalProjectBuilder.INCREMENTAL_BUILD</code> or
71      * <code>IncrementalProjectBuilder.FULL_BUILD</code>
72      *
73      * @deprecated use GlobalBuildAction(IWorkbenchWindow, type) instead
74      */

75     public GlobalBuildAction(IWorkbench workbench, Shell shell, int type) {
76         // always use active window; ignore shell
77
this(workbench.getActiveWorkbenchWindow(), type);
78         if (shell == null) {
79             throw new IllegalArgumentException JavaDoc();
80         }
81     }
82
83     /**
84      * Creates a new action of the appropriate type. The action id is
85      * <code>IWorkbenchActionConstants.BUILD</code> for incremental builds and
86      * <code>IWorkbenchActionConstants.REBUILD_ALL</code> for full builds.
87      *
88      * @param window
89      * the window in which this action appears
90      * @param type
91      * the type of build; one of
92      * <code>IncrementalProjectBuilder.INCREMENTAL_BUILD</code> or
93      * <code>IncrementalProjectBuilder.FULL_BUILD</code>
94      */

95     public GlobalBuildAction(IWorkbenchWindow window, int type) {
96         if (window == null) {
97             throw new IllegalArgumentException JavaDoc();
98         }
99         this.workbenchWindow = window;
100         setBuildType(type);
101     }
102
103     /**
104      * Sets the build type.
105      *
106      * @param type
107      * the type of build; one of
108      * <code>IncrementalProjectBuilder.INCREMENTAL_BUILD</code> or
109      * <code>IncrementalProjectBuilder.FULL_BUILD</code>
110      */

111     private void setBuildType(int type) {
112         // allow AUTO_BUILD as well for backwards compatibility, but treat it
113
// the same as INCREMENTAL_BUILD
114
switch (type) {
115         case IncrementalProjectBuilder.INCREMENTAL_BUILD:
116         case IncrementalProjectBuilder.AUTO_BUILD:
117             setText(IDEWorkbenchMessages.GlobalBuildAction_text);
118             setToolTipText(IDEWorkbenchMessages.GlobalBuildAction_toolTip);
119             setId("build"); //$NON-NLS-1$
120
workbenchWindow.getWorkbench().getHelpSystem().setHelp(this,
121                     IIDEHelpContextIds.GLOBAL_INCREMENTAL_BUILD_ACTION);
122             setImageDescriptor(IDEInternalWorkbenchImages
123                     .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC));
124             setDisabledImageDescriptor(IDEInternalWorkbenchImages
125                     .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_DISABLED));
126             setActionDefinitionId("org.eclipse.ui.project.buildAll"); //$NON-NLS-1$
127
break;
128         case IncrementalProjectBuilder.FULL_BUILD:
129             setText(IDEWorkbenchMessages.GlobalBuildAction_rebuildText);
130             setToolTipText(IDEWorkbenchMessages.GlobalBuildAction_rebuildToolTip);
131             setId("rebuildAll"); //$NON-NLS-1$
132
workbenchWindow.getWorkbench().getHelpSystem().setHelp(this,
133                     IIDEHelpContextIds.GLOBAL_FULL_BUILD_ACTION);
134             setActionDefinitionId("org.eclipse.ui.project.rebuildAll"); //$NON-NLS-1$
135
break;
136         default:
137             throw new IllegalArgumentException JavaDoc("Invalid build type"); //$NON-NLS-1$
138
}
139         this.buildType = type;
140     }
141
142     /**
143      * Returns the shell to use.
144      */

145     private Shell getShell() {
146         return workbenchWindow.getShell();
147     }
148
149     /**
150      * Returns the operation name to use
151      */

152     private String JavaDoc getOperationMessage() {
153         if (buildType == IncrementalProjectBuilder.INCREMENTAL_BUILD) {
154             return IDEWorkbenchMessages.GlobalBuildAction_buildOperationTitle;
155         }
156         return IDEWorkbenchMessages.GlobalBuildAction_rebuildAllOperationTitle;
157     }
158
159     /**
160      * Builds all projects within the workspace. Does not save any open editors.
161      */

162     public void doBuild() {
163         doBuildOperation();
164     }
165
166     /**
167      * Invokes a build on all projects within the workspace. Reports any errors
168      * with the build to the user.
169      */

170     /* package */void doBuildOperation() {
171         Job buildJob = new Job(IDEWorkbenchMessages.GlobalBuildAction_jobTitle) {
172             /*
173              * (non-Javadoc)
174              *
175              * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
176              */

177             protected IStatus run(IProgressMonitor monitor) {
178                 final MultiStatus status = new MultiStatus(
179                         PlatformUI.PLUGIN_ID, 0, IDEWorkbenchMessages.GlobalBuildAction_buildProblems,
180                         null);
181                 monitor.beginTask(getOperationMessage(), 100);
182                 try {
183                     ResourcesPlugin.getWorkspace().build(buildType,
184                             new SubProgressMonitor(monitor, 100));
185                 } catch (CoreException e) {
186                     status.add(e.getStatus());
187                 } finally {
188                     monitor.done();
189                 }
190                 return status;
191             }
192
193             /*
194              * (non-Javadoc)
195              *
196              * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
197              */

198             public boolean belongsTo(Object JavaDoc family) {
199                 return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
200             }
201         };
202         buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory()
203                 .buildRule());
204         buildJob.setUser(true);
205         buildJob.schedule();
206     }
207
208     /**
209      * Returns an array of all projects in the workspace
210      */

211     /* package */IProject[] getWorkspaceProjects() {
212         return ResourcesPlugin.getWorkspace().getRoot().getProjects();
213     }
214
215     /*
216      * (non-Javadoc) Method declared on IAction.
217      *
218      * Builds all projects within the workspace. Saves all editors prior to
219      * build depending on user's preference.
220      */

221     public void run() {
222         if (workbenchWindow == null) {
223             // action has been disposed
224
return;
225         }
226         // Do nothing if there are no projects...
227
IProject[] roots = getWorkspaceProjects();
228         if (roots.length < 1) {
229             return;
230         }
231         // Verify that there are builders registered on at
232
// least one project
233
if (!verifyBuildersAvailable(roots)) {
234             return;
235         }
236         if (!verifyNoManualRunning()) {
237             return;
238         }
239         // Save all resources prior to doing build
240
BuildUtilities.saveEditors(null);
241         // Perform the build on all the projects
242
doBuildOperation();
243     }
244
245     /**
246      * Checks that there is at least one project with a builder registered on
247      * it.
248      */

249     /* package */boolean verifyBuildersAvailable(IProject[] roots) {
250         try {
251             for (int i = 0; i < roots.length; i++) {
252                 if (roots[i].isAccessible()) {
253                     if (roots[i].getDescription().getBuildSpec().length > 0) {
254                         return true;
255                     }
256                 }
257             }
258         } catch (CoreException e) {
259             IDEWorkbenchPlugin.log(getClass(), "verifyBuildersAvailable", e); //$NON-NLS-1$
260
ErrorDialog
261                     .openError(
262                             getShell(),
263                             IDEWorkbenchMessages.GlobalBuildAction_buildProblems,
264                             NLS.bind(IDEWorkbenchMessages.GlobalBuildAction_internalError, e.getMessage()),
265                             e.getStatus());
266             return false;
267         }
268         return false;
269     }
270
271     /*
272      * (non-Javadoc) Method declared on ActionFactory.IWorkbenchAction.
273      *
274      * @since 3.0
275      */

276     public void dispose() {
277         if (workbenchWindow == null) {
278             // action has already been disposed
279
return;
280         }
281         workbenchWindow = null;
282     }
283
284     /**
285      * Verify that no manual build is running. If it is then give the use the
286      * option to cancel. If they cancel, cancel the jobs and return true,
287      * otherwise return false.
288      *
289      * @return whether or not there is a manual build job running.
290      */

291     private boolean verifyNoManualRunning() {
292         Job[] buildJobs = Platform.getJobManager().find(
293                 ResourcesPlugin.FAMILY_MANUAL_BUILD);
294         if (buildJobs.length == 0) {
295             return true;
296         }
297         boolean cancel = MessageDialog.openQuestion(workbenchWindow.getShell(),
298                 IDEWorkbenchMessages.GlobalBuildAction_BuildRunningTitle,
299                 IDEWorkbenchMessages.GlobalBuildAction_BuildRunningMessage);
300         if (cancel) {
301             for (int i = 0; i < buildJobs.length; i++) {
302                 Job job = buildJobs[i];
303                 job.cancel();
304             }
305         }
306         //If they cancelled get them to do it again.
307
return false;
308     }
309 }
310
Popular Tags