forked from gwt-plugins/gwt-eclipse-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewWebAppTemplateProjectWizardPage.java
More file actions
545 lines (463 loc) · 17.9 KB
/
NewWebAppTemplateProjectWizardPage.java
File metadata and controls
545 lines (463 loc) · 17.9 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*******************************************************************************
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* 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 com.google.gdt.eclipse.suite.wizards;
import com.google.gdt.eclipse.core.sdk.Sdk;
import com.google.gdt.eclipse.core.sdk.SdkClasspathContainer;
import com.google.gdt.eclipse.core.ui.SdkSelectionBlock;
import com.google.gdt.eclipse.core.ui.SdkSelectionBlock.SdkSelection;
import com.google.gdt.eclipse.platform.ui.PixelConverterFactory;
import com.google.gwt.eclipse.core.preferences.GWTPreferences;
import com.google.gwt.eclipse.core.preferences.ui.GwtPreferencePage;
import com.google.gwt.eclipse.core.runtime.GWTRuntimeContainer;
import com.google.gwt.eclipse.core.runtime.GwtSdk;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferencesUtil;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Wizard page where the user specifies the parameters for a new GWT project.
*/
public class NewWebAppTemplateProjectWizardPage extends WizardPage {
/**
* Select a GWT {@link Sdk} based on the set of {@link Sdk} known to the workspace.
*/
private final class GwtWorkspaceSdkSelectionBlock extends SdkSelectionBlock<GwtSdk> {
private GwtWorkspaceSdkSelectionBlock(Composite parent, int style) {
super(parent, style);
updateSdkBlockControls();
initializeSdkComboBox();
setSelection(-1);
}
@Override
protected void doConfigure() {
if (Window.OK == PreferencesUtil
.createPreferenceDialogOn(getShell(), GwtPreferencePage.ID, new String[] { GwtPreferencePage.ID }, null)
.open()) {
NewWebAppTemplateProjectWizardPage.this.updateControls();
}
}
@Override
protected GwtSdk doGetDefaultSdk() {
return GWTPreferences.getDefaultRuntime();
}
@Override
protected List<GwtSdk> doGetSpecificSdks() {
return new ArrayList<>(GWTPreferences.getSdks());
}
}
private final List<String> existingProjectNames = new ArrayList<>();
private SdkSelectionBlock<GwtSdk> gwtSelectionBlock;
private Button outDirBrowseButton;
private String outDirCustom = "";
private Button outDirCustomButton;
private Label outDirLabel;
private Text outDirText;
private Button outDirWorkspaceButton;
private Text packageText;
private Text projectNameText;
private Button useGwtCheckbox;
private Combo templateCombo;
private final String workspaceDirectory;
private List<ProjectTemplate> templates;
public NewWebAppTemplateProjectWizardPage() {
super("createProject");
setTitle("Create a Web Application Project");
setDescription("Create a Web Application project in the workspace or in an external location");
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
workspaceDirectory = workspaceRoot.getLocation().toOSString();
for (IProject project : workspaceRoot.getProjects()) {
existingProjectNames.add(project.getName());
}
}
@Override
public void createControl(Composite parent) {
final ScrolledComposite scroller = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
final GridLayout gridLayout = new GridLayout();
Composite containerOfComponents = new Composite(scroller, SWT.NULL);
containerOfComponents.setLayout(gridLayout);
setControl(containerOfComponents);
// TODO: convert these fields to use StringDialogField instead?
// Project name
final Label projectNameLabel = new Label(containerOfComponents, SWT.NONE);
projectNameLabel.setText("Project name:");
projectNameText = new Text(containerOfComponents, SWT.BORDER);
final GridData gd1 = new GridData(GridData.FILL_HORIZONTAL);
gd1.horizontalSpan = 2;
projectNameText.setLayoutData(gd1);
projectNameText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateControls();
}
});
// Package
final Label packageLabel = new Label(containerOfComponents, SWT.NONE);
packageLabel.setText("Package: (e.g. com.example.myproject)");
packageText = new Text(containerOfComponents, SWT.BORDER);
final GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
gd2.horizontalSpan = 2;
packageText.setLayoutData(gd2);
packageText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateControls();
}
});
final Label templateLabel = new Label(containerOfComponents, SWT.NONE);
templateLabel.setText("Select Template:");
templateCombo = new Combo(containerOfComponents, SWT.NONE);
templateCombo.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent evt) {
int index = templateCombo.getSelectionIndex();
ProjectTemplate template = templates.get(index);
templateCombo.setToolTipText(template.getDescription());
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
gd3.horizontalSpan = 2;
templateCombo.setLayoutData(gd3);
try {
templates = ProjectTemplate.getTemplates();
Collections.sort(templates);
for(int i=0;i<templates.size();i++)
{
ProjectTemplate templ = templates.get(i);
templateCombo.add(templ.getName());
}
} catch (IOException e1) {
e1.printStackTrace();
}
createLocationGroup(containerOfComponents);
createSdkGroup(containerOfComponents);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
scroller.setContent(containerOfComponents);
scroller.setMinSize(containerOfComponents.computeSize(SWT.DEFAULT, SWT.DEFAULT));
updateControls();
projectNameText.forceFocus();
}
public ProjectTemplate getTemplate()
{
int index = templateCombo.getSelectionIndex();
return templates.get(index);
}
public String getCreationLocation() {
if (outDirWorkspaceButton.getSelection()) {
// Use the default location.
return null;
}
return getOutputDirectory();
}
public URI getCreationLocationURI() {
if (outDirWorkspaceButton.getSelection()) {
// Default location is the workspace URI
return ResourcesPlugin.getWorkspace().getRoot().getLocationURI();
}
return new Path(getOutputDirectory()).toFile().toURI();
}
public String getPackage() {
return packageText.getText().trim();
}
public String getProjectName() {
return projectNameText.getText().trim();
}
public GwtSdk getSelectedGwtSdk() {
if (useGwtCheckbox.getSelection()) {
SdkSelection<GwtSdk> sdkSelection = gwtSelectionBlock.getSdkSelection();
if (sdkSelection != null) {
return sdkSelection.getSelectedSdk();
}
}
return null;
}
IPath getGWTSdkContainerPath() {
return getSdkContainerPath(gwtSelectionBlock.getSdkSelection(), GWTRuntimeContainer.CONTAINER_ID);
}
IPath getSdkContainerPath(SdkSelection<? extends Sdk> sdkSelection, String containerId) {
if (sdkSelection != null) {
return SdkClasspathContainer.computeContainerPath(containerId, sdkSelection.getSelectedSdk(),
sdkSelection.isDefault() ? SdkClasspathContainer.Type.DEFAULT : SdkClasspathContainer.Type.NAMED);
}
return null;
}
boolean useGWT() {
return useGwtCheckbox.getSelection();
}
private String browseForOutputDirectory() {
DirectoryDialog dlg = new DirectoryDialog(getShell(), SWT.OPEN);
dlg.setFilterPath(getOutputDirectory());
dlg.setMessage("Choose a directory for the project contents:");
return dlg.open();
}
private int convertValidationSeverity(int severity) {
switch (severity) {
case IStatus.ERROR:
return ERROR;
case IStatus.WARNING:
return WARNING;
case IStatus.INFO:
default:
return NONE;
}
}
private void createSdkGroup(Composite container) {
int widthIndent = PixelConverterFactory.createPixelConverter(this.getControl()).convertWidthInCharsToPixels(2);
Group sdkGroup = new Group(container, SWT.NONE);
sdkGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final GridLayout sdkGroupLayout = new GridLayout();
sdkGroupLayout.verticalSpacing = 0;
sdkGroupLayout.numColumns = 1;
sdkGroup.setLayout(sdkGroupLayout);
sdkGroup.setText("SDKs");
SelectionListener useSdkCheckboxSelectionListener = new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
updateControls();
}
@Override
public void widgetSelected(SelectionEvent e) {
updateControls();
}
};
createGwtSdkGroup(sdkGroup, useSdkCheckboxSelectionListener, widthIndent);
// Add a horizontal spacer
new Label(sdkGroup, SWT.HORIZONTAL);
}
private void createGwtSdkGroup(Group sdkGroup, SelectionListener useSdkCheckboxSelectionListener,
int widthIndent) {
useGwtCheckbox = new Button(sdkGroup, SWT.CHECK);
useGwtCheckbox.addSelectionListener(useSdkCheckboxSelectionListener);
useGwtCheckbox.setText("Use GWT");
useGwtCheckbox.setSelection(true);
gwtSelectionBlock = new GwtWorkspaceSdkSelectionBlock(sdkGroup, SWT.NONE);
gwtSelectionBlock.addSdkSelectionListener(new SdkSelectionBlock.SdkSelectionListener() {
@Override
public void onSdkSelection(SdkSelectionEvent e) {
updateControls();
}
});
((GridData) gwtSelectionBlock.getLayoutData()).horizontalIndent = widthIndent;
}
private void createLocationGroup(Composite container) {
// Output directory (defaults to subdirectory under workspace)
final Group outDirGroup = new Group(container, SWT.NULL);
outDirGroup.setText("Location");
final GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
gd3.horizontalSpan = 2;
outDirGroup.setLayoutData(gd3);
final GridLayout outDirGridLayout = new GridLayout();
outDirGridLayout.numColumns = 3;
outDirGroup.setLayout(outDirGridLayout);
outDirWorkspaceButton = new Button(outDirGroup, SWT.RADIO);
outDirWorkspaceButton.setText("Create new project in workspace");
outDirWorkspaceButton.setSelection(true);
final GridData gd4 = new GridData();
gd4.horizontalAlignment = GridData.FILL;
gd4.grabExcessHorizontalSpace = true;
gd4.horizontalSpan = 3;
outDirWorkspaceButton.setLayoutData(gd4);
outDirWorkspaceButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateControls();
}
});
outDirCustomButton = new Button(outDirGroup, SWT.RADIO);
outDirCustomButton.setText("Create new project in:");
final GridData gd5 = new GridData();
gd5.horizontalAlignment = GridData.FILL;
gd5.grabExcessHorizontalSpace = true;
gd5.horizontalSpan = 3;
outDirCustomButton.setLayoutData(gd5);
outDirCustomButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
outDirText.setText(outDirCustom);
updateControls();
}
});
outDirLabel = new Label(outDirGroup, SWT.NONE);
outDirLabel.setText("Directory:");
outDirText = new Text(outDirGroup, SWT.BORDER);
final GridData gd6 = new GridData();
gd6.horizontalAlignment = GridData.FILL;
gd6.grabExcessHorizontalSpace = true;
outDirText.setLayoutData(gd6);
outDirText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
if (outDirCustomButton.getSelection()) {
outDirCustom = getOutputDirectory();
validatePageAndSetCompletionStatus();
}
}
});
outDirBrowseButton = new Button(outDirGroup, SWT.NONE);
outDirBrowseButton.setText("Browse...");
outDirBrowseButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String outDir = browseForOutputDirectory();
if (outDir != null) {
outDirText.setText(outDir);
}
}
});
}
private String getOutputDirectory() {
return outDirText.getText().trim();
}
private void updateControls() {
// Set the output directory to the workspace
if (outDirWorkspaceButton.getSelection()) {
outDirLabel.setEnabled(false);
outDirText.setEnabled(false);
outDirBrowseButton.setEnabled(true);
String outDir = workspaceDirectory;
if (getProjectName().length() > 0) {
outDir += (System.getProperty("file.separator") + getProjectName());
}
outDirText.setText(outDir);
} else {
outDirLabel.setEnabled(true);
outDirText.setEnabled(true);
outDirBrowseButton.setEnabled(true);
}
gwtSelectionBlock.setEnabled(useGwtCheckbox.getSelection());
validatePageAndSetCompletionStatus();
}
private boolean validateFromStatus(IStatus status) {
if (!status.isOK()) {
setMessage(status.getMessage(), convertValidationSeverity(status.getSeverity()));
return false;
}
return true;
}
private void validatePageAndSetCompletionStatus() {
IStatus status;
boolean pageComplete = false;
try {
// Project name cannot be blank
if (getProjectName().length() == 0) {
setMessage("Enter a name for the project");
return;
}
// Verify that project name is valid
status = ResourcesPlugin.getWorkspace().validateName(getProjectName(), IResource.PROJECT);
if (!validateFromStatus(status)) {
return;
}
// Make sure project doesn't already exist in workspace
if (existingProjectNames.contains(getProjectName())) {
setMessage("A project with this name already exists.", ERROR);
return;
}
// Output directory cannot be blank
if (getOutputDirectory().length() == 0) {
setMessage("Enter the output directory");
return;
}
// If the user wants to use a custom output directory,
// verify that the directory exists
if (outDirCustomButton.getSelection()) {
File outDir = new Path(getOutputDirectory()).toFile();
if (!outDir.isDirectory()) {
setMessage("The output directory does not exist", ERROR);
return;
}
}
// Make sure resource with project's name doesn't already exist in output
// directory
IPath outPath = new Path(getOutputDirectory());
if (outDirWorkspaceButton.getSelection()) {
if (outPath.toFile().exists()) {
setMessage("A resource with the project name already exists in the workspace root", ERROR);
return;
}
}
// Make sure output directory doesn't already contain an Eclipse project
if (outDirCustomButton.getSelection()) {
outPath = outPath.append(IProjectDescription.DESCRIPTION_FILE_NAME);
if (outPath.toFile().exists()) {
setMessage("The output directory already contains a project file", ERROR);
return;
}
}
// Package name cannot be blank
if (getPackage().length() == 0) {
setMessage("Enter a package name");
return;
}
String complianceLevel = JavaCore.getOption("org.eclipse.jdt.core.compiler.compliance");
String sourceLevel = JavaCore.getOption("org.eclipse.jdt.core.compiler.source");
// Verify that package name is valid
status = JavaConventions.validatePackageName(getPackage(), complianceLevel, sourceLevel);
if (!validateFromStatus(status)) {
return;
}
// If we are using GWT then an SDK must be selected
if (useGwtCheckbox.getSelection()) {
IStatus gwtRuntimeValidationStatus;
GwtSdk selectedGwtRuntime = getSelectedGwtSdk();
if (selectedGwtRuntime == null) {
setMessage("Please configure a GWT SDK.", ERROR);
return;
} else if (!(gwtRuntimeValidationStatus = selectedGwtRuntime.validate()).isOK()) {
setMessage("The selected GWT SDK is not valid: " + gwtRuntimeValidationStatus.getMessage(), ERROR);
return;
}
}
pageComplete = true;
setMessage(null);
} finally {
setPageComplete(pageComplete);
}
}
}