1 11 package org.eclipse.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 46 public class BuildAction extends WorkspaceAction { 47 48 51 public static final String ID_BUILD = PlatformUI.PLUGIN_ID + ".BuildAction"; 53 56 public static final String ID_REBUILD_ALL = PlatformUI.PLUGIN_ID 57 + ".RebuildAllAction"; 59 private int buildType; 60 61 64 private List projectsToBuild = null; 65 66 76 public BuildAction(Shell shell, int type) { 77 super(shell, ""); 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 100 private void addAllProjects(IProject project, HashSet 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 } 114 } 115 116 119 protected List getActionResources() { 120 return getProjectsToBuild(); 121 } 122 123 126 protected String getOperationMessage() { 127 return IDEWorkbenchMessages.BuildAction_operationMessage; 128 } 129 130 133 protected String getProblemsMessage() { 134 return IDEWorkbenchMessages.BuildAction_problemMessage; 135 } 136 137 140 protected String getProblemsTitle() { 141 return IDEWorkbenchMessages.BuildAction_problemTitle; 142 } 143 144 148 List getProjectsToBuild() { 149 if (projectsToBuild == null) { 150 projectsToBuild = new ArrayList (3); 151 for (Iterator 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 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 } 186 return false; 187 } 188 189 192 protected void invokeOperation(IResource resource, IProgressMonitor monitor) 193 throws CoreException { 194 ((IProject) resource).build(buildType, monitor); 195 } 196 197 200 public boolean isEnabled() { 201 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 203 if (window != null) { 204 selectionChanged(new StructuredSelection(BuildUtilities.findSelectedProjects(window))); 205 } 206 return super.isEnabled(); 207 } 208 209 215 public static boolean isSaveAllSet() { 216 IPreferenceStore store = IDEWorkbenchPlugin.getDefault() 217 .getPreferenceStore(); 218 return store.getBoolean(IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD); 219 } 220 221 229 List pruneResources(List resourceCollection) { 230 HashSet toBuild = new HashSet (); 232 for (Iterator it = resourceCollection.iterator(); it.hasNext();) { 233 addAllProjects((IProject) it.next(), toBuild); 234 } 235 236 if (toBuild.size() < 2) { 238 return resourceCollection; 239 } 240 241 String [] orderedNames = ResourcesPlugin.getWorkspace().getDescription() 243 .getBuildOrder(); 244 if (orderedNames != null) { 245 List orderedProjects = new ArrayList (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 orderedProjects.addAll(toBuild); 256 return orderedProjects; 257 } 258 259 IProject[] projects = new IProject[toBuild.size()]; 261 projects = (IProject[]) toBuild.toArray(projects); 262 IWorkspace.ProjectOrder po = ResourcesPlugin.getWorkspace() 263 .computeProjectOrder(projects); 264 ArrayList orderedProjects = new ArrayList (); 265 orderedProjects.addAll(Arrays.asList(po.projects)); 266 return orderedProjects; 267 } 268 269 274 public void run() { 275 List projects = getProjectsToBuild(); 276 if (projects == null || projects.isEmpty()) { 277 return; 278 } 279 280 BuildUtilities.saveEditors(projects); 282 runInBackground(ResourcesPlugin.getWorkspace().getRuleFactory() 283 .buildRule(), ResourcesPlugin.FAMILY_MANUAL_BUILD); 284 } 285 286 289 protected boolean shouldPerformResourcePruning() { 290 return true; 291 } 292 293 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 |