-
Notifications
You must be signed in to change notification settings - Fork 86
/
CoreUtility.java
196 lines (176 loc) · 6.86 KB
/
CoreUtility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.util;
import org.osgi.framework.Bundle;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaUIMessages;
public class CoreUtility {
public static void createDerivedFolder(IFolder folder, boolean force, boolean local, IProgressMonitor monitor) throws CoreException {
ResourcesUtility.createDerivedFolder(folder, force, local, monitor);
}
/**
* Creates a folder and all parent folders if not existing.
* Project must exist.
* <code> org.eclipse.ui.dialogs.ContainerGenerator</code> is too heavy
* (creates a runnable)
* @param folder the folder to create
* @param force a flag controlling how to deal with resources that
* are not in sync with the local file system
* @param local a flag controlling whether or not the folder will be local
* after the creation
* @param monitor the progress monitor
* @throws CoreException thrown if the creation failed
*/
public static void createFolder(IFolder folder, boolean force, boolean local, IProgressMonitor monitor) throws CoreException {
ResourcesUtility.createFolder(folder, force, local, monitor);
}
/**
* Creates an extension. If the extension plugin has not
* been loaded a busy cursor will be activated during the duration of
* the load.
*
* @param element the config element defining the extension
* @param classAttribute the name of the attribute carrying the class
* @return the extension object
* @throws CoreException thrown if the creation failed
*/
public static Object createExtension(final IConfigurationElement element, final String classAttribute) throws CoreException {
// If plugin has been loaded create extension.
// Otherwise, show busy cursor then create extension.
String pluginId = element.getContributor().getName();
Bundle bundle = Platform.getBundle(pluginId);
if (bundle != null && bundle.getState() == Bundle.ACTIVE ) {
return element.createExecutableExtension(classAttribute);
} else {
final Object[] ret = new Object[1];
final CoreException[] exc = new CoreException[1];
BusyIndicator.showWhile(null, () -> {
try {
ret[0] = element.createExecutableExtension(classAttribute);
} catch (CoreException e) {
exc[0] = e;
}
});
if (exc[0] != null)
throw exc[0];
else
return ret[0];
}
}
/**
* Starts a build in the background.
* @param project The project to build or <code>null</code> to build the workspace.
*/
public static void startBuildInBackground(final IProject project) {
getBuildJob(project).schedule();
}
private static final class BuildJob extends Job {
private final IProject fProject;
private BuildJob(String name, IProject project) {
super(name);
fProject= project;
}
public boolean isCoveredBy(BuildJob other) {
if (other.fProject == null) {
return true;
}
return fProject != null && fProject.equals(other.fProject);
}
@Override
protected IStatus run(IProgressMonitor monitor) {
synchronized (getClass()) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
for (Job curr : Job.getJobManager().find(ResourcesPlugin.FAMILY_MANUAL_BUILD)) {
if (curr != this && curr instanceof BuildJob) {
BuildJob job= (BuildJob) curr;
if (job.isCoveredBy(this)) {
curr.cancel(); // cancel all other build jobs of our kind
}
}
}
}
try {
if (fProject != null) {
SubMonitor subMonitor= SubMonitor.convert(monitor, Messages.format(JavaUIMessages.CoreUtility_buildproject_taskname, BasicElementLabels.getResourceName(fProject)), 2);
fProject.build(IncrementalProjectBuilder.FULL_BUILD, subMonitor.split(1));
JavaPlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, subMonitor.split(1));
} else {
SubMonitor subMonitor= SubMonitor.convert(monitor, JavaUIMessages.CoreUtility_buildall_taskname, 2);
JavaPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, subMonitor.split(2));
}
} catch (CoreException e) {
return e.getStatus();
} catch (OperationCanceledException e) {
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
}
}
/**
* Returns a build job
* @param project The project to build or <code>null</code> to build the workspace.
* @return the build job
*/
public static Job getBuildJob(final IProject project) {
Job buildJob= new BuildJob(JavaUIMessages.CoreUtility_job_title, project);
buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().buildRule());
buildJob.setUser(true);
return buildJob;
}
/**
* Sets whether building automatically is enabled in the workspace or not and returns the old
* value.
*
* @param state <code>true</code> if automatically building is enabled, <code>false</code>
* otherwise
* @return the old state
* @throws CoreException thrown if the operation failed
*/
public static boolean setAutoBuilding(boolean state) throws CoreException {
IWorkspace workspace= ResourcesPlugin.getWorkspace();
IWorkspaceDescription desc= workspace.getDescription();
boolean isAutoBuilding= desc.isAutoBuilding();
if (isAutoBuilding != state) {
desc.setAutoBuilding(state);
workspace.setDescription(desc);
}
return isAutoBuilding;
}
private CoreUtility() {
}
}