1 11 package org.eclipse.ui.dialogs; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.net.URI ; 17 import java.util.Iterator ; 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IFolder; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IResourceStatus; 23 import org.eclipse.core.resources.IWorkspace; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IAdaptable; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.OperationCanceledException; 32 import org.eclipse.core.runtime.Preferences; 33 import org.eclipse.core.runtime.jobs.ISchedulingRule; 34 import org.eclipse.jface.dialogs.ErrorDialog; 35 import org.eclipse.jface.dialogs.MessageDialog; 36 import org.eclipse.jface.operation.IRunnableWithProgress; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.wizard.WizardPage; 39 import org.eclipse.osgi.util.NLS; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.graphics.Point; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Event; 49 import org.eclipse.swt.widgets.Listener; 50 import org.eclipse.swt.widgets.Shell; 51 import org.eclipse.ui.PlatformUI; 52 import org.eclipse.ui.ide.undo.CreateFileOperation; 53 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil; 54 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 55 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 56 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 57 import org.eclipse.ui.internal.ide.dialogs.CreateLinkedResourceGroup; 58 import org.eclipse.ui.internal.ide.misc.ResourceAndContainerGroup; 59 60 79 public class WizardNewFileCreationPage extends WizardPage implements Listener { 80 private static final int SIZING_CONTAINER_GROUP_HEIGHT = 250; 81 82 private IStructuredSelection currentSelection; 84 85 private IFile newFile; 87 88 private URI linkTargetPath; 89 90 private ResourceAndContainerGroup resourceGroup; 92 93 private Button advancedButton; 94 95 private CreateLinkedResourceGroup linkedResourceGroup; 96 97 private Composite linkedResourceParent; 98 99 private Composite linkedResourceComposite; 100 101 private String initialFileName; 103 104 110 private String initialFileExtension; 111 112 private IPath initialContainerFullPath; 113 114 118 private int linkedResourceGroupHeight = -1; 119 120 123 private boolean firstLinkCheck = true; 124 125 135 public WizardNewFileCreationPage(String pageName, 136 IStructuredSelection selection) { 137 super(pageName); 138 setPageComplete(false); 139 this.currentSelection = selection; 140 } 141 142 148 protected void createAdvancedControls(Composite parent) { 149 Preferences preferences = ResourcesPlugin.getPlugin() 150 .getPluginPreferences(); 151 152 if (preferences.getBoolean(ResourcesPlugin.PREF_DISABLE_LINKING) == false) { 153 linkedResourceParent = new Composite(parent, SWT.NONE); 154 linkedResourceParent.setFont(parent.getFont()); 155 linkedResourceParent.setLayoutData(new GridData( 156 GridData.FILL_HORIZONTAL)); 157 GridLayout layout = new GridLayout(); 158 layout.marginHeight = 0; 159 layout.marginWidth = 0; 160 linkedResourceParent.setLayout(layout); 161 162 advancedButton = new Button(linkedResourceParent, SWT.PUSH); 163 advancedButton.setFont(linkedResourceParent.getFont()); 164 advancedButton.setText(IDEWorkbenchMessages.showAdvanced); 165 GridData data = setButtonLayoutData(advancedButton); 166 data.horizontalAlignment = GridData.BEGINNING; 167 advancedButton.setLayoutData(data); 168 advancedButton.addSelectionListener(new SelectionAdapter() { 169 public void widgetSelected(SelectionEvent e) { 170 handleAdvancedButtonSelect(); 171 } 172 }); 173 } 174 linkedResourceGroup = new CreateLinkedResourceGroup(IResource.FILE, 175 new Listener() { 176 public void handleEvent(Event e) { 177 setPageComplete(validatePage()); 178 firstLinkCheck = false; 179 } 180 }, new CreateLinkedResourceGroup.IStringValue() { 181 public void setValue(String string) { 182 resourceGroup.setResource(string); 183 } 184 185 public String getValue() { 186 return resourceGroup.getResource(); 187 } 188 }); 189 } 190 191 194 public void createControl(Composite parent) { 195 initializeDialogUnits(parent); 196 Composite topLevel = new Composite(parent, SWT.NONE); 198 topLevel.setLayout(new GridLayout()); 199 topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 200 | GridData.HORIZONTAL_ALIGN_FILL)); 201 topLevel.setFont(parent.getFont()); 202 PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel, 203 IIDEHelpContextIds.NEW_FILE_WIZARD_PAGE); 204 205 resourceGroup = new ResourceAndContainerGroup(topLevel, this, 207 getNewFileLabel(), 208 IDEWorkbenchMessages.WizardNewFileCreationPage_file, false, 209 SIZING_CONTAINER_GROUP_HEIGHT); 210 resourceGroup.setAllowExistingResources(false); 211 initialPopulateContainerNameField(); 212 createAdvancedControls(topLevel); 213 if (initialFileName != null) { 214 resourceGroup.setResource(initialFileName); 215 } 216 if (initialFileExtension != null) { 217 resourceGroup.setResourceExtension(initialFileExtension); 218 } 219 validatePage(); 220 setErrorMessage(null); 222 setMessage(null); 223 setControl(topLevel); 224 } 225 226 246 protected void createFile(IFile fileHandle, InputStream contents, 247 IProgressMonitor monitor) throws CoreException { 248 if (contents == null) { 249 contents = new ByteArrayInputStream (new byte[0]); 250 } 251 252 try { 253 if (linkTargetPath != null) { 255 fileHandle.createLink(linkTargetPath, 256 IResource.ALLOW_MISSING_LOCAL, monitor); 257 } else { 258 IPath path = fileHandle.getFullPath(); 259 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 260 int numSegments = path.segmentCount(); 261 if (numSegments > 2 262 && !root.getFolder(path.removeLastSegments(1)).exists()) { 263 for (int i = numSegments - 2; i > 0; i--) { 267 IFolder folder = root.getFolder(path 268 .removeLastSegments(i)); 269 if (!folder.exists()) { 270 folder.create(false, true, monitor); 271 } 272 } 273 } 274 fileHandle.create(contents, false, monitor); 275 } 276 } catch (CoreException e) { 277 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) { 279 fileHandle.refreshLocal(IResource.DEPTH_ZERO, null); 280 } else { 281 throw e; 282 } 283 } 284 285 if (monitor.isCanceled()) { 286 throw new OperationCanceledException(); 287 } 288 } 289 290 300 protected IFile createFileHandle(IPath filePath) { 301 return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFile( 302 filePath); 303 } 304 305 308 protected void createLinkTarget() { 309 linkTargetPath = linkedResourceGroup.getLinkTargetURI(); 310 } 311 312 334 public IFile createNewFile() { 335 if (newFile != null) { 336 return newFile; 337 } 338 339 341 final IPath containerPath = resourceGroup.getContainerFullPath(); 342 IPath newFilePath = containerPath.append(resourceGroup.getResource()); 343 final IFile newFileHandle = createFileHandle(newFilePath); 344 final InputStream initialContents = getInitialContents(); 345 346 createLinkTarget(); 347 IRunnableWithProgress op = new IRunnableWithProgress() { 348 public void run(IProgressMonitor monitor) { 349 CreateFileOperation op = new CreateFileOperation(newFileHandle, 350 linkTargetPath, initialContents, 351 IDEWorkbenchMessages.WizardNewFileCreationPage_title); 352 try { 353 PlatformUI.getWorkbench().getOperationSupport() 354 .getOperationHistory().execute( 355 op, 356 monitor, 357 WorkspaceUndoUtil 358 .getUIInfoAdapter(getShell())); 359 } catch (final ExecutionException e) { 360 getContainer().getShell().getDisplay().syncExec( 361 new Runnable () { 362 public void run() { 363 if (e.getCause() instanceof CoreException) { 364 ErrorDialog 365 .openError( 366 getContainer() 367 .getShell(), IDEWorkbenchMessages.WizardNewFileCreationPage_errorTitle, 370 null, ((CoreException) e 373 .getCause()) 374 .getStatus()); 375 } else { 376 IDEWorkbenchPlugin 377 .log( 378 getClass(), 379 "createNewFile()", e.getCause()); MessageDialog 381 .openError( 382 getContainer() 383 .getShell(), 384 IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle, 385 NLS 386 .bind( 387 IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage, 388 e 389 .getCause() 390 .getMessage())); 391 } 392 } 393 }); 394 } 395 } 396 }; 397 try { 398 getContainer().run(true, true, op); 399 } catch (InterruptedException e) { 400 return null; 401 } catch (InvocationTargetException e) { 402 IDEWorkbenchPlugin.log(getClass(), 405 "createNewFile()", e.getTargetException()); MessageDialog 407 .openError( 408 getContainer().getShell(), 409 IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle, 410 NLS 411 .bind( 412 IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage, 413 e.getTargetException().getMessage())); 414 415 return null; 416 } 417 418 newFile = newFileHandle; 419 420 return newFile; 421 } 422 423 435 protected ISchedulingRule createRule(IResource resource) { 436 IResource parent = resource.getParent(); 437 while (parent != null) { 438 if (parent.exists()) { 439 return resource.getWorkspace().getRuleFactory().createRule( 440 resource); 441 } 442 resource = parent; 443 parent = parent.getParent(); 444 } 445 return resource.getWorkspace().getRoot(); 446 } 447 448 455 public IPath getContainerFullPath() { 456 return resourceGroup.getContainerFullPath(); 457 } 458 459 470 public String getFileName() { 471 if (resourceGroup == null) { 472 return initialFileName; 473 } 474 475 return resourceGroup.getResource(); 476 } 477 478 485 public String getFileExtension() { 486 if (resourceGroup == null) { 487 return initialFileExtension; 488 } 489 return resourceGroup.getResourceExtension(); 490 } 491 492 499 protected InputStream getInitialContents() { 500 return null; 501 } 502 503 513 protected String getNewFileLabel() { 514 return IDEWorkbenchMessages.WizardNewFileCreationPage_fileLabel; 515 } 516 517 520 protected void handleAdvancedButtonSelect() { 521 Shell shell = getShell(); 522 Point shellSize = shell.getSize(); 523 Composite composite = (Composite) getControl(); 524 525 if (linkedResourceComposite != null) { 526 linkedResourceComposite.dispose(); 527 linkedResourceComposite = null; 528 composite.layout(); 529 shell.setSize(shellSize.x, shellSize.y - linkedResourceGroupHeight); 530 advancedButton.setText(IDEWorkbenchMessages.showAdvanced); 531 } else { 532 linkedResourceComposite = linkedResourceGroup 533 .createContents(linkedResourceParent); 534 if (linkedResourceGroupHeight == -1) { 535 Point groupSize = linkedResourceComposite.computeSize( 536 SWT.DEFAULT, SWT.DEFAULT, true); 537 linkedResourceGroupHeight = groupSize.y; 538 } 539 shell.setSize(shellSize.x, shellSize.y + linkedResourceGroupHeight); 540 composite.layout(); 541 advancedButton.setText(IDEWorkbenchMessages.hideAdvanced); 542 } 543 } 544 545 550 public void handleEvent(Event event) { 551 setPageComplete(validatePage()); 552 } 553 554 559 protected void initialPopulateContainerNameField() { 560 if (initialContainerFullPath != null) { 561 resourceGroup.setContainerFullPath(initialContainerFullPath); 562 } else { 563 Iterator it = currentSelection.iterator(); 564 if (it.hasNext()) { 565 Object object = it.next(); 566 IResource selectedResource = null; 567 if (object instanceof IResource) { 568 selectedResource = (IResource) object; 569 } else if (object instanceof IAdaptable) { 570 selectedResource = (IResource) ((IAdaptable) object) 571 .getAdapter(IResource.class); 572 } 573 if (selectedResource != null) { 574 if (selectedResource.getType() == IResource.FILE) { 575 selectedResource = selectedResource.getParent(); 576 } 577 if (selectedResource.isAccessible()) { 578 resourceGroup.setContainerFullPath(selectedResource 579 .getFullPath()); 580 } 581 } 582 } 583 } 584 } 585 586 593 public void setContainerFullPath(IPath path) { 594 if (resourceGroup == null) { 595 initialContainerFullPath = path; 596 } else { 597 resourceGroup.setContainerFullPath(path); 598 } 599 } 600 601 608 public void setFileName(String value) { 609 if (resourceGroup == null) { 610 initialFileName = value; 611 } else { 612 resourceGroup.setResource(value); 613 } 614 } 615 616 639 public void setFileExtension(String value) { 640 if (resourceGroup == null) { 641 initialFileExtension = value; 642 } else { 643 resourceGroup.setResourceExtension(value); 644 } 645 } 646 647 653 protected IStatus validateLinkedResource() { 654 IPath containerPath = resourceGroup.getContainerFullPath(); 655 IPath newFilePath = containerPath.append(resourceGroup.getResource()); 656 IFile newFileHandle = createFileHandle(newFilePath); 657 IStatus status = linkedResourceGroup 658 .validateLinkLocation(newFileHandle); 659 660 if (status.getSeverity() == IStatus.ERROR) { 661 if (firstLinkCheck) { 662 setMessage(status.getMessage()); 663 } else { 664 setErrorMessage(status.getMessage()); 665 } 666 } else if (status.getSeverity() == IStatus.WARNING) { 667 setMessage(status.getMessage(), WARNING); 668 setErrorMessage(null); 669 } 670 return status; 671 } 672 673 679 protected boolean validatePage() { 680 boolean valid = true; 681 682 if (!resourceGroup.areAllValuesValid()) { 683 if (resourceGroup.getProblemType() == ResourceAndContainerGroup.PROBLEM_RESOURCE_EMPTY 685 || resourceGroup.getProblemType() == ResourceAndContainerGroup.PROBLEM_CONTAINER_EMPTY) { 686 setMessage(resourceGroup.getProblemMessage()); 687 setErrorMessage(null); 688 } else { 689 setErrorMessage(resourceGroup.getProblemMessage()); 690 } 691 valid = false; 692 } 693 694 String resourceName = resourceGroup.getResource(); 695 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 696 IStatus result = workspace.validateName(resourceName, IResource.FILE); 697 if (!result.isOK()) { 698 setErrorMessage(result.getMessage()); 699 return false; 700 } 701 702 IStatus linkedResourceStatus = null; 703 if (valid) { 704 linkedResourceStatus = validateLinkedResource(); 705 if (linkedResourceStatus.getSeverity() == IStatus.ERROR) { 706 valid = false; 707 } 708 } 709 if (valid 711 && (linkedResourceStatus == null || linkedResourceStatus.isOK())) { 712 setMessage(null); 713 setErrorMessage(null); 714 } 715 return valid; 716 } 717 718 721 public void setVisible(boolean visible) { 722 super.setVisible(visible); 723 if (visible) { 724 resourceGroup.setFocus(); 725 } 726 } 727 } 728 | Popular Tags |