1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.io.FileFilter ; 15 import java.lang.reflect.InvocationTargetException ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IProjectDescription; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IResourceStatus; 21 import org.eclipse.core.resources.IWorkspace; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.OperationCanceledException; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.core.runtime.SubProgressMonitor; 30 import org.eclipse.jface.dialogs.ErrorDialog; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.wizard.WizardPage; 33 import org.eclipse.osgi.util.NLS; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.graphics.Font; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.DirectoryDialog; 43 import org.eclipse.swt.widgets.Event; 44 import org.eclipse.swt.widgets.Label; 45 import org.eclipse.swt.widgets.Listener; 46 import org.eclipse.swt.widgets.Text; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.actions.WorkspaceModifyOperation; 49 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 50 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 51 import org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages; 52 53 68 public class WizardExternalProjectImportPage extends WizardPage { 69 70 private FileFilter projectFilter = new FileFilter () { 71 public boolean accept(File pathName) { 73 return pathName.getName().equals( 74 IProjectDescription.DESCRIPTION_FILE_NAME); 75 } 76 }; 77 78 private static String previouslyBrowsedDirectory = ""; 82 private Text projectNameField; 84 85 private Text locationPathField; 86 87 private Button browseButton; 88 89 private IProjectDescription description; 90 91 private Listener locationModifyListener = new Listener() { 92 public void handleEvent(Event e) { 93 setPageComplete(validatePage()); 94 } 95 }; 96 97 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 99 100 104 public WizardExternalProjectImportPage() { 105 super("wizardExternalProjectPage"); setPageComplete(false); 107 setTitle(DataTransferMessages.WizardExternalProjectImportPage_title); 108 setDescription(DataTransferMessages.WizardExternalProjectImportPage_description); 109 110 } 111 112 115 public void createControl(Composite parent) { 116 117 initializeDialogUnits(parent); 118 119 Composite composite = new Composite(parent, SWT.NULL); 120 121 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, 122 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE); 123 124 composite.setLayout(new GridLayout()); 125 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 126 composite.setFont(parent.getFont()); 127 128 createProjectNameGroup(composite); 129 createProjectLocationGroup(composite); 130 validatePage(); 131 setErrorMessage(null); 133 setMessage(null); 134 setControl(composite); 135 } 136 137 142 private final void createProjectLocationGroup(Composite parent) { 143 144 Composite projectGroup = new Composite(parent, SWT.NONE); 146 GridLayout layout = new GridLayout(); 147 layout.numColumns = 3; 148 projectGroup.setLayout(layout); 149 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 150 projectGroup.setFont(parent.getFont()); 151 152 Label projectContentsLabel = new Label(projectGroup, SWT.NONE); 154 projectContentsLabel.setText(DataTransferMessages.WizardExternalProjectImportPage_projectContentsLabel); 155 projectContentsLabel.setFont(parent.getFont()); 156 157 createUserSpecifiedProjectLocationGroup(projectGroup); 158 } 159 160 165 private final void createProjectNameGroup(Composite parent) { 166 167 Font dialogFont = parent.getFont(); 168 169 Composite projectGroup = new Composite(parent, SWT.NONE); 171 GridLayout layout = new GridLayout(); 172 layout.numColumns = 2; 173 projectGroup.setFont(dialogFont); 174 projectGroup.setLayout(layout); 175 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 176 177 Label projectLabel = new Label(projectGroup, SWT.NONE); 179 projectLabel.setText(DataTransferMessages.WizardExternalProjectImportPage_nameLabel); 180 projectLabel.setFont(dialogFont); 181 182 projectNameField = new Text(projectGroup, SWT.BORDER | SWT.READ_ONLY); 184 GridData data = new GridData(GridData.FILL_HORIZONTAL); 185 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 186 projectNameField.setLayoutData(data); 187 projectNameField.setFont(dialogFont); 188 projectNameField.setBackground(parent.getDisplay().getSystemColor( 189 SWT.COLOR_WIDGET_BACKGROUND)); 190 } 191 192 197 private void createUserSpecifiedProjectLocationGroup(Composite projectGroup) { 198 199 Font dialogFont = projectGroup.getFont(); 200 201 this.locationPathField = new Text(projectGroup, SWT.BORDER); 203 GridData data = new GridData(GridData.FILL_HORIZONTAL); 204 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 205 this.locationPathField.setLayoutData(data); 206 this.locationPathField.setFont(dialogFont); 207 208 this.browseButton = new Button(projectGroup, SWT.PUSH); 210 this.browseButton.setText(DataTransferMessages.DataTransfer_browse); 211 this.browseButton.setFont(dialogFont); 212 setButtonLayoutData(this.browseButton); 213 214 this.browseButton.addSelectionListener(new SelectionAdapter() { 215 public void widgetSelected(SelectionEvent event) { 216 handleLocationBrowseButtonPressed(); 217 } 218 }); 219 220 locationPathField.addListener(SWT.Modify, locationModifyListener); 221 } 222 223 230 public IPath getLocationPath() { 231 232 return new Path(getProjectLocationFieldValue()); 233 } 234 235 244 public IProject getProjectHandle() { 245 return ResourcesPlugin.getWorkspace().getRoot().getProject( 246 getProjectName()); 247 } 248 249 256 public String getProjectName() { 257 return getProjectNameFieldValue(); 258 } 259 260 266 private String getProjectNameFieldValue() { 267 if (projectNameField == null) { 268 return ""; } 270 271 return projectNameField.getText().trim(); 272 } 273 274 280 private String getProjectLocationFieldValue() { 281 return locationPathField.getText().trim(); 282 } 283 284 287 private void handleLocationBrowseButtonPressed() { 288 DirectoryDialog dialog = new DirectoryDialog(locationPathField 289 .getShell()); 290 dialog.setMessage(DataTransferMessages.WizardExternalProjectImportPage_directoryLabel); 291 292 String dirName = getProjectLocationFieldValue(); 293 if (dirName.length() == 0) { 294 dirName = previouslyBrowsedDirectory; 295 } 296 297 if (dirName.length() == 0) { 298 dialog.setFilterPath(getWorkspace().getRoot().getLocation() 299 .toOSString()); 300 } else { 301 File path = new File (dirName); 302 if (path.exists()) { 303 dialog.setFilterPath(new Path(dirName).toOSString()); 304 } 305 } 306 307 String selectedDirectory = dialog.open(); 308 if (selectedDirectory != null) { 309 previouslyBrowsedDirectory = selectedDirectory; 310 locationPathField.setText(previouslyBrowsedDirectory); 311 setProjectName(projectFile(previouslyBrowsedDirectory)); 312 } 313 } 314 315 322 private boolean validatePage() { 323 324 String locationFieldContents = getProjectLocationFieldValue(); 325 326 if (locationFieldContents.equals("")) { setErrorMessage(null); 328 setMessage(DataTransferMessages.WizardExternalProjectImportPage_projectLocationEmpty); 329 return false; 330 } 331 332 IPath path = new Path(""); if (!path.isValidPath(locationFieldContents)) { 334 setErrorMessage(DataTransferMessages.WizardExternalProjectImportPage_locationError); 335 return false; 336 } 337 338 File projectFile = projectFile(locationFieldContents); 339 if (projectFile == null) { 340 setErrorMessage(NLS.bind(DataTransferMessages.WizardExternalProjectImportPage_notAProject, locationFieldContents)); 341 return false; 342 } 343 setProjectName(projectFile); 344 345 if (getProjectHandle().exists()) { 346 setErrorMessage(DataTransferMessages.WizardExternalProjectImportPage_projectExistsMessage); 347 return false; 348 } 349 350 setErrorMessage(null); 351 setMessage(null); 352 return true; 353 } 354 355 private IWorkspace getWorkspace() { 356 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 357 return workspace; 358 } 359 360 364 private boolean isPrefixOfRoot(IPath locationPath) { 365 return Platform.getLocation().isPrefixOf(locationPath); 366 } 367 368 373 private void setProjectName(File projectFile) { 374 375 if (projectFile == null) { 377 return; 378 } 379 380 IPath path = new Path(projectFile.getPath()); 381 382 IProjectDescription newDescription = null; 383 384 try { 385 newDescription = getWorkspace().loadProjectDescription(path); 386 } catch (CoreException exception) { 387 } 389 390 if (newDescription == null) { 391 this.description = null; 392 this.projectNameField.setText(""); } else { 394 this.description = newDescription; 395 this.projectNameField.setText(this.description.getName()); 396 } 397 } 398 399 403 private File projectFile(String locationFieldContents) { 404 File directory = new File (locationFieldContents); 405 if (directory.isFile()) { 406 return null; 407 } 408 409 File [] files = directory.listFiles(this.projectFilter); 410 if (files != null && files.length == 1) { 411 return files[0]; 412 } 413 414 return null; 415 } 416 417 428 IProject createExistingProject() { 429 430 String projectName = projectNameField.getText(); 431 final IWorkspace workspace = ResourcesPlugin.getWorkspace(); 432 final IProject project = workspace.getRoot().getProject(projectName); 433 if (this.description == null) { 434 this.description = workspace.newProjectDescription(projectName); 435 IPath locationPath = getLocationPath(); 436 if (isPrefixOfRoot(locationPath)) { 438 this.description.setLocation(null); 439 } else { 440 this.description.setLocation(locationPath); 441 } 442 } else { 443 this.description.setName(projectName); 444 } 445 446 WorkspaceModifyOperation op = new WorkspaceModifyOperation() { 448 protected void execute(IProgressMonitor monitor) 449 throws CoreException { 450 monitor.beginTask("", 2000); project.create(description, new SubProgressMonitor(monitor, 452 1000)); 453 if (monitor.isCanceled()) { 454 throw new OperationCanceledException(); 455 } 456 project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000)); 457 458 } 459 }; 460 461 try { 463 getContainer().run(true, true, op); 464 } catch (InterruptedException e) { 465 return null; 466 } catch (InvocationTargetException e) { 467 Throwable t = e.getTargetException(); 469 if (t instanceof CoreException) { 470 if (((CoreException) t).getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) { 471 MessageDialog 472 .openError( 473 getShell(), 474 DataTransferMessages.WizardExternalProjectImportPage_errorMessage, 475 NLS.bind( 476 DataTransferMessages.WizardExternalProjectImportPage_caseVariantExistsError, 477 projectName) 478 ); 479 } else { 480 ErrorDialog 481 .openError( 482 getShell(), 483 DataTransferMessages.WizardExternalProjectImportPage_errorMessage, 484 null, ((CoreException) t).getStatus()); 485 } 486 } 487 return null; 488 } 489 490 return project; 491 } 492 493 496 public void setVisible(boolean visible) { 497 super.setVisible(visible); 498 if (visible) { 499 this.locationPathField.setFocus(); 500 } 501 } 502 503 } 504 | Popular Tags |