/
SARLProjectCreationWizard.java
309 lines (274 loc) · 10.2 KB
/
SARLProjectCreationWizard.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.eclipse.wizards.newproject;
import io.sarl.eclipse.SARLConfig;
import io.sarl.eclipse.SARLEclipsePlugin;
import io.sarl.eclipse.properties.RuntimeEnvironmentPropertyPage;
import io.sarl.eclipse.runtime.ISREInstall;
import io.sarl.lang.ui.preferences.SARLProjectPreferences;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.wizards.JavaProjectWizard;
import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.ui.IPackagesViewPart;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
/**
* SARL new project wizard.
* Most part of the code of this class comes from {@link JavaProjectWizard}.
*
* @author $Author: ngaud$
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class SARLProjectCreationWizard extends NewElementWizard implements IExecutableExtension {
private MainProjectPage fFirstPage;
private BuildSettingPage fSecondPage;
private IConfigurationElement fConfigElement;
/** Construct a new wizard for creating a SARL project.
*/
public SARLProjectCreationWizard() {
this(null, null);
}
/** Construct a new wizard for creating a SARL project.
*
* @param pageOne - reference to the first page of the wizard.
* @param pageTwo - reference to the second page of the wizard.
*/
public SARLProjectCreationWizard(MainProjectPage pageOne, BuildSettingPage pageTwo) {
setDefaultPageImageDescriptor(SARLEclipsePlugin.getImageDescriptor(
SARLConfig.NEW_PROJECT_WIZARD_DIALOG_IMAGE));
setDialogSettings(JavaPlugin.getDefault().getDialogSettings());
setWindowTitle(Messages.SARLProjectNewWizard_0);
this.fFirstPage = pageOne;
this.fSecondPage = pageTwo;
}
@Override
public void addPages() {
if (this.fFirstPage == null) {
this.fFirstPage = new MainProjectPage();
}
addPage(this.fFirstPage);
if (this.fSecondPage == null) {
this.fSecondPage = new BuildSettingPage(this.fFirstPage);
}
addPage(this.fSecondPage);
this.fFirstPage.init(getSelection(), getActivePart());
}
private static boolean hasSourcePath(IJavaProject javaProject, IPath path) {
if (path != null) {
IPath pathInProject = javaProject.getProject().getFullPath().append(path);
try {
for(IClasspathEntry entry : javaProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
&& pathInProject.equals(entry.getPath())) {
return true;
}
}
} catch (Throwable _) {
//
}
}
return false;
}
/** Validate the SARL properties of the new projects.
*
* @param element - the created element
* @return validity
*/
protected boolean validateSARLSpecificElements(IJavaElement element) {
IJavaProject javaProject = (IJavaProject) element;
// Check if the "SARL" generation directory is a source folder.
IPath projectPath = SARLProjectPreferences.getSARLOutputPathFor(javaProject.getProject());
IPath outputPath = projectPath;
IPath generalPath = null;
if (outputPath == null) {
generalPath = SARLProjectPreferences.getSARLOutputPathFor(null);
outputPath = generalPath;
}
if (!hasSourcePath(javaProject, outputPath)) {
StringBuilder sourceFolders = new StringBuilder();
try {
for(IClasspathEntry entry : javaProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
sourceFolders.append("\t"); //$NON-NLS-1$
sourceFolders.append(entry.getPath().toOSString());
sourceFolders.append("\n"); //$NON-NLS-1$
}
}
} catch (Throwable _) {
//
}
String message = MessageFormat.format(
Messages.SARLProjectCreationWizard_0,
outputPath == null ? null : outputPath.toOSString(),
projectPath == null ? null : projectPath.toOSString(),
generalPath == null ? null : generalPath.toOSString(),
sourceFolders.toString());
IStatus status = SARLEclipsePlugin.createStatus(IStatus.ERROR, message);
handleFinishException(getShell(), new InvocationTargetException(new CoreException(status)));
return false;
}
return true;
}
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
// use the full progress monitor
this.fSecondPage.performFinish(monitor);
}
@Override
public boolean performFinish() {
boolean res = super.performFinish();
if (res) {
final IJavaElement newElement;
try {
newElement = getCreatedElement();
} catch (Throwable e) {
handleFinishException(getShell(), new InvocationTargetException(e));
return false;
}
// Validate the SARL specific elements
if (!validateSARLSpecificElements(newElement)) {
return false;
}
IWorkingSet[] workingSets = this.fFirstPage.getWorkingSets();
if (workingSets.length > 0) {
PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(newElement, workingSets);
}
BasicNewProjectResourceWizard.updatePerspective(this.fConfigElement);
selectAndReveal(this.fSecondPage.getJavaProject().getProject());
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
IWorkbenchPart activePart = getActivePart();
if (activePart instanceof IPackagesViewPart) {
PackageExplorerPart view = PackageExplorerPart.openInActivePerspective();
view.tryToReveal(newElement);
}
}
});
}
return res;
}
/** Replies the active part in the workbench.
*
* @return the active part.
*/
IWorkbenchPart getActivePart() {
IWorkbenchWindow activeWindow = getWorkbench().getActiveWorkbenchWindow();
if (activeWindow != null) {
IWorkbenchPage activePage = activeWindow.getActivePage();
if (activePage != null) {
return activePage.getActivePart();
}
}
return null;
}
@Override
protected void handleFinishException(Shell shell, InvocationTargetException e) {
String title = NewWizardMessages.JavaProjectWizard_op_error_title;
String message = NewWizardMessages.JavaProjectWizard_op_error_create_message;
ExceptionHandler.handle(e, getShell(), title, message);
}
@Override
public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
this.fConfigElement = cfig;
}
@Override
public boolean performCancel() {
this.fSecondPage.performCancel();
return super.performCancel();
}
@Override
public IJavaElement getCreatedElement() {
IJavaProject javaProject = this.fSecondPage.getJavaProject();
try {
addNatures(javaProject.getProject());
// Set the SRE configuration
IProject project = javaProject.getProject();
ISREInstall sre = this.fFirstPage.getSRE();
boolean useDefaultSRE = (sre == null || this.fFirstPage.isSystemDefaultSRE());
QualifiedName qn = RuntimeEnvironmentPropertyPage.qualify(
RuntimeEnvironmentPropertyPage.PROPERTY_NAME_HAS_PROJECT_SPECIFIC);
project.setPersistentProperty(qn, Boolean.toString(!useDefaultSRE));
if (!useDefaultSRE) {
assert (sre != null);
qn = RuntimeEnvironmentPropertyPage.qualify(
RuntimeEnvironmentPropertyPage.PROPERTY_NAME_USE_SYSTEM_WIDE_SRE);
project.setPersistentProperty(qn, Boolean.FALSE.toString());
qn = RuntimeEnvironmentPropertyPage.qualify(
RuntimeEnvironmentPropertyPage.PROPERTY_NAME_SRE_INSTALL_ID);
project.setPersistentProperty(qn, sre.getId());
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
return javaProject;
}
private static void addNatures(IProject project) throws CoreException {
final IProjectDescription description = project.getDescription();
final List<String> natures = new ArrayList<>(Arrays.asList(description.getNatureIds()));
natures.add(0, SARLConfig.NATURE_ID);
natures.add(1, SARLConfig.XTEXT_NATURE_ID);
// natures.add(2, JavaCore.NATURE_ID); not necessary since the project is already a java project
final String[] newNatures = natures.toArray(new String[natures.size()]);
final IStatus status = ResourcesPlugin.getWorkspace().validateNatureSet(newNatures);
// check the status and decide what to do
if (status.getCode() == IStatus.OK) {
description.setNatureIds(newNatures);
IProgressMonitor monitor = new NullProgressMonitor();
project.setDescription(description, monitor);
} else {
JavaPlugin.logErrorStatus(
Messages.SARLProjectNewWizard_4,
status);
}
}
}