1 11 package org.eclipse.jdt.internal.ui.wizards; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Observable ; 21 import java.util.Observer ; 22 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.core.runtime.Platform; 27 28 import org.eclipse.core.resources.IProject; 29 import org.eclipse.core.resources.IResource; 30 import org.eclipse.core.resources.IWorkspace; 31 import org.eclipse.core.resources.ResourcesPlugin; 32 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionEvent; 35 import org.eclipse.swt.events.SelectionListener; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Combo; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.DirectoryDialog; 41 import org.eclipse.swt.widgets.Group; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.swt.widgets.Link; 44 45 import org.eclipse.jface.dialogs.Dialog; 46 import org.eclipse.jface.dialogs.IDialogConstants; 47 import org.eclipse.jface.dialogs.IDialogSettings; 48 import org.eclipse.jface.util.Policy; 49 import org.eclipse.jface.wizard.WizardPage; 50 51 import org.eclipse.ui.IWorkingSet; 52 import org.eclipse.ui.PlatformUI; 53 import org.eclipse.ui.dialogs.PreferencesUtil; 54 55 import org.eclipse.jdt.core.JavaCore; 56 57 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 58 import org.eclipse.jdt.internal.corext.util.Messages; 59 60 import org.eclipse.jdt.launching.IVMInstall; 61 import org.eclipse.jdt.launching.IVMInstall2; 62 import org.eclipse.jdt.launching.IVMInstallType; 63 import org.eclipse.jdt.launching.JavaRuntime; 64 import org.eclipse.jdt.launching.VMStandin; 65 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 66 67 import org.eclipse.jdt.ui.JavaUI; 68 import org.eclipse.jdt.ui.PreferenceConstants; 69 70 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 71 import org.eclipse.jdt.internal.ui.JavaPlugin; 72 import org.eclipse.jdt.internal.ui.preferences.CompliancePreferencePage; 73 import org.eclipse.jdt.internal.ui.preferences.NewJavaProjectPreferencePage; 74 import org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage; 75 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport; 76 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField; 77 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 78 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 79 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 80 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 81 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 82 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 83 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 84 import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater; 85 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetConfigurationBlock; 86 87 90 public class JavaProjectWizardFirstPage extends WizardPage { 91 92 96 private final class NameGroup extends Observable implements IDialogFieldListener { 97 98 protected final StringDialogField fNameField; 99 100 public NameGroup(Composite composite, String initialName) { 101 final Composite nameComposite= new Composite(composite, SWT.NONE); 102 nameComposite.setFont(composite.getFont()); 103 nameComposite.setLayout(initGridLayout(new GridLayout(2, false), false)); 104 nameComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 105 106 fNameField= new StringDialogField(); 108 fNameField.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_NameGroup_label_text); 109 fNameField.setDialogFieldListener(this); 110 111 setName(initialName); 112 113 fNameField.doFillIntoGrid(nameComposite, 2); 114 LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null)); 115 } 116 117 protected void fireEvent() { 118 setChanged(); 119 notifyObservers(); 120 } 121 122 public String getName() { 123 return fNameField.getText().trim(); 124 } 125 126 public void postSetFocus() { 127 fNameField.postSetFocusOnDialogField(getShell().getDisplay()); 128 } 129 130 public void setName(String name) { 131 fNameField.setText(name); 132 } 133 134 137 public void dialogFieldChanged(DialogField field) { 138 fireEvent(); 139 } 140 141 } 142 143 148 private final class LocationGroup extends Observable implements Observer , IStringButtonAdapter, IDialogFieldListener { 149 150 protected final SelectionButtonDialogField fWorkspaceRadio; 151 protected final SelectionButtonDialogField fExternalRadio; 152 protected final StringButtonDialogField fLocation; 153 154 private String fPreviousExternalLocation; 155 156 private static final String DIALOGSTORE_LAST_EXTERNAL_LOC= JavaUI.ID_PLUGIN + ".last.external.project"; 158 public LocationGroup(Composite composite) { 159 160 final int numColumns= 3; 161 162 final Group group= new Group(composite, SWT.NONE); 163 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 164 group.setLayout(initGridLayout(new GridLayout(numColumns, false), true)); 165 group.setText(NewWizardMessages.JavaProjectWizardFirstPage_LocationGroup_title); 166 167 fWorkspaceRadio= new SelectionButtonDialogField(SWT.RADIO); 168 fWorkspaceRadio.setDialogFieldListener(this); 169 fWorkspaceRadio.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_LocationGroup_workspace_desc); 170 171 fExternalRadio= new SelectionButtonDialogField(SWT.RADIO); 172 fExternalRadio.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_LocationGroup_external_desc); 173 174 fLocation= new StringButtonDialogField(this); 175 fLocation.setDialogFieldListener(this); 176 fLocation.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_LocationGroup_locationLabel_desc); 177 fLocation.setButtonLabel(NewWizardMessages.JavaProjectWizardFirstPage_LocationGroup_browseButton_desc); 178 179 fExternalRadio.attachDialogField(fLocation); 180 181 fWorkspaceRadio.setSelection(true); 182 fExternalRadio.setSelection(false); 183 184 fPreviousExternalLocation= ""; 186 fWorkspaceRadio.doFillIntoGrid(group, numColumns); 187 fExternalRadio.doFillIntoGrid(group, numColumns); 188 fLocation.doFillIntoGrid(group, numColumns); 189 LayoutUtil.setHorizontalGrabbing(fLocation.getTextControl(null)); 190 } 191 192 protected void fireEvent() { 193 setChanged(); 194 notifyObservers(); 195 } 196 197 protected String getDefaultPath(String name) { 198 final IPath path= Platform.getLocation().append(name); 199 return path.toOSString(); 200 } 201 202 208 public void update(Observable o, Object arg) { 209 if (isInWorkspace()) { 210 fLocation.setText(getDefaultPath(fNameGroup.getName())); 211 } 212 fireEvent(); 213 } 214 215 public IPath getLocation() { 216 if (isInWorkspace()) { 217 return Platform.getLocation(); 218 } 219 return Path.fromOSString(fLocation.getText().trim()); 220 } 221 222 public boolean isInWorkspace() { 223 return fWorkspaceRadio.isSelected(); 224 } 225 226 229 public void changeControlPressed(DialogField field) { 230 final DirectoryDialog dialog= new DirectoryDialog(getShell()); 231 dialog.setMessage(NewWizardMessages.JavaProjectWizardFirstPage_directory_message); 232 String directoryName = fLocation.getText().trim(); 233 if (directoryName.length() == 0) { 234 String prevLocation= JavaPlugin.getDefault().getDialogSettings().get(DIALOGSTORE_LAST_EXTERNAL_LOC); 235 if (prevLocation != null) { 236 directoryName= prevLocation; 237 } 238 } 239 240 if (directoryName.length() > 0) { 241 final File path = new File (directoryName); 242 if (path.exists()) 243 dialog.setFilterPath(directoryName); 244 } 245 final String selectedDirectory = dialog.open(); 246 if (selectedDirectory != null) { 247 fLocation.setText(selectedDirectory); 248 JavaPlugin.getDefault().getDialogSettings().put(DIALOGSTORE_LAST_EXTERNAL_LOC, selectedDirectory); 249 } 250 } 251 252 255 public void dialogFieldChanged(DialogField field) { 256 if (field == fWorkspaceRadio) { 257 final boolean checked= fWorkspaceRadio.isSelected(); 258 if (checked) { 259 fPreviousExternalLocation= fLocation.getText(); 260 fLocation.setText(getDefaultPath(fNameGroup.getName())); 261 } else { 262 fLocation.setText(fPreviousExternalLocation); 263 } 264 } 265 fireEvent(); 266 } 267 } 268 269 272 private final class LayoutGroup implements Observer , SelectionListener { 273 274 private final SelectionButtonDialogField fStdRadio, fSrcBinRadio; 275 private final Group fGroup; 276 private final Link fPreferenceLink; 277 278 public LayoutGroup(Composite composite) { 279 280 fGroup= new Group(composite, SWT.NONE); 281 fGroup.setFont(composite.getFont()); 282 fGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 283 fGroup.setLayout(initGridLayout(new GridLayout(3, false), true)); 284 fGroup.setText(NewWizardMessages.JavaProjectWizardFirstPage_LayoutGroup_title); 285 286 fStdRadio= new SelectionButtonDialogField(SWT.RADIO); 287 fStdRadio.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_LayoutGroup_option_oneFolder); 288 289 fSrcBinRadio= new SelectionButtonDialogField(SWT.RADIO); 290 fSrcBinRadio.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_LayoutGroup_option_separateFolders); 291 292 fStdRadio.doFillIntoGrid(fGroup, 3); 293 LayoutUtil.setHorizontalGrabbing(fStdRadio.getSelectionButton(null)); 294 295 fSrcBinRadio.doFillIntoGrid(fGroup, 2); 296 297 fPreferenceLink= new Link(fGroup, SWT.NONE); 298 fPreferenceLink.setText(NewWizardMessages.JavaProjectWizardFirstPage_LayoutGroup_link_description); 299 fPreferenceLink.setLayoutData(new GridData(GridData.END, GridData.END, false, false)); 300 fPreferenceLink.addSelectionListener(this); 301 302 boolean useSrcBin= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ); 303 fSrcBinRadio.setSelection(useSrcBin); 304 fStdRadio.setSelection(!useSrcBin); 305 } 306 307 public void update(Observable o, Object arg) { 308 final boolean detect= fDetectGroup.mustDetect(); 309 fStdRadio.setEnabled(!detect); 310 fSrcBinRadio.setEnabled(!detect); 311 fPreferenceLink.setEnabled(!detect); 312 fGroup.setEnabled(!detect); 313 } 314 315 public boolean isSrcBin() { 316 return fSrcBinRadio.isSelected(); 317 } 318 319 322 public void widgetSelected(SelectionEvent e) { 323 widgetDefaultSelected(e); 324 } 325 326 329 public void widgetDefaultSelected(SelectionEvent e) { 330 String id= NewJavaProjectPreferencePage.ID; 331 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String [] { id }, null).open(); 332 fDetectGroup.handlePossibleJVMChange(); 333 fJREGroup.handlePossibleJVMChange(); 334 } 335 } 336 337 private final class JREGroup implements Observer , SelectionListener, IDialogFieldListener { 338 339 private static final String LAST_SELECTED_EE_SETTINGS_KEY= JavaUI.ID_PLUGIN + ".last.selected.execution.enviroment"; private static final String LAST_SELECTED_JRE_SETTINGS_KEY= JavaUI.ID_PLUGIN + ".last.selected.project.jre"; private static final String LAST_SELECTED_JRE_KIND= JavaUI.ID_PLUGIN + ".last.selected.jre.kind"; 343 private static final int DEFAULT_JRE= 0; 344 private static final int PROJECT_JRE= 1; 345 private static final int EE_JRE= 2; 346 347 private final SelectionButtonDialogField fUseDefaultJRE, fUseProjectJRE, fUseEEJRE; 348 private final ComboDialogField fJRECombo; 349 private final ComboDialogField fEECombo; 350 private final Group fGroup; 351 private final Link fPreferenceLink; 352 private IVMInstall[] fInstalledJVMs; 353 private String [] fJRECompliance; 354 private IExecutionEnvironment[] fInstalledEEs; 355 private String [] fEECompliance; 356 357 public JREGroup(Composite composite) { 358 fGroup= new Group(composite, SWT.NONE); 359 fGroup.setFont(composite.getFont()); 360 fGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 361 fGroup.setLayout(initGridLayout(new GridLayout(3, false), true)); 362 fGroup.setText(NewWizardMessages.JavaProjectWizardFirstPage_JREGroup_title); 363 364 fUseDefaultJRE= new SelectionButtonDialogField(SWT.RADIO); 365 fUseDefaultJRE.setLabelText(getDefaultJVMLabel()); 366 fUseDefaultJRE.doFillIntoGrid(fGroup, 2); 367 368 fPreferenceLink= new Link(fGroup, SWT.NONE); 369 fPreferenceLink.setFont(fGroup.getFont()); 370 fPreferenceLink.setText(NewWizardMessages.JavaProjectWizardFirstPage_JREGroup_link_description); 371 fPreferenceLink.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false)); 372 fPreferenceLink.addSelectionListener(this); 373 374 fUseProjectJRE= new SelectionButtonDialogField(SWT.RADIO); 375 fUseProjectJRE.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_JREGroup_specific_compliance); 376 fUseProjectJRE.doFillIntoGrid(fGroup, 1); 377 378 fJRECombo= new ComboDialogField(SWT.READ_ONLY); 379 fillInstalledJREs(fJRECombo); 380 fJRECombo.setDialogFieldListener(this); 381 382 Combo comboControl= fJRECombo.getComboControl(fGroup); 383 comboControl.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); comboControl.setVisibleItemCount(30); 385 386 DialogField.createEmptySpace(fGroup); 387 388 fUseEEJRE= new SelectionButtonDialogField(SWT.RADIO); 389 fUseEEJRE.setLabelText(NewWizardMessages.JavaProjectWizardFirstPage_JREGroup_specific_EE); 390 fUseEEJRE.doFillIntoGrid(fGroup, 1); 391 392 fEECombo= new ComboDialogField(SWT.READ_ONLY); 393 fillExecutionEnvironments(fEECombo); 394 fEECombo.setDialogFieldListener(this); 395 396 Combo eeComboControl= fEECombo.getComboControl(fGroup); 397 eeComboControl.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); eeComboControl.setVisibleItemCount(30); 399 400 DialogField.createEmptySpace(fGroup); 401 402 switch (getLastSelectedJREKind()) { 403 case DEFAULT_JRE: 404 fUseDefaultJRE.setSelection(true); 405 break; 406 case PROJECT_JRE: 407 fUseProjectJRE.setSelection(true); 408 break; 409 case EE_JRE: 410 fUseEEJRE.setSelection(true); 411 break; 412 } 413 414 fJRECombo.setEnabled(fUseProjectJRE.isSelected()); 415 fEECombo.setEnabled(fUseEEJRE.isSelected()); 416 417 fUseDefaultJRE.setDialogFieldListener(this); 418 fUseProjectJRE.setDialogFieldListener(this); 419 fUseEEJRE.setDialogFieldListener(this); 420 } 421 422 private void fillInstalledJREs(ComboDialogField comboField) { 423 String selectedItem= getLastSelectedJRE(); 424 int selectionIndex= -1; 425 if (fUseProjectJRE.isSelected()) { 426 selectionIndex= comboField.getSelectionIndex(); 427 if (selectionIndex != -1) { selectedItem= comboField.getItems()[selectionIndex]; 429 } 430 } 431 432 fInstalledJVMs= getWorkspaceJREs(); 433 Arrays.sort(fInstalledJVMs, new Comparator () { 434 435 public int compare(Object arg0, Object arg1) { 436 IVMInstall i0= (IVMInstall)arg0; 437 IVMInstall i1= (IVMInstall)arg1; 438 if (i1 instanceof IVMInstall2 && i0 instanceof IVMInstall2) { 439 String cc0= JavaModelUtil.getCompilerCompliance((IVMInstall2) i0, JavaCore.VERSION_1_4); 440 String cc1= JavaModelUtil.getCompilerCompliance((IVMInstall2) i1, JavaCore.VERSION_1_4); 441 int result= cc1.compareTo(cc0); 442 if (result != 0) 443 return result; 444 } 445 return Policy.getComparator().compare(i0.getName(), i1.getName()); 446 } 447 448 }); 449 selectionIndex= -1; String [] jreLabels= new String [fInstalledJVMs.length]; 451 fJRECompliance= new String [fInstalledJVMs.length]; 452 for (int i= 0; i < fInstalledJVMs.length; i++) { 453 jreLabels[i]= fInstalledJVMs[i].getName(); 454 if (selectedItem != null && jreLabels[i].equals(selectedItem)) { 455 selectionIndex= i; 456 } 457 if (fInstalledJVMs[i] instanceof IVMInstall2) { 458 fJRECompliance[i]= JavaModelUtil.getCompilerCompliance((IVMInstall2) fInstalledJVMs[i], JavaCore.VERSION_1_4); 459 } else { 460 fJRECompliance[i]= JavaCore.VERSION_1_4; 461 } 462 } 463 comboField.setItems(jreLabels); 464 if (selectionIndex == -1) { 465 comboField.selectItem(getDefaultJVMName()); 466 } else { 467 comboField.selectItem(selectedItem); 468 } 469 } 470 471 private void fillExecutionEnvironments(ComboDialogField comboField) { 472 String selectedItem= getLastSelectedEE(); 473 int selectionIndex= -1; 474 if (fUseEEJRE.isSelected()) { 475 selectionIndex= comboField.getSelectionIndex(); 476 if (selectionIndex != -1) { selectedItem= comboField.getItems()[selectionIndex]; 478 } 479 } 480 481 fInstalledEEs= JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); 482 Arrays.sort(fInstalledEEs, new Comparator () { 483 public int compare(Object arg0, Object arg1) { 484 return Policy.getComparator().compare(((IExecutionEnvironment)arg0).getId(), ((IExecutionEnvironment)arg1).getId()); 485 } 486 }); 487 selectionIndex= -1; String [] eeLabels= new String [fInstalledEEs.length]; 489 fEECompliance= new String [fInstalledEEs.length]; 490 for (int i= 0; i < fInstalledEEs.length; i++) { 491 eeLabels[i]= fInstalledEEs[i].getId(); 492 if (selectedItem != null && eeLabels[i].equals(selectedItem)) { 493 selectionIndex= i; 494 } 495 fEECompliance[i]= JavaModelUtil.getExecutionEnvironmentCompliance(fInstalledEEs[i]); 496 } 497 comboField.setItems(eeLabels); 498 if (selectionIndex == -1) { 499 comboField.selectItem(getDefaultEEName()); 500 } else { 501 comboField.selectItem(selectedItem); 502 } 503 } 504 505 private IVMInstall[] getWorkspaceJREs() { 506 List standins = new ArrayList (); 507 IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); 508 for (int i = 0; i < types.length; i++) { 509 IVMInstallType type = types[i]; 510 IVMInstall[] installs = type.getVMInstalls(); 511 for (int j = 0; j < installs.length; j++) { 512 IVMInstall install = installs[j]; 513 standins.add(new VMStandin(install)); 514 } 515 } 516 return ((IVMInstall[])standins.toArray(new IVMInstall[standins.size()])); 517 } 518 519 private String getDefaultJVMName() { 520 IVMInstall install= JavaRuntime.getDefaultVMInstall(); 521 if (install != null) { 522 return install.getName(); 523 } else { 524 return NewWizardMessages.JavaProjectWizardFirstPage_UnknownDefaultJRE_name; 525 } 526 } 527 528 private String getDefaultEEName() { 529 IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); 530 531 IExecutionEnvironment[] environments= JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); 532 if (defaultVM != null) { 533 for (int i= 0; i < environments.length; i++) { 534 IVMInstall eeDefaultVM= environments[i].getDefaultVM(); 535 if (eeDefaultVM != null && defaultVM.getId().equals(eeDefaultVM.getId())) 536 return environments[i].getId(); 537 } 538 } 539 540 String defaultCC; 541 if (defaultVM instanceof IVMInstall2) { 542 defaultCC= JavaModelUtil.getCompilerCompliance((IVMInstall2)defaultVM, JavaCore.VERSION_1_4); 543 } else { 544 defaultCC= JavaCore.VERSION_1_4; 545 } 546 547 for (int i= 0; i < environments.length; i++) { 548 String eeCompliance= JavaModelUtil.getExecutionEnvironmentCompliance(environments[i]); 549 if (defaultCC.endsWith(eeCompliance)) 550 return environments[i].getId(); 551 } 552 553 return "J2SE-1.5"; } 555 556 private String getDefaultJVMLabel() { 557 return Messages.format(NewWizardMessages.JavaProjectWizardFirstPage_JREGroup_default_compliance, getDefaultJVMName()); 558 } 559 560 public void update(Observable o, Object arg) { 561 updateEnableState(); 562 } 563 564 private void updateEnableState() { 565 final boolean detect= fDetectGroup.mustDetect(); 566 fUseDefaultJRE.setEnabled(!detect); 567 fUseProjectJRE.setEnabled(!detect); 568 fUseEEJRE.setEnabled(!detect); 569 fJRECombo.setEnabled(!detect && fUseProjectJRE.isSelected()); 570 fEECombo.setEnabled(!detect && fUseEEJRE.isSelected()); 571 fPreferenceLink.setEnabled(!detect); 572 fGroup.setEnabled(!detect); 573 } 574 575 578 public void widgetSelected(SelectionEvent e) { 579 widgetDefaultSelected(e); 580 } 581 582 585 public void widgetDefaultSelected(SelectionEvent e) { 586 String jreID= BuildPathSupport.JRE_PREF_PAGE_ID; 587 String complianceId= CompliancePreferencePage.PREF_ID; 588 Map data= new HashMap (); 589 data.put(PropertyAndPreferencePage.DATA_NO_LINK, Boolean.TRUE); 590 PreferencesUtil.createPreferenceDialogOn(getShell(), jreID, new String [] { jreID, complianceId }, data).open(); 591 592 handlePossibleJVMChange(); 593 fDetectGroup.handlePossibleJVMChange(); 594 } 595 596 public void handlePossibleJVMChange() { 597 fUseDefaultJRE.setLabelText(getDefaultJVMLabel()); 598 fillInstalledJREs(fJRECombo); 599 fillExecutionEnvironments(fEECombo); 600 } 601 602 603 606 public void dialogFieldChanged(DialogField field) { 607 updateEnableState(); 608 fDetectGroup.handlePossibleJVMChange(); 609 if (field == fJRECombo) { 610 if (fUseProjectJRE.isSelected()) { 611 612 613 storeSelectionValue(fJRECombo, LAST_SELECTED_JRE_SETTINGS_KEY); 614 } 615 } else if (field == fEECombo) { 616 if (fUseEEJRE.isSelected()) { 617 storeSelectionValue(fEECombo, LAST_SELECTED_EE_SETTINGS_KEY); 618 } 619 } else if (field == fUseDefaultJRE) { 620 if (fUseDefaultJRE.isSelected()) { 621 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND, DEFAULT_JRE); 622 } 623 } else if (field == fUseProjectJRE) { 624 if (fUseProjectJRE.isSelected()) { 625 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND, PROJECT_JRE); 626 } 627 } else if (field == fUseEEJRE) { 628 if (fUseEEJRE.isSelected()) { 629 JavaPlugin.getDefault().getDialogSettings().put(LAST_SELECTED_JRE_KIND, EE_JRE); 630 } 631 } 632 } 633 634 private void storeSelectionValue(ComboDialogField combo, String preferenceKey) { 635 int index= combo.getSelectionIndex(); 636 if (index == -1) 637 return; 638 639 String item= combo.getItems()[index]; 640 JavaPlugin.getDefault().getDialogSettings().put(preferenceKey, item); 641 } 642 643 private int getLastSelectedJREKind() { 644 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); 645 if (settings.get(LAST_SELECTED_JRE_KIND) == null) 646 return DEFAULT_JRE; 647 648 return settings.getInt(LAST_SELECTED_JRE_KIND); 649 } 650 651 private String getLastSelectedEE() { 652 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); 653 return settings.get(LAST_SELECTED_EE_SETTINGS_KEY); 654 } 655 656 private String getLastSelectedJRE() { 657 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); 658 return settings.get(LAST_SELECTED_JRE_SETTINGS_KEY); 659 } 660 661 public IVMInstall getSelectedJVM() { 662 if (fUseProjectJRE.isSelected()) { 663 int index= fJRECombo.getSelectionIndex(); 664 if (index >= 0 && index < fInstalledJVMs.length) { return fInstalledJVMs[index]; 666 } 667 } else if (fUseEEJRE.isSelected()) { 668 669 } 670 return null; 671 } 672 673 public IPath getJREContainerPath() { 674 if (fUseProjectJRE.isSelected()) { 675 int index= fJRECombo.getSelectionIndex(); 676 if (index >= 0 && index < fInstalledJVMs.length) { return JavaRuntime.newJREContainerPath(fInstalledJVMs[index]); 678 } 679 } else if (fUseEEJRE.isSelected()) { 680 int index= fEECombo.getSelectionIndex(); 681 if (index >= 0 && index < fInstalledEEs.length) { return JavaRuntime.newJREContainerPath(fInstalledEEs[index]); 683 } 684 } 685 return null; 686 } 687 688 public String getSelectedCompilerCompliance() { 689 if (fUseProjectJRE.isSelected()) { 690 int index= fJRECombo.getSelectionIndex(); 691 if (index >= 0 && index < fJRECompliance.length) { return fJRECompliance[index]; 693 } 694 } else if (fUseEEJRE.isSelected()) { 695 int index= fEECombo.getSelectionIndex(); 696 if (index >= 0 && index < fEECompliance.length) { return fEECompliance[index]; 698 } 699 } 700 return null; 701 } 702 703 704 } 705 706 private final class WorkingSetGroup { 707 708 private WorkingSetConfigurationBlock fWorkingSetBlock; 709 710 public WorkingSetGroup(Composite composite, IWorkingSet[] initialWorkingSets) { 711 Group workingSetGroup= new Group(composite, SWT.NONE); 712 workingSetGroup.setFont(composite.getFont()); 713 workingSetGroup.setText(NewWizardMessages.JavaProjectWizardFirstPage_WorkingSets_group); 714 workingSetGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); 715 workingSetGroup.setLayout(new GridLayout(1, false)); 716 717 String [] workingSetIds= new String [] {JavaWorkingSetUpdater.ID, "org.eclipse.ui.resourceWorkingSetPage"}; fWorkingSetBlock= new WorkingSetConfigurationBlock(workingSetIds, NewWizardMessages.JavaProjectWizardFirstPage_EnableWorkingSet_button, JavaPlugin.getDefault().getDialogSettings()); 719 fWorkingSetBlock.setDialogMessage(NewWizardMessages.JavaProjectWizardFirstPage_WorkingSetSelection_message); 720 fWorkingSetBlock.setSelection(initialWorkingSets); 721 fWorkingSetBlock.createContent(workingSetGroup); 722 } 723 724 public IWorkingSet[] getSelectedWorkingSets() { 725 return fWorkingSetBlock.getSelectedWorkingSets(); 726 } 727 } 728 729 732 private final class DetectGroup extends Observable implements Observer , SelectionListener { 733 734 private final Link fHintText; 735 private Label fIcon; 736 private boolean fDetect; 737 738 public DetectGroup(Composite parent) { 739 740 Composite composite= new Composite(parent, SWT.NONE); 741 composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); 742 GridLayout layout= new GridLayout(2, false); 743 layout.horizontalSpacing= 10; 744 composite.setLayout(layout); 745 746 fIcon= new Label(composite, SWT.LEFT); 747 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 748 GridData gridData= new GridData(SWT.LEFT, SWT.CENTER, false, false); 749 fIcon.setLayoutData(gridData); 750 751 fHintText= new Link(composite, SWT.WRAP); 752 fHintText.setFont(composite.getFont()); 753 fHintText.addSelectionListener(this); 754 gridData= new GridData(GridData.FILL, SWT.FILL, true, true); 755 gridData.widthHint= convertWidthInCharsToPixels(50); 756 gridData.heightHint= convertHeightInCharsToPixels(3); 757 fHintText.setLayoutData(gridData); 758 759 handlePossibleJVMChange(); 760 } 761 762 public void handlePossibleJVMChange() { 763 764 if (JavaRuntime.getDefaultVMInstall() == null) { 765 fHintText.setText(NewWizardMessages.JavaProjectWizardFirstPage_NoJREFound_link); 766 fHintText.setVisible(true); 767 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 768 fIcon.setVisible(true); 769 return; 770 } 771 772 String selectedCompliance= fJREGroup.getSelectedCompilerCompliance(); 773 if (selectedCompliance != null) { 774 String defaultCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE); 775 if (selectedCompliance.equals(defaultCompliance)) { 776 fHintText.setVisible(false); 777 fIcon.setVisible(false); 778 } else { 779 fHintText.setText(Messages.format(NewWizardMessages.JavaProjectWizardFirstPage_DetectGroup_differendWorkspaceCC_message, new String [] {defaultCompliance, selectedCompliance})); 780 fHintText.setVisible(true); 781 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO)); 782 fIcon.setVisible(true); 783 } 784 return; 785 } 786 787 selectedCompliance= JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE); 788 IVMInstall selectedJVM= fJREGroup.getSelectedJVM(); 789 if (selectedJVM == null) { 790 selectedJVM= JavaRuntime.getDefaultVMInstall(); 791 } 792 String jvmCompliance= JavaCore.VERSION_1_4; 793 if (selectedJVM instanceof IVMInstall2) { 794 jvmCompliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) selectedJVM, JavaCore.VERSION_1_4); 795 } 796 if (!selectedCompliance.equals(jvmCompliance) && (JavaModelUtil.is50OrHigher(selectedCompliance) || JavaModelUtil.is50OrHigher(jvmCompliance))) { 797 if (selectedCompliance.equals(JavaCore.VERSION_1_5)) 798 selectedCompliance= "5.0"; else if (selectedCompliance.equals(JavaCore.VERSION_1_6)) 800 selectedCompliance= "6.0"; 802 fHintText.setText(Messages.format(NewWizardMessages.JavaProjectWizardFirstPage_DetectGroup_jre_message, new String [] {selectedCompliance, jvmCompliance})); 803 fHintText.setVisible(true); 804 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 805 fIcon.setVisible(true); 806 } else { 807 fHintText.setVisible(false); 808 fIcon.setVisible(false); 809 } 810 } 811 812 public void update(Observable o, Object arg) { 813 if (o instanceof LocationGroup) { 814 boolean oldDetectState= fDetect; 815 if (fLocationGroup.isInWorkspace()) { 816 fDetect= false; 817 } else { 818 final File directory= fLocationGroup.getLocation().toFile(); 819 fDetect= directory.isDirectory(); 820 } 821 822 if (oldDetectState != fDetect) { 823 setChanged(); 824 notifyObservers(); 825 826 if (fDetect) { 827 fHintText.setVisible(true); 828 fHintText.setText(NewWizardMessages.JavaProjectWizardFirstPage_DetectGroup_message); 829 fIcon.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_INFO)); 830 fIcon.setVisible(true); 831 } else { 832 handlePossibleJVMChange(); 833 } 834 } 835 } 836 } 837 838 public boolean mustDetect() { 839 return fDetect; 840 } 841 842 845 public void widgetSelected(SelectionEvent e) { 846 widgetDefaultSelected(e); 847 } 848 849 852 public void widgetDefaultSelected(SelectionEvent e) { 853 String jreID= BuildPathSupport.JRE_PREF_PAGE_ID; 854 String complianceId= CompliancePreferencePage.PREF_ID; 855 Map data= new HashMap (); 856 data.put(PropertyAndPreferencePage.DATA_NO_LINK, Boolean.TRUE); 857 String id= "JRE".equals(e.text) ? jreID : complianceId; PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String [] { jreID, complianceId }, data).open(); 859 860 fJREGroup.handlePossibleJVMChange(); 861 handlePossibleJVMChange(); 862 } 863 } 864 865 868 private final class Validator implements Observer { 869 870 public void update(Observable o, Object arg) { 871 872 final IWorkspace workspace= JavaPlugin.getWorkspace(); 873 874 final String name= fNameGroup.getName(); 875 876 if (name.length() == 0) { 878 setErrorMessage(null); 879 setMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_enterProjectName); 880 setPageComplete(false); 881 return; 882 } 883 884 final IStatus nameStatus= workspace.validateName(name, IResource.PROJECT); 886 if (!nameStatus.isOK()) { 887 setErrorMessage(nameStatus.getMessage()); 888 setPageComplete(false); 889 return; 890 } 891 892 final IProject handle= getProjectHandle(); 894 if (handle.exists()) { 895 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_projectAlreadyExists); 896 setPageComplete(false); 897 return; 898 } 899 900 final String location= fLocationGroup.getLocation().toOSString(); 901 902 if (location.length() == 0) { 904 setErrorMessage(null); 905 setMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_enterLocation); 906 setPageComplete(false); 907 return; 908 } 909 910 if (!Path.EMPTY.isValidPath(location)) { 912 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_invalidDirectory); 913 setPageComplete(false); 914 return; 915 } 916 917 IPath projectPath= Path.fromOSString(location); 918 if (!fLocationGroup.isInWorkspace()) { 920 if (!canCreate(projectPath.toFile())) { 921 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_cannotCreateAtExternalLocation); 922 setPageComplete(false); 923 return; 924 } 925 926 if (!Platform.getLocation().equals(projectPath) && Platform.getLocation().isPrefixOf(projectPath)) { 927 if (!Platform.getLocation().equals(projectPath.removeLastSegments(1))) { 928 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_notOnWorkspaceRoot); 929 setPageComplete(false); 930 return; 931 } 932 933 if (!projectPath.toFile().exists()) { 934 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_notExisingProjectOnWorkspaceRoot); 935 setPageComplete(false); 936 return; 937 } 938 939 String existingName= projectPath.lastSegment(); 940 if (!existingName.equals(fNameGroup.getName())) { 941 setErrorMessage(Messages.format(NewWizardMessages.JavaProjectWizardFirstPage_Message_invalidProjectNameForWorkspaceRoot, existingName)); 942 setPageComplete(false); 943 return; 944 } 945 } else { 946 final IStatus locationStatus= workspace.validateProjectLocation(handle, projectPath); 949 if (!locationStatus.isOK()) { 950 setErrorMessage(locationStatus.getMessage()); 951 setPageComplete(false); 952 return; 953 } 954 } 955 } else { 956 IPath projectFolder= projectPath.append(fNameGroup.getName()); 957 if (projectFolder.toFile().exists()) { 958 setErrorMessage(NewWizardMessages.JavaProjectWizardFirstPage_Message_existingFolderInWorkspace); 959 setPageComplete(false); 960 return; 961 } 962 } 963 964 setPageComplete(true); 965 966 setErrorMessage(null); 967 setMessage(null); 968 } 969 970 private boolean canCreate(File file) { 971 while (!file.exists()) { 972 file= file.getParentFile(); 973 if (file == null) 974 return false; 975 } 976 977 return file.canWrite(); 978 } 979 980 } 981 982 private NameGroup fNameGroup; 983 private LocationGroup fLocationGroup; 984 private LayoutGroup fLayoutGroup; 985 private JREGroup fJREGroup; 986 private DetectGroup fDetectGroup; 987 private Validator fValidator; 988 989 private String fInitialName; 990 991 private static final String PAGE_NAME= NewWizardMessages.JavaProjectWizardFirstPage_page_pageName; 992 private WorkingSetGroup fWorkingSetGroup; 993 private IWorkingSet[] fInitWorkingSets; 994 995 998 public JavaProjectWizardFirstPage() { 999 super(PAGE_NAME); 1000 setPageComplete(false); 1001 setTitle(NewWizardMessages.JavaProjectWizardFirstPage_page_title); 1002 setDescription(NewWizardMessages.JavaProjectWizardFirstPage_page_description); 1003 fInitialName= ""; initializeDefaultVM(); 1005 } 1006 1007 private void initializeDefaultVM() { 1008 JavaRuntime.getDefaultVMInstall(); 1009 } 1010 1011 public void setName(String name) { 1012 fInitialName= name; 1013 if (fNameGroup != null) { 1014 fNameGroup.setName(name); 1015 } 1016 } 1017 1018 public void createControl(Composite parent) { 1019 initializeDialogUnits(parent); 1020 1021 final Composite composite= new Composite(parent, SWT.NULL); 1022 composite.setFont(parent.getFont()); 1023 composite.setLayout(initGridLayout(new GridLayout(1, false), true)); 1024 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 1025 1026 fNameGroup= new NameGroup(composite, fInitialName); 1028 fLocationGroup= new LocationGroup(composite); 1029 fJREGroup= new JREGroup(composite); 1030 fLayoutGroup= new LayoutGroup(composite); 1031 fWorkingSetGroup= new WorkingSetGroup(composite, fInitWorkingSets); 1032 fDetectGroup= new DetectGroup(composite); 1033 1034 fNameGroup.addObserver(fLocationGroup); 1036 fDetectGroup.addObserver(fLayoutGroup); 1037 fDetectGroup.addObserver(fJREGroup); 1038 fLocationGroup.addObserver(fDetectGroup); 1039 1040 fNameGroup.notifyObservers(); 1042 1043 fValidator= new Validator(); 1045 fNameGroup.addObserver(fValidator); 1046 fLocationGroup.addObserver(fValidator); 1047 1048 setControl(composite); 1049 Dialog.applyDialogFont(composite); 1050 1051 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE); 1052 } 1053 1054 1066 public IPath getLocationPath() { 1067 return fLocationGroup.getLocation(); 1068 } 1069 1070 1071 1082 public IProject getProjectHandle() { 1083 return ResourcesPlugin.getWorkspace().getRoot().getProject(fNameGroup.getName()); 1084 } 1085 1086 public boolean isInWorkspace() { 1087 final String location= fLocationGroup.getLocation().toOSString(); 1088 IPath projectPath= Path.fromOSString(location); 1089 return Platform.getLocation().isPrefixOf(projectPath); 1090 } 1091 1092 public String getProjectName() { 1093 return fNameGroup.getName(); 1094 } 1095 1096 public boolean getDetect() { 1097 return fDetectGroup.mustDetect(); 1098 } 1099 1100 public boolean isSrcBin() { 1101 return fLayoutGroup.isSrcBin(); 1102 } 1103 1104 1108 public IPath getJREContainerPath() { 1109 return fJREGroup.getJREContainerPath(); 1110 } 1111 1112 1115 public String getCompilerCompliance() { 1116 return fJREGroup.getSelectedCompilerCompliance(); 1117 } 1118 1119 1122 public void setVisible(boolean visible) { 1123 super.setVisible(visible); 1124 if (visible) { 1125 fNameGroup.postSetFocus(); 1126 } 1127 } 1128 1129 1135 protected GridLayout initGridLayout(GridLayout layout, boolean margins) { 1136 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 1137 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 1138 if (margins) { 1139 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 1140 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 1141 } else { 1142 layout.marginWidth= 0; 1143 layout.marginHeight= 0; 1144 } 1145 return layout; 1146 } 1147 1148 1151 public void setWorkingSets(IWorkingSet[] workingSets) { 1152 fInitWorkingSets= workingSets; 1153 } 1154 1155 1158 public IWorkingSet[] getWorkingSets() { 1159 return fWorkingSetGroup.getSelectedWorkingSets(); 1160 } 1161} 1162 | Popular Tags |