1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 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 67 public Object getParent(Object element) { 68 Object object= fNonExistingFolders.get(element); 69 if (object != null) 70 return object; 71 72 return super.getParent(element); 73 } 74 75 78 public Object [] getChildren(Object element) { 79 List result= new ArrayList (); 80 Set keys= fNonExistingFolders.keySet(); 82 for (Iterator iter= keys.iterator(); iter.hasNext();) { 83 Object 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 [] 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 fOutputLocation; 102 private final HashSet fRemovedElements; 103 private final HashSet fModifiedElements; 104 private final HashSet fInsertedElements; 105 private final Hashtable fNonExistingFolders; 106 107 public CreateMultipleSourceFoldersDialog(final IJavaProject javaProject, final CPListElement[] existingElements, final String outputLocation, Shell shell) { 108 super(shell); 109 fJavaProject= javaProject; 110 fExistingElements= existingElements; 111 fOutputLocation= outputLocation; 112 fRemovedElements= new HashSet (); 113 fModifiedElements= new HashSet (); 114 fInsertedElements= new HashSet (); 115 fNonExistingFolders= new Hashtable (); 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 [] acceptedClasses= new Class [] { IProject.class, IFolder.class }; 127 List existingContainers= getExistingContainers(fExistingElements); 128 129 IProject[] allProjects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 130 ArrayList rejectedElements= new ArrayList (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 title= NewWizardMessages.SourceContainerWorkbookPage_ExistingSourceFolderDialog_new_title; 143 String 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 createFolder(final IContainer container) { 154 final Object [] result= new Object [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 [] 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 added= new ArrayList (fInsertedElements); 196 HashSet updatedEclusionPatterns= new HashSet (); 197 addExlusionPatterns(added, updatedEclusionPatterns); 198 fModifiedElements.addAll(updatedEclusionPatterns); 199 } 200 } 201 } else { 202 ArrayList added= new ArrayList (fInsertedElements); 203 HashSet updatedEclusionPatterns= new HashSet (); 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 getInsertedElements() { 214 return new ArrayList (fInsertedElements); 215 } 216 217 public List getRemovedElements() { 218 return new ArrayList (fRemovedElements); 219 } 220 221 public List getModifiedElements() { 222 return new ArrayList (fModifiedElements); 223 } 224 225 public IPath getOutputLocation() { 226 return new Path(fOutputLocation).makeAbsolute(); 227 } 228 229 232 private boolean removeProjectFromBP(CPListElement existing) { 233 IPath outputFolder= new Path(fOutputLocation); 234 235 IPath newOutputFolder= null; 236 String message; 237 if (outputFolder.segmentCount() == 1) { 238 String 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 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 newEntries, Set modifiedEntries) { 256 BuildPathBasePage.fixNestingConflicts((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]), fExistingElements, modifiedEntries); 257 if (!modifiedEntries.isEmpty()) { 258 String title= NewWizardMessages.SourceContainerWorkbookPage_exclusion_added_title; 259 String message= NewWizardMessages.SourceContainerWorkbookPage_exclusion_added_message; 260 MessageDialog.openInformation(getShell(), title, message); 261 } 262 } 263 264 private AddSourceFolderWizard newSourceFolderWizard(CPListElement element, CPListElement[] existing, String 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 getExistingContainers(CPListElement[] existingElements) { 271 List res= new ArrayList (); 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 keys= fNonExistingFolders.keySet(); 279 for (Iterator 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 |