KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23
24 import org.eclipse.core.resources.IContainer;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33
34 import org.eclipse.jface.action.IAction;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.dialogs.TrayDialog;
37 import org.eclipse.jface.util.IPropertyChangeListener;
38 import org.eclipse.jface.util.PropertyChangeEvent;
39 import org.eclipse.jface.viewers.ILabelProvider;
40 import org.eclipse.jface.viewers.ITreeContentProvider;
41 import org.eclipse.jface.viewers.ViewerFilter;
42 import org.eclipse.jface.window.Window;
43
44 import org.eclipse.ui.INewWizard;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
47 import org.eclipse.ui.model.WorkbenchLabelProvider;
48
49 import org.eclipse.jdt.core.IClasspathEntry;
50 import org.eclipse.jdt.core.IJavaProject;
51
52 import org.eclipse.jdt.internal.corext.util.Messages;
53
54 import org.eclipse.jdt.ui.PreferenceConstants;
55 import org.eclipse.jdt.ui.actions.AbstractOpenWizardAction;
56
57 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
58 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
59 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
60
61 public class CreateMultipleSourceFoldersDialog extends TrayDialog {
62
63     private final class FakeFolderBaseWorkbenchContentProvider extends BaseWorkbenchContentProvider {
64         /**
65          * {@inheritDoc}
66          */

67         public Object JavaDoc getParent(Object JavaDoc element) {
68             Object JavaDoc object= fNonExistingFolders.get(element);
69             if (object != null)
70                 return object;
71             
72             return super.getParent(element);
73         }
74
75         /**
76          * {@inheritDoc}
77          */

78         public Object JavaDoc[] getChildren(Object JavaDoc element) {
79             List JavaDoc result= new ArrayList JavaDoc();
80             //all keys with value element
81
Set JavaDoc keys= fNonExistingFolders.keySet();
82             for (Iterator JavaDoc iter= keys.iterator(); iter.hasNext();) {
83                 Object JavaDoc key= iter.next();
84                 if (fNonExistingFolders.get(key).equals(element)) {
85                     result.add(key);
86                 }
87             }
88             if (result.size() == 0)
89                 return super.getChildren(element);
90             
91             Object JavaDoc[] children= super.getChildren(element);
92             for (int i= 0; i < children.length; i++) {
93                 result.add(children[i]);
94             }
95             return result.toArray();
96         }
97     }
98
99     private final IJavaProject fJavaProject;
100     private final CPListElement[] fExistingElements;
101     private String JavaDoc fOutputLocation;
102     private final HashSet JavaDoc fRemovedElements;
103     private final HashSet JavaDoc fModifiedElements;
104     private final HashSet JavaDoc fInsertedElements;
105     private final Hashtable JavaDoc fNonExistingFolders;
106
107     public CreateMultipleSourceFoldersDialog(final IJavaProject javaProject, final CPListElement[] existingElements, final String JavaDoc outputLocation, Shell shell) {
108         super(shell);
109         fJavaProject= javaProject;
110         fExistingElements= existingElements;
111         fOutputLocation= outputLocation;
112         fRemovedElements= new HashSet JavaDoc();
113         fModifiedElements= new HashSet JavaDoc();
114         fInsertedElements= new HashSet JavaDoc();
115         fNonExistingFolders= new Hashtable JavaDoc();
116         
117         for (int i= 0; i < existingElements.length; i++) {
118             CPListElement cur= existingElements[i];
119             if (cur.getResource() == null || !cur.getResource().exists()) {
120                 addFakeFolder(fJavaProject.getProject(), cur);
121             }
122         }
123     }
124
125     public int open() {
126         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IProject.class, IFolder.class };
127         List JavaDoc existingContainers= getExistingContainers(fExistingElements);
128
129         IProject[] allProjects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
130         ArrayList JavaDoc rejectedElements= new ArrayList JavaDoc(allProjects.length);
131         IProject currProject= fJavaProject.getProject();
132         for (int i= 0; i < allProjects.length; i++) {
133             if (!allProjects[i].equals(currProject)) {
134                 rejectedElements.add(allProjects[i]);
135             }
136         }
137         ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
138         
139         ILabelProvider lp= new WorkbenchLabelProvider();
140         ITreeContentProvider cp= new FakeFolderBaseWorkbenchContentProvider();
141
142         String JavaDoc title= NewWizardMessages.SourceContainerWorkbookPage_ExistingSourceFolderDialog_new_title;
143         String JavaDoc message= NewWizardMessages.SourceContainerWorkbookPage_ExistingSourceFolderDialog_edit_description;
144
145
146         MultipleFolderSelectionDialog dialog= new MultipleFolderSelectionDialog(getShell(), lp, cp) {
147             protected Control createDialogArea(Composite parent) {
148                 Control result= super.createDialogArea(parent);
149                 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.BP_CHOOSE_EXISTING_FOLDER_TO_MAKE_SOURCE_FOLDER);
150                 return result;
151             }
152             
153             protected Object JavaDoc createFolder(final IContainer container) {
154                 final Object JavaDoc[] result= new Object JavaDoc[1];
155                 final CPListElement newElement= new CPListElement(fJavaProject, IClasspathEntry.CPE_SOURCE);
156                 final AddSourceFolderWizard wizard= newSourceFolderWizard(newElement, fExistingElements, fOutputLocation, container);
157                 AbstractOpenWizardAction action= new AbstractOpenWizardAction() {
158                     protected INewWizard createWizard() throws CoreException {
159                         return wizard;
160                     }
161                 };
162                 action.addPropertyChangeListener(new IPropertyChangeListener() {
163                     public void propertyChange(PropertyChangeEvent event) {
164                         if (event.getProperty().equals(IAction.RESULT)) {
165                             if (event.getNewValue().equals(Boolean.TRUE)) {
166                                 result[0]= addFakeFolder(fJavaProject.getProject(), newElement);
167                             } else {
168                                 wizard.cancel();
169                             }
170                         }
171                     }
172                 });
173                 action.run();
174                 return result[0];
175             }
176         };
177         dialog.setExisting(existingContainers.toArray());
178         dialog.setTitle(title);
179         dialog.setMessage(message);
180         dialog.addFilter(filter);
181         dialog.setInput(fJavaProject.getProject().getParent());
182         dialog.setInitialFocus(fJavaProject.getProject());
183
184         if (dialog.open() == Window.OK) {
185             Object JavaDoc[] elements= dialog.getResult();
186             for (int i= 0; i < elements.length; i++) {
187                 IResource res= (IResource)elements[i];
188                 fInsertedElements.add(new CPListElement(fJavaProject, IClasspathEntry.CPE_SOURCE, res.getFullPath(), res));
189             }
190
191             if (fExistingElements.length == 1) {
192                 CPListElement existingElement= fExistingElements[0];
193                 if (existingElement.getResource() instanceof IProject) {
194                     if (!removeProjectFromBP(existingElement)) {
195                         ArrayList JavaDoc added= new ArrayList JavaDoc(fInsertedElements);
196                         HashSet JavaDoc updatedEclusionPatterns= new HashSet JavaDoc();
197                         addExlusionPatterns(added, updatedEclusionPatterns);
198                         fModifiedElements.addAll(updatedEclusionPatterns);
199                     }
200                 }
201             } else {
202                 ArrayList JavaDoc added= new ArrayList JavaDoc(fInsertedElements);
203                 HashSet JavaDoc updatedEclusionPatterns= new HashSet JavaDoc();
204                 addExlusionPatterns(added, updatedEclusionPatterns);
205                 fModifiedElements.addAll(updatedEclusionPatterns);
206             }
207             return Window.OK;
208         } else {
209             return Window.CANCEL;
210         }
211     }
212
213     public List JavaDoc getInsertedElements() {
214         return new ArrayList JavaDoc(fInsertedElements);
215     }
216
217     public List JavaDoc getRemovedElements() {
218         return new ArrayList JavaDoc(fRemovedElements);
219     }
220
221     public List JavaDoc getModifiedElements() {
222         return new ArrayList JavaDoc(fModifiedElements);
223     }
224
225     public IPath getOutputLocation() {
226         return new Path(fOutputLocation).makeAbsolute();
227     }
228
229     /**
230      * Asks to change the output folder to 'proj/bin' when no source folders were existing
231      */

232     private boolean removeProjectFromBP(CPListElement existing) {
233         IPath outputFolder= new Path(fOutputLocation);
234
235         IPath newOutputFolder= null;
236         String JavaDoc message;
237         if (outputFolder.segmentCount() == 1) {
238             String JavaDoc outputFolderName= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME);
239             newOutputFolder= outputFolder.append(outputFolderName);
240             message= Messages.format(NewWizardMessages.SourceContainerWorkbookPage_ChangeOutputLocationDialog_project_and_output_message, newOutputFolder);
241         } else {
242             message= NewWizardMessages.SourceContainerWorkbookPage_ChangeOutputLocationDialog_project_message;
243         }
244         String JavaDoc title= NewWizardMessages.SourceContainerWorkbookPage_ChangeOutputLocationDialog_title;
245         if (MessageDialog.openQuestion(getShell(), title, message)) {
246             fRemovedElements.add(existing);
247             if (newOutputFolder != null) {
248                 fOutputLocation= newOutputFolder.toString();
249             }
250             return true;
251         }
252         return false;
253     }
254
255     private void addExlusionPatterns(List JavaDoc newEntries, Set JavaDoc modifiedEntries) {
256         BuildPathBasePage.fixNestingConflicts((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]), fExistingElements, modifiedEntries);
257         if (!modifiedEntries.isEmpty()) {
258             String JavaDoc title= NewWizardMessages.SourceContainerWorkbookPage_exclusion_added_title;
259             String JavaDoc message= NewWizardMessages.SourceContainerWorkbookPage_exclusion_added_message;
260             MessageDialog.openInformation(getShell(), title, message);
261         }
262     }
263     
264     private AddSourceFolderWizard newSourceFolderWizard(CPListElement element, CPListElement[] existing, String JavaDoc outputLocation, IContainer parent) {
265         AddSourceFolderWizard wizard= new AddSourceFolderWizard(existing, element, new Path(outputLocation).makeAbsolute(), false, true, false, false, false, parent);
266         wizard.setDoFlushChange(false);
267         return wizard;
268     }
269     
270     private List JavaDoc getExistingContainers(CPListElement[] existingElements) {
271         List JavaDoc res= new ArrayList JavaDoc();
272         for (int i= 0; i < existingElements.length; i++) {
273             IResource resource= existingElements[i].getResource();
274             if (resource instanceof IContainer) {
275                 res.add(resource);
276             }
277         }
278         Set JavaDoc keys= fNonExistingFolders.keySet();
279         for (Iterator JavaDoc iter= keys.iterator(); iter.hasNext();) {
280             IFolder folder= (IFolder)iter.next();
281             res.add(folder);
282         }
283         return res;
284     }
285     
286     private IFolder addFakeFolder(final IContainer container, final CPListElement element) {
287         IFolder result;
288         IPath projectPath= fJavaProject.getPath();
289         IPath path= element.getPath();
290         if (projectPath.isPrefixOf(path)) {
291             path= path.removeFirstSegments(projectPath.segmentCount());
292         }
293         result= container.getFolder(path);
294         IFolder folder= result;
295         do {
296             IContainer parent= folder.getParent();
297             fNonExistingFolders.put(folder, parent);
298             if (parent instanceof IFolder) {
299                 folder= (IFolder)parent;
300             } else {
301                 folder= null;
302             }
303         } while (folder != null && !folder.exists());
304         return result;
305     }
306 }
307
Popular Tags