001: /*******************************************************************************
002: * Copyright (c) 2004, 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 - Initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.actions;
011:
012: import java.util.Collection;
013: import java.util.HashSet;
014:
015: import org.eclipse.core.resources.ICommand;
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IProjectDescription;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.resources.IncrementalProjectBuilder;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.resources.mapping.ResourceMapping;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.jface.viewers.ISelection;
025: import org.eclipse.jface.viewers.IStructuredSelection;
026: import org.eclipse.ui.IEditorPart;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.IWorkbenchPart;
029: import org.eclipse.ui.IWorkbenchWindow;
030: import org.eclipse.ui.PlatformUI;
031: import org.eclipse.ui.actions.BuildAction;
032: import org.eclipse.ui.ide.ResourceUtil;
033:
034: /**
035: * This class contains convenience methods used by the various build commands
036: * to determine enablement. These utilities cannot be factored into a common
037: * class because some build actions are API and some are not.
038: *
039: * @since 3.1
040: */
041: public class BuildUtilities {
042: /**
043: * Extracts the selected projects from a selection.
044: *
045: * @param selection The selection to analyze
046: * @return The selected projects
047: */
048: public static IProject[] extractProjects(Object[] selection) {
049: HashSet projects = new HashSet();
050: for (int i = 0; i < selection.length; i++) {
051: IResource resource = ResourceUtil.getResource(selection[i]);
052: if (resource != null) {
053: projects.add(resource.getProject());
054: } else {
055: ResourceMapping mapping = ResourceUtil
056: .getResourceMapping(selection[i]);
057: if (mapping != null) {
058: IProject[] theProjects = mapping.getProjects();
059: for (int j = 0; j < theProjects.length; j++) {
060: projects.add(theProjects[j]);
061: }
062: }
063: }
064: }
065: return (IProject[]) projects.toArray(new IProject[projects
066: .size()]);
067: }
068:
069: /**
070: * Finds and returns the selected projects in the given window
071: *
072: * @param window The window to find the selection in
073: * @return The selected projects, or an empty array if no selection could be found.
074: */
075: public static IProject[] findSelectedProjects(
076: IWorkbenchWindow window) {
077: if (window == null) {
078: return new IProject[0];
079: }
080: ISelection selection = window.getSelectionService()
081: .getSelection();
082: IProject[] selected = null;
083: if (selection != null && !selection.isEmpty()
084: && selection instanceof IStructuredSelection) {
085: selected = extractProjects(((IStructuredSelection) selection)
086: .toArray());
087: } else {
088: //see if we can extract a selected project from the active editor
089: IWorkbenchPart part = window.getPartService()
090: .getActivePart();
091: if (part instanceof IEditorPart) {
092: IEditorPart editor = (IEditorPart) part;
093: IFile file = ResourceUtil.getFile(editor
094: .getEditorInput());
095: if (file != null) {
096: selected = new IProject[] { file.getProject() };
097: }
098: }
099: }
100: if (selected == null) {
101: selected = new IProject[0];
102: }
103: return selected;
104: }
105:
106: /**
107: * Returns whether a build command with the given trigger should
108: * be enabled for the given selection.
109: * @param projects The projects to use to determine enablement
110: * @param trigger The build trigger (<code>IncrementalProjectBuilder.*_BUILD</code> constants).
111: * @return <code>true</code> if the action should be enabled, and
112: * <code>false</code> otherwise.
113: */
114: public static boolean isEnabled(IProject[] projects, int trigger) {
115: //incremental build is only enabled if all projects are not autobuilding
116: if (trigger == IncrementalProjectBuilder.INCREMENTAL_BUILD
117: && ResourcesPlugin.getWorkspace().isAutoBuilding()) {
118: if (!matchingTrigger(projects,
119: IncrementalProjectBuilder.AUTO_BUILD, false)) {
120: return false;
121: }
122: }
123: //finally we are building only if there is a builder that accepts the trigger
124: return matchingTrigger(projects, trigger, true);
125: }
126:
127: /**
128: * Returns whether one of the projects has a builder whose trigger setting
129: * for the given trigger matches the given value.
130: *
131: * @param projects The projects to check
132: * @param trigger The trigger to look for
133: * @param value The trigger value to look for
134: * @return <code>true</code> if one of the projects has a builder whose
135: * trigger activation matches the provided value, and <code>false</code> otherwise.
136: */
137: private static boolean matchingTrigger(IProject[] projects,
138: int trigger, boolean value) {
139: for (int i = 0; i < projects.length; i++) {
140: if (!projects[i].isAccessible()) {
141: continue;
142: }
143: try {
144: IProjectDescription description = projects[i]
145: .getDescription();
146: ICommand[] buildSpec = description.getBuildSpec();
147: for (int j = 0; j < buildSpec.length; j++) {
148: if (buildSpec[j].isBuilding(trigger) == value) {
149: return true;
150: }
151: }
152: } catch (CoreException e) {
153: //ignore projects that are not available
154: }
155: }
156: return false;
157: }
158:
159: /**
160: * Causes all editors to save any modified resources in the provided collection
161: * of projects depending on the user's preference.
162: * @param projects The projects in which to save editors, or <code>null</code>
163: * to save editors in all projects.
164: */
165: public static void saveEditors(Collection projects) {
166: if (!BuildAction.isSaveAllSet()) {
167: return;
168: }
169: IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
170: .getWorkbenchWindows();
171: for (int i = 0; i < windows.length; i++) {
172: IWorkbenchPage[] pages = windows[i].getPages();
173: for (int j = 0; j < pages.length; j++) {
174: IWorkbenchPage page = pages[j];
175: if (projects == null) {
176: page.saveAllEditors(false);
177: } else {
178: IEditorPart[] editors = page.getDirtyEditors();
179: for (int k = 0; k < editors.length; k++) {
180: IEditorPart editor = editors[k];
181: IFile inputFile = ResourceUtil.getFile(editor
182: .getEditorInput());
183: if (inputFile != null) {
184: if (projects.contains(inputFile
185: .getProject())) {
186: page.saveEditor(editor, false);
187: }
188: }
189: }
190: }
191: }
192: }
193: }
194:
195: /**
196: * Doesn't need to be instantiated
197: */
198: private BuildUtilities() {
199: }
200: }
|