1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.core.resources.IProject; 14 import org.eclipse.core.resources.IncrementalProjectBuilder; 15 import org.eclipse.core.resources.ResourcesPlugin; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.MultiStatus; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 import org.eclipse.core.runtime.jobs.Job; 23 import org.eclipse.jface.action.Action; 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.ui.IWorkbench; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; 32 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 33 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 34 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 35 import org.eclipse.ui.internal.ide.actions.BuildUtilities; 36 37 44 public class GlobalBuildAction extends Action implements 45 ActionFactory.IWorkbenchAction { 46 51 private int buildType; 52 53 57 private IWorkbenchWindow workbenchWindow; 58 59 75 public GlobalBuildAction(IWorkbench workbench, Shell shell, int type) { 76 this(workbench.getActiveWorkbenchWindow(), type); 78 if (shell == null) { 79 throw new IllegalArgumentException (); 80 } 81 } 82 83 95 public GlobalBuildAction(IWorkbenchWindow window, int type) { 96 if (window == null) { 97 throw new IllegalArgumentException (); 98 } 99 this.workbenchWindow = window; 100 setBuildType(type); 101 } 102 103 111 private void setBuildType(int type) { 112 switch (type) { 115 case IncrementalProjectBuilder.INCREMENTAL_BUILD: 116 case IncrementalProjectBuilder.AUTO_BUILD: 117 setText(IDEWorkbenchMessages.GlobalBuildAction_text); 118 setToolTipText(IDEWorkbenchMessages.GlobalBuildAction_toolTip); 119 setId("build"); workbenchWindow.getWorkbench().getHelpSystem().setHelp(this, 121 IIDEHelpContextIds.GLOBAL_INCREMENTAL_BUILD_ACTION); 122 setImageDescriptor(IDEInternalWorkbenchImages 123 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC)); 124 setDisabledImageDescriptor(IDEInternalWorkbenchImages 125 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_DISABLED)); 126 setActionDefinitionId("org.eclipse.ui.project.buildAll"); break; 128 case IncrementalProjectBuilder.FULL_BUILD: 129 setText(IDEWorkbenchMessages.GlobalBuildAction_rebuildText); 130 setToolTipText(IDEWorkbenchMessages.GlobalBuildAction_rebuildToolTip); 131 setId("rebuildAll"); workbenchWindow.getWorkbench().getHelpSystem().setHelp(this, 133 IIDEHelpContextIds.GLOBAL_FULL_BUILD_ACTION); 134 setActionDefinitionId("org.eclipse.ui.project.rebuildAll"); break; 136 default: 137 throw new IllegalArgumentException ("Invalid build type"); } 139 this.buildType = type; 140 } 141 142 145 private Shell getShell() { 146 return workbenchWindow.getShell(); 147 } 148 149 152 private String getOperationMessage() { 153 if (buildType == IncrementalProjectBuilder.INCREMENTAL_BUILD) { 154 return IDEWorkbenchMessages.GlobalBuildAction_buildOperationTitle; 155 } 156 return IDEWorkbenchMessages.GlobalBuildAction_rebuildAllOperationTitle; 157 } 158 159 162 public void doBuild() { 163 doBuildOperation(); 164 } 165 166 170 void doBuildOperation() { 171 Job buildJob = new Job(IDEWorkbenchMessages.GlobalBuildAction_jobTitle) { 172 177 protected IStatus run(IProgressMonitor monitor) { 178 final MultiStatus status = new MultiStatus( 179 PlatformUI.PLUGIN_ID, 0, IDEWorkbenchMessages.GlobalBuildAction_buildProblems, 180 null); 181 monitor.beginTask(getOperationMessage(), 100); 182 try { 183 ResourcesPlugin.getWorkspace().build(buildType, 184 new SubProgressMonitor(monitor, 100)); 185 } catch (CoreException e) { 186 status.add(e.getStatus()); 187 } finally { 188 monitor.done(); 189 } 190 return status; 191 } 192 193 198 public boolean belongsTo(Object family) { 199 return ResourcesPlugin.FAMILY_MANUAL_BUILD == family; 200 } 201 }; 202 buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory() 203 .buildRule()); 204 buildJob.setUser(true); 205 buildJob.schedule(); 206 } 207 208 211 IProject[] getWorkspaceProjects() { 212 return ResourcesPlugin.getWorkspace().getRoot().getProjects(); 213 } 214 215 221 public void run() { 222 if (workbenchWindow == null) { 223 return; 225 } 226 IProject[] roots = getWorkspaceProjects(); 228 if (roots.length < 1) { 229 return; 230 } 231 if (!verifyBuildersAvailable(roots)) { 234 return; 235 } 236 if (!verifyNoManualRunning()) { 237 return; 238 } 239 BuildUtilities.saveEditors(null); 241 doBuildOperation(); 243 } 244 245 249 boolean verifyBuildersAvailable(IProject[] roots) { 250 try { 251 for (int i = 0; i < roots.length; i++) { 252 if (roots[i].isAccessible()) { 253 if (roots[i].getDescription().getBuildSpec().length > 0) { 254 return true; 255 } 256 } 257 } 258 } catch (CoreException e) { 259 IDEWorkbenchPlugin.log(getClass(), "verifyBuildersAvailable", e); ErrorDialog 261 .openError( 262 getShell(), 263 IDEWorkbenchMessages.GlobalBuildAction_buildProblems, 264 NLS.bind(IDEWorkbenchMessages.GlobalBuildAction_internalError, e.getMessage()), 265 e.getStatus()); 266 return false; 267 } 268 return false; 269 } 270 271 276 public void dispose() { 277 if (workbenchWindow == null) { 278 return; 280 } 281 workbenchWindow = null; 282 } 283 284 291 private boolean verifyNoManualRunning() { 292 Job[] buildJobs = Platform.getJobManager().find( 293 ResourcesPlugin.FAMILY_MANUAL_BUILD); 294 if (buildJobs.length == 0) { 295 return true; 296 } 297 boolean cancel = MessageDialog.openQuestion(workbenchWindow.getShell(), 298 IDEWorkbenchMessages.GlobalBuildAction_BuildRunningTitle, 299 IDEWorkbenchMessages.GlobalBuildAction_BuildRunningMessage); 300 if (cancel) { 301 for (int i = 0; i < buildJobs.length; i++) { 302 Job job = buildJobs[i]; 303 job.cancel(); 304 } 305 } 306 return false; 308 } 309 } 310 | Popular Tags |