]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/ui/wizards/NewJavaProjectWizardPageOne.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / wizards / NewJavaProjectWizardPageOne.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2012 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 *******************************************************************************/
11package org.eclipse.jdt.ui.wizards;
12
13import java.io.File;
14import java.io.IOException;
15import java.net.URI;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.Comparator;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Map;
23import java.util.Observable;
24import java.util.Observer;
25
26import org.eclipse.swt.SWT;
27import org.eclipse.swt.events.SelectionEvent;
28import org.eclipse.swt.events.SelectionListener;
29import org.eclipse.swt.layout.GridData;
30import org.eclipse.swt.layout.GridLayout;
31import org.eclipse.swt.widgets.Combo;
32import org.eclipse.swt.widgets.Composite;
33import org.eclipse.swt.widgets.Control;
34import org.eclipse.swt.widgets.DirectoryDialog;
35import org.eclipse.swt.widgets.Group;
36import org.eclipse.swt.widgets.Label;
37import org.eclipse.swt.widgets.Link;
38
39import org.eclipse.core.filesystem.URIUtil;
40
41import org.eclipse.core.runtime.IPath;
42import org.eclipse.core.runtime.IStatus;
43import org.eclipse.core.runtime.Path;
44import org.eclipse.core.runtime.Platform;
45
46import org.eclipse.core.resources.IProject;
47import org.eclipse.core.resources.IResource;
48import org.eclipse.core.resources.IWorkspace;
49import org.eclipse.core.resources.ResourcesPlugin;
50
51import org.eclipse.jface.dialogs.Dialog;
52import org.eclipse.jface.dialogs.IDialogConstants;
53import org.eclipse.jface.dialogs.IDialogSettings;
54import org.eclipse.jface.util.Policy;
55import org.eclipse.jface.viewers.IStructuredSelection;
56import org.eclipse.jface.viewers.ITreeSelection;
57import org.eclipse.jface.viewers.TreePath;
58import org.eclipse.jface.wizard.WizardPage;
59
60import org.eclipse.ui.IWorkbenchPart;
61import org.eclipse.ui.IWorkingSet;
62import org.eclipse.ui.PlatformUI;
63import org.eclipse.ui.dialogs.PreferencesUtil;
64import org.eclipse.ui.dialogs.WorkingSetConfigurationBlock;
65
66import org.eclipse.jdt.core.IClasspathEntry;
67import org.eclipse.jdt.core.JavaCore;
68
69import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
70import org.eclipse.jdt.internal.corext.util.Messages;
71
72import org.eclipse.jdt.launching.IVMInstall;
73import org.eclipse.jdt.launching.IVMInstall2;
74import org.eclipse.jdt.launching.IVMInstallType;
75import org.eclipse.jdt.launching.JavaRuntime;
76import org.eclipse.jdt.launching.VMStandin;
77import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
78
79import org.eclipse.jdt.ui.JavaUI;
80import org.eclipse.jdt.ui.PreferenceConstants;
81
82import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
83import org.eclipse.jdt.internal.ui.JavaPlugin;
84import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
85import org.eclipse.jdt.internal.ui.preferences.CompliancePreferencePage;
86import org.eclipse.jdt.internal.ui.preferences.NewJavaProjectPreferencePage;
87import org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage;
88import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
89import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
90import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
91import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField;
92import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
93import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
94import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
95import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
96import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
97import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
98import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
99import org.eclipse.jdt.internal.ui.workingsets.IWorkingSetIDs;
100
101/**
102 * The first page of the New Java Project wizard. This page is typically used in combination with
103 * {@link NewJavaProjectWizardPageTwo}. Clients can extend this page to modify the UI: Add, remove
104 * or reorder sections.
105 *
106 * <p>
107 * Clients may instantiate or subclass.
108 * </p>
109 *
110 * @since 3.4
111 */
112public class NewJavaProjectWizardPageOne extends WizardPage {
113
114 /**
115 * Request a project name. Fires an event whenever the text field is
116 * changed, regardless of its content.
117 */
118 private final class NameGroup extends Observable implements IDialogFieldListener {
119
120 protected final StringDialogField fNameField;
121
122 public NameGroup() {
123 // text field for project name
124 fNameField= new StringDialogField();
125 fNameField.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_NameGroup_label_text);
126 fNameField.setDialogFieldListener(this);
127 }
128
129 public Control createControl(Composite composite) {
130 Composite nameComposite= new Composite(composite, SWT.NONE);
131 nameComposite.setFont(composite.getFont());
132 nameComposite.setLayout(new GridLayout(2, false));
133
134 fNameField.doFillIntoGrid(nameComposite, 2);
135 LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null));
136
137 return nameComposite;
138 }
139
140 protected void fireEvent() {
141 setChanged();
142 notifyObservers();
143 }
144
145 public String getName() {
146 return fNameField.getText().trim();
147 }
148
149 public void postSetFocus() {
150 fNameField.postSetFocusOnDialogField(getShell().getDisplay());
151 }
152
153 public void setName(String name) {
154 fNameField.setText(name);
155 }
156
157 /* (non-Javadoc)
158 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
159 */
160 public void dialogFieldChanged(DialogField field) {
161 fireEvent();
162 }
163 }
164
165 /**
166 * Request a location. Fires an event whenever the checkbox or the location
167 * field is changed, regardless of whether the change originates from the
168 * user or has been invoked programmatically.
169 */
170 private final class LocationGroup extends Observable implements Observer, IStringButtonAdapter, IDialogFieldListener {
171
172 protected final SelectionButtonDialogField fUseDefaults;
173 protected final StringButtonDialogField fLocation;
174
175 private String fPreviousExternalLocation;
176
177 private static final String DIALOGSTORE_LAST_EXTERNAL_LOC= JavaUI.ID_PLUGIN + ".last.external.project"; //$NON-NLS-1$
178
179 public LocationGroup() {
180 fUseDefaults= new SelectionButtonDialogField(SWT.CHECK);
181 fUseDefaults.setDialogFieldListener(this);
182 fUseDefaults.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_LocationGroup_location_desc);
183
184 fLocation= new StringButtonDialogField(this);
185 fLocation.setDialogFieldListener(this);
186 fLocation.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_LocationGroup_locationLabel_desc);
187 fLocation.setButtonLabel(NewWizardMessages.NewJavaProjectWizardPageOne_LocationGroup_browseButton_desc);
188
189 fUseDefaults.setSelection(true);
190
191 fPreviousExternalLocation= ""; //$NON-NLS-1$
192 }
193
194 public Control createControl(Composite composite) {
195 final int numColumns= 4;
196
197 final Composite locationComposite= new Composite(composite, SWT.NONE);
198 locationComposite.setLayout(new GridLayout(numColumns, false));
199
200 fUseDefaults.doFillIntoGrid(locationComposite, numColumns);
201 fLocation.doFillIntoGrid(locationComposite, numColumns);
202 LayoutUtil.setHorizontalGrabbing(fLocation.getTextControl(null));
203
204 return locationComposite;
205 }
206
207 protected void fireEvent() {
208 setChanged();
209 notifyObservers();
210 }
211
212 protected String getDefaultPath(String name) {
213 final IPath path= Platform.getLocation().append(name);
214 return path.toOSString();
215 }
216
217 /* (non-Javadoc)
218 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
219 */
220 public void update(Observable o, Object arg) {
221 if (isUseDefaultSelected()) {
222 fLocation.setText(getDefaultPath(fNameGroup.getName()));
223 }
224 fireEvent();
225 }
226
227 public IPath getLocation() {
228 if (isUseDefaultSelected()) {
229 return Platform.getLocation();
230 }
231 return Path.fromOSString(fLocation.getText().trim());
232 }
233
234 public boolean isUseDefaultSelected() {
235 return fUseDefaults.isSelected();
236 }
237
238 public void setLocation(IPath path) {
239 fUseDefaults.setSelection(path == null);
240 if (path != null) {
241 fLocation.setText(path.toOSString());
242 } else {
243 fLocation.setText(getDefaultPath(fNameGroup.getName()));
244 }
245 fireEvent();
246 }
247
248 /* (non-Javadoc)
249 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter#changeControlPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
250 */
251 public void changeControlPressed(DialogField field) {
252 final DirectoryDialog dialog= new DirectoryDialog(getShell());
253 dialog.setMessage(NewWizardMessages.NewJavaProjectWizardPageOne_directory_message);
254 String directoryName = fLocation.getText().trim();
255 if (directoryName.length() == 0) {
256 String prevLocation= JavaPlugin.getDefault().getDialogSettings().get(DIALOGSTORE_LAST_EXTERNAL_LOC);
257 if (prevLocation != null) {
258 directoryName= prevLocation;
259 }
260 }
261
262 if (directoryName.length() > 0) {
263 final File path = new File(directoryName);
264 if (path.exists())
265 dialog.setFilterPath(directoryName);
266 }
267 final String selectedDirectory = dialog.open();
268 if (selectedDirectory != null) {
269 String oldDirectory= new Path(fLocation.getText().trim()).lastSegment();
270 fLocation.setText(selectedDirectory);
271 String lastSegment= new Path(selectedDirectory).lastSegment();
272 if (lastSegment != null && (fNameGroup.getName().length() == 0 || fNameGroup.getName().equals(oldDirectory))) {
273 fNameGroup.setName(lastSegment);
274 }
275 JavaPlugin.getDefault().getDialogSettings().put(DIALOGSTORE_LAST_EXTERNAL_LOC, selectedDirectory);
276 }
277 }
278
279 /* (non-Javadoc)
280 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
281 */
282 public void dialogFieldChanged(DialogField field) {
283 if (field == fUseDefaults) {
284 final boolean checked= fUseDefaults.isSelected();
285 if (checked) {
286 fPreviousExternalLocation= fLocation.getText();
287 fLocation.setText(getDefaultPath(fNameGroup.getName()));
288 fLocation.setEnabled(false);
289 } else {
290 fLocation.setText(fPreviousExternalLocation);
291 fLocation.setEnabled(true);
292 }
293 }
294 fireEvent();
295 }
296 }
297
298 /**
299 * Request a project layout.
300 */
301 private final class LayoutGroup implements Observer, SelectionListener {
302
303 private final SelectionButtonDialogField fStdRadio, fSrcBinRadio;
304 private Group fGroup;
305 private Link fPreferenceLink;
306
307 public LayoutGroup() {
308 fStdRadio= new SelectionButtonDialogField(SWT.RADIO);
309 fStdRadio.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_LayoutGroup_option_oneFolder);
310
311 fSrcBinRadio= new SelectionButtonDialogField(SWT.RADIO);
312 fSrcBinRadio.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_LayoutGroup_option_separateFolders);
313
314 boolean useSrcBin= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ);
315 fSrcBinRadio.setSelection(useSrcBin);
316 fStdRadio.setSelection(!useSrcBin);
317 }
318
319
320 public Control createContent(Composite composite) {
321 fGroup= new Group(composite, SWT.NONE);
322 fGroup.setFont(composite.getFont());
323 fGroup.setLayout(initGridLayout(new GridLayout(3, false), true));
324 fGroup.setText(NewWizardMessages.NewJavaProjectWizardPageOne_LayoutGroup_title);
325
326 fStdRadio.doFillIntoGrid(fGroup, 3);
327 LayoutUtil.setHorizontalGrabbing(fStdRadio.getSelectionButton(null));
328
329 fSrcBinRadio.doFillIntoGrid(fGroup, 2);
330
331 fPreferenceLink= new Link(fGroup, SWT.NONE);
332 fPreferenceLink.setText(NewWizardMessages.NewJavaProjectWizardPageOne_LayoutGroup_link_description);
333 fPreferenceLink.setLayoutData(new GridData(GridData.END, GridData.END, false, false));
334 fPreferenceLink.addSelectionListener(this);
335
336 updateEnableState();
337 return fGroup;
338 }
339
340
341 /* (non-Javadoc)
342 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
343 */
344 public void update(Observable o, Object arg) {
345 updateEnableState();
346 }
347
348 private void updateEnableState() {
349 if (fDetectGroup == null)
350 return;
351
352 final boolean detect= fDetectGroup.mustDetect();
353 fStdRadio.setEnabled(!detect);
354 fSrcBinRadio.setEnabled(!detect);
355 if (fPreferenceLink != null) {
356 fPreferenceLink.setEnabled(!detect);
357 }
358 if (fGroup != null) {
359 fGroup.setEnabled(!detect);
360 }
361 }
362
363 /**
364 * Return <code>true</code> if the user specified to create 'source' and 'bin' folders.
365 *
366 * @return returns <code>true</code> if the user specified to create 'source' and 'bin' folders.
367 */
368 public boolean isSrcBin() {
369 return fSrcBinRadio.isSelected();
370 }
371
372 /* (non-Javadoc)
373 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
374 */
375 public void widgetSelected(SelectionEvent e) {
376 widgetDefaultSelected(e);
377 }
378
379 /* (non-Javadoc)
380 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
381 */
382 public void widgetDefaultSelected(SelectionEvent e) {
383 String id= NewJavaProjectPreferencePage.ID;
384 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String[] { id }, null).open();
385 fDetectGroup.handlePossibleJVMChange();
386 fJREGroup.handlePossibleJVMChange();
387 }
388 }
389
390 private final class JREGroup implements Observer, SelectionListener, IDialogFieldListener {
391
392 private static final String LAST_SELECTED_EE_SETTINGS_KEY= JavaUI.ID_PLUGIN + ".last.selected.execution.enviroment"; //$NON-NLS-1$
393 private static final String LAST_SELECTED_JRE_SETTINGS_KEY= JavaUI.ID_PLUGIN + ".last.selected.project.jre"; //$NON-NLS-1$
394// private static final String LAST_SELECTED_JRE_KIND= JavaUI.ID_PLUGIN + ".last.selected.jre.kind"; // used before EE became default
395 private static final String LAST_SELECTED_JRE_KIND2= JavaUI.ID_PLUGIN + ".last.selected.jre.kind2"; //$NON-NLS-1$
396
397 private static final int DEFAULT_JRE= 0;
398 private static final int PROJECT_JRE= 1;
399 private static final int EE_JRE= 2;
400
401 private final SelectionButtonDialogField fUseDefaultJRE, fUseProjectJRE, fUseEEJRE;
402 private final ComboDialogField fJRECombo;
403 private final ComboDialogField fEECombo;
404 private Group fGroup;
405 private Link fPreferenceLink;
406 private IVMInstall[] fInstalledJVMs;
407 private String[] fJRECompliance;
408 private IExecutionEnvironment[] fInstalledEEs;
409 private String[] fEECompliance;
410
411 public JREGroup() {
412 fUseDefaultJRE= new SelectionButtonDialogField(SWT.RADIO);
413 fUseDefaultJRE.setLabelText(getDefaultJVMLabel());
414
415 fUseProjectJRE= new SelectionButtonDialogField(SWT.RADIO);
416 fUseProjectJRE.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_JREGroup_specific_compliance);
417
418 fJRECombo= new ComboDialogField(SWT.READ_ONLY);
419 fillInstalledJREs(fJRECombo);
420 fJRECombo.setDialogFieldListener(this);
421
422 fUseEEJRE= new SelectionButtonDialogField(SWT.RADIO);
423 fUseEEJRE.setLabelText(NewWizardMessages.NewJavaProjectWizardPageOne_JREGroup_specific_EE);
424
425 fEECombo= new ComboDialogField(SWT.READ_ONLY);
426 fillExecutionEnvironments(fEECombo);
427 fEECombo.setDialogFieldListener(this);
428
429 switch (getLastSelectedJREKind()) {
430 case DEFAULT_JRE:
431 fUseDefaultJRE.setSelection(true);
432 break;
433 case PROJECT_JRE:
434 fUseProjectJRE.setSelection(true);
435 break;
436 case EE_JRE:
437 fUseEEJRE.setSelection(true);
438 break;
439 }
440
441 fJRECombo.setEnabled(fUseProjectJRE.isSelected());
442 fEECombo.setEnabled(fUseEEJRE.isSelected());
443
444 fUseDefaultJRE.setDialogFieldListener(this);
445 fUseProjectJRE.setDialogFieldListener(this);
446 fUseEEJRE.setDialogFieldListener(this);
447 }
448
449 public Control createControl(Composite composite) {
450 fGroup= new Group(composite, SWT.NONE);
451 fGroup.setFont(composite.getFont());
452 fGroup.setLayout(initGridLayout(new GridLayout(2, false), true));
453 fGroup.setText(NewWizardMessages.NewJavaProjectWizardPageOne_JREGroup_title);
454
455 fUseEEJRE.doFillIntoGrid(fGroup, 1);
456 Combo eeComboControl= fEECombo.getComboControl(fGroup);
457 eeComboControl.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
458
459 fUseProjectJRE.doFillIntoGrid(fGroup, 1);
460 Combo comboControl= fJRECombo.getComboControl(fGroup);
461 comboControl.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
462
463 fUseDefaultJRE.doFillIntoGrid(fGroup, 1);
464
465 fPreferenceLink= new Link(fGroup, SWT.NONE);
466 fPreferenceLink.setFont(fGroup.getFont());
467 fPreferenceLink.setText(NewWizardMessages.NewJavaProjectWizardPageOne_JREGroup_link_description);
468 fPreferenceLink.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
469 fPreferenceLink.addSelectionListener(this);
470
471 updateEnableState();
472 return fGroup;
473 }
474
475
476 private void fillInstalledJREs(ComboDialogField comboField) {
477 String selectedItem= getLastSelectedJRE();
478 int selectionIndex= -1;
479 if (fUseProjectJRE.isSelected()) {
480 selectionIndex= comboField.getSelectionIndex();
481 if (selectionIndex != -1) {//paranoia
482 selectedItem= comboField.getItems()[selectionIndex];
483 }
484 }
485
486 fInstalledJVMs= getWorkspaceJREs();
487 Arrays.sort(fInstalledJVMs, new Comparator<IVMInstall>() {
488
489 public int compare(IVMInstall i0, IVMInstall i1) {
490 if (i1 instanceof IVMInstall2 && i0 instanceof IVMInstall2) {
491 String cc0= JavaModelUtil.getCompilerCompliance((IVMInstall2) i0, JavaCore.VERSION_1_4);
492 String cc1= JavaModelUtil.getCompilerCompliance((IVMInstall2) i1, JavaCore.VERSION_1_4);
493 int result= cc1.compareTo(cc0);
494 if (result != 0)
495 return result;
496 }
497 return Policy.getComparator().compare(i0.getName(), i1.getName());
498 }
499
500 });
501 selectionIndex= -1;//find new index
502 String[] jreLabels= new String[fInstalledJVMs.length];
503 fJRECompliance= new String[fInstalledJVMs.length];
504 for (int i= 0; i < fInstalledJVMs.length; i++) {
505 jreLabels[i]= fInstalledJVMs[i].getName();
506 if (selectedItem != null && jreLabels[i].equals(selectedItem)) {
507 selectionIndex= i;
508 }
509 if (fInstalledJVMs[i] instanceof IVMInstall2) {
510 fJRECompliance[i]= JavaModelUtil.getCompilerCompliance((IVMInstall2) fInstalledJVMs[i], JavaCore.VERSION_1_4);
511 } else {
512 fJRECompliance[i]= JavaCore.VERSION_1_4;
513 }
514 }
515 comboField.setItems(jreLabels);
516 if (selectionIndex == -1) {
517 comboField.selectItem(getDefaultJVMName());
518 } else {
519 comboField.selectItem(selectedItem);
520 }
521 }
522
523 private void fillExecutionEnvironments(ComboDialogField comboField) {
524 String selectedItem= getLastSelectedEE();
525 int selectionIndex= -1;
526 if (fUseEEJRE.isSelected()) {
527 selectionIndex= comboField.getSelectionIndex();
528 if (selectionIndex != -1) {// paranoia
529 selectedItem= comboField.getItems()[selectionIndex];
530 }
531 }
532
533 fInstalledEEs= JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
534 Arrays.sort(fInstalledEEs, new Comparator<IExecutionEnvironment>() {
535 public int compare(IExecutionEnvironment arg0, IExecutionEnvironment arg1) {
536 return Policy.getComparator().compare(arg0.getId(), arg1.getId());
537 }
538 });
539 selectionIndex= -1;//find new index
540 String[] eeLabels= new String[fInstalledEEs.length];
541 fEECompliance= new String[fInstalledEEs.length];
542 for (int i= 0; i < fInstalledEEs.length; i++) {
543 eeLabels[i]= fInstalledEEs[i].getId();
544 if (selectedItem != null && eeLabels[i].equals(selectedItem)) {
545 selectionIndex= i;
546 }
547 fEECompliance[i]= JavaModelUtil.getExecutionEnvironmentCompliance(fInstalledEEs[i]);
548 }
549 comboField.setItems(eeLabels);
550 if (selectionIndex == -1) {
551 comboField.selectItem(getDefaultEEName());
552 } else {
553 comboField.selectItem(selectedItem);
554 }
555 }
556
557 private IVMInstall[] getWorkspaceJREs() {
558 List<VMStandin> standins = new ArrayList<VMStandin>();
559 IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
560 for (int i = 0; i < types.length; i++) {
561 IVMInstallType type = types[i];
562 IVMInstall[] installs = type.getVMInstalls();
563 for (int j = 0; j < installs.length; j++) {
564 IVMInstall install = installs[j];
565 standins.add(new VMStandin(install));
566 }
567 }
568 return standins.toArray(new IVMInstall[standins.size()]);
569 }
570
571 private String getDefaultJVMName() {
572 IVMInstall install= JavaRuntime.getDefaultVMInstall();
573 if (install != null) {
574 return install.getName();
575 } else {
576 return NewWizardMessages.NewJavaProjectWizardPageOne_UnknownDefaultJRE_name;
577 }
578 }
579
580 private String getDefaultEEName() {
581 IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall();
582
583 IExecutionEnvironment[] environments= JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
584 if (defaultVM != null) {
585 for (int i= 0; i < environments.length; i++) {
586 IVMInstall eeDefaultVM= environments[i].getDefaultVM();
587 if (eeDefaultVM != null && defaultVM.getId().equals(eeDefaultVM.getId()))
588 return environments[i].getId();
589 }
590 }
591
592 String defaultCC;
593 if (defaultVM instanceof IVMInstall2) {
594 defaultCC= JavaModelUtil.getCompilerCompliance((IVMInstall2)defaultVM, JavaCore.VERSION_1_4);
595 } else {
596 defaultCC= JavaCore.VERSION_1_4;
597 }
598
599 for (int i= 0; i < environments.length; i++) {
600 String eeCompliance= JavaModelUtil.getExecutionEnvironmentCompliance(environments[i]);
601 if (defaultCC.endsWith(eeCompliance))
602 return environments[i].getId();
603 }
604
605 return "JavaSE-1.6"; //$NON-NLS-1$
606 }
607
608 private String getDefaultJVMLabel() {
609 return Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_JREGroup_default_compliance, getDefaultJVMName());
610 }
611
612 public void update(Observable o, Object arg) {
613 updateEnableState();
614 }
615
616 private void updateEnableState() {
617 final boolean detect= fDetectGroup.mustDetect();
618 fUseDefaultJRE.setEnabled(!detect);
619 fUseProjectJRE.setEnabled(!detect);
620 fUseEEJRE.setEnabled(!detect);
621 fJRECombo.setEnabled(!detect && fUseProjectJRE.isSelected());
622 fEECombo.setEnabled(!detect && fUseEEJRE.isSelected());
623 if (fPreferenceLink != null) {
624 fPreferenceLink.setEnabled(!detect);
625 }
626 if (fGroup != null) {
627 fGroup.setEnabled(!detect);
628 }
629 }
630
631 /* (non-Javadoc)
632 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
633 */
634 public void widgetSelected(SelectionEvent e) {
635 widgetDefaultSelected(e);
636 }
637
638 /* (non-Javadoc)
639 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
640 */
641 public void widgetDefaultSelected(SelectionEvent e) {
642 String jreID= BuildPathSupport.JRE_PREF_PAGE_ID;
643 String eeID= BuildPathSupport.EE_PREF_PAGE_ID;
644 String complianceId= CompliancePreferencePage.PREF_ID;
645 Map<String, Boolean> data= new HashMap<String, Boolean>();
646 data.put(PropertyAndPreferencePage.DATA_NO_LINK, Boolean.TRUE);
647 PreferencesUtil.createPreferenceDialogOn(getShell(), jreID, new String[] { jreID, complianceId , eeID }, data).open();
648
649 handlePossibleJVMChange();
650 fDetectGroup.handlePossibleJVMChange();
651 }
652
653 public void handlePossibleJVMChange() {
654 fUseDefaultJRE.setLabelText(getDefaultJVMLabel());
655 fillInstalledJREs(fJRECombo);
656 fillExecutionEnvironments(fEECombo);
657 }
658
659 /* (non-Javadoc)
660 * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
661 */
662 public void dialogFieldChanged(DialogField field) {
663 updateEnableState();
664 fDetectGroup.handlePossibleJVMChange();
665 if (field == fJRECombo) {
666 if (fUseProjectJRE.isSelected()) {
667 storeSelectionValue(fJRECombo, LAST_SELECTED_JRE_SETTINGS_KEY);
668 }
669 } else if (field == fEECombo) {
670 if (fUseEEJRE.isSelected()) {
671 storeSelectionValue(fEECombo, LAST_SELECTED_EE_SETTINGS_KEY);
672 }
673 } else if (field == fUseDefaultJRE) {
674 if (fUseDefaultJRE.isSelected()) {
675 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND2, DEFAULT_JRE);
676 fUseProjectJRE.setSelection(false);
677 fUseEEJRE.setSelection(false);
678 }
679 } else if (field == fUseProjectJRE) {
680 if (fUseProjectJRE.isSelected()) {
681 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND2, PROJECT_JRE);
682 fUseDefaultJRE.setSelection(false);
683 fUseEEJRE.setSelection(false);
684 }
685 } else if (field == fUseEEJRE) {
686 if (fUseEEJRE.isSelected()) {
687 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND2, EE_JRE);
688 fUseDefaultJRE.setSelection(false);
689 fUseProjectJRE.setSelection(false);
690 }
691 }
692 }
693
694 private void storeSelectionValue(ComboDialogField combo, String preferenceKey) {
695 int index= combo.getSelectionIndex();
696 if (index == -1)
697 return;
698
699 String item= combo.getItems()[index];
700 JavaPlugin.getDefault().getDialogSettings().put(preferenceKey, item);
701 }
702
703 private int getLastSelectedJREKind() {
704 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
705 if (settings.get(LAST_SELECTED_JRE_KIND2) == null)
706 return EE_JRE;
707
708 return settings.getInt(LAST_SELECTED_JRE_KIND2);
709 }
710
711 private String getLastSelectedEE() {
712 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
713 return settings.get(LAST_SELECTED_EE_SETTINGS_KEY);
714 }
715
716 private String getLastSelectedJRE() {
717 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
718 return settings.get(LAST_SELECTED_JRE_SETTINGS_KEY);
719 }
720
721 public IVMInstall getSelectedJVM() {
722 if (fUseProjectJRE.isSelected()) {
723 int index= fJRECombo.getSelectionIndex();
724 if (index >= 0 && index < fInstalledJVMs.length) { // paranoia
725 return fInstalledJVMs[index];
726 }
727 } else if (fUseEEJRE.isSelected()) {
728
729 }
730 return null;
731 }
732
733 public IPath getJREContainerPath() {
734 if (fUseProjectJRE.isSelected()) {
735 int index= fJRECombo.getSelectionIndex();
736 if (index >= 0 && index < fInstalledJVMs.length) { // paranoia
737 return JavaRuntime.newJREContainerPath(fInstalledJVMs[index]);
738 }
739 } else if (fUseEEJRE.isSelected()) {
740 int index= fEECombo.getSelectionIndex();
741 if (index >= 0 && index < fInstalledEEs.length) { // paranoia
742 return JavaRuntime.newJREContainerPath(fInstalledEEs[index]);
743 }
744 }
745 return null;
746 }
747
748 public String getSelectedCompilerCompliance() {
749 if (fUseProjectJRE.isSelected()) {
750 int index= fJRECombo.getSelectionIndex();
751 if (index >= 0 && index < fJRECompliance.length) { // paranoia
752 return fJRECompliance[index];
753 }
754 } else if (fUseEEJRE.isSelected()) {
755 int index= fEECombo.getSelectionIndex();
756 if (index >= 0 && index < fEECompliance.length) { // paranoia
757 return fEECompliance[index];
758 }
759 }
760 return null;
761 }
762 }
763
764 private final class WorkingSetGroup {
765
766 private WorkingSetConfigurationBlock fWorkingSetBlock;
767
768 public WorkingSetGroup() {
769 String[] workingSetIds= new String[] { IWorkingSetIDs.JAVA, IWorkingSetIDs.RESOURCE };
770 fWorkingSetBlock= new WorkingSetConfigurationBlock(workingSetIds, JavaPlugin.getDefault().getDialogSettings());
771 //fWorkingSetBlock.setDialogMessage(NewWizardMessages.NewJavaProjectWizardPageOne_WorkingSetSelection_message);
772 }
773
774 public Control createControl(Composite composite) {
775 Group workingSetGroup= new Group(composite, SWT.NONE);
776 workingSetGroup.setFont(composite.getFont());
777 workingSetGroup.setText(NewWizardMessages.NewJavaProjectWizardPageOne_WorkingSets_group);
778 workingSetGroup.setLayout(new GridLayout(1, false));
779
780 fWorkingSetBlock.createContent(workingSetGroup);
781
782 return workingSetGroup;
783 }
784
785
786 public void setWorkingSets(IWorkingSet[] workingSets) {
787 fWorkingSetBlock.setWorkingSets(workingSets);
788 }
789
790 public IWorkingSet[] getSelectedWorkingSets() {
791 return fWorkingSetBlock.getSelectedWorkingSets();
792 }
793 }
794
795 /**
796 * Show a warning when the project location contains files.
797 */
798 private final class DetectGroup extends Observable implements Observer, SelectionListener {
799
800 private Link fHintText;
801 private Label fIcon;
802 private boolean fDetect;
803
804 public DetectGroup() {
805 fDetect= false;
806 }
807
808 public Control createControl(Composite parent) {
809
810 Composite composite= new Composite(parent, SWT.NONE);
811 composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
812 GridLayout layout= new GridLayout(2, false);
813 layout.horizontalSpacing= 10;
814 composite.setLayout(layout);
815
816 fIcon= new Label(composite, SWT.LEFT);
817 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
818 GridData gridData= new GridData(SWT.LEFT, SWT.TOP, false, false);
819 fIcon.setLayoutData(gridData);
820
821 fHintText= new Link(composite, SWT.WRAP);
822 fHintText.setFont(composite.getFont());
823 fHintText.addSelectionListener(this);
824 gridData= new GridData(GridData.FILL, SWT.FILL, true, true);
825 gridData.widthHint= convertWidthInCharsToPixels(50);
826 gridData.heightHint= convertHeightInCharsToPixels(3);
827 fHintText.setLayoutData(gridData);
828
829 handlePossibleJVMChange();
830 return composite;
831 }
832
833 public void handlePossibleJVMChange() {
834
835 if (JavaRuntime.getDefaultVMInstall() == null) {
836 fHintText.setText(NewWizardMessages.NewJavaProjectWizardPageOne_NoJREFound_link);
837 fHintText.setVisible(true);
838 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
839 fIcon.setVisible(true);
840 return;
841 }
842
843 String selectedCompliance= fJREGroup.getSelectedCompilerCompliance();
844 if (selectedCompliance != null) {
845 String defaultCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
846 if (selectedCompliance.equals(defaultCompliance)) {
847 fHintText.setVisible(false);
848 fIcon.setVisible(false);
849 } else {
850 fHintText.setText(Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_differendWorkspaceCC_message, new String[] { BasicElementLabels.getVersionName(defaultCompliance), BasicElementLabels.getVersionName(selectedCompliance)}));
851 fHintText.setVisible(true);
852 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO));
853 fIcon.setVisible(true);
854 }
855 return;
856 }
857
858 selectedCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
859 IVMInstall selectedJVM= fJREGroup.getSelectedJVM();
860 if (selectedJVM == null) {
861 selectedJVM= JavaRuntime.getDefaultVMInstall();
862 }
863 String jvmCompliance= JavaCore.VERSION_1_4;
864 if (selectedJVM instanceof IVMInstall2) {
865 jvmCompliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) selectedJVM, JavaCore.VERSION_1_4);
866 }
867 if (!selectedCompliance.equals(jvmCompliance) && (JavaModelUtil.is50OrHigher(selectedCompliance) || JavaModelUtil.is50OrHigher(jvmCompliance))) {
868 fHintText.setText(Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_jre_message, new String[] {BasicElementLabels.getVersionName(selectedCompliance), BasicElementLabels.getVersionName(jvmCompliance)}));
869 fHintText.setVisible(true);
870 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
871 fIcon.setVisible(true);
872 } else {
873 fHintText.setVisible(false);
874 fIcon.setVisible(false);
875 }
876
877 }
878
879 private boolean computeDetectState() {
880 if (fLocationGroup.isUseDefaultSelected()) {
881 String name= fNameGroup.getName();
882 if (name.length() == 0 || JavaPlugin.getWorkspace().getRoot().findMember(name) != null) {
883 return false;
884 } else {
885 final File directory= fLocationGroup.getLocation().append(name).toFile();
886 return directory.isDirectory();
887 }
888 } else {
889 final File directory= fLocationGroup.getLocation().toFile();
890 return directory.isDirectory();
891 }
892 }
893
894 public void update(Observable o, Object arg) {
895 if (o instanceof LocationGroup) {
896 boolean oldDetectState= fDetect;
897 fDetect= computeDetectState();
898
899 if (oldDetectState != fDetect) {
900 setChanged();
901 notifyObservers();
902
903 if (fDetect) {
904 fHintText.setVisible(true);
905 fHintText.setText(NewWizardMessages.NewJavaProjectWizardPageOne_DetectGroup_message);
906 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO));
907 fIcon.setVisible(true);
908 } else {
909 handlePossibleJVMChange();
910 }
911 }
912 }
913 }
914
915 public boolean mustDetect() {
916 return fDetect;
917 }
918
919 /* (non-Javadoc)
920 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
921 */
922 public void widgetSelected(SelectionEvent e) {
923 widgetDefaultSelected(e);
924 }
925
926 /* (non-Javadoc)
927 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
928 */
929 public void widgetDefaultSelected(SelectionEvent e) {
930 String jreID= BuildPathSupport.JRE_PREF_PAGE_ID;
931 String eeID= BuildPathSupport.EE_PREF_PAGE_ID;
932 String complianceId= CompliancePreferencePage.PREF_ID;
933 Map<String, Boolean> data= new HashMap<String, Boolean>();
934 data.put(PropertyAndPreferencePage.DATA_NO_LINK, Boolean.TRUE);
935 String id= "JRE".equals(e.text) ? jreID : complianceId; //$NON-NLS-1$
936 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String[] { jreID, complianceId, eeID }, data).open();
937
938 fJREGroup.handlePossibleJVMChange();
939 handlePossibleJVMChange();
940 }
941 }
942
943 /**
944 * Validate this page and show appropriate warnings and error NewWizardMessages.
945 */
946 private final class Validator implements Observer {
947
948 public void update(Observable o, Object arg) {
949
950 final IWorkspace workspace= JavaPlugin.getWorkspace();
951
952 final String name= fNameGroup.getName();
953
954 // check whether the project name field is empty
955 if (name.length() == 0) {
956 setErrorMessage(null);
957 setMessage(NewWizardMessages.NewJavaProjectWizardPageOne_Message_enterProjectName);
958 setPageComplete(false);
959 return;
960 }
961
962 // check whether the project name is valid
963 final IStatus nameStatus= workspace.validateName(name, IResource.PROJECT);
964 if (!nameStatus.isOK()) {
965 setErrorMessage(nameStatus.getMessage());
966 setPageComplete(false);
967 return;
968 }
969
970 // check whether project already exists
971 final IProject handle= workspace.getRoot().getProject(name);
972 if (handle.exists()) {
973 setErrorMessage(NewWizardMessages.NewJavaProjectWizardPageOne_Message_projectAlreadyExists);
974 setPageComplete(false);
975 return;
976 }
977
978 IPath projectLocation= ResourcesPlugin.getWorkspace().getRoot().getLocation().append(name);
979 if (projectLocation.toFile().exists()) {
980 try {
981 //correct casing
982 String canonicalPath= projectLocation.toFile().getCanonicalPath();
983 projectLocation= new Path(canonicalPath);
984 } catch (IOException e) {
985 JavaPlugin.log(e);
986 }
987
988 String existingName= projectLocation.lastSegment();
989 if (!existingName.equals(fNameGroup.getName())) {
990 setErrorMessage(Messages.format(NewWizardMessages.NewJavaProjectWizardPageOne_Message_invalidProjectNameForWorkspaceRoot, BasicElementLabels.getResourceName(existingName)));
991 setPageComplete(false);
992 return;
993 }
994
995 }
996
997 final String location= fLocationGroup.getLocation().toOSString();
998
999 // check whether location is empty
1000 if (location.length() == 0) {
1001 setErrorMessage(null);
1002 setMessage(NewWizardMessages.NewJavaProjectWizardPageOne_Message_enterLocation);
1003 setPageComplete(false);
1004 return;
1005 }
1006
1007 // check whether the location is a syntactically correct path
1008 if (!Path.EMPTY.isValidPath(location)) {
1009 setErrorMessage(NewWizardMessages.NewJavaProjectWizardPageOne_Message_invalidDirectory);
1010 setPageComplete(false);
1011 return;
1012 }
1013
1014 IPath projectPath= null;
1015 if (!fLocationGroup.isUseDefaultSelected()) {
1016 projectPath= Path.fromOSString(location);
1017 if (!projectPath.toFile().exists()) {
1018 // check non-existing external location
1019 if (!canCreate(projectPath.toFile())) {
1020 setErrorMessage(NewWizardMessages.NewJavaProjectWizardPageOne_Message_cannotCreateAtExternalLocation);
1021 setPageComplete(false);
1022 return;
1023 }
1024 }
1025 }
1026
1027 // validate the location
1028 final IStatus locationStatus= workspace.validateProjectLocation(handle, projectPath);
1029 if (!locationStatus.isOK()) {
1030 setErrorMessage(locationStatus.getMessage());
1031 setPageComplete(false);
1032 return;
1033 }
1034
1035 setPageComplete(true);
1036
1037 setErrorMessage(null);
1038 setMessage(null);
1039 }
1040
1041 private boolean canCreate(File file) {
1042 while (!file.exists()) {
1043 file= file.getParentFile();
1044 if (file == null)
1045 return false;
1046 }
1047
1048 return file.canWrite();
1049 }
1050 }
1051
1052 private static final String PAGE_NAME= "NewJavaProjectWizardPageOne"; //$NON-NLS-1$
1053
1054 private final NameGroup fNameGroup;
1055 private final LocationGroup fLocationGroup;
1056 private final LayoutGroup fLayoutGroup;
1057 private final JREGroup fJREGroup;
1058 private final DetectGroup fDetectGroup;
1059 private final Validator fValidator;
1060 private final WorkingSetGroup fWorkingSetGroup;
1061
1062 /**
1063 * Creates a new {@link NewJavaProjectWizardPageOne}.
1064 */
1065 public NewJavaProjectWizardPageOne() {
1066 super(PAGE_NAME);
1067 setPageComplete(false);
1068 setTitle(NewWizardMessages.NewJavaProjectWizardPageOne_page_title);
1069 setDescription(NewWizardMessages.NewJavaProjectWizardPageOne_page_description);
1070
1071 fNameGroup= new NameGroup();
1072 fLocationGroup= new LocationGroup();
1073 fJREGroup= new JREGroup();
1074 fLayoutGroup= new LayoutGroup();
1075 fWorkingSetGroup= new WorkingSetGroup();
1076 fDetectGroup= new DetectGroup();
1077
1078 // establish connections
1079 fNameGroup.addObserver(fLocationGroup);
1080 fDetectGroup.addObserver(fLayoutGroup);
1081 fDetectGroup.addObserver(fJREGroup);
1082 fLocationGroup.addObserver(fDetectGroup);
1083
1084 // initialize all elements
1085 fNameGroup.notifyObservers();
1086
1087 // create and connect validator
1088 fValidator= new Validator();
1089 fNameGroup.addObserver(fValidator);
1090 fLocationGroup.addObserver(fValidator);
1091
1092 // initialize defaults
1093 setProjectName(""); //$NON-NLS-1$
1094 setProjectLocationURI(null);
1095 setWorkingSets(new IWorkingSet[0]);
1096
1097 initializeDefaultVM();
1098 }
1099
1100 /**
1101 * The wizard owning this page can call this method to initialize the fields from the
1102 * current selection and active part.
1103 *
1104 * @param selection used to initialize the fields
1105 * @param activePart the (typically active) part to initialize the fields or <code>null</code>
1106 */
1107 public void init(IStructuredSelection selection, IWorkbenchPart activePart) {
1108 setWorkingSets(getSelectedWorkingSet(selection, activePart));
1109 }
1110
1111 private void initializeDefaultVM() {
1112 JavaRuntime.getDefaultVMInstall();
1113 }
1114
1115 /* (non-Javadoc)
1116 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
1117 */
1118 public void createControl(Composite parent) {
1119 initializeDialogUnits(parent);
1120
1121 final Composite composite= new Composite(parent, SWT.NULL);
1122 composite.setFont(parent.getFont());
1123 composite.setLayout(initGridLayout(new GridLayout(1, false), true));
1124 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
1125
1126 // create UI elements
1127 Control nameControl= createNameControl(composite);
1128 nameControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1129
1130 Control locationControl= createLocationControl(composite);
1131 locationControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1132
1133 Control jreControl= createJRESelectionControl(composite);
1134 jreControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1135
1136 Control layoutControl= createProjectLayoutControl(composite);
1137 layoutControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1138
1139 Control workingSetControl= createWorkingSetControl(composite);
1140 workingSetControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1141
1142 Control infoControl= createInfoControl(composite);
1143 infoControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
1144
1145 setControl(composite);
1146 }
1147
1148 @Override
1149 protected void setControl(Control newControl) {
1150 Dialog.applyDialogFont(newControl);
1151
1152 PlatformUI.getWorkbench().getHelpSystem().setHelp(newControl, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE);
1153
1154 super.setControl(newControl);
1155 }
1156
1157
1158 /**
1159 * Creates the controls for the name field.
1160 *
1161 * @param composite the parent composite
1162 * @return the created control
1163 */
1164 protected Control createNameControl(Composite composite) {
1165 return fNameGroup.createControl(composite);
1166 }
1167
1168 /**
1169 * Creates the controls for the location field.
1170 *
1171 * @param composite the parent composite
1172 * @return the created control
1173 */
1174 protected Control createLocationControl(Composite composite) {
1175 return fLocationGroup.createControl(composite);
1176 }
1177
1178 /**
1179 * Creates the controls for the JRE selection
1180 *
1181 * @param composite the parent composite
1182 * @return the created control
1183 */
1184 protected Control createJRESelectionControl(Composite composite) {
1185 return fJREGroup.createControl(composite);
1186 }
1187
1188 /**
1189 * Creates the controls for the project layout selection.
1190 *
1191 * @param composite the parent composite
1192 * @return the created control
1193 */
1194 protected Control createProjectLayoutControl(Composite composite) {
1195 return fLayoutGroup.createContent(composite);
1196 }
1197
1198 /**
1199 * Creates the controls for the working set selection.
1200 *
1201 * @param composite the parent composite
1202 * @return the created control
1203 */
1204 protected Control createWorkingSetControl(Composite composite) {
1205 return fWorkingSetGroup.createControl(composite);
1206 }
1207
1208 /**
1209 * Creates the controls for the info section.
1210 *
1211 * @param composite the parent composite
1212 * @return the created control
1213 */
1214 protected Control createInfoControl(Composite composite) {
1215 return fDetectGroup.createControl(composite);
1216 }
1217
1218 /**
1219 * Gets a project name for the new project.
1220 *
1221 * @return the new project resource handle
1222 */
1223 public String getProjectName() {
1224 return fNameGroup.getName();
1225 }
1226
1227 /**
1228 * Sets the name of the new project
1229 *
1230 * @param name the new name
1231 */
1232 public void setProjectName(String name) {
1233 if (name == null)
1234 throw new IllegalArgumentException();
1235
1236 fNameGroup.setName(name);
1237 }
1238
1239 /**
1240 * Returns the current project location path as entered by the user, or <code>null</code>
1241 * if the project should be created in the workspace.
1242
1243 * @return the project location path or its anticipated initial value.
1244 */
1245 public URI getProjectLocationURI() {
1246 if (fLocationGroup.isUseDefaultSelected()) {
1247 return null;
1248 }
1249 return URIUtil.toURI(fLocationGroup.getLocation());
1250 }
1251
1252 /**
1253 * Sets the project location of the new project or <code>null</code> if the project
1254 * should be created in the workspace
1255 *
1256 * @param uri the new project location
1257 */
1258 public void setProjectLocationURI(URI uri) {
1259 IPath path= uri != null ? URIUtil.toPath(uri) : null;
1260 fLocationGroup.setLocation(path);
1261 }
1262
1263 /**
1264 * Returns the compiler compliance to be used for the project, or <code>null</code> to use the workspace
1265 * compiler compliance.
1266 *
1267 * @return compiler compliance to be used for the project or <code>null</code>
1268 */
1269 public String getCompilerCompliance() {
1270 return fJREGroup.getSelectedCompilerCompliance();
1271 }
1272
1273 /**
1274 * Returns the default class path entries to be added on new projects. By default this is the JRE container as
1275 * selected by the user.
1276 *
1277 * @return returns the default class path entries
1278 */
1279 public IClasspathEntry[] getDefaultClasspathEntries() {
1280 IPath newPath= fJREGroup.getJREContainerPath();
1281 if (newPath != null) {
1282 return new IClasspathEntry[] { JavaCore.newContainerEntry(newPath) };
1283 }
1284 return PreferenceConstants.getDefaultJRELibrary();
1285 }
1286
1287 /**
1288 * Returns the source class path entries to be added on new projects.
1289 * The underlying resources may not exist. All entries that are returned must be of kind
1290 * {@link IClasspathEntry#CPE_SOURCE}.
1291 *
1292 * @return returns the source class path entries for the new project
1293 */
1294 public IClasspathEntry[] getSourceClasspathEntries() {
1295 IPath sourceFolderPath= new Path(getProjectName()).makeAbsolute();
1296
1297 if (fLayoutGroup.isSrcBin()) {
1298 IPath srcPath= new Path(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_SRCNAME));
1299 if (srcPath.segmentCount() > 0) {
1300 sourceFolderPath= sourceFolderPath.append(srcPath);
1301 }
1302 }
1303 return new IClasspathEntry[] { JavaCore.newSourceEntry(sourceFolderPath) };
1304 }
1305
1306 /**
1307 * Returns the source class path entries to be added on new projects.
1308 * The underlying resource may not exist.
1309 *
1310 * @return returns the default class path entries
1311 */
1312 public IPath getOutputLocation() {
1313 IPath outputLocationPath= new Path(getProjectName()).makeAbsolute();
1314 if (fLayoutGroup.isSrcBin()) {
1315 IPath binPath= new Path(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
1316 if (binPath.segmentCount() > 0) {
1317 outputLocationPath= outputLocationPath.append(binPath);
1318 }
1319 }
1320 return outputLocationPath;
1321 }
1322
1323 /**
1324 * Returns the working sets to which the new project should be added.
1325 *
1326 * @return the selected working sets to which the new project should be added
1327 */
1328 public IWorkingSet[] getWorkingSets() {
1329 return fWorkingSetGroup.getSelectedWorkingSets();
1330 }
1331
1332 /**
1333 * Sets the working sets to which the new project should be added.
1334 *
1335 * @param workingSets the initial selected working sets
1336 */
1337 public void setWorkingSets(IWorkingSet[] workingSets) {
1338 if (workingSets == null) {
1339 throw new IllegalArgumentException();
1340 }
1341 fWorkingSetGroup.setWorkingSets(workingSets);
1342 }
1343
1344 /* (non-Javadoc)
1345 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
1346 */
1347 @Override
1348 public void setVisible(boolean visible) {
1349 super.setVisible(visible);
1350 if (visible) {
1351 fNameGroup.postSetFocus();
1352 }
1353 }
1354
1355 private GridLayout initGridLayout(GridLayout layout, boolean margins) {
1356 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
1357 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
1358 if (margins) {
1359 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
1360 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
1361 } else {
1362 layout.marginWidth= 0;
1363 layout.marginHeight= 0;
1364 }
1365 return layout;
1366 }
1367
1368 private static final IWorkingSet[] EMPTY_WORKING_SET_ARRAY = new IWorkingSet[0];
1369
1370 private IWorkingSet[] getSelectedWorkingSet(IStructuredSelection selection, IWorkbenchPart activePart) {
1371 IWorkingSet[] selected= getSelectedWorkingSet(selection);
1372 if (selected != null && selected.length > 0) {
1373 for (int i= 0; i < selected.length; i++) {
1374 if (!isValidWorkingSet(selected[i]))
1375 return EMPTY_WORKING_SET_ARRAY;
1376 }
1377 return selected;
1378 }
1379
1380 if (!(activePart instanceof PackageExplorerPart))
1381 return EMPTY_WORKING_SET_ARRAY;
1382
1383 PackageExplorerPart explorerPart= (PackageExplorerPart) activePart;
1384 if (explorerPart.getRootMode() == PackageExplorerPart.PROJECTS_AS_ROOTS) {
1385 //Get active filter
1386 IWorkingSet filterWorkingSet= explorerPart.getFilterWorkingSet();
1387 if (filterWorkingSet == null)
1388 return EMPTY_WORKING_SET_ARRAY;
1389
1390 if (!isValidWorkingSet(filterWorkingSet))
1391 return EMPTY_WORKING_SET_ARRAY;
1392
1393 return new IWorkingSet[] {filterWorkingSet};
1394 } else {
1395 //If we have been gone into a working set return the working set
1396 Object input= explorerPart.getViewPartInput();
1397 if (!(input instanceof IWorkingSet))
1398 return EMPTY_WORKING_SET_ARRAY;
1399
1400 IWorkingSet workingSet= (IWorkingSet)input;
1401 if (!isValidWorkingSet(workingSet))
1402 return EMPTY_WORKING_SET_ARRAY;
1403
1404 return new IWorkingSet[] {workingSet};
1405 }
1406 }
1407
1408 private IWorkingSet[] getSelectedWorkingSet(IStructuredSelection selection) {
1409 if (!(selection instanceof ITreeSelection))
1410 return EMPTY_WORKING_SET_ARRAY;
1411
1412 ITreeSelection treeSelection= (ITreeSelection) selection;
1413 if (treeSelection.isEmpty())
1414 return EMPTY_WORKING_SET_ARRAY;
1415
1416 List<?> elements= treeSelection.toList();
1417 if (elements.size() == 1) {
1418 Object element= elements.get(0);
1419 TreePath[] paths= treeSelection.getPathsFor(element);
1420 if (paths.length != 1)
1421 return EMPTY_WORKING_SET_ARRAY;
1422
1423 TreePath path= paths[0];
1424 if (path.getSegmentCount() == 0)
1425 return EMPTY_WORKING_SET_ARRAY;
1426
1427 Object candidate= path.getSegment(0);
1428 if (!(candidate instanceof IWorkingSet))
1429 return EMPTY_WORKING_SET_ARRAY;
1430
1431 IWorkingSet workingSetCandidate= (IWorkingSet) candidate;
1432 if (isValidWorkingSet(workingSetCandidate))
1433 return new IWorkingSet[] { workingSetCandidate };
1434
1435 return EMPTY_WORKING_SET_ARRAY;
1436 }
1437
1438 ArrayList<IWorkingSet> result= new ArrayList<IWorkingSet>();
1439 for (Iterator<?> iterator= elements.iterator(); iterator.hasNext();) {
1440 Object element= iterator.next();
1441 if (element instanceof IWorkingSet && isValidWorkingSet((IWorkingSet) element)) {
1442 result.add((IWorkingSet) element);
1443 }
1444 }
1445 return result.toArray(new IWorkingSet[result.size()]);
1446 }
1447
1448
1449 private static boolean isValidWorkingSet(IWorkingSet workingSet) {
1450 String id= workingSet.getId();
1451 if (!IWorkingSetIDs.JAVA.equals(id) && !IWorkingSetIDs.RESOURCE.equals(id))
1452 return false;
1453
1454 if (workingSet.isAggregateWorkingSet())
1455 return false;
1456
1457 return true;
1458 }
1459
1460
1461}