1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 import org.eclipse.core.runtime.jobs.Job; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFolder; 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.IWorkspace; 28 import org.eclipse.core.resources.IWorkspaceDescription; 29 import org.eclipse.core.resources.IncrementalProjectBuilder; 30 import org.eclipse.core.resources.ResourcesPlugin; 31 32 import org.eclipse.swt.custom.BusyIndicator; 33 34 import org.eclipse.jdt.internal.corext.util.Messages; 35 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.JavaUIMessages; 38 39 import org.osgi.framework.Bundle; 40 41 42 public class CoreUtility { 43 44 public static void createDerivedFolder(IFolder folder, boolean force, boolean local, IProgressMonitor monitor) throws CoreException { 45 if (!folder.exists()) { 46 IContainer parent= folder.getParent(); 47 if (parent instanceof IFolder) { 48 createDerivedFolder((IFolder)parent, force, local, null); 49 } 50 folder.create(force ? (IResource.FORCE | IResource.DERIVED) : IResource.DERIVED, local, monitor); 51 } 52 } 53 54 60 public static void createFolder(IFolder folder, boolean force, boolean local, IProgressMonitor monitor) throws CoreException { 61 if (!folder.exists()) { 62 IContainer parent= folder.getParent(); 63 if (parent instanceof IFolder) { 64 createFolder((IFolder)parent, force, local, null); 65 } 66 folder.create(force, local, monitor); 67 } 68 } 69 70 79 public static Object createExtension(final IConfigurationElement element, final String classAttribute) throws CoreException { 80 String pluginId = element.getContributor().getName(); 83 Bundle bundle = Platform.getBundle(pluginId); 84 if (bundle != null && bundle.getState() == Bundle.ACTIVE ) { 85 return element.createExecutableExtension(classAttribute); 86 } else { 87 final Object [] ret = new Object [1]; 88 final CoreException[] exc = new CoreException[1]; 89 BusyIndicator.showWhile(null, new Runnable () { 90 public void run() { 91 try { 92 ret[0] = element.createExecutableExtension(classAttribute); 93 } catch (CoreException e) { 94 exc[0] = e; 95 } 96 } 97 }); 98 if (exc[0] != null) 99 throw exc[0]; 100 else 101 return ret[0]; 102 } 103 } 104 105 106 110 public static void startBuildInBackground(final IProject project) { 111 getBuildJob(project).schedule(); 112 } 113 114 115 private static final class BuildJob extends Job { 116 private final IProject fProject; 117 private BuildJob(String name, IProject project) { 118 super(name); 119 fProject= project; 120 } 121 122 public boolean isCoveredBy(BuildJob other) { 123 if (other.fProject == null) { 124 return true; 125 } 126 return fProject != null && fProject.equals(other.fProject); 127 } 128 129 132 protected IStatus run(IProgressMonitor monitor) { 133 synchronized (getClass()) { 134 if (monitor.isCanceled()) { 135 return Status.CANCEL_STATUS; 136 } 137 Job[] buildJobs = Job.getJobManager().find(ResourcesPlugin.FAMILY_MANUAL_BUILD); 138 for (int i= 0; i < buildJobs.length; i++) { 139 Job curr= buildJobs[i]; 140 if (curr != this && curr instanceof BuildJob) { 141 BuildJob job= (BuildJob) curr; 142 if (job.isCoveredBy(this)) { 143 curr.cancel(); } 145 } 146 } 147 } 148 try { 149 if (fProject != null) { 150 monitor.beginTask(Messages.format(JavaUIMessages.CoreUtility_buildproject_taskname, fProject.getName()), 2); 151 fProject.build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor,1)); 152 JavaPlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(monitor,1)); 153 } else { 154 monitor.beginTask(JavaUIMessages.CoreUtility_buildall_taskname, 2); 155 JavaPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor, 2)); 156 } 157 } catch (CoreException e) { 158 return e.getStatus(); 159 } catch (OperationCanceledException e) { 160 return Status.CANCEL_STATUS; 161 } 162 finally { 163 monitor.done(); 164 } 165 return Status.OK_STATUS; 166 } 167 public boolean belongsTo(Object family) { 168 return ResourcesPlugin.FAMILY_MANUAL_BUILD == family; 169 } 170 } 171 172 176 public static Job getBuildJob(final IProject project) { 177 Job buildJob= new BuildJob(JavaUIMessages.CoreUtility_job_title, project); 178 buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().buildRule()); 179 buildJob.setUser(true); 180 return buildJob; 181 } 182 183 190 public static boolean enableAutoBuild(boolean state) throws CoreException { 191 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 192 IWorkspaceDescription desc= workspace.getDescription(); 193 boolean isAutoBuilding= desc.isAutoBuilding(); 194 if (isAutoBuilding != state) { 195 desc.setAutoBuilding(state); 196 workspace.setDescription(desc); 197 } 198 return isAutoBuilding; 199 } 200 201 } 202 | Popular Tags |