KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > BuildUtilities


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.actions;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.core.resources.ICommand;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IProjectDescription;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IncrementalProjectBuilder;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.resources.mapping.ResourceMapping;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.actions.BuildAction;
33 import org.eclipse.ui.ide.ResourceUtil;
34
35 /**
36  * This class contains convenience methods used by the various build commands
37  * to determine enablement. These utilities cannot be factored into a common
38  * class because some build actions are API and some are not.
39  *
40  * @since 3.1
41  */

42 public class BuildUtilities {
43     /**
44      * Extracts the selected projects from a selection.
45      *
46      * @param selection The selection to analyze
47      * @return The selected projects
48      */

49     public static IProject[] extractProjects(Object JavaDoc[] selection) {
50         HashSet JavaDoc projects = new HashSet JavaDoc();
51         for (int i = 0; i < selection.length; i++) {
52             IResource resource = ResourceUtil.getResource(selection[i]);
53             if (resource != null) {
54                 projects.add(resource.getProject());
55             } else {
56                 ResourceMapping mapping = ResourceUtil.getResourceMapping(selection[i]);
57                 if (mapping != null) {
58                     IProject[] theProjects = mapping.getProjects();
59                     for(int j=0; j < theProjects.length; j++) {
60                            projects.add(theProjects[j]);
61                     }
62                 }
63             }
64         }
65         return (IProject[]) projects.toArray(new IProject[projects.size()]);
66     }
67
68     /**
69      * Finds and returns the selected projects in the given window
70      *
71      * @param window The window to find the selection in
72      * @return The selected projects, or an empty array if no selection could be found.
73      */

74     public static IProject[] findSelectedProjects(IWorkbenchWindow window) {
75         if (window == null) {
76             return new IProject[0];
77         }
78         ISelection selection = window.getSelectionService().getSelection();
79         IProject[] selected = null;
80         if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
81             selected = extractProjects(((IStructuredSelection) selection).toArray());
82         } else {
83             //see if we can extract a selected project from the active editor
84
IWorkbenchPart part = window.getPartService().getActivePart();
85             if (part instanceof IEditorPart) {
86                 IEditorPart editor = (IEditorPart) part;
87                 IFile file = ResourceUtil.getFile(editor.getEditorInput());
88                 if (file != null) {
89                     selected = new IProject[] {file.getProject()};
90                 }
91             }
92         }
93         if (selected == null) {
94             selected = new IProject[0];
95         }
96         return selected;
97     }
98
99     /**
100      * Returns whether a build command with the given trigger should
101      * be enabled for the given selection.
102      * @param projects The projects to use to determine enablement
103      * @param trigger The build trigger (<code>IncrementalProjectBuilder.*_BUILD</code> constants).
104      * @return <code>true</code> if the action should be enabled, and
105      * <code>false</code> otherwise.
106      */

107     public static boolean isEnabled(IProject[] projects, int trigger) {
108         //incremental build is only enabled if all projects are not autobuilding
109
if (trigger == IncrementalProjectBuilder.INCREMENTAL_BUILD && ResourcesPlugin.getWorkspace().isAutoBuilding()) {
110             if (!matchingTrigger(projects, IncrementalProjectBuilder.AUTO_BUILD, false)) {
111                 return false;
112             }
113         }
114         //finally we are building only if there is a builder that accepts the trigger
115
return matchingTrigger(projects, trigger, true);
116     }
117
118     /**
119      * Returns whether one of the projects has a builder whose trigger setting
120      * for the given trigger matches the given value.
121      *
122      * @param projects The projects to check
123      * @param trigger The trigger to look for
124      * @param value The trigger value to look for
125      * @return <code>true</code> if one of the projects has a builder whose
126      * trigger activation matches the provided value, and <code>false</code> otherwise.
127      */

128     private static boolean matchingTrigger(IProject[] projects, int trigger, boolean value) {
129         for (int i = 0; i < projects.length; i++) {
130             if (!projects[i].isAccessible()) {
131                 continue;
132             }
133             try {
134                 IProjectDescription description = projects[i].getDescription();
135                 ICommand[] buildSpec = description.getBuildSpec();
136                 for (int j = 0; j < buildSpec.length; j++) {
137                     if (buildSpec[j].isBuilding(trigger) == value) {
138                         return true;
139                     }
140                 }
141             } catch (CoreException e) {
142                 //ignore projects that are not available
143
}
144         }
145         return false;
146     }
147
148     /**
149      * Causes all editors to save any modified resources in the provided collection
150      * of projects depending on the user's preference.
151      * @param projects The projects in which to save editors, or <code>null</code>
152      * to save editors in all projects.
153      */

154     public static void saveEditors(Collection JavaDoc projects) {
155         if (!BuildAction.isSaveAllSet()) {
156             return;
157         }
158         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
159         for (int i = 0; i < windows.length; i++) {
160             IWorkbenchPage[] pages = windows[i].getPages();
161             for (int j = 0; j < pages.length; j++) {
162                 IWorkbenchPage page = pages[j];
163                 if (projects == null) {
164                     page.saveAllEditors(false);
165                 } else {
166                     IEditorPart[] editors = page.getDirtyEditors();
167                     for (int k = 0; k < editors.length; k++) {
168                         IEditorPart editor = editors[k];
169                         IFile inputFile = ResourceUtil.getFile(editor.getEditorInput());
170                         if (inputFile != null) {
171                             if (projects.contains(inputFile.getProject())) {
172                                 page.saveEditor(editor, false);
173                             }
174                         }
175                     }
176                 }
177             }
178         }
179     }
180
181     /**
182      * Doesn't need to be instantiated
183      */

184     private BuildUtilities() {
185     }
186 }
Popular Tags