KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > ExtendedNewFolderDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.text.MessageFormat JavaDoc;
19 import java.util.Observable JavaDoc;
20 import java.util.Observer JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc DIALOGSTORE_LAST_EXTERNAL_LOC= JavaUI.ID_PLUGIN + ".last.external.project"; //$NON-NLS-1$
153

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 JavaDoc getLinkTarget() {
221             return fLinkLocation.getText();
222         }
223         
224         public String JavaDoc 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         /* (non-Javadoc)
237          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter#changeControlPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
238          */

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 JavaDoc directoryName = getLinkTarget().trim();
244             if (directoryName.length() == 0) {
245                 String JavaDoc 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 JavaDoc path = new File JavaDoc(directoryName);
253                 if (path.exists())
254                     dialog.setFilterPath(new Path(directoryName).toOSString());
255             }
256             final String JavaDoc 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         /**
264          * Opens a path variable selection dialog
265          */

266         private void handleVariablesButtonPressed() {
267             int variableTypes = IResource.FOLDER;
268
269             // allow selecting file and folder variables when creating a
270
// linked file
271
/*if (type == IResource.FILE)
272                 variableTypes |= IResource.FILE;*/

273
274             PathVariableSelectionDialog dialog = new PathVariableSelectionDialog(getShell(), variableTypes);
275             if (dialog.open() == IDialogConstants.OK_ID) {
276                 String JavaDoc[] variableNames = (String JavaDoc[]) 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     /**
293      * Validate this page and show appropriate warnings and error NewWizardMessages.
294      */

295     private final class Validator implements Observer JavaDoc {
296
297         public void update(Observable JavaDoc o, Object JavaDoc arg) {
298
299             final String JavaDoc 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, ""); //$NON-NLS-1$
315
}
316         
317         /**
318          * Validates this page's controls.
319          *
320          * @return IStatus indicating the validation result. IStatus.OK if the
321          * specified link target is valid given the linkHandle.
322          */

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             // use the resolved link target name
335
String JavaDoc resolvedLinkTarget = resolveVariable();
336             path = new Path(resolvedLinkTarget);
337             File JavaDoc 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                 // locationStatus takes precedence over missing location warning.
344
return createStatus(
345                         IStatus.WARNING,
346                         NewWizardMessages.NewFolderDialog_linkTargetNonExistent); //$NON-NLS-1$
347
}
348             return locationStatus;
349         }
350         
351         /**
352          * Validates the type of the given file against the link type specified
353          * in the constructor.
354          *
355          * @param linkTargetFile file to validate
356          * @return IStatus indicating the validation result. IStatus.OK if the
357          * given file is valid.
358          */

359         private IStatus validateFileType(File JavaDoc linkTargetFile) {
360             if (linkTargetFile.isDirectory() == false)
361                 return createStatus(IStatus.ERROR, NewWizardMessages.NewFolderDialog_linkTargetNotFolder); //$NON-NLS-1$
362
return createStatus(IStatus.OK, ""); //$NON-NLS-1$
363
}
364         
365         /**
366          * Returns a new status object with the given severity and message.
367          *
368          * @return a new status object with the given severity and message.
369          */

370         private IStatus createStatus(int severity, String JavaDoc message) {
371             return new Status(severity, JavaPlugin.getPluginId(), severity, message, null);
372         }
373         
374         /**
375          * Tries to resolve the value entered in the link target field as
376          * a variable, if the value is a relative path.
377          * Displays the resolved value if the entered value is a variable.
378          */

379         private String JavaDoc 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         /**
388          * Checks whether the link location is valid.
389          * Disable the OK button if the location is not valid valid and display
390          * a message that indicates the problem, otherwise enable the OK button.
391          */

392         private void validateLinkedResource(String JavaDoc 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 JavaDoc text) {
406             IPath path = new Path(text);
407             if (!path.toFile().exists()) {
408                 updateStatus(IStatus.ERROR, Messages.format(
409                         NewWizardMessages.NewFolderDialog_notExists, new Object JavaDoc[] { text })); //$NON-NLS-1$
410
return false;
411             }
412             updateStatus(IStatus.OK, ""); //$NON-NLS-1$
413
return true;
414         }
415         
416         /**
417          * Checks if the folder name is valid.
418          *
419          * @return <code>true</code> if validation was
420          * correct, <code>false</code> otherwise
421          */

422         private boolean validateFolderName(String JavaDoc name) {
423             IWorkspace workspace = container.getWorkspace();
424             IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
425
426             if (name.length() == 0) { //$NON-NLS-1$
427
updateStatus(IStatus.ERROR, NewWizardMessages.NewFolderDialog_folderNameEmpty); //$NON-NLS-1$
428
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 JavaDoc[] { name })); //$NON-NLS-1$
441
return false;
442             }
443             updateStatus(IStatus.OK, ""); //$NON-NLS-1$
444
return true;
445         }
446     }
447     
448     private final class CopyFolder {
449
450         public void copy(String JavaDoc sourceDir, String JavaDoc destDir, IWorkspaceRoot workspaceRoot, String JavaDoc name) throws IOException JavaDoc, StringIndexOutOfBoundsException JavaDoc {
451             File JavaDoc source = new File JavaDoc(sourceDir);
452             File JavaDoc dest = new File JavaDoc(destDir);
453             if (!(source.isDirectory() && source.exists()))
454               throw new IOException JavaDoc();
455             if( !dest.exists() )
456               dest.mkdir();
457             copy(source.getPath(), name, source, workspaceRoot);
458         }
459
460         private void copy(String JavaDoc from, String JavaDoc folderName, File JavaDoc source, IWorkspaceRoot workspaceRoot) {
461             File JavaDoc dir[] = new File JavaDoc(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 JavaDoc fileFrom = new File JavaDoc(dir[i].getPath());
469                     try {
470                        FileInputStream JavaDoc in = new FileInputStream JavaDoc(fileFrom);
471                        createFile(new Path(dir[i].getPath().substring(source.getPath().length())).makeRelative().toString(), in, folderName, workspaceRoot);
472                     }
473                     catch(FileNotFoundException JavaDoc e) {
474                           e.printStackTrace();
475                     }
476                 }
477             }
478         }
479         
480         private void createFolder(String JavaDoc name, String JavaDoc 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 JavaDoc name, FileInputStream JavaDoc stream, String JavaDoc 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     /**
508      * Creates a NewFolderDialog
509      *
510      * @param parentShell parent of the new dialog
511      * @param container parent of the new folder
512      * @param javaProjectSelected <code>true</code> if the parent of the folder to be created
513      * is the Java project, <code>false</code> otherwise.
514      *
515      * @see HintTextGroup
516      */

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     /**
527      * Creates the folder using the name and link target entered
528      * by the user.
529      * Sets the dialog result to the created folder.
530      */

531     protected void computeResult() {
532         //Do nothing here as we
533
//need to know the result
534
}
535
536     /* (non-Javadoc)
537      * Method declared in Window.
538      */

539     protected void configureShell(Shell shell) {
540         super.configureShell(shell);
541     }
542
543     /**
544      * @see org.eclipse.jface.window.Window#create()
545      */

546     public void create() {
547         super.create();
548         // initially disable the ok button since we don't preset the
549
// folder name field
550
getButton(IDialogConstants.OK_ID).setEnabled(false);
551     }
552
553     /* (non-Javadoc)
554      * Method declared on Dialog.
555      */

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); //$NON-NLS-1$
568
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     /**
580      * Creates a folder resource handle for the folder with the given name.
581      * The folder handle is created relative to the container specified during
582      * object creation.
583      *
584      * @param folderName the name of the folder resource to create a handle for
585      * @return the new folder resource handle
586      */

587     private IFolder createFolderHandle(String JavaDoc 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     /**
596      * Creates a new folder with the given name and optionally linking to
597      * the specified link target.
598      *
599      * @param folderName name of the new folder
600      * @param linkTargetName name of the link target folder. may be null.
601      * @return IFolder the new folder
602      */

603     private IFolder createNewFolder(final String JavaDoc folderName, final String JavaDoc 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); //$NON-NLS-1$
613
if (monitor.isCanceled())
614                         throw new OperationCanceledException();
615                     
616                     if (copyFromFolder) {
617                         // copy folder
618
folderHandle.create(false, true, monitor);
619                         copyFactory.copy(fDependenciesGroup.getCopyTarget(), container.getLocation().toOSString()+"\\"+folderName, container.getWorkspace().getRoot(), folderName+"/"); //$NON-NLS-1$ //$NON-NLS-2$
620
}
621                     else if (linkToFolder){
622                         // create link to folder
623
folderHandle.createLink(new Path(fDependenciesGroup.getLinkTarget()),
624                                 IResource.ALLOW_MISSING_LOCAL, monitor);
625                     }
626                     else {
627                         //create normal folder
628
folderHandle.create(false, true, monitor);
629                     }
630                     
631                     if (monitor.isCanceled())
632                         throw new OperationCanceledException();
633                 } catch (IOException JavaDoc e) {
634                     e.printStackTrace();
635                 } catch (StringIndexOutOfBoundsException JavaDoc 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 JavaDoc exception) {
648             return null;
649         } catch (InvocationTargetException JavaDoc exception) {
650             if (exception.getTargetException() instanceof CoreException) {
651                 ErrorDialog.openError(getShell(), NewWizardMessages.NewFolderDialog_errorTitle, //$NON-NLS-1$
652
null, // no special message
653
((CoreException) exception.getTargetException())
654                                 .getStatus());
655             } else {
656                 // CoreExceptions are handled above, but unexpected runtime exceptions and errors may still occur.
657
JavaPlugin.log(new Exception JavaDoc(MessageFormat.format(
658                         "Exception in {0}.createNewFolder(): {1}", //$NON-NLS-1$
659
new Object JavaDoc[] { getClass().getName(),
660                                 exception.getTargetException() })));
661                 MessageDialog.openError(getShell(), NewWizardMessages.NewFolderDialog_errorTitle, //$NON-NLS-1$
662
Messages.format(
663                                 NewWizardMessages.NewFolderDialog_internalError, //$NON-NLS-1$
664
new Object JavaDoc[] { exception.getTargetException()
665                                         .getMessage() }));
666             }
667             return null;
668         }
669         
670         return folderHandle;
671     }
672
673     /**
674      * Update the dialog's status line to reflect the given status. It is safe to call
675      * this method before the dialog has been opened.
676      */

677     protected void updateStatus(IStatus status) {
678         super.updateStatus(status);
679     }
680
681     /**
682      * Update the dialog's status line to reflect the given status. It is safe to call
683      * this method before the dialog has been opened.
684      * @param severity
685      * @param message
686      */

687     private void updateStatus(int severity, String JavaDoc message) {
688         updateStatus(new Status(severity, JavaPlugin.getPluginId(), severity, message, null));
689     }
690     
691     /* (non-Javadoc)
692      * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
693      */

694     protected void okPressed() {
695         String JavaDoc linkTarget = fDependenciesGroup.getLinkTarget();
696         linkTarget= linkTarget.equals("") ? null : linkTarget; //$NON-NLS-1$
697
IFolder folder = createNewFolder(fFolderNameField.getText(), linkTarget);
698         if (folder == null)
699             return;
700         
701         Boolean JavaDoc isSourceFolderType= new Boolean JavaDoc(fFolderTypeGroup.isSourceFolderType());
702         setSelectionResult(new Object JavaDoc[] {folder, isSourceFolderType});
703         
704         super.okPressed();
705     }
706     
707     /**
708      * Returns the created folder or <code>null</code>
709      * if there is none.
710      *
711      * @return created folder or <code>null</code>
712      */

713     public IFolder getCreatedFolder() {
714         Object JavaDoc[] result= getResult();
715         if (result != null && result.length == 2)
716             return (IFolder) result[0];
717         return null;
718     }
719     
720     /**
721      * Returns wheter the new folder is meant
722      * to be a source folder or not.
723      *
724      * @return <code>true</code> if it is a
725      * source folder, <code>false</code> otherwise.
726      */

727     public boolean isSourceFolder() {
728         Object JavaDoc[] result= getResult();
729         if (result != null && result.length == 2)
730             return ((Boolean JavaDoc)result[1]).booleanValue();
731         return false;
732     }
733 }
734
Popular Tags