1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspaceRunnable; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.SubProgressMonitor; 23 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.PlatformUI; 28 29 import org.eclipse.jdt.core.IClasspathEntry; 30 import org.eclipse.jdt.core.IPackageFragmentRoot; 31 import org.eclipse.jdt.core.JavaModelException; 32 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 39 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 40 41 52 public class RemoveFromClasspathAction extends SelectionDispatchAction { 53 54 61 public RemoveFromClasspathAction(IWorkbenchSite site) { 62 super(site); 63 setText(ActionMessages.RemoveFromClasspathAction_Remove); 64 setToolTipText(ActionMessages.RemoveFromClasspathAction_tooltip); 65 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REMOVE_FROM_CLASSPATH_ACTION); 66 } 67 68 71 public void selectionChanged(IStructuredSelection selection) { 72 setEnabled(checkEnabled(selection)); 73 } 74 75 private static boolean checkEnabled(IStructuredSelection selection) { 76 if (selection.isEmpty()) 77 return false; 78 for (Iterator iter= selection.iterator(); iter.hasNext();) { 79 if (! canRemove(iter.next())) 80 return false; 81 } 82 return true; 83 } 84 85 88 public void run(final IStructuredSelection selection) { 89 try { 90 PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() { 91 public void run(IProgressMonitor pm) throws CoreException { 92 try{ 93 IPackageFragmentRoot[] roots= getRootsToRemove(selection); 94 pm.beginTask(ActionMessages.RemoveFromClasspathAction_Removing, roots.length); 95 for (int i= 0; i < roots.length; i++) { 96 int jCoreFlags= IPackageFragmentRoot.NO_RESOURCE_MODIFICATION | IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH; 97 roots[i].delete(IResource.NONE, jCoreFlags, new SubProgressMonitor(pm, 1)); 98 } 99 } finally { 100 pm.done(); 101 } 102 } 103 })); 104 } catch (InvocationTargetException e) { 105 ExceptionHandler.handle(e, getShell(), 106 ActionMessages.RemoveFromClasspathAction_exception_dialog_title, 107 ActionMessages.RemoveFromClasspathAction_Problems_occurred); 108 } catch (InterruptedException e) { 109 } 111 } 112 113 private static IPackageFragmentRoot[] getRootsToRemove(IStructuredSelection selection){ 114 List result= new ArrayList (selection.size()); 115 for (Iterator iter= selection.iterator(); iter.hasNext();) { 116 Object element= iter.next(); 117 if (canRemove(element)) 118 result.add(element); 119 } 120 return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]); 121 } 122 123 private static boolean canRemove(Object element){ 124 if (! (element instanceof IPackageFragmentRoot)) 125 return false; 126 IPackageFragmentRoot root= (IPackageFragmentRoot)element; 127 try { 128 IClasspathEntry cpe= root.getRawClasspathEntry(); 129 if (cpe == null || cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) 130 return false; return true; 132 } catch (JavaModelException e) { 133 if (JavaModelUtil.isExceptionToBeLogged(e)) 134 JavaPlugin.log(e); 135 } 136 return false; 137 } 138 } 139 140 | Popular Tags |