KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > CoreUtility


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /**
55      * Creates a folder and all parent folders if not existing.
56      * Project must exist.
57      * <code> org.eclipse.ui.dialogs.ContainerGenerator</code> is too heavy
58      * (creates a runnable)
59      */

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     /**
71      * Creates an extension. If the extension plugin has not
72      * been loaded a busy cursor will be activated during the duration of
73      * the load.
74      *
75      * @param element the config element defining the extension
76      * @param classAttribute the name of the attribute carrying the class
77      * @return the extension object
78      */

79     public static Object JavaDoc createExtension(final IConfigurationElement element, final String JavaDoc classAttribute) throws CoreException {
80         // If plugin has been loaded create extension.
81
// Otherwise, show busy cursor then create extension.
82
String JavaDoc 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 JavaDoc[] ret = new Object JavaDoc[1];
88             final CoreException[] exc = new CoreException[1];
89             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
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     /**
107      * Starts a build in the background.
108      * @param project The project to build or <code>null</code> to build the workspace.
109      */

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 JavaDoc 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         /* (non-Javadoc)
130          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
131          */

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(); // cancel all other build jobs of our kind
144
}
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 JavaDoc family) {
168             return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
169         }
170     }
171     
172     /**
173      * Returns a build job
174      * @param project The project to build or <code>null</code> to build the workspace.
175      */

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     /**
184      * Set the autobuild to the value of the parameter and
185      * return the old one.
186      *
187      * @param state the value to be set for autobuilding.
188      * @return the old value of the autobuild state
189      */

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