1 13 package org.eclipse.ui.internal.ide.dialogs; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IProjectDescription; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IWorkspace; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.jface.wizard.WizardPage; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.events.SelectionListener; 29 import org.eclipse.swt.graphics.Font; 30 import org.eclipse.swt.layout.GridData; 31 import org.eclipse.swt.layout.GridLayout; 32 import org.eclipse.swt.widgets.Button; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.DirectoryDialog; 35 import org.eclipse.swt.widgets.Event; 36 import org.eclipse.swt.widgets.Label; 37 import org.eclipse.swt.widgets.Listener; 38 import org.eclipse.swt.widgets.Text; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 41 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 42 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 43 44 56 public class WizardNewProjectNameAndLocationPage extends WizardPage { 57 private boolean useDefaults = true; 59 60 private String initialProjectFieldValue; 62 63 private IPath initialLocationFieldValue; 64 65 private String customLocationFieldValue; 67 68 private Text projectNameField; 70 71 private Text locationPathField; 72 73 private Label locationLabel; 74 75 private Button browseButton; 76 77 private Listener nameModifyListener = new Listener() { 78 public void handleEvent(Event e) { 79 setLocationForSelection(); 80 setPageComplete(validatePage()); 81 } 82 }; 83 84 private Listener locationModifyListener = new Listener() { 85 public void handleEvent(Event e) { 86 setPageComplete(validatePage()); 87 } 88 }; 89 90 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 92 93 98 public WizardNewProjectNameAndLocationPage(String pageName) { 99 super(pageName); 100 setPageComplete(false); 101 initialLocationFieldValue = Platform.getLocation(); 102 customLocationFieldValue = ""; } 104 105 108 public boolean canFlipToNextPage() { 109 return isPageComplete(); 111 } 112 113 116 public void createControl(Composite parent) { 117 Composite composite = new Composite(parent, SWT.NULL); 118 composite.setLayout(new GridLayout()); 119 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 120 composite.setFont(parent.getFont()); 121 122 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, 123 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE); 124 125 createProjectNameGroup(composite); 126 createProjectLocationGroup(composite); 127 128 validatePage(); 129 130 setErrorMessage(null); 132 setMessage(null); 133 setControl(composite); 134 } 135 136 141 private final void createProjectLocationGroup(Composite parent) { 142 Font font = parent.getFont(); 143 Composite projectGroup = new Composite(parent, SWT.NONE); 145 GridLayout layout = new GridLayout(); 146 layout.numColumns = 3; 147 projectGroup.setLayout(layout); 148 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 149 projectGroup.setFont(font); 150 151 Label projectContentsLabel = new Label(projectGroup, SWT.NONE); 153 projectContentsLabel.setFont(font); 154 projectContentsLabel 155 .setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectContentsLabel); 156 157 GridData labelData = new GridData(); 158 labelData.horizontalSpan = 3; 159 projectContentsLabel.setLayoutData(labelData); 160 161 final Button useDefaultsButton = new Button(projectGroup, SWT.CHECK 162 | SWT.RIGHT); 163 useDefaultsButton.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_useDefaultLabel); 164 useDefaultsButton.setSelection(useDefaults); 165 useDefaultsButton.setFont(font); 166 167 GridData buttonData = new GridData(); 168 buttonData.horizontalSpan = 3; 169 useDefaultsButton.setLayoutData(buttonData); 170 171 createUserSpecifiedProjectLocationGroup(projectGroup, !useDefaults); 172 173 SelectionListener listener = new SelectionAdapter() { 174 public void widgetSelected(SelectionEvent e) { 175 useDefaults = useDefaultsButton.getSelection(); 176 browseButton.setEnabled(!useDefaults); 177 locationPathField.setEnabled(!useDefaults); 178 locationLabel.setEnabled(!useDefaults); 179 if (useDefaults) { 180 customLocationFieldValue = locationPathField.getText(); 181 setLocationForSelection(); 182 } else { 183 locationPathField.setText(customLocationFieldValue); 184 } 185 } 186 }; 187 useDefaultsButton.addSelectionListener(listener); 188 } 189 190 195 private final void createProjectNameGroup(Composite parent) { 196 Font font = parent.getFont(); 197 Composite projectGroup = new Composite(parent, SWT.NONE); 199 GridLayout layout = new GridLayout(); 200 layout.numColumns = 2; 201 projectGroup.setLayout(layout); 202 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 203 204 Label projectLabel = new Label(projectGroup, SWT.NONE); 206 projectLabel.setFont(font); 207 projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel); 208 209 projectNameField = new Text(projectGroup, SWT.BORDER); 211 GridData data = new GridData(GridData.FILL_HORIZONTAL); 212 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 213 projectNameField.setLayoutData(data); 214 projectNameField.setFont(font); 215 216 if (initialProjectFieldValue != null) { 219 projectNameField.setText(initialProjectFieldValue); 220 } 221 projectNameField.addListener(SWT.Modify, nameModifyListener); 222 } 223 224 230 private void createUserSpecifiedProjectLocationGroup( 231 Composite projectGroup, boolean enabled) { 232 Font font = projectGroup.getFont(); 233 locationLabel = new Label(projectGroup, SWT.NONE); 235 locationLabel.setFont(font); 236 locationLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_locationLabel); 237 locationLabel.setEnabled(enabled); 238 239 locationPathField = new Text(projectGroup, SWT.BORDER); 241 GridData data = new GridData(GridData.FILL_HORIZONTAL); 242 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 243 locationPathField.setLayoutData(data); 244 locationPathField.setFont(font); 245 locationPathField.setEnabled(enabled); 246 247 browseButton = new Button(projectGroup, SWT.PUSH); 249 browseButton.setFont(font); 250 browseButton.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_browseLabel); 251 browseButton.addSelectionListener(new SelectionAdapter() { 252 public void widgetSelected(SelectionEvent event) { 253 handleLocationBrowseButtonPressed(); 254 } 255 }); 256 257 browseButton.setEnabled(enabled); 258 259 if (initialLocationFieldValue != null) { 262 locationPathField.setText(initialLocationFieldValue.toOSString()); 263 } 264 locationPathField.addListener(SWT.Modify, locationModifyListener); 265 } 266 267 274 IPath getLocationPath() { 275 if (useDefaults) { 276 return initialLocationFieldValue; 277 } 278 279 return new Path(getProjectLocationFieldValue()); 280 } 281 282 291 IProject getProjectHandle() { 292 return ResourcesPlugin.getWorkspace().getRoot().getProject( 293 getProjectName()); 294 } 295 296 303 String getProjectName() { 304 if (projectNameField == null) { 305 return initialProjectFieldValue; 306 } 307 308 return getProjectNameFieldValue(); 309 } 310 311 317 private String getProjectNameFieldValue() { 318 if (projectNameField == null) { 319 return IDEResourceInfoUtils.EMPTY_STRING; 320 } 321 return projectNameField.getText().trim(); 322 } 323 324 330 private String getProjectLocationFieldValue() { 331 if (locationPathField == null) { 332 return IDEResourceInfoUtils.EMPTY_STRING; 333 } 334 return locationPathField.getText().trim(); 335 } 336 337 340 private void handleLocationBrowseButtonPressed() { 341 DirectoryDialog dialog = new DirectoryDialog(locationPathField 342 .getShell()); 343 dialog.setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_directoryLabel); 344 345 String dirName = getProjectLocationFieldValue(); 346 if (dirName.length() > 0) { 347 if (IDEResourceInfoUtils.exists(dirName)) { 348 dialog.setFilterPath(new Path(dirName).toOSString()); 349 } 350 } 351 352 String selectedDirectory = dialog.open(); 353 if (selectedDirectory != null) { 354 customLocationFieldValue = selectedDirectory; 355 locationPathField.setText(customLocationFieldValue); 356 } 357 } 358 359 363 private boolean isExistingProjectLocation() { 364 IPath path = getLocationPath(); 365 path = path.append(IProjectDescription.DESCRIPTION_FILE_NAME); 366 return path.toFile().exists(); 367 } 368 369 377 void setInitialProjectName(String name) { 378 if (name == null) { 379 initialProjectFieldValue = null; 380 } else { 381 initialProjectFieldValue = name.trim(); 382 } 383 } 384 385 388 private void setLocationForSelection() { 389 if (useDefaults) { 390 IPath defaultPath = Platform.getLocation().append( 391 getProjectNameFieldValue()); 392 locationPathField.setText(defaultPath.toOSString()); 393 } 394 } 395 396 403 private boolean validatePage() { 404 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 405 406 String projectFieldContents = getProjectNameFieldValue(); 407 if (projectFieldContents.equals("")) { setErrorMessage(null); 409 setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty); 410 return false; 411 } 412 413 IStatus nameStatus = workspace.validateName(projectFieldContents, 414 IResource.PROJECT); 415 if (!nameStatus.isOK()) { 416 setErrorMessage(nameStatus.getMessage()); 417 return false; 418 } 419 420 String locationFieldContents = getProjectLocationFieldValue(); 421 422 if (locationFieldContents.equals("")) { setErrorMessage(null); 424 setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationEmpty); 425 return false; 426 } 427 428 IPath path = new Path(""); if (!path.isValidPath(locationFieldContents)) { 430 setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_locationError); 431 return false; 432 } 433 if (!useDefaults 434 && Platform.getLocation().isPrefixOf( 435 new Path(locationFieldContents))) { 436 setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_defaultLocationError); 437 return false; 438 } 439 440 if (getProjectHandle().exists()) { 441 setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage); 442 return false; 443 } 444 445 if (isExistingProjectLocation()) { 446 setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationExistsMessage); 447 return false; 448 } 449 450 setErrorMessage(null); 451 setMessage(null); 452 return true; 453 } 454 455 458 public void setVisible(boolean visible) { 459 super.setVisible(visible); 460 if (visible) { 461 projectNameField.setFocus(); 462 } 463 } 464 465 } 466 | Popular Tags |