1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 23 import org.eclipse.core.resources.IFolder; 24 import org.eclipse.core.resources.IResource; 25 26 import org.eclipse.swt.widgets.Shell; 27 28 import org.eclipse.jface.operation.IRunnableContext; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.window.Window; 33 34 import org.eclipse.ui.IWorkbenchSite; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.part.ISetSelectionTarget; 37 38 import org.eclipse.jdt.core.IClasspathEntry; 39 import org.eclipse.jdt.core.IJavaElement; 40 import org.eclipse.jdt.core.IJavaProject; 41 import org.eclipse.jdt.core.IPackageFragmentRoot; 42 import org.eclipse.jdt.core.JavaModelException; 43 44 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 45 import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject; 46 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaPluginImages; 51 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer; 52 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IRemoveLinkedFolderQuery; 55 56 public class RemoveFromBuildpathAction extends BuildpathModifierAction { 58 59 private final IRunnableContext fContext; 60 61 public RemoveFromBuildpathAction(IWorkbenchSite site) { 62 this(site, null, PlatformUI.getWorkbench().getProgressService()); 63 64 setImageDescriptor(JavaPluginImages.DESC_ELCL_REMOVE_FROM_BP); 65 } 66 67 public RemoveFromBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 68 this(null, selectionTarget, context); 69 70 setImageDescriptor(JavaPluginImages.DESC_ELCL_REMOVE_AS_SOURCE_FOLDER); 71 setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_REMOVE_AS_SOURCE_FOLDER); 72 } 73 74 public RemoveFromBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 75 super(site, selectionTarget, BuildpathModifierAction.REMOVE_FROM_BP); 76 77 fContext= context; 78 79 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_RemoveFromCP_label); 80 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_RemoveFromCP_tooltip); 81 } 82 83 86 public String getDetailedDescription() { 87 if (!isEnabled()) 88 return null; 89 90 if (getSelectedElements().size() != 1) 91 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_FromBuildpath; 92 93 Object elem= getSelectedElements().get(0); 94 95 if (elem instanceof IJavaProject) { 96 String name= ClasspathModifier.escapeSpecialChars(((IJavaElement)elem).getElementName()); 97 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectFromBuildpath, name); 98 } else if (elem instanceof IPackageFragmentRoot) { 99 String name= ClasspathModifier.escapeSpecialChars(((IJavaElement)elem).getElementName()); 100 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_fromBuildpath, name); 101 } else if (elem instanceof ClassPathContainer) { 102 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_FromBuildpath; 103 } 104 105 return null; 106 } 107 108 111 public void run() { 112 try { 113 114 final IJavaProject project; 115 Object object= getSelectedElements().get(0); 116 if (object instanceof IJavaProject) { 117 project= (IJavaProject)object; 118 } else if (object instanceof IPackageFragmentRoot) { 119 IPackageFragmentRoot root= (IPackageFragmentRoot)object; 120 project= root.getJavaProject(); 121 } else { 122 ClassPathContainer container= (ClassPathContainer)object; 123 project= container.getJavaProject(); 124 } 125 126 final List elementsToRemove= new ArrayList (); 127 final List foldersToDelete= new ArrayList (); 128 queryToRemoveLinkedFolders(elementsToRemove, foldersToDelete); 129 130 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 131 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 132 try { 133 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_RemoveFromBuildpath, foldersToDelete.size() + 10); 134 135 CPJavaProject cpProject= CPJavaProject.createFromExisting(project); 136 CPListElement[] toRemove= new CPListElement[elementsToRemove.size()]; 137 int i= 0; 138 for (Iterator iterator= elementsToRemove.iterator(); iterator.hasNext();) { 139 Object element= iterator.next(); 140 if (element instanceof IJavaProject) { 141 toRemove[i]= ClasspathModifier.getListElement(((IJavaProject)element).getPath(), cpProject.getCPListElements()); 142 } else if (element instanceof IPackageFragmentRoot) { 143 toRemove[i]= CPListElement.createFromExisting(((IPackageFragmentRoot)element).getRawClasspathEntry(), project); 144 } else { 145 toRemove[i]= CPListElement.createFromExisting(((ClassPathContainer)element).getClasspathEntry(), project); 146 } 147 i++; 148 } 149 150 BuildpathDelta delta= ClasspathModifier.removeFromBuildpath(toRemove, cpProject); 151 ClasspathModifier.commitClassPath(cpProject, new SubProgressMonitor(monitor, 10)); 152 153 deleteFolders(foldersToDelete, new SubProgressMonitor(monitor, foldersToDelete.size())); 154 155 informListeners(delta); 156 157 if (delta.getDeletedResources().length == foldersToDelete.size()) { 158 selectAndReveal(new StructuredSelection(project)); 159 } else { 160 List result= new ArrayList (Arrays.asList(delta.getDeletedResources())); 161 result.removeAll(foldersToDelete); 162 selectAndReveal(new StructuredSelection(result)); 163 } 164 } catch (CoreException e) { 165 throw new InvocationTargetException (e); 166 } finally { 167 monitor.done(); 168 } 169 } 170 }; 171 fContext.run(false, false, runnable); 172 173 } catch (CoreException e) { 174 showExceptionDialog(e, NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle); 175 } catch (InvocationTargetException e) { 176 if (e.getCause() instanceof CoreException) { 177 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle); 178 } else { 179 JavaPlugin.log(e); 180 } 181 } catch (InterruptedException e) { 182 } 183 } 184 185 private void deleteFolders(List folders, IProgressMonitor monitor) throws CoreException { 186 try { 187 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_RemoveFromBuildpath, folders.size()); 188 189 for (Iterator iter= folders.iterator(); iter.hasNext();) { 190 IFolder folder= (IFolder)iter.next(); 191 folder.delete(true, true, new SubProgressMonitor(monitor, 1)); 192 } 193 } finally { 194 monitor.done(); 195 } 196 } 197 198 private void queryToRemoveLinkedFolders(final List elementsToRemove, final List foldersToDelete) throws JavaModelException { 199 final Shell shell= getShell(); 200 for (Iterator iter= getSelectedElements().iterator(); iter.hasNext();) { 201 Object element= iter.next(); 202 if (element instanceof IPackageFragmentRoot) { 203 IFolder folder= getLinkedSourceFolder((IPackageFragmentRoot)element); 204 if (folder != null) { 205 RemoveLinkedFolderDialog dialog= new RemoveLinkedFolderDialog(shell, folder); 206 207 final int result= dialog.open() == Window.OK?dialog.getRemoveStatus():IRemoveLinkedFolderQuery.REMOVE_CANCEL; 208 209 if (result != IRemoveLinkedFolderQuery.REMOVE_CANCEL) { 210 if (result == IRemoveLinkedFolderQuery.REMOVE_BUILD_PATH) { 211 elementsToRemove.add(element); 212 } else if (result == IRemoveLinkedFolderQuery.REMOVE_BUILD_PATH_AND_FOLDER) { 213 elementsToRemove.add(element); 214 foldersToDelete.add(folder); 215 } 216 } 217 } else { 218 elementsToRemove.add(element); 219 } 220 } else { 221 elementsToRemove.add(element); 222 } 223 } 224 } 225 226 private IFolder getLinkedSourceFolder(IPackageFragmentRoot root) throws JavaModelException { 227 if (root.getKind() != IPackageFragmentRoot.K_SOURCE) 228 return null; 229 230 final IResource resource= root.getCorrespondingResource(); 231 if (!(resource instanceof IFolder)) 232 return null; 233 234 final IFolder folder= (IFolder) resource; 235 if (!folder.isLinked()) 236 return null; 237 238 return folder; 239 } 240 241 protected boolean canHandle(IStructuredSelection elements) { 242 if (elements.size() == 0) 243 return false; 244 245 try { 246 for (Iterator iter= elements.iterator(); iter.hasNext();) { 247 Object element= iter.next(); 248 249 if (element instanceof IJavaProject) { 250 IJavaProject project= (IJavaProject)element; 251 if (!ClasspathModifier.isSourceFolder(project)) 252 return false; 253 254 } else if (element instanceof IPackageFragmentRoot) { 255 IClasspathEntry entry= ((IPackageFragmentRoot) element).getRawClasspathEntry(); 256 if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 257 return false; 258 } 259 } else if (element instanceof ClassPathContainer) { 260 return true; 261 } else { 262 return false; 263 } 264 } 265 return true; 266 } catch (JavaModelException e) { 267 } 268 return false; 269 } 270 } | Popular Tags |