KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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;
12
13 import java.io.File JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.filesystem.EFS;
23 import org.eclipse.core.filesystem.IFileStore;
24
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29
30 import org.eclipse.core.resources.IContainer;
31 import org.eclipse.core.resources.IFolder;
32 import org.eclipse.core.resources.IPathVariableManager;
33 import org.eclipse.core.resources.IProject;
34 import org.eclipse.core.resources.IResource;
35 import org.eclipse.core.resources.IWorkspace;
36 import org.eclipse.core.resources.IWorkspaceRoot;
37 import org.eclipse.core.resources.ResourcesPlugin;
38
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.DirectoryDialog;
44
45 import org.eclipse.jface.dialogs.Dialog;
46 import org.eclipse.jface.dialogs.IDialogConstants;
47 import org.eclipse.jface.viewers.ILabelProvider;
48 import org.eclipse.jface.viewers.ITreeContentProvider;
49 import org.eclipse.jface.viewers.ViewerFilter;
50 import org.eclipse.jface.window.Window;
51
52 import org.eclipse.ui.PlatformUI;
53 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
54 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
55 import org.eclipse.ui.model.WorkbenchContentProvider;
56 import org.eclipse.ui.model.WorkbenchLabelProvider;
57
58 import org.eclipse.ui.views.navigator.ResourceComparator;
59
60 import org.eclipse.ui.ide.dialogs.PathVariableSelectionDialog;
61
62 import org.eclipse.jdt.core.IClasspathEntry;
63 import org.eclipse.jdt.core.IJavaModelStatus;
64 import org.eclipse.jdt.core.IJavaProject;
65 import org.eclipse.jdt.core.JavaConventions;
66
67 import org.eclipse.jdt.internal.corext.util.Messages;
68
69 import org.eclipse.jdt.ui.JavaUI;
70 import org.eclipse.jdt.ui.PreferenceConstants;
71 import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
72
73 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
74 import org.eclipse.jdt.internal.ui.JavaPlugin;
75 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
76 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
77 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
78 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
79 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
80 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
81 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
82 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
83 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
84 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
85 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
86
87
88 public class AddSourceFolderWizardPage extends NewElementWizardPage {
89     
90     private final class LinkFields implements IStringButtonAdapter, IDialogFieldListener{
91         private StringButtonDialogField fLinkLocation;
92         
93         private static final String JavaDoc DIALOGSTORE_LAST_EXTERNAL_LOC= JavaUI.ID_PLUGIN + ".last.external.project"; //$NON-NLS-1$
94

95         private RootFieldAdapter fAdapter;
96
97         private SelectionButtonDialogField fVariables;
98         
99         public LinkFields() {
100             fLinkLocation= new StringButtonDialogField(this);
101             
102             fLinkLocation.setLabelText(NewWizardMessages.LinkFolderDialog_dependenciesGroup_locationLabel_desc);
103             fLinkLocation.setButtonLabel(NewWizardMessages.LinkFolderDialog_dependenciesGroup_browseButton_desc);
104             fLinkLocation.setDialogFieldListener(this);
105             
106             fVariables= new SelectionButtonDialogField(SWT.PUSH);
107             fVariables.setLabelText(NewWizardMessages.LinkFolderDialog_dependenciesGroup_variables_desc);
108             fVariables.setDialogFieldListener(new IDialogFieldListener() {
109                 public void dialogFieldChanged(DialogField field) {
110                     handleVariablesButtonPressed();
111                 }
112             });
113         }
114         
115         public void setDialogFieldListener(RootFieldAdapter adapter) {
116             fAdapter= adapter;
117         }
118         
119         private void doFillIntoGrid(Composite parent, int numColumns) {
120             fLinkLocation.doFillIntoGrid(parent, numColumns);
121             
122             LayoutUtil.setHorizontalSpan(fLinkLocation.getLabelControl(null), numColumns);
123             LayoutUtil.setHorizontalGrabbing(fLinkLocation.getTextControl(null));
124             
125             fVariables.doFillIntoGrid(parent, 1);
126         }
127         
128         public IPath getLinkTarget() {
129             return Path.fromOSString(fLinkLocation.getText());
130         }
131         
132         public void setLinkTarget(IPath path) {
133             fLinkLocation.setText(path.toOSString());
134         }
135         
136         /*(non-Javadoc)
137          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter#changeControlPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
138          */

139         public void changeControlPressed(DialogField field) {
140             final DirectoryDialog dialog= new DirectoryDialog(getShell());
141             dialog.setMessage(NewWizardMessages.JavaProjectWizardFirstPage_directory_message);
142             String JavaDoc directoryName = fLinkLocation.getText().trim();
143             if (directoryName.length() == 0) {
144                 String JavaDoc prevLocation= JavaPlugin.getDefault().getDialogSettings().get(DIALOGSTORE_LAST_EXTERNAL_LOC);
145                 if (prevLocation != null) {
146                     directoryName= prevLocation;
147                 }
148             }
149             
150             if (directoryName.length() > 0) {
151                 final File JavaDoc path = new File JavaDoc(directoryName);
152                 if (path.exists())
153                     dialog.setFilterPath(directoryName);
154             }
155             final String JavaDoc selectedDirectory = dialog.open();
156             if (selectedDirectory != null) {
157                 fLinkLocation.setText(selectedDirectory);
158                 fRootDialogField.setText(selectedDirectory.substring(selectedDirectory.lastIndexOf(File.separatorChar) + 1));
159                 JavaPlugin.getDefault().getDialogSettings().put(DIALOGSTORE_LAST_EXTERNAL_LOC, selectedDirectory);
160                 if (fAdapter != null) {
161                     fAdapter.dialogFieldChanged(fRootDialogField);
162                 }
163             }
164         }
165         
166         /**
167          * Opens a path variable selection dialog
168          */

169         private void handleVariablesButtonPressed() {
170             int variableTypes = IResource.FOLDER;
171             PathVariableSelectionDialog dialog = new PathVariableSelectionDialog(getShell(), variableTypes);
172             if (dialog.open() == IDialogConstants.OK_ID) {
173                 String JavaDoc[] variableNames = (String JavaDoc[]) dialog.getResult();
174                 if (variableNames != null && variableNames.length == 1) {
175                     fLinkLocation.setText(variableNames[0]);
176                     fRootDialogField.setText(variableNames[0]);
177                     if (fAdapter != null) {
178                         fAdapter.dialogFieldChanged(fRootDialogField);
179                     }
180                 }
181             }
182         }
183         
184         public void dialogFieldChanged(DialogField field) {
185             if (fAdapter != null) {
186                 fAdapter.dialogFieldChanged(fLinkLocation);
187             }
188         }
189     }
190         
191     private static final String JavaDoc PAGE_NAME= "NewSourceFolderWizardPage"; //$NON-NLS-1$
192

193     private final StringDialogField fRootDialogField;
194     private final SelectionButtonDialogField fAddExclusionPatterns, fRemoveProjectFolder, fIgnoreConflicts;
195     private final LinkFields fLinkFields;
196     
197     private final CPListElement fNewElement;
198     private final List JavaDoc/*<CPListElement>*/ fExistingEntries;
199     private final Hashtable JavaDoc/*<CPListElement, IPath[]>*/ fOrginalExlusionFilters, fOrginalInclusionFilters, fOrginalExlusionFiltersCopy, fOrginalInclusionFiltersCopy;
200     private final IPath fOrginalPath;
201     private final boolean fLinkedMode;
202     
203     private IPath fOutputLocation;
204     private IPath fNewOutputLocation;
205     private CPListElement fOldProjectSourceFolder;
206
207     private List JavaDoc fModifiedElements;
208     private List JavaDoc fRemovedElements;
209
210     private final boolean fAllowConflict;
211     private final boolean fAllowRemoveProjectFolder;
212     private final boolean fAllowAddExclusionPatterns;
213     private final boolean fCanCommitConflictingBuildpath;
214     private final IContainer fParent;
215     
216     public AddSourceFolderWizardPage(CPListElement newElement, List JavaDoc/*<CPListElement>*/ existingEntries, IPath outputLocation,
217             boolean linkedMode, boolean canCommitConflictingBuildpath,
218             boolean allowIgnoreConflicts, boolean allowRemoveProjectFolder, boolean allowAddExclusionPatterns, IContainer parent) {
219         
220         super(PAGE_NAME);
221         
222         fLinkedMode= linkedMode;
223         fCanCommitConflictingBuildpath= canCommitConflictingBuildpath;
224         fAllowConflict= allowIgnoreConflicts;
225         fAllowRemoveProjectFolder= allowRemoveProjectFolder;
226         fAllowAddExclusionPatterns= allowAddExclusionPatterns;
227         fParent= parent;
228                 
229         fOrginalExlusionFilters= new Hashtable JavaDoc();
230         fOrginalInclusionFilters= new Hashtable JavaDoc();
231         fOrginalExlusionFiltersCopy= new Hashtable JavaDoc();
232         fOrginalInclusionFiltersCopy= new Hashtable JavaDoc();
233         for (Iterator JavaDoc iter= existingEntries.iterator(); iter.hasNext();) {
234             CPListElement element= (CPListElement)iter.next();
235             IPath[] exlusions= (IPath[])element.getAttribute(CPListElement.EXCLUSION);
236             if (exlusions != null) {
237                 IPath[] save= new IPath[exlusions.length];
238                 for (int i= 0; i < save.length; i++) {
239                     save[i]= exlusions[i];
240                 }
241                 fOrginalExlusionFiltersCopy.put(element, save);
242                 fOrginalExlusionFilters.put(element, exlusions);
243             }
244             IPath[] inclusions= (IPath[])element.getAttribute(CPListElement.INCLUSION);
245             if (inclusions != null) {
246                 IPath[] save= new IPath[inclusions.length];
247                 for (int i= 0; i < save.length; i++) {
248                     save[i]= inclusions[i];
249                 }
250                 fOrginalInclusionFiltersCopy.put(element, save);
251                 fOrginalInclusionFilters.put(element, inclusions);
252             }
253         }
254         
255         setTitle(NewWizardMessages.NewSourceFolderWizardPage_title);
256         fOrginalPath= newElement.getPath();
257         if (fOrginalPath == null) {
258             if (linkedMode) {
259                 setDescription(Messages.format(NewWizardMessages.NewFolderDialog_createIn, newElement.getJavaProject().getElementName()));
260             } else {
261                 setDescription(Messages.format(NewWizardMessages.AddSourceFolderWizardPage_description, fParent.getFullPath().toString()));
262             }
263         } else {
264             setDescription(NewWizardMessages.NewSourceFolderWizardPage_edit_description);
265         }
266         
267         fNewElement= newElement;
268         fExistingEntries= existingEntries;
269         fModifiedElements= new ArrayList JavaDoc();
270         fRemovedElements= new ArrayList JavaDoc();
271         fOutputLocation= outputLocation;
272         
273         RootFieldAdapter adapter= new RootFieldAdapter();
274         
275         fRootDialogField= new StringDialogField();
276         fRootDialogField.setLabelText(NewWizardMessages.NewSourceFolderWizardPage_root_label);
277         if (fNewElement.getPath() == null) {
278             fRootDialogField.setText(""); //$NON-NLS-1$
279
} else {
280             setFolderDialogText(fNewElement.getPath());
281         }
282         fRootDialogField.setEnabled(fNewElement.getJavaProject() != null);
283         
284         int buttonStyle= SWT.CHECK;
285         if ((fAllowConflict && fAllowAddExclusionPatterns) ||
286             (fAllowConflict && fAllowRemoveProjectFolder) ||
287             (fAllowAddExclusionPatterns && fAllowRemoveProjectFolder)) {
288             buttonStyle= SWT.RADIO;
289         }
290         
291         fAddExclusionPatterns= new SelectionButtonDialogField(buttonStyle);
292         fAddExclusionPatterns.setLabelText(NewWizardMessages.NewSourceFolderWizardPage_exclude_label);
293         fAddExclusionPatterns.setSelection(!fCanCommitConflictingBuildpath && !fAllowRemoveProjectFolder);
294         
295         fRemoveProjectFolder= new SelectionButtonDialogField(buttonStyle);
296         fRemoveProjectFolder.setLabelText(NewWizardMessages.NewSourceFolderWizardPage_ReplaceExistingSourceFolder_label);
297         fRemoveProjectFolder.setSelection(!fCanCommitConflictingBuildpath && fAllowRemoveProjectFolder);
298         
299         fIgnoreConflicts= new SelectionButtonDialogField(buttonStyle);
300         fIgnoreConflicts.setLabelText(NewWizardMessages.AddSourceFolderWizardPage_ignoreNestingConflicts);
301         fIgnoreConflicts.setSelection(fCanCommitConflictingBuildpath);
302         
303         fLinkFields= new LinkFields();
304         if (fNewElement.getLinkTarget() != null) {
305             fLinkFields.setLinkTarget(fNewElement.getLinkTarget());
306         }
307         
308         fRemoveProjectFolder.setDialogFieldListener(adapter);
309         fAddExclusionPatterns.setDialogFieldListener(adapter);
310         fIgnoreConflicts.setDialogFieldListener(adapter);
311         fRootDialogField.setDialogFieldListener(adapter);
312         fLinkFields.setDialogFieldListener(adapter);
313         
314         packRootDialogFieldChanged();
315     }
316
317     // -------- UI Creation ---------
318

319     /*
320      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
321      */

322     public void createControl(Composite parent) {
323         initializeDialogUnits(parent);
324         
325         Composite composite= new Composite(parent, SWT.NONE);
326             
327         GridLayout layout= new GridLayout();
328         layout.numColumns= 4;
329         composite.setLayout(layout);
330         
331         if (fLinkedMode) {
332             fLinkFields.doFillIntoGrid(composite, layout.numColumns);
333             fRootDialogField.doFillIntoGrid(composite, layout.numColumns - 1);
334         } else {
335             fRootDialogField.doFillIntoGrid(composite, layout.numColumns - 1);
336         }
337         
338         if (fAllowRemoveProjectFolder)
339             fRemoveProjectFolder.doFillIntoGrid(composite, layout.numColumns);
340         
341         if (fAllowAddExclusionPatterns)
342             fAddExclusionPatterns.doFillIntoGrid(composite, layout.numColumns);
343         
344         if (fAllowConflict)
345             fIgnoreConflicts.doFillIntoGrid(composite, layout.numColumns);
346         
347         LayoutUtil.setHorizontalSpan(fRootDialogField.getLabelControl(null), layout.numColumns);
348         LayoutUtil.setHorizontalGrabbing(fRootDialogField.getTextControl(null));
349             
350         setControl(composite);
351         Dialog.applyDialogFont(composite);
352         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_PACKAGEROOT_WIZARD_PAGE);
353     }
354     
355     /*
356      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
357      */

358     public void setVisible(boolean visible) {
359         super.setVisible(visible);
360         if (visible) {
361             fRootDialogField.setFocus();
362         }
363     }
364         
365     // -------- ContainerFieldAdapter --------
366

367     private class RootFieldAdapter implements IStringButtonAdapter, IDialogFieldListener {
368
369         // -------- IStringButtonAdapter
370
public void changeControlPressed(DialogField field) {
371             packRootChangeControlPressed(field);
372         }
373         
374         // -------- IDialogFieldListener
375
public void dialogFieldChanged(DialogField field) {
376             packRootDialogFieldChanged();
377         }
378     }
379     
380     protected void packRootChangeControlPressed(DialogField field) {
381         if (field == fRootDialogField) {
382             IPath initialPath= new Path(fRootDialogField.getText());
383             String JavaDoc title= NewWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_title;
384             String JavaDoc message= NewWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_description;
385             IFolder folder= chooseFolder(title, message, initialPath);
386             if (folder != null) {
387                 setFolderDialogText(folder.getFullPath());
388             }
389         }
390     }
391
392     private void setFolderDialogText(IPath path) {
393         IPath shortPath= path.removeFirstSegments(1);
394         fRootDialogField.setText(shortPath.toString());
395     }
396     
397     protected void packRootDialogFieldChanged() {
398         StatusInfo status= updateRootStatus();
399         updateStatus(new IStatus[] {status});
400     }
401
402     private StatusInfo updateRootStatus() {
403         IJavaProject javaProject= fNewElement.getJavaProject();
404         IProject project= javaProject.getProject();
405         
406         StatusInfo pathNameStatus= validatePathName(fRootDialogField.getText(), fParent);
407         
408         if (!pathNameStatus.isOK())
409             return pathNameStatus;
410         
411         if (fLinkedMode) {
412             IStatus linkNameStatus= validateLinkLocation(fRootDialogField.getText());
413             if (linkNameStatus.matches(IStatus.ERROR)) {
414                 StatusInfo result= new StatusInfo();
415                 result.setError(linkNameStatus.getMessage());
416                 return result;
417             }
418         }
419         
420         StatusInfo result= new StatusInfo();
421         result.setOK();
422
423         IPath projPath= project.getFullPath();
424         IPath path= fParent.getFullPath().append(fRootDialogField.getText());
425
426         restoreCPElements();
427         
428         int projectEntryIndex= -1;
429         boolean createFolderForExisting= false;
430         
431         IFolder folder= fParent.getFolder(new Path(fRootDialogField.getText()));
432         for (int i= 0; i < fExistingEntries.size(); i++) {
433             IClasspathEntry curr= ((CPListElement)fExistingEntries.get(i)).getClasspathEntry();
434             if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
435                 if (path.equals(curr.getPath()) && fExistingEntries.get(i) != fNewElement) {
436                     if (folder.exists()) {
437                         result.setError(NewWizardMessages.NewSourceFolderWizardPage_error_AlreadyExisting);
438                         return result;
439                     } else {
440                         createFolderForExisting= true;
441                     }
442                 }
443                 if (projPath.equals(curr.getPath())) {
444                     projectEntryIndex= i;
445                 }
446             }
447         }
448         
449         if (folder.exists() && !folder.getFullPath().equals(fOrginalPath))
450             return new StatusInfo(IStatus.ERROR, Messages.format(NewWizardMessages.NewFolderDialog_folderNameEmpty_alreadyExists, folder.getFullPath().toString()));
451
452         boolean isProjectASourceFolder= projectEntryIndex != -1;
453         
454         fModifiedElements.clear();
455         updateFilters(fNewElement.getPath(), path);
456         
457         fNewElement.setPath(path);
458         if (fLinkedMode) {
459             fNewElement.setLinkTarget(fLinkFields.getLinkTarget());
460         }
461         fRemovedElements.clear();
462         Set JavaDoc modified= new HashSet JavaDoc();
463         boolean isProjectSourceFolderReplaced= false;
464         if (fAddExclusionPatterns.isSelected()) {
465             if (fOrginalPath == null) {
466                 addExclusionPatterns(fNewElement, fExistingEntries, modified);
467                 fModifiedElements.addAll(modified);
468                 if (!createFolderForExisting)
469                     CPListElement.insert(fNewElement, fExistingEntries);
470             }
471         } else {
472             if (isProjectASourceFolder) {
473                 if (fRemoveProjectFolder.isSelected()) {
474                     fOldProjectSourceFolder= (CPListElement)fExistingEntries.get(projectEntryIndex);
475                     fRemovedElements.add(fOldProjectSourceFolder);
476                     fExistingEntries.set(projectEntryIndex, fNewElement);
477                     isProjectSourceFolderReplaced= true;
478                 } else {
479                     if (!createFolderForExisting)
480                     CPListElement.insert(fNewElement, fExistingEntries);
481                 }
482             } else {
483                 if (!createFolderForExisting)
484                     CPListElement.insert(fNewElement, fExistingEntries);
485             }
486         }
487         
488         if ((!fAllowConflict && fCanCommitConflictingBuildpath) || createFolderForExisting)
489             return new StatusInfo();
490         
491         fNewOutputLocation= null;
492         IJavaModelStatus status= JavaConventions.validateClasspath(javaProject, CPListElement.convertToClasspathEntries(fExistingEntries), fOutputLocation);
493         if (!status.isOK()) {
494             if (fOutputLocation.equals(projPath)) {
495                 //Try to change the output folder
496
fNewOutputLocation= projPath.append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
497                 IStatus status2= JavaConventions.validateClasspath(javaProject, CPListElement.convertToClasspathEntries(fExistingEntries), fNewOutputLocation);
498                 if (status2.isOK()) {
499                     if (isProjectSourceFolderReplaced) {
500                         result.setInfo(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSFandOL, fNewOutputLocation.makeRelative().toString()));
501                     } else {
502                         result.setInfo(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceOL, fNewOutputLocation.makeRelative().toString()));
503                     }
504                     return result;
505                 }
506             }
507             //Don't know what the problem is, report to user
508
fNewOutputLocation= null;
509             if (fCanCommitConflictingBuildpath) {
510                 result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_conflictWarning + status.getMessage());
511             } else {
512                 result.setError(status.getMessage());
513             }
514             return result;
515         }
516         if (!modified.isEmpty()) {
517             //Added exclusion patterns to solve problem
518
if (modified.size() == 1) {
519                 CPListElement elem= (CPListElement)modified.toArray()[0];
520                 IPath changed= elem.getPath().makeRelative();
521                 IPath excl= fNewElement.getPath().makeRelative();
522                 result.setInfo(Messages.format(NewWizardMessages.AddSourceFolderWizardPage_addSinglePattern, new Object JavaDoc[] {excl, changed}));
523             } else {
524                 result.setInfo(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions, String.valueOf(modified.size())));
525             }
526             return result;
527         }
528         if (isProjectSourceFolderReplaced) {
529             result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_replaceSourceFolderInfo);
530             return result;
531         }
532         
533         return result;
534     }
535     
536     public void restore() {
537         for (Iterator JavaDoc iter= fExistingEntries.iterator(); iter.hasNext();) {
538             CPListElement element= (CPListElement)iter.next();
539             if (fOrginalExlusionFilters.containsKey(element)) {
540                 element.setAttribute(CPListElement.EXCLUSION, fOrginalExlusionFiltersCopy.get(element));
541             }
542             if (fOrginalInclusionFilters.containsKey(element)) {
543                 element.setAttribute(CPListElement.INCLUSION, fOrginalInclusionFiltersCopy.get(element));
544             }
545         }
546         fNewElement.setPath(fOrginalPath);
547     }
548
549     private void restoreCPElements() {
550         if (fNewElement.getPath() != null) {
551             for (Iterator JavaDoc iter= fExistingEntries.iterator(); iter.hasNext();) {
552                 CPListElement element= (CPListElement)iter.next();
553                 if (fOrginalExlusionFilters.containsKey(element)) {
554                     element.setAttribute(CPListElement.EXCLUSION, fOrginalExlusionFilters.get(element));
555                 }
556                 if (fOrginalInclusionFilters.containsKey(element)) {
557                     element.setAttribute(CPListElement.INCLUSION, fOrginalInclusionFilters.get(element));
558                 }
559             }
560             
561             if (fOldProjectSourceFolder != null) {
562                 fExistingEntries.set(fExistingEntries.indexOf(fNewElement), fOldProjectSourceFolder);
563                 fOldProjectSourceFolder= null;
564             } else if (fExistingEntries.contains(fNewElement)) {
565                 fExistingEntries.remove(fNewElement);
566             }
567         }
568     }
569     
570     private void updateFilters(IPath oldPath, IPath newPath) {
571         if (oldPath == null)
572             return;
573         
574         IPath projPath= fNewElement.getJavaProject().getProject().getFullPath();
575         if (projPath.isPrefixOf(oldPath)) {
576             oldPath= oldPath.removeFirstSegments(projPath.segmentCount()).addTrailingSeparator();
577         }
578         if (projPath.isPrefixOf(newPath)) {
579             newPath= newPath.removeFirstSegments(projPath.segmentCount()).addTrailingSeparator();
580         }
581         
582         for (Iterator JavaDoc iter= fExistingEntries.iterator(); iter.hasNext();) {
583             CPListElement element= (CPListElement)iter.next();
584             IPath elementPath= element.getPath();
585             if (projPath.isPrefixOf(elementPath)) {
586                 elementPath= elementPath.removeFirstSegments(projPath.segmentCount());
587                 if (elementPath.segmentCount() > 0)
588                     elementPath= elementPath.addTrailingSeparator();
589             }
590             
591             IPath[] exlusions= (IPath[])element.getAttribute(CPListElement.EXCLUSION);
592             if (exlusions != null) {
593                 for (int i= 0; i < exlusions.length; i++) {
594                     if (elementPath.append(exlusions[i]).equals(oldPath)) {
595                         fModifiedElements.add(element);
596                         exlusions[i]= newPath.removeFirstSegments(elementPath.segmentCount());
597                     }
598                 }
599                 element.setAttribute(CPListElement.EXCLUSION, exlusions);
600             }
601             
602             IPath[] inclusion= (IPath[])element.getAttribute(CPListElement.INCLUSION);
603             if (inclusion != null) {
604                 for (int i= 0; i < inclusion.length; i++) {
605                     if (elementPath.append(inclusion[i]).equals(oldPath)) {
606                         fModifiedElements.add(element);
607                         inclusion[i]= newPath.removeFirstSegments(elementPath.segmentCount());
608                     }
609                 }
610                 element.setAttribute(CPListElement.INCLUSION, inclusion);
611             }
612         }
613     }
614     
615     /**
616      * Validates this page's controls.
617      *
618      * @return IStatus indicating the validation result. IStatus.OK if the
619      * specified link target is valid given the linkHandle.
620      */

621     private IStatus validateLinkLocation(String JavaDoc folderName) {
622         IWorkspace workspace= JavaPlugin.getWorkspace();
623         IPath path= Path.fromOSString(fLinkFields.fLinkLocation.getText());
624
625         IFolder folder= fNewElement.getJavaProject().getProject().getFolder(new Path(folderName));
626         IStatus locationStatus= workspace.validateLinkLocation(folder, path);
627         if (locationStatus.matches(IStatus.ERROR))
628             return locationStatus;
629         
630         IPathVariableManager pathVariableManager = ResourcesPlugin.getWorkspace().getPathVariableManager();
631         IPath path1= Path.fromOSString(fLinkFields.fLinkLocation.getText());
632         IPath resolvedPath= pathVariableManager.resolvePath(path1);
633         // use the resolved link target name
634
String JavaDoc resolvedLinkTarget= resolvedPath.toOSString();
635         
636         path= new Path(resolvedLinkTarget);
637         File JavaDoc linkTargetFile= new Path(resolvedLinkTarget).toFile();
638         if (linkTargetFile.exists()) {
639             if (!linkTargetFile.isDirectory())
640                 return new StatusInfo(IStatus.ERROR, NewWizardMessages.NewFolderDialog_linkTargetNotFolder);
641         } else {
642             return new StatusInfo(IStatus.ERROR, NewWizardMessages.NewFolderDialog_linkTargetNonExistent);
643         }
644         if (locationStatus.isOK()) {
645             return new StatusInfo();
646         }
647         return new StatusInfo(locationStatus.getSeverity(), locationStatus.getMessage());
648     }
649
650     private static StatusInfo validatePathName(String JavaDoc str, IContainer parent) {
651         StatusInfo result= new StatusInfo();
652         result.setOK();
653
654         IPath parentPath= parent.getFullPath();
655         
656         if (str.length() == 0) {
657             result.setError(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_error_EnterRootName, parentPath.toString()));
658             return result;
659         }
660         
661         IPath path= parentPath.append(str);
662
663         IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
664         IStatus validate= workspaceRoot.getWorkspace().validatePath(path.toString(), IResource.FOLDER);
665         if (validate.matches(IStatus.ERROR)) {
666             result.setError(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_error_InvalidRootName, validate.getMessage()));
667             return result;
668         }
669             
670         IResource res= workspaceRoot.findMember(path);
671         if (res != null) {
672             if (res.getType() != IResource.FOLDER) {
673                 result.setError(NewWizardMessages.NewSourceFolderWizardPage_error_NotAFolder);
674                 return result;
675             }
676         } else {
677             
678             URI JavaDoc parentLocation= parent.getLocationURI();
679             if (parentLocation != null) {
680                 try {
681                     IFileStore store= EFS.getStore(parentLocation).getChild(str);
682                     if (store.fetchInfo().exists()) {
683                         result.setError(NewWizardMessages.NewSourceFolderWizardPage_error_AlreadyExistingDifferentCase);
684                         return result;
685                     }
686                 } catch (CoreException e) {
687                     // we couldn't create the file store. Ignore the exception
688
// since we can't check if the file exist. Pretend that it
689
// doesn't.
690
}
691             }
692         }
693         
694         return result;
695     }
696     
697     private void addExclusionPatterns(CPListElement newEntry, List JavaDoc existing, Set JavaDoc modifiedEntries) {
698         IPath entryPath= newEntry.getPath();
699         for (int i= 0; i < existing.size(); i++) {
700             CPListElement curr= (CPListElement) existing.get(i);
701             IPath currPath= curr.getPath();
702             if (curr != newEntry && curr.getEntryKind() == IClasspathEntry.CPE_SOURCE && currPath.isPrefixOf(entryPath)) {
703                 boolean added= curr.addToExclusions(entryPath);
704                 if (added) {
705                     modifiedEntries.add(curr);
706                 }
707             }
708         }
709     }
710     
711     public IResource getCorrespondingResource() {
712         return fParent.getFolder(new Path(fRootDialogField.getText()));
713     }
714     
715     public IPath getOutputLocation() {
716         if (fNewOutputLocation != null) {
717             return fNewOutputLocation;
718         }
719         
720         return fOutputLocation;
721     }
722     
723     // ------------- choose dialogs
724

725     private IFolder chooseFolder(String JavaDoc title, String JavaDoc message, IPath initialPath) {
726         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IFolder.class };
727         ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
728         ViewerFilter filter= new TypedViewerFilter(acceptedClasses, null);
729         
730         ILabelProvider lp= new WorkbenchLabelProvider();
731         ITreeContentProvider cp= new WorkbenchContentProvider();
732
733         IProject currProject= fNewElement.getJavaProject().getProject();
734
735         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), lp, cp) {
736             protected Control createDialogArea(Composite parent) {
737                 Control result= super.createDialogArea(parent);
738                 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.BP_CHOOSE_EXISTING_FOLDER_TO_MAKE_SOURCE_FOLDER);
739                 return result;
740             }
741         };
742         dialog.setValidator(validator);
743         dialog.setTitle(title);
744         dialog.setMessage(message);
745         dialog.addFilter(filter);
746         dialog.setInput(currProject);
747         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
748         IResource res= currProject.findMember(initialPath);
749         if (res != null) {
750             dialog.setInitialSelection(res);
751         }
752
753         if (dialog.open() == Window.OK) {
754             return (IFolder) dialog.getFirstResult();
755         }
756         return null;
757     }
758
759     public List JavaDoc getModifiedElements() {
760         if (fOrginalPath != null && !fModifiedElements.contains(fNewElement))
761             fModifiedElements.add(fNewElement);
762         
763         return fModifiedElements;
764     }
765     
766     public List JavaDoc getRemovedElements() {
767         return fRemovedElements;
768     }
769         
770 }
771
Popular Tags