1 11 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.net.URI ; 15 import java.net.URISyntaxException ; 16 import org.eclipse.core.filesystem.IFileInfo; 17 import org.eclipse.core.filesystem.URIUtil; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 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.osgi.util.TextProcessor; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.ModifyEvent; 27 import org.eclipse.swt.events.ModifyListener; 28 import org.eclipse.swt.events.SelectionAdapter; 29 import org.eclipse.swt.events.SelectionEvent; 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.Label; 36 import org.eclipse.swt.widgets.Text; 37 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 38 import org.eclipse.ui.internal.ide.filesystem.FileSystemConfiguration; 39 import org.eclipse.ui.internal.ide.filesystem.FileSystemSupportRegistry; 40 41 48 public class ProjectContentsLocationArea { 49 54 public interface IErrorMessageReporter { 55 62 public void reportError(String errorMessage); 63 } 64 65 private static String BROWSE_LABEL = IDEWorkbenchMessages.ProjectLocationSelectionDialog_browseLabel; 66 67 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 68 69 private static final String FILE_SCHEME = "file"; 71 private Label locationLabel; 72 73 private Text locationPathField; 74 75 private Button browseButton; 76 77 private IErrorMessageReporter errorReporter; 78 79 private String projectName = IDEResourceInfoUtils.EMPTY_STRING; 80 81 private String userPath = IDEResourceInfoUtils.EMPTY_STRING; 82 83 private Button useDefaultsButton; 84 85 private IProject existingProject; 86 87 private FileSystemSelectionArea fileSystemSelectionArea; 88 89 96 public ProjectContentsLocationArea(IErrorMessageReporter reporter, 97 Composite composite, IProject startProject) { 98 99 errorReporter = reporter; 100 projectName = startProject.getName(); 101 existingProject = startProject; 102 103 boolean defaultEnabled = true; 104 try { 105 defaultEnabled = startProject.getDescription().getLocationURI() == null; 106 } catch (CoreException e1) { 107 } 109 createContents(composite, defaultEnabled); 110 } 111 112 118 public ProjectContentsLocationArea(IErrorMessageReporter reporter, 119 Composite composite) { 120 errorReporter = reporter; 121 122 createContents(composite, true); 124 } 125 126 132 private void createContents(Composite composite, boolean defaultEnabled) { 133 134 int columns = 4; 135 136 Composite projectGroup = new Composite(composite, SWT.NONE); 138 GridLayout layout = new GridLayout(); 139 layout.numColumns = columns; 140 projectGroup.setLayout(layout); 141 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 142 143 useDefaultsButton = new Button(projectGroup, SWT.CHECK | SWT.RIGHT); 144 useDefaultsButton 145 .setText(IDEWorkbenchMessages.ProjectLocationSelectionDialog_useDefaultLabel); 146 useDefaultsButton.setSelection(defaultEnabled); 147 GridData buttonData = new GridData(); 148 buttonData.horizontalSpan = columns; 149 useDefaultsButton.setLayoutData(buttonData); 150 151 createUserEntryArea(projectGroup, defaultEnabled); 152 153 useDefaultsButton.addSelectionListener(new SelectionAdapter() { 154 public void widgetSelected(SelectionEvent e) { 155 boolean useDefaults = useDefaultsButton.getSelection(); 156 157 if (useDefaults) { 158 userPath = locationPathField.getText(); 159 locationPathField.setText(TextProcessor 160 .process(getDefaultPathDisplayString())); 161 } else { 162 locationPathField.setText(TextProcessor.process(userPath)); 163 } 164 setUserAreaEnabled(!useDefaults); 165 } 166 }); 167 setUserAreaEnabled(!defaultEnabled); 168 } 169 170 176 public boolean isDefault() { 177 return useDefaultsButton.getSelection(); 178 } 179 180 186 private void createUserEntryArea(Composite composite, boolean defaultEnabled) { 187 locationLabel = new Label(composite, SWT.NONE); 189 locationLabel 190 .setText(IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationLabel); 191 192 locationPathField = new Text(composite, SWT.BORDER); 194 GridData data = new GridData(GridData.FILL_HORIZONTAL); 195 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 196 data.horizontalSpan = 2; 197 locationPathField.setLayoutData(data); 198 199 browseButton = new Button(composite, SWT.PUSH); 201 browseButton.setText(BROWSE_LABEL); 202 browseButton.addSelectionListener(new SelectionAdapter() { 203 public void widgetSelected(SelectionEvent event) { 204 handleLocationBrowseButtonPressed(); 205 } 206 }); 207 208 createFileSystemSelection(composite); 209 210 if (defaultEnabled) { 211 locationPathField.setText(TextProcessor 212 .process(getDefaultPathDisplayString())); 213 } else { 214 if (existingProject == null) { 215 locationPathField.setText(IDEResourceInfoUtils.EMPTY_STRING); 216 } else { 217 locationPathField.setText(TextProcessor.process(existingProject 218 .getLocation().toString())); 219 } 220 } 221 222 locationPathField.addModifyListener(new ModifyListener() { 223 228 public void modifyText(ModifyEvent e) { 229 errorReporter.reportError(checkValidLocation()); 230 } 231 }); 232 } 233 234 239 private void createFileSystemSelection(Composite composite) { 240 241 if (FileSystemSupportRegistry.getInstance().hasOneFileSystem()) { 243 return; 244 } 245 246 new Label(composite, SWT.NONE); 247 248 fileSystemSelectionArea = new FileSystemSelectionArea(); 249 fileSystemSelectionArea.createContents(composite); 250 } 251 252 258 private String getDefaultPathDisplayString() { 259 260 URI defaultURI = null; 261 if (existingProject != null) { 262 defaultURI = existingProject.getLocationURI(); 263 } 264 265 if (defaultURI == null || defaultURI.getScheme().equals(FILE_SCHEME)) { 267 return Platform.getLocation().append(projectName).toString(); 268 } 269 return defaultURI.toString(); 270 271 } 272 273 278 private void setUserAreaEnabled(boolean enabled) { 279 280 locationLabel.setEnabled(enabled); 281 locationPathField.setEnabled(enabled); 282 browseButton.setEnabled(enabled); 283 if (fileSystemSelectionArea != null) { 284 fileSystemSelectionArea.setEnabled(enabled); 285 } 286 } 287 288 294 public Button getBrowseButton() { 295 return browseButton; 296 } 297 298 301 private void handleLocationBrowseButtonPressed() { 302 303 String selectedDirectory = null; 304 String dirName = getPathFromLocationField(); 305 306 if (!dirName.equals(IDEResourceInfoUtils.EMPTY_STRING)) { 307 IFileInfo info; 308 info = IDEResourceInfoUtils.getFileInfo(dirName); 309 310 if (info == null || !(info.exists())) 311 dirName = IDEResourceInfoUtils.EMPTY_STRING; 312 } 313 314 FileSystemConfiguration config = getSelectedConfiguration(); 315 if (config== null || config.equals( 316 FileSystemSupportRegistry.getInstance() 317 .getDefaultConfiguration())) { 318 DirectoryDialog dialog = new DirectoryDialog(locationPathField 319 .getShell()); 320 dialog 321 .setMessage(IDEWorkbenchMessages.ProjectLocationSelectionDialog_directoryLabel); 322 323 dialog.setFilterPath(dirName); 324 325 selectedDirectory = dialog.open(); 326 327 } else { 328 URI uri = getSelectedConfiguration().getContributor() 329 .browseFileSystem(dirName, browseButton.getShell()); 330 if (uri != null) 331 selectedDirectory = uri.toString(); 332 } 333 334 if (selectedDirectory != null) 335 updateLocationField(selectedDirectory); 336 } 337 338 343 private void updateLocationField(String selectedPath) { 344 locationPathField.setText(TextProcessor.process(selectedPath)); 345 } 346 347 352 private String getPathFromLocationField() { 353 URI fieldURI; 354 try { 355 fieldURI = new URI (locationPathField.getText()); 356 } catch (URISyntaxException e) { 357 return locationPathField.getText(); 358 } 359 return fieldURI.getPath(); 360 } 361 362 368 public String checkValidLocation() { 369 370 if (isDefault()) { 371 return null; 372 } 373 374 String locationFieldContents = locationPathField.getText(); 375 if (locationFieldContents.length() == 0) { 376 return (IDEWorkbenchMessages.WizardNewProjectCreationPage_projectLocationEmpty); 377 } 378 379 URI newPath = getProjectLocationURI(); 380 if (newPath == null) { 381 return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError; 382 } 383 384 IProject project = existingProject; 386 if (project == null) { 387 String name = new Path(locationFieldContents).lastSegment(); 388 if (name != null && Path.EMPTY.isValidSegment(name)){ 389 project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 390 } 391 else { 392 return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError; 393 } 394 } 395 IStatus locationStatus = project.getWorkspace() 396 .validateProjectLocationURI(project, newPath); 397 398 if (!locationStatus.isOK()) { 399 return locationStatus.getMessage(); 400 } 401 if (existingProject != null) { 402 URI projectPath = existingProject.getLocationURI(); 403 if (projectPath != null && URIUtil.equals(projectPath, newPath)) { 404 return IDEWorkbenchMessages.ProjectLocationSelectionDialog_locationError; 405 } 406 } 407 408 return null; 409 } 410 411 416 public URI getProjectLocationURI() { 417 418 FileSystemConfiguration configuration = getSelectedConfiguration(); 419 if (configuration == null) { 420 return null; 421 } 422 423 return configuration.getContributor().getURI( 424 locationPathField.getText()); 425 426 } 427 428 434 private FileSystemConfiguration getSelectedConfiguration() { 435 if (fileSystemSelectionArea == null) { 436 return FileSystemSupportRegistry.getInstance() 437 .getDefaultConfiguration(); 438 } 439 440 return fileSystemSelectionArea.getSelectedConfiguration(); 441 } 442 443 451 public void updateProjectName(String newName) { 452 projectName = newName; 453 if (isDefault()) { 454 locationPathField.setText(TextProcessor 455 .process(getDefaultPathDisplayString())); 456 } 457 458 } 459 460 466 public String getProjectLocation() { 467 if (isDefault()) { 468 return Platform.getLocation().toString(); 469 } 470 return locationPathField.getText(); 471 } 472 } 473 | Popular Tags |