1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.text.MessageFormat ; 19 import java.util.Observable ; 20 import java.util.Observer ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 import org.eclipse.core.runtime.OperationCanceledException; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.runtime.Status; 30 31 import org.eclipse.core.resources.IContainer; 32 import org.eclipse.core.resources.IFile; 33 import org.eclipse.core.resources.IFolder; 34 import org.eclipse.core.resources.IPathVariableManager; 35 import org.eclipse.core.resources.IResource; 36 import org.eclipse.core.resources.IWorkspace; 37 import org.eclipse.core.resources.IWorkspaceRoot; 38 import org.eclipse.core.resources.ResourcesPlugin; 39 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.layout.GridLayout; 43 import org.eclipse.swt.widgets.Button; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.swt.widgets.Control; 46 import org.eclipse.swt.widgets.DirectoryDialog; 47 import org.eclipse.swt.widgets.Group; 48 import org.eclipse.swt.widgets.Label; 49 import org.eclipse.swt.widgets.Shell; 50 51 import org.eclipse.jface.dialogs.Dialog; 52 import org.eclipse.jface.dialogs.ErrorDialog; 53 import org.eclipse.jface.dialogs.IDialogConstants; 54 import org.eclipse.jface.dialogs.MessageDialog; 55 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 56 57 import org.eclipse.ui.actions.WorkspaceModifyOperation; 58 import org.eclipse.ui.dialogs.SelectionStatusDialog; 59 60 import org.eclipse.ui.ide.dialogs.PathVariableSelectionDialog; 61 62 import org.eclipse.jdt.internal.corext.util.Messages; 63 64 import org.eclipse.jdt.ui.JavaUI; 65 66 import org.eclipse.jdt.internal.ui.JavaPlugin; 67 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 68 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 69 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 70 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 71 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 72 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 73 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 74 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 75 76 public class ExtendedNewFolderDialog extends SelectionStatusDialog { 77 private final class FolderNameField extends Observable implements IDialogFieldListener { 78 private StringDialogField fNameDialogField; 79 80 public FolderNameField(Composite parent) { 81 createControls(parent); 82 } 83 84 private void createControls(Composite parent) { 85 Composite folderGroup = new Composite(parent, SWT.NONE); 86 GridLayout layout = new GridLayout(); 87 layout.numColumns = 2; 88 folderGroup.setLayout(layout); 89 GridData gridData= new GridData(GridData.FILL_HORIZONTAL); 90 91 fNameDialogField= new StringDialogField(); 92 fNameDialogField.setLabelText(NewWizardMessages.NewFolderDialog_folderNameGroup_label); 93 fNameDialogField.doFillIntoGrid(folderGroup, layout.numColumns); 94 LayoutUtil.setHorizontalGrabbing(fNameDialogField.getTextControl(null)); 95 folderGroup.setLayoutData(gridData); 96 fNameDialogField.setDialogFieldListener(this); 97 } 98 99 public StringDialogField getNameDialogField() { 100 return fNameDialogField; 101 } 102 103 public String getText() { 104 return fNameDialogField.getText(); 105 } 106 107 protected void fireEvent() { 108 setChanged(); 109 notifyObservers(); 110 } 111 112 public void dialogFieldChanged(DialogField field) { 113 fireEvent(); 114 } 115 } 116 117 private final class FolderTypeGroup { 118 protected Button fSourceFolderRadio; 119 120 public FolderTypeGroup(Composite parent) { 121 createControls(parent); 122 } 123 124 private void createControls(Composite parent) { 125 final Group group= new Group(parent, SWT.NONE); 126 GridLayout layout = new GridLayout(); 127 layout.numColumns = 1; 128 group.setLayout(layout); 129 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 130 group.setText(NewWizardMessages.NewFolderDialog_TypeGroup_title); 131 132 fSourceFolderRadio= new Button(group, SWT.RADIO); 133 fSourceFolderRadio.setText(NewWizardMessages.NewFolderDialog_folderTypeGroup_source_desc); 134 fSourceFolderRadio.setSelection(true); 135 136 Button normalFolderRadio= new Button(group, SWT.RADIO); 137 normalFolderRadio.setText(NewWizardMessages.NewFolderDialog_folderTypeGroup_normal_desc); 138 normalFolderRadio.setSelection(fSourceFolderRadio == null); 139 } 140 141 public boolean isSourceFolderType() { 142 return fSourceFolderRadio != null && fSourceFolderRadio.getSelection(); 143 } 144 } 145 146 private final class DependenciesGroup extends Observable implements IStringButtonAdapter, IDialogFieldListener{ 147 protected SelectionButtonDialogField fCopyFromButton; 148 protected SelectionButtonDialogField fLinkToButton; 149 protected StringButtonDialogField fCopyLocation; 150 protected StringButtonDialogField fLinkLocation; 151 152 private static final String DIALOGSTORE_LAST_EXTERNAL_LOC= JavaUI.ID_PLUGIN + ".last.external.project"; 154 public DependenciesGroup(Composite parent) { 155 createControls(parent); 156 } 157 158 private void createControls(Composite parent) { 159 final int numColumns= 4; 160 161 final Group group= new Group(parent, SWT.NONE); 162 GridLayout layout = new GridLayout(); 163 layout.numColumns = numColumns; 164 group.setLayout(layout); 165 GridData gridData= new GridData(GridData.FILL_HORIZONTAL); 166 gridData.minimumWidth= 430; 167 group.setLayoutData(gridData); 168 group.setText(NewWizardMessages.NewFolderDialog_DependencyGroup_title); 169 170 SelectionButtonDialogField noneButton= new SelectionButtonDialogField(SWT.RADIO); 171 noneButton.setDialogFieldListener(this); 172 173 noneButton.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_none_desc); 174 noneButton.setSelection(true); 175 176 fCopyFromButton= new SelectionButtonDialogField(SWT.RADIO); 177 fCopyFromButton.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_copy_desc); 178 179 fCopyLocation= new StringButtonDialogField(this); 180 fCopyLocation.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_locationLabel_desc); 181 fCopyLocation.setButtonLabel(NewWizardMessages.NewFolderDialog_dependenciesGroup_browseButton_desc); 182 fCopyFromButton.attachDialogField(fCopyLocation); 183 fCopyFromButton.setDialogFieldListener(this); 184 fCopyLocation.setDialogFieldListener(this); 185 186 fLinkToButton= new SelectionButtonDialogField(SWT.RADIO); 187 fLinkToButton.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_link_desc); 188 189 190 fLinkLocation= new StringButtonDialogField(this); 191 fLinkLocation.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_locationLabel_desc); 192 fLinkLocation.setButtonLabel(NewWizardMessages.NewFolderDialog_dependenciesGroup_browseButton_desc); 193 fLinkLocation.setDialogFieldListener(this); 194 195 SelectionButtonDialogField variables= new SelectionButtonDialogField(SWT.PUSH); 196 variables.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_variables_desc); 197 variables.setDialogFieldListener(new IDialogFieldListener() { 198 public void dialogFieldChanged(DialogField field) { 199 handleVariablesButtonPressed(); 200 } 201 }); 202 fLinkToButton.attachDialogFields(new DialogField[] {fLinkLocation, variables}); 203 fLinkToButton.setDialogFieldListener(this); 204 205 noneButton.doFillIntoGrid(group, numColumns); 206 fCopyFromButton.doFillIntoGrid(group, numColumns); 207 fCopyLocation.doFillIntoGrid(group, numColumns - 1); 208 if (!fJavaProjectSelected) { 209 fLinkToButton.setLabelText(NewWizardMessages.NewFolderDialog_dependenciesGroup_link_descDisabled); 210 fLinkToButton.setEnabled(false); 211 } 212 fLinkToButton.doFillIntoGrid(group, numColumns); 213 fLinkLocation.doFillIntoGrid(group, numColumns - 1); 214 LayoutUtil.setHorizontalGrabbing(fLinkLocation.getTextControl(null)); 215 216 variables.doFillIntoGrid(group, 1); 217 LayoutUtil.setHorizontalGrabbing(fCopyLocation.getTextControl(null)); 218 } 219 220 public String getLinkTarget() { 221 return fLinkLocation.getText(); 222 } 223 224 public String getCopyTarget() { 225 return fCopyLocation.getText(); 226 } 227 228 public boolean linkTargetSelected() { 229 return fLinkToButton.isSelected(); 230 } 231 232 public boolean copyTargetSelected() { 233 return fCopyFromButton.isSelected(); 234 } 235 236 239 public void changeControlPressed(DialogField field) { 240 StringButtonDialogField selectedField= field == fLinkLocation ? fLinkLocation : fCopyLocation; 241 final DirectoryDialog dialog= new DirectoryDialog(getShell()); 242 dialog.setMessage(NewWizardMessages.JavaProjectWizardFirstPage_directory_message); 243 String directoryName = getLinkTarget().trim(); 244 if (directoryName.length() == 0) { 245 String prevLocation= JavaPlugin.getDefault().getDialogSettings().get(DIALOGSTORE_LAST_EXTERNAL_LOC); 246 if (prevLocation != null) { 247 directoryName= prevLocation; 248 } 249 } 250 251 if (directoryName.length() > 0) { 252 final File path = new File (directoryName); 253 if (path.exists()) 254 dialog.setFilterPath(new Path(directoryName).toOSString()); 255 } 256 final String selectedDirectory = dialog.open(); 257 if (selectedDirectory != null) { 258 selectedField.setText(selectedDirectory); 259 JavaPlugin.getDefault().getDialogSettings().put(DIALOGSTORE_LAST_EXTERNAL_LOC, selectedDirectory); 260 } 261 } 262 263 266 private void handleVariablesButtonPressed() { 267 int variableTypes = IResource.FOLDER; 268 269 273 274 PathVariableSelectionDialog dialog = new PathVariableSelectionDialog(getShell(), variableTypes); 275 if (dialog.open() == IDialogConstants.OK_ID) { 276 String [] variableNames = (String []) dialog.getResult(); 277 if (variableNames != null && variableNames.length == 1) 278 fLinkLocation.setText(variableNames[0]); 279 } 280 } 281 282 public void dialogFieldChanged(DialogField field) { 283 fireEvent(); 284 } 285 286 protected void fireEvent() { 287 setChanged(); 288 notifyObservers(); 289 } 290 } 291 292 295 private final class Validator implements Observer { 296 297 public void update(Observable o, Object arg) { 298 299 final String name= fFolderNameField.getText(); 300 301 if (!validateFolderName(name)) 302 return; 303 304 if (fDependenciesGroup.linkTargetSelected()) { 305 validateLinkedResource(fDependenciesGroup.getLinkTarget()); 306 return; 307 } 308 309 if (fDependenciesGroup.copyTargetSelected()) { 310 validateLocation(fDependenciesGroup.getCopyTarget()); 311 return; 312 } 313 314 updateStatus(IStatus.OK, ""); } 316 317 323 private IStatus validateLinkLocation(IResource linkHandle) { 324 IWorkspace workspace = JavaPlugin.getWorkspace(); 325 IPath path = new Path(fDependenciesGroup.getLinkTarget()); 326 327 IStatus locationStatus = workspace.validateLinkLocation(linkHandle, path); 328 if (locationStatus.getMessage().equals(Messages.format(NewWizardMessages.NewFolderDialog_links_parentNotProject, linkHandle.getName())) && 329 container.getType() == IResource.PROJECT) 330 locationStatus= Status.OK_STATUS; 331 else if (locationStatus.getSeverity() == IStatus.ERROR) 332 return locationStatus; 333 334 String resolvedLinkTarget = resolveVariable(); 336 path = new Path(resolvedLinkTarget); 337 File linkTargetFile = new Path(resolvedLinkTarget).toFile(); 338 if (linkTargetFile.exists()) { 339 IStatus fileTypeStatus = validateFileType(linkTargetFile); 340 if (fileTypeStatus.isOK() == false) 341 return fileTypeStatus; 342 } else if (locationStatus.getSeverity() == IStatus.OK) { 343 return createStatus( 345 IStatus.WARNING, 346 NewWizardMessages.NewFolderDialog_linkTargetNonExistent); } 348 return locationStatus; 349 } 350 351 359 private IStatus validateFileType(File linkTargetFile) { 360 if (linkTargetFile.isDirectory() == false) 361 return createStatus(IStatus.ERROR, NewWizardMessages.NewFolderDialog_linkTargetNotFolder); return createStatus(IStatus.OK, ""); } 364 365 370 private IStatus createStatus(int severity, String message) { 371 return new Status(severity, JavaPlugin.getPluginId(), severity, message, null); 372 } 373 374 379 private String resolveVariable() { 380 IPathVariableManager pathVariableManager = ResourcesPlugin 381 .getWorkspace().getPathVariableManager(); 382 IPath path = new Path(fDependenciesGroup.getLinkTarget()); 383 IPath resolvedPath = pathVariableManager.resolvePath(path); 384 return resolvedPath.toOSString(); 385 } 386 387 392 private void validateLinkedResource(String text) { 393 IFolder linkHandle = createFolderHandle(text); 394 IStatus status = validateLinkLocation(linkHandle); 395 396 if (status.getSeverity() != IStatus.ERROR) 397 getOkButton().setEnabled(true); 398 else 399 getOkButton().setEnabled(false); 400 401 if (status.isOK() == false) 402 updateStatus(status); 403 } 404 405 private boolean validateLocation(String text) { 406 IPath path = new Path(text); 407 if (!path.toFile().exists()) { 408 updateStatus(IStatus.ERROR, Messages.format( 409 NewWizardMessages.NewFolderDialog_notExists, new Object [] { text })); return false; 411 } 412 updateStatus(IStatus.OK, ""); return true; 414 } 415 416 422 private boolean validateFolderName(String name) { 423 IWorkspace workspace = container.getWorkspace(); 424 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); 425 426 if (name.length() == 0) { updateStatus(IStatus.ERROR, NewWizardMessages.NewFolderDialog_folderNameEmpty); return false; 429 } 430 431 if (nameStatus.isOK() == false) { 432 updateStatus(nameStatus); 433 return false; 434 } 435 436 IPath path = new Path(name); 437 if (container.getFolder(path).exists() 438 || container.getFile(path).exists()) { 439 updateStatus(IStatus.ERROR, Messages.format( 440 NewWizardMessages.NewFolderDialog_folderNameEmpty_alreadyExists, new Object [] { name })); return false; 442 } 443 updateStatus(IStatus.OK, ""); return true; 445 } 446 } 447 448 private final class CopyFolder { 449 450 public void copy(String sourceDir, String destDir, IWorkspaceRoot workspaceRoot, String name) throws IOException , StringIndexOutOfBoundsException { 451 File source = new File (sourceDir); 452 File dest = new File (destDir); 453 if (!(source.isDirectory() && source.exists())) 454 throw new IOException (); 455 if( !dest.exists() ) 456 dest.mkdir(); 457 copy(source.getPath(), name, source, workspaceRoot); 458 } 459 460 private void copy(String from, String folderName, File source, IWorkspaceRoot workspaceRoot) { 461 File dir[] = new File (from).listFiles(); 462 for (int i = 0; i < dir.length; i++) { 463 if (dir[i].isDirectory()) { 464 createFolder(new Path(dir[i].getPath().substring(source.getPath().length())).makeRelative().toString(), folderName, workspaceRoot); 465 copy(dir[i].getPath(), folderName, source, workspaceRoot); 466 } 467 else { 468 File fileFrom = new File (dir[i].getPath()); 469 try { 470 FileInputStream in = new FileInputStream (fileFrom); 471 createFile(new Path(dir[i].getPath().substring(source.getPath().length())).makeRelative().toString(), in, folderName, workspaceRoot); 472 } 473 catch(FileNotFoundException e) { 474 e.printStackTrace(); 475 } 476 } 477 } 478 } 479 480 private void createFolder(String name, String folderName, IWorkspaceRoot workspaceRoot) { 481 IPath path= container.getFullPath().append(folderName+name); 482 IFolder folderHandle = workspaceRoot.getFolder(path); 483 try { 484 folderHandle.create(false, true, new NullProgressMonitor()); 485 } catch (CoreException e) { 486 JavaPlugin.log(e); 487 } 488 } 489 490 private void createFile(String name, FileInputStream stream, String folderName, IWorkspaceRoot workspaceRoot) { 491 IPath path= container.getFullPath().append(folderName+name); 492 IFile fileHandle= workspaceRoot.getFile(path); 493 try { 494 fileHandle.create(stream, true, null); 495 } catch (CoreException e) { 496 JavaPlugin.log(e); 497 } 498 } 499 } 500 501 private FolderNameField fFolderNameField; 502 protected FolderTypeGroup fFolderTypeGroup; 503 protected DependenciesGroup fDependenciesGroup; 504 private boolean fJavaProjectSelected; 505 private IContainer container; 506 507 517 public ExtendedNewFolderDialog(Shell parentShell, IContainer container, boolean javaProjectSelected) { 518 super(parentShell); 519 this.container = container; 520 this.fJavaProjectSelected= javaProjectSelected; 521 setTitle(NewWizardMessages.NewFolderDialog_title); 522 setShellStyle(getShellStyle() | SWT.RESIZE); 523 setStatusLineAboveButtons(true); 524 } 525 526 531 protected void computeResult() { 532 } 535 536 539 protected void configureShell(Shell shell) { 540 super.configureShell(shell); 541 } 542 543 546 public void create() { 547 super.create(); 548 getButton(IDialogConstants.OK_ID).setEnabled(false); 551 } 552 553 556 protected Control createDialogArea(Composite parent) { 557 Composite composite = (Composite) super.createDialogArea(parent); 558 composite.setLayout(new GridLayout()); 559 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 560 561 Label label= new Label(composite, SWT.NONE); 562 label.setFont(composite.getFont()); 563 label.setText(Messages.format(NewWizardMessages.NewFolderDialog_createIn, container.getFullPath().toString())); 564 565 fFolderNameField= new FolderNameField(composite); 566 567 fFolderTypeGroup= new FolderTypeGroup(composite); fDependenciesGroup= new DependenciesGroup(composite); 569 570 Validator validator= new Validator(); 571 fDependenciesGroup.addObserver(validator); 572 fFolderNameField.addObserver(validator); 573 574 Dialog.applyDialogFont(composite); 575 576 return composite; 577 } 578 579 587 private IFolder createFolderHandle(String folderName) { 588 IWorkspaceRoot workspaceRoot = container.getWorkspace().getRoot(); 589 IPath folderPath = container.getFullPath().append(folderName); 590 IFolder folderHandle = workspaceRoot.getFolder(folderPath); 591 592 return folderHandle; 593 } 594 595 603 private IFolder createNewFolder(final String folderName, final String linkTargetName) { 604 final IFolder folderHandle = createFolderHandle(folderName); 605 final boolean copyFromFolder= fDependenciesGroup.copyTargetSelected(); 606 final boolean linkToFolder= fDependenciesGroup.linkTargetSelected(); 607 final CopyFolder copyFactory= new CopyFolder(); 608 609 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { 610 public void execute(IProgressMonitor monitor) throws CoreException { 611 try { 612 monitor.beginTask(NewWizardMessages.NewFolderDialog_progress, 2000); if (monitor.isCanceled()) 614 throw new OperationCanceledException(); 615 616 if (copyFromFolder) { 617 folderHandle.create(false, true, monitor); 619 copyFactory.copy(fDependenciesGroup.getCopyTarget(), container.getLocation().toOSString()+"\\"+folderName, container.getWorkspace().getRoot(), folderName+"/"); } 621 else if (linkToFolder){ 622 folderHandle.createLink(new Path(fDependenciesGroup.getLinkTarget()), 624 IResource.ALLOW_MISSING_LOCAL, monitor); 625 } 626 else { 627 folderHandle.create(false, true, monitor); 629 } 630 631 if (monitor.isCanceled()) 632 throw new OperationCanceledException(); 633 } catch (IOException e) { 634 e.printStackTrace(); 635 } catch (StringIndexOutOfBoundsException e) { 636 e.printStackTrace(); 637 } 638 finally { 639 monitor.done(); 640 } 641 } 642 }; 643 644 try { 645 new ProgressMonitorDialog(getShell()) 646 .run(true, true, operation); 647 } catch (InterruptedException exception) { 648 return null; 649 } catch (InvocationTargetException exception) { 650 if (exception.getTargetException() instanceof CoreException) { 651 ErrorDialog.openError(getShell(), NewWizardMessages.NewFolderDialog_errorTitle, null, ((CoreException) exception.getTargetException()) 654 .getStatus()); 655 } else { 656 JavaPlugin.log(new Exception (MessageFormat.format( 658 "Exception in {0}.createNewFolder(): {1}", new Object [] { getClass().getName(), 660 exception.getTargetException() }))); 661 MessageDialog.openError(getShell(), NewWizardMessages.NewFolderDialog_errorTitle, Messages.format( 663 NewWizardMessages.NewFolderDialog_internalError, new Object [] { exception.getTargetException() 665 .getMessage() })); 666 } 667 return null; 668 } 669 670 return folderHandle; 671 } 672 673 677 protected void updateStatus(IStatus status) { 678 super.updateStatus(status); 679 } 680 681 687 private void updateStatus(int severity, String message) { 688 updateStatus(new Status(severity, JavaPlugin.getPluginId(), severity, message, null)); 689 } 690 691 694 protected void okPressed() { 695 String linkTarget = fDependenciesGroup.getLinkTarget(); 696 linkTarget= linkTarget.equals("") ? null : linkTarget; IFolder folder = createNewFolder(fFolderNameField.getText(), linkTarget); 698 if (folder == null) 699 return; 700 701 Boolean isSourceFolderType= new Boolean (fFolderTypeGroup.isSourceFolderType()); 702 setSelectionResult(new Object [] {folder, isSourceFolderType}); 703 704 super.okPressed(); 705 } 706 707 713 public IFolder getCreatedFolder() { 714 Object [] result= getResult(); 715 if (result != null && result.length == 2) 716 return (IFolder) result[0]; 717 return null; 718 } 719 720 727 public boolean isSourceFolder() { 728 Object [] result= getResult(); 729 if (result != null && result.length == 2) 730 return ((Boolean )result[1]).booleanValue(); 731 return false; 732 } 733 } 734 | Popular Tags |