KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.ICommand;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.IncrementalProjectBuilder;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.internal.ide.IDEInternalPreferences;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
37 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
38 import org.eclipse.ui.internal.ide.actions.BuildUtilities;
39
40 /**
41  * Standard actions for full and incremental builds of the selected project(s).
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  */

46 public class BuildAction extends WorkspaceAction {
47
48     /**
49      * The id of an incremental build action.
50      */

51     public static final String JavaDoc ID_BUILD = PlatformUI.PLUGIN_ID + ".BuildAction";//$NON-NLS-1$
52

53     /**
54      * The id of a rebuild all action.
55      */

56     public static final String JavaDoc ID_REBUILD_ALL = PlatformUI.PLUGIN_ID
57             + ".RebuildAllAction";//$NON-NLS-1$
58

59     private int buildType;
60
61     /**
62      * The list of IProjects to build (computed lazily).
63      */

64     private List JavaDoc projectsToBuild = null;
65
66     /**
67      * Creates a new action of the appropriate type. The action id is
68      * <code>ID_BUILD</code> for incremental builds and <code>ID_REBUILD_ALL</code>
69      * for full builds.
70      *
71      * @param shell the shell for any dialogs
72      * @param type the type of build; one of
73      * <code>IncrementalProjectBuilder.INCREMENTAL_BUILD</code> or
74      * <code>IncrementalProjectBuilder.FULL_BUILD</code>
75      */

76     public BuildAction(Shell shell, int type) {
77         super(shell, "");//$NON-NLS-1$
78

79         if (type == IncrementalProjectBuilder.INCREMENTAL_BUILD) {
80             setText(IDEWorkbenchMessages.BuildAction_text);
81             setToolTipText(IDEWorkbenchMessages.BuildAction_toolTip);
82             setId(ID_BUILD);
83             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
84                     IIDEHelpContextIds.INCREMENTAL_BUILD_ACTION);
85         } else {
86             setText(IDEWorkbenchMessages.RebuildAction_text);
87             setToolTipText(IDEWorkbenchMessages.RebuildAction_tooltip);
88             setId(ID_REBUILD_ALL);
89             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
90                     IIDEHelpContextIds.FULL_BUILD_ACTION);
91         }
92
93         this.buildType = type;
94     }
95
96     /**
97      * Adds the given project and all of its prerequisities, transitively,
98      * to the provided set.
99      */

100     private void addAllProjects(IProject project, HashSet JavaDoc projects) {
101         if (project == null || !project.isAccessible()
102                 || projects.contains(project)) {
103             return;
104         }
105         projects.add(project);
106         try {
107             IProject[] preReqs = project.getReferencedProjects();
108             for (int i = 0; i < preReqs.length; i++) {
109                 addAllProjects(preReqs[i], projects);
110             }
111         } catch (CoreException e) {
112             //ignore inaccessible projects
113
}
114     }
115
116     /* (non-Javadoc)
117      * Method declared on WorkspaceAction.
118      */

119     protected List JavaDoc getActionResources() {
120         return getProjectsToBuild();
121     }
122
123     /* (non-Javadoc)
124      * Method declared on WorkspaceAction.
125      */

126     protected String JavaDoc getOperationMessage() {
127         return IDEWorkbenchMessages.BuildAction_operationMessage;
128     }
129
130     /* (non-Javadoc)
131      * Method declared on WorkspaceAction.
132      */

133     protected String JavaDoc getProblemsMessage() {
134         return IDEWorkbenchMessages.BuildAction_problemMessage;
135     }
136
137     /* (non-Javadoc)
138      * Method declared on WorkspaceAction.
139      */

140     protected String JavaDoc getProblemsTitle() {
141         return IDEWorkbenchMessages.BuildAction_problemTitle;
142     }
143
144     /**
145      * Returns the projects to build.
146      * This contains the set of projects which have builders, across all selected resources.
147      */

148     List JavaDoc getProjectsToBuild() {
149         if (projectsToBuild == null) {
150             projectsToBuild = new ArrayList JavaDoc(3);
151             for (Iterator JavaDoc i = getSelectedResources().iterator(); i.hasNext();) {
152                 IResource resource = (IResource) i.next();
153                 IProject project = resource.getProject();
154                 if (project != null) {
155                     if (!projectsToBuild.contains(project)) {
156                         if (hasBuilder(project)) {
157                             projectsToBuild.add(project);
158                         }
159                     }
160                 }
161             }
162         }
163         return projectsToBuild;
164     }
165
166     /**
167      * Returns whether there are builders configured on the given project.
168      *
169      * @return <code>true</code> if it has builders,
170      * <code>false</code> if not, or if this couldn't be determined
171      */

172     boolean hasBuilder(IProject project) {
173         if (!project.isAccessible())
174             return false;
175         try {
176             ICommand[] commands = project.getDescription().getBuildSpec();
177             if (commands.length > 0) {
178                 return true;
179             }
180         } catch (CoreException e) {
181             // this method is called when selection changes, so
182
// just fall through if it fails.
183
// this shouldn't happen anyway, since the list of selected resources
184
// has already been checked for accessibility before this is called
185
}
186         return false;
187     }
188
189     /* (non-Javadoc)
190      * Method declared on WorkspaceAction.
191      */

192     protected void invokeOperation(IResource resource, IProgressMonitor monitor)
193             throws CoreException {
194         ((IProject) resource).build(buildType, monitor);
195     }
196     
197     /* (non-Javadoc)
198      * Method declared on Action
199      */

200     public boolean isEnabled() {
201         //update enablement based on active window and part
202
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
203         if (window != null) {
204             selectionChanged(new StructuredSelection(BuildUtilities.findSelectedProjects(window)));
205         }
206         return super.isEnabled();
207     }
208     
209     /**
210      * Returns whether the user's preference is set to automatically save modified
211      * resources before a manual build is done.
212      *
213      * @return <code>true</code> if Save All Before Build is enabled
214      */

215     public static boolean isSaveAllSet() {
216         IPreferenceStore store = IDEWorkbenchPlugin.getDefault()
217                 .getPreferenceStore();
218         return store.getBoolean(IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD);
219     }
220
221     /* (non-Javadoc)
222      * Method declared on WorkspaceAction.
223      *
224      * Change the order of the resources so that
225      * it matches the build order. Closed and
226      * non existant projects are eliminated. Also,
227      * any projects in cycles are eliminated.
228      */

229     List JavaDoc pruneResources(List JavaDoc resourceCollection) {
230         //recursively compute project prerequisites
231
HashSet JavaDoc toBuild = new HashSet JavaDoc();
232         for (Iterator JavaDoc it = resourceCollection.iterator(); it.hasNext();) {
233             addAllProjects((IProject) it.next(), toBuild);
234         }
235
236         // Optimize...
237
if (toBuild.size() < 2) {
238             return resourceCollection;
239         }
240
241         // Try the workspace's description build order if specified
242
String JavaDoc[] orderedNames = ResourcesPlugin.getWorkspace().getDescription()
243                 .getBuildOrder();
244         if (orderedNames != null) {
245             List JavaDoc orderedProjects = new ArrayList JavaDoc(toBuild.size());
246             IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
247             for (int i = 0; i < orderedNames.length; i++) {
248                 IProject handle = root.getProject(orderedNames[i]);
249                 if (toBuild.contains(handle)) {
250                     orderedProjects.add(handle);
251                     toBuild.remove(handle);
252                 }
253             }
254             //Add anything not specified before we return
255
orderedProjects.addAll(toBuild);
256             return orderedProjects;
257         }
258
259         // Try the project prerequisite order then
260
IProject[] projects = new IProject[toBuild.size()];
261         projects = (IProject[]) toBuild.toArray(projects);
262         IWorkspace.ProjectOrder po = ResourcesPlugin.getWorkspace()
263                 .computeProjectOrder(projects);
264         ArrayList JavaDoc orderedProjects = new ArrayList JavaDoc();
265         orderedProjects.addAll(Arrays.asList(po.projects));
266         return orderedProjects;
267     }
268
269     /* (non-Javadoc)
270      * Method declared on IAction; overrides method on WorkspaceAction.
271      * This override allows the user to save the contents of selected
272      * open editors so that the updated contents will be used for building.
273      */

274     public void run() {
275         List JavaDoc projects = getProjectsToBuild();
276         if (projects == null || projects.isEmpty()) {
277             return;
278         }
279
280         // Save all resources prior to doing build
281
BuildUtilities.saveEditors(projects);
282         runInBackground(ResourcesPlugin.getWorkspace().getRuleFactory()
283                 .buildRule(), ResourcesPlugin.FAMILY_MANUAL_BUILD);
284     }
285
286     /* (non-Javadoc)
287      * Method declared on WorkspaceAction.
288      */

289     protected boolean shouldPerformResourcePruning() {
290         return true;
291     }
292
293     /**
294      * The <code>BuildAction</code> implementation of this
295      * <code>SelectionListenerAction</code> method ensures that this action is
296      * enabled only if all of the selected resources have buildable projects.
297      */

298     protected boolean updateSelection(IStructuredSelection s) {
299         projectsToBuild = null;
300         IProject[] projects = (IProject[]) getProjectsToBuild().toArray(new IProject[0]);
301         return BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.INCREMENTAL_BUILD);
302     }
303 }
304
Popular Tags