1 package org.enhydra.kelp.eclipse.enhydraWizard; 2 3 import java.io.File ; 4 5 import org.eclipse.core.resources.IProject; 6 import org.eclipse.core.resources.IResource; 7 import org.eclipse.core.resources.IWorkspace; 8 import org.eclipse.core.resources.ResourcesPlugin; 9 import org.eclipse.core.runtime.IPath; 10 import org.eclipse.core.runtime.IStatus; 11 import org.eclipse.core.runtime.Path; 12 import org.eclipse.core.runtime.Platform; 13 import org.eclipse.jdt.core.dom.ThisExpression; 14 import org.eclipse.jface.resource.ImageDescriptor; 15 import org.eclipse.jface.wizard.WizardPage; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.events.SelectionListener; 20 import org.eclipse.swt.layout.GridData; 21 import org.eclipse.swt.layout.GridLayout; 22 import org.eclipse.swt.widgets.Button; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.DirectoryDialog; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.swt.widgets.Label; 27 import org.eclipse.swt.widgets.Listener; 28 import org.eclipse.swt.widgets.Text; 29 import org.eclipse.ui.help.WorkbenchHelp; 30 import org.eclipse.ui.internal.IHelpContextIds; 31 import org.eclipse.ui.internal.WorkbenchMessages; 32 import org.eclipse.ui.internal.WorkbenchPlugin; 33 34 42 public class NewEnhydraWizardPage extends WizardPage { 43 44 private boolean useDefaults = true; 45 46 private String initialProjectFieldValue; 48 private IPath initialLocationFieldValue; 49 50 private String customLocationFieldValue; 52 53 private Text projectNameField; 55 private Text locationPathField; 56 private Label locationLabel; 57 private Button browseButton; 58 static private String prjRootValue = null; 59 private Listener nameModifyListener = new Listener() { 60 public void handleEvent(Event e) { 61 setLocationForSelection(); 62 setPageComplete(validatePage()); 63 } 64 }; 65 66 private Listener locationModifyListener = new Listener() { 67 public void handleEvent(Event e) { 68 setPageComplete(validatePage()); 69 } 70 }; 71 72 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 74 private static final int SIZING_INDENTATION_WIDTH = 10; 75 76 77 81 public NewEnhydraWizardPage(String pageName) { 82 super(pageName); 83 84 setPageComplete(false); 85 initialLocationFieldValue = Platform.getLocation(); 86 customLocationFieldValue = ""; 87 } 88 89 95 public NewEnhydraWizardPage( 96 String pageName, 97 String title, 98 ImageDescriptor titleImage) { 99 super(pageName, title, titleImage); 100 101 setPageComplete(false); 102 initialLocationFieldValue = Platform.getLocation(); 103 customLocationFieldValue = ""; 104 } 105 106 109 public void createControl(Composite parent) { 110 Composite composite = new Composite(parent, SWT.NULL); 111 112 WorkbenchHelp.setHelp(composite, IHelpContextIds.NEW_PROJECT_WIZARD_PAGE); 113 114 composite.setLayout(new GridLayout()); 115 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 116 117 createProjectNameGroup(composite); 118 createProjectLocationGroup(composite); 119 validatePage(); 120 setErrorMessage(null); 122 setMessage(null); 123 setControl(composite); 124 } 125 130 private final void createProjectLocationGroup(Composite parent) { 131 132 Composite projectGroup = new Composite(parent, SWT.NONE); 134 GridLayout layout = new GridLayout(); 135 layout.numColumns = 3; 136 projectGroup.setLayout(layout); 137 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 138 139 Label projectContentsLabel = new Label(projectGroup, SWT.NONE); 141 projectContentsLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectContentsLabel")); 142 143 GridData labelData = new GridData(); 144 labelData.horizontalSpan = 3; 145 projectContentsLabel.setLayoutData(labelData); 146 147 final Button useDefaultsButton = new Button(projectGroup, SWT.CHECK | SWT.RIGHT); 148 useDefaultsButton.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.useDefaultLabel")); 149 useDefaultsButton.setSelection(useDefaults); 150 151 GridData buttonData = new GridData(); 152 buttonData.horizontalSpan = 3; 153 useDefaultsButton.setLayoutData(buttonData); 154 155 createUserSpecifiedProjectLocationGroup(projectGroup, !useDefaults); 156 157 SelectionListener listener = new SelectionAdapter() { 158 public void widgetSelected(SelectionEvent e) { 159 useDefaults = useDefaultsButton.getSelection(); 160 browseButton.setEnabled(!useDefaults); 161 locationPathField.setEnabled(!useDefaults); 162 locationLabel.setEnabled(!useDefaults); 163 if (useDefaults) { 164 customLocationFieldValue = locationPathField.getText(); 165 setLocationForSelection(); 166 } else { 167 locationPathField.setText(customLocationFieldValue); 168 } 169 } 170 }; 171 useDefaultsButton.addSelectionListener(listener); 172 } 173 178 private final void createProjectNameGroup(Composite parent) { 179 Composite projectGroup = new Composite(parent, SWT.NONE); 181 GridLayout layout = new GridLayout(); 182 layout.numColumns = 2; 183 projectGroup.setLayout(layout); 184 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 185 186 Label projectLabel = new Label(projectGroup, SWT.NONE); 188 projectLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.nameLabel")); 189 190 projectNameField = new Text(projectGroup, SWT.BORDER); 192 GridData data = new GridData(GridData.FILL_HORIZONTAL); 193 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 194 projectNameField.setLayoutData(data); 195 196 if (initialProjectFieldValue != null) 199 projectNameField.setText(initialProjectFieldValue); 200 projectNameField.addListener(SWT.Modify, nameModifyListener); 201 } 202 208 private void createUserSpecifiedProjectLocationGroup(Composite projectGroup, boolean enabled) { 209 210 locationLabel = new Label(projectGroup,SWT.NONE); 212 locationLabel.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.locationLabel")); 213 locationLabel.setEnabled(enabled); 214 215 locationPathField = new Text(projectGroup, SWT.BORDER); 217 GridData data = new GridData(GridData.FILL_HORIZONTAL); 218 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 219 locationPathField.setLayoutData(data); 220 locationPathField.setEnabled(enabled); 221 222 browseButton = new Button(projectGroup, SWT.PUSH); 224 browseButton.setText(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.browseLabel")); 225 browseButton.addSelectionListener(new SelectionAdapter() { 226 public void widgetSelected(SelectionEvent event) { 227 handleLocationBrowseButtonPressed(); 228 } 229 }); 230 231 browseButton.setEnabled(enabled); 232 233 if (initialLocationFieldValue != null) 236 locationPathField.setText(initialLocationFieldValue.toOSString()); 237 locationPathField.addListener(SWT.Modify, locationModifyListener); 238 } 239 249 public IPath getLocationPath() { 250 if (useDefaults) 251 return initialLocationFieldValue; 252 253 return new Path(getProjectLocationFieldValue()); 254 } 255 264 public IProject getProjectHandle() { 265 return ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName()); 266 } 267 274 public String getProjectName() { 275 if (projectNameField == null) 276 return initialProjectFieldValue; 277 278 return getProjectNameFieldValue(); 279 } 280 286 private String getProjectNameFieldValue() { 287 if (projectNameField == null) 288 return ""; 289 else 290 return projectNameField.getText().trim(); 291 } 292 293 294 300 private String getProjectLocationFieldValue() { 301 if (locationPathField == null) 302 return ""; 303 else 304 return locationPathField.getText().trim(); 305 } 306 309 private void handleLocationBrowseButtonPressed() { 310 DirectoryDialog dialog = new DirectoryDialog(locationPathField.getShell()); 311 dialog.setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.directoryLabel")); 312 313 String dirName = getProjectLocationFieldValue(); 314 if (!dirName.equals("")) { 315 File path = new File (dirName); 316 if (path.exists()) 317 dialog.setFilterPath(new Path(dirName).toOSString()); 318 } 319 320 String selectedDirectory = dialog.open(); 321 if (selectedDirectory != null) { 322 customLocationFieldValue = selectedDirectory; 323 locationPathField.setText(customLocationFieldValue); 324 } 325 } 326 334 public void setInitialProjectName(String name) { 335 if (name == null) 336 initialProjectFieldValue = null; 337 else 338 initialProjectFieldValue = name.trim(); 339 } 340 343 private void setLocationForSelection() { 344 if (useDefaults) { 345 IPath defaultPath = Platform.getLocation().append(getProjectNameFieldValue()); 346 locationPathField.setText(defaultPath.toOSString()); 347 } 348 } 349 356 private boolean validatePage() { 357 IWorkspace workspace = WorkbenchPlugin.getPluginWorkspace(); 358 359 String projectFieldContents = getProjectNameFieldValue(); 360 if (projectFieldContents.equals("")) { 361 setErrorMessage(null); 362 setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectNameEmpty")); 363 return false; 364 } 365 366 IStatus nameStatus = 367 workspace.validateName(projectFieldContents, IResource.PROJECT); 368 if (!nameStatus.isOK()) { 369 setErrorMessage(nameStatus.getMessage()); 370 return false; 371 } 372 373 String locationFieldContents = getProjectLocationFieldValue(); 374 375 if (locationFieldContents.equals("")) { 376 setErrorMessage(null); 377 setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectLocationEmpty")); 378 return false; 379 } 380 if (!(locationFieldContents.endsWith("\\"+projectFieldContents) || locationFieldContents.endsWith("/"+projectFieldContents))) { 382 setErrorMessage(null); 383 setMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectRootNotEndsName")); 384 return false; 385 } 386 IPath path = new Path(""); 388 if (!path.isValidPath(locationFieldContents)) { 389 setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.locationError")); 390 return false; 391 } 392 if (!useDefaults && Platform.getLocation().isPrefixOf(new Path(locationFieldContents))) { 393 setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.defaultLocationError")); 394 return false; 395 } 396 397 398 if (getProjectHandle().exists()) { 399 setErrorMessage(NewEnhydraWizardMessages.getString("NewEnhydraWizardPage.projectExistsMessage")); 400 return false; 401 } 402 403 setErrorMessage(null); 404 setMessage(null); 405 return true; 406 } 407 408 411 public void setVisible(boolean visible) { 412 super.setVisible(visible); 413 if(visible) 414 projectNameField.setFocus(); 415 } 416 417 } 418 | Popular Tags |