1 13 package org.eclipse.ui.dialogs; 14 15 import java.net.URI ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.jface.wizard.WizardPage; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Text; 33 import org.eclipse.ui.PlatformUI; 34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 35 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 36 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 37 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea; 38 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter; 39 40 54 public class WizardNewProjectCreationPage extends WizardPage { 55 56 private String initialProjectFieldValue; 58 59 Text projectNameField; 61 62 private Listener nameModifyListener = new Listener() { 63 public void handleEvent(Event e) { 64 setLocationForSelection(); 65 boolean valid = validatePage(); 66 setPageComplete(valid); 67 68 } 69 }; 70 71 private ProjectContentsLocationArea locationArea; 72 73 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 75 76 81 public WizardNewProjectCreationPage(String pageName) { 82 super(pageName); 83 setPageComplete(false); 84 } 85 86 89 public void createControl(Composite parent) { 90 Composite composite = new Composite(parent, SWT.NULL); 91 composite.setFont(parent.getFont()); 92 93 initializeDialogUnits(parent); 94 95 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, 96 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE); 97 98 composite.setLayout(new GridLayout()); 99 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 100 101 createProjectNameGroup(composite); 102 locationArea = new ProjectContentsLocationArea(getErrorReporter(), composite); 103 if(initialProjectFieldValue != null) { 104 locationArea.updateProjectName(initialProjectFieldValue); 105 } 106 107 setButtonLayoutData(locationArea.getBrowseButton()); 109 110 setPageComplete(validatePage()); 111 setErrorMessage(null); 113 setMessage(null); 114 setControl(composite); 115 } 116 117 121 private IErrorMessageReporter getErrorReporter() { 122 return new IErrorMessageReporter(){ 123 126 public void reportError(String errorMessage) { 127 setErrorMessage(errorMessage); 128 boolean valid = errorMessage == null; 129 if(valid) { 130 valid = validatePage(); 131 } 132 133 setPageComplete(valid); 134 } 135 }; 136 } 137 138 143 private final void createProjectNameGroup(Composite parent) { 144 Composite projectGroup = new Composite(parent, SWT.NONE); 146 GridLayout layout = new GridLayout(); 147 layout.numColumns = 2; 148 projectGroup.setLayout(layout); 149 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 150 151 Label projectLabel = new Label(projectGroup, SWT.NONE); 153 projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel); 154 projectLabel.setFont(parent.getFont()); 155 156 projectNameField = new Text(projectGroup, SWT.BORDER); 158 GridData data = new GridData(GridData.FILL_HORIZONTAL); 159 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 160 projectNameField.setLayoutData(data); 161 projectNameField.setFont(parent.getFont()); 162 163 if (initialProjectFieldValue != null) { 166 projectNameField.setText(initialProjectFieldValue); 167 } 168 projectNameField.addListener(SWT.Modify, nameModifyListener); 169 } 170 171 172 181 public IPath getLocationPath() { 182 return new Path(locationArea.getProjectLocation()); 183 } 184 185 194 public URI getLocationURI() { 195 return locationArea.getProjectLocationURI(); 196 } 197 198 209 public IProject getProjectHandle() { 210 return ResourcesPlugin.getWorkspace().getRoot().getProject( 211 getProjectName()); 212 } 213 214 221 public String getProjectName() { 222 if (projectNameField == null) { 223 return initialProjectFieldValue; 224 } 225 226 return getProjectNameFieldValue(); 227 } 228 229 235 private String getProjectNameFieldValue() { 236 if (projectNameField == null) { 237 return ""; } 239 240 return projectNameField.getText().trim(); 241 } 242 243 258 public void setInitialProjectName(String name) { 259 if (name == null) { 260 initialProjectFieldValue = null; 261 } else { 262 initialProjectFieldValue = name.trim(); 263 if(locationArea != null) { 264 locationArea.updateProjectName(name.trim()); 265 } 266 } 267 } 268 269 272 void setLocationForSelection() { 273 locationArea.updateProjectName(getProjectNameFieldValue()); 274 } 275 276 277 284 protected boolean validatePage() { 285 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 286 287 String projectFieldContents = getProjectNameFieldValue(); 288 if (projectFieldContents.equals("")) { setErrorMessage(null); 290 setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty); 291 return false; 292 } 293 294 IStatus nameStatus = workspace.validateName(projectFieldContents, 295 IResource.PROJECT); 296 if (!nameStatus.isOK()) { 297 setErrorMessage(nameStatus.getMessage()); 298 return false; 299 } 300 301 IProject handle = getProjectHandle(); 302 if (handle.exists()) { 303 setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage); 304 return false; 305 } 306 307 310 if (!locationArea.isDefault()) { 311 String validLocationMessage = locationArea.checkValidLocation(); 312 if (validLocationMessage != null) { setErrorMessage(validLocationMessage); 314 return false; 315 } 316 } 317 318 setErrorMessage(null); 319 setMessage(null); 320 return true; 321 } 322 323 326 public void setVisible(boolean visible) { 327 super.setVisible(visible); 328 if (visible) { 329 projectNameField.setFocus(); 330 } 331 } 332 333 337 public boolean useDefaults() { 338 return locationArea.isDefault(); 339 } 340 341 } 342 | Popular Tags |