1 11 package org.eclipse.ui.internal.ide.actions; 12 13 import java.util.Collection ; 14 import java.util.HashSet ; 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 42 public class BuildUtilities { 43 49 public static IProject[] extractProjects(Object [] selection) { 50 HashSet projects = new HashSet (); 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 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 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 107 public static boolean isEnabled(IProject[] projects, int trigger) { 108 if (trigger == IncrementalProjectBuilder.INCREMENTAL_BUILD && ResourcesPlugin.getWorkspace().isAutoBuilding()) { 110 if (!matchingTrigger(projects, IncrementalProjectBuilder.AUTO_BUILD, false)) { 111 return false; 112 } 113 } 114 return matchingTrigger(projects, trigger, true); 116 } 117 118 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 } 144 } 145 return false; 146 } 147 148 154 public static void saveEditors(Collection 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 184 private BuildUtilities() { 185 } 186 } | Popular Tags |