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.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.jface.operation.IRunnableContext; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 30 import org.eclipse.ui.IWorkbenchSite; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.part.ISetSelectionTarget; 33 34 import org.eclipse.jdt.core.ICompilationUnit; 35 import org.eclipse.jdt.core.IJavaElement; 36 import org.eclipse.jdt.core.IJavaProject; 37 import org.eclipse.jdt.core.IPackageFragment; 38 import org.eclipse.jdt.core.IPackageFragmentRoot; 39 import org.eclipse.jdt.core.JavaModelException; 40 41 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 42 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 43 import org.eclipse.jdt.internal.corext.util.Messages; 44 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.JavaPluginImages; 47 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 48 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 49 50 public class ExcludeFromBuildpathAction extends BuildpathModifierAction { 52 53 private final IRunnableContext fContext; 54 55 public ExcludeFromBuildpathAction(IWorkbenchSite site) { 56 this(site, null, PlatformUI.getWorkbench().getProgressService()); 57 } 58 59 public ExcludeFromBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 60 this(null, selectionTarget, context); 61 } 62 63 private ExcludeFromBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 64 super(site, selectionTarget, BuildpathModifierAction.EXCLUDE); 65 66 fContext= context; 67 68 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Exclude_label); 69 setImageDescriptor(JavaPluginImages.DESC_ELCL_EXCLUDE_FROM_BUILDPATH); 70 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Exclude_tooltip); 71 setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_EXCLUDE_FROM_BUILDPATH); 72 } 73 74 77 public String getDetailedDescription() { 78 if (!isEnabled()) 79 return null; 80 81 if (getSelectedElements().size() != 1) 82 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Exclude; 83 84 IJavaElement elem= (IJavaElement) getSelectedElements().get(0); 85 String name= ClasspathModifier.escapeSpecialChars(elem.getElementName()); 86 if (elem instanceof IPackageFragment) { 87 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ExcludePackage, name); 88 } else if (elem instanceof ICompilationUnit) { 89 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ExcludeFile, name); 90 } 91 92 return null; 93 } 94 95 98 public void run() { 99 final IJavaProject project; 100 Object object= getSelectedElements().get(0); 101 if (object instanceof ICompilationUnit) { 102 project= ((ICompilationUnit)object).getJavaProject(); 103 } else { 104 project= ((IPackageFragment)object).getJavaProject(); 105 } 106 107 try { 108 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 109 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 110 try { 111 List result= exclude(getSelectedElements(), project, monitor); 112 selectAndReveal(new StructuredSelection(result)); 113 } catch (CoreException e) { 114 throw new InvocationTargetException (e); 115 } 116 } 117 }; 118 fContext.run(false, false, runnable); 119 } catch (final InvocationTargetException e) { 120 if (e.getCause() instanceof CoreException) { 121 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.ExcludeFromBuildathAction_ErrorTitle); 122 } else { 123 JavaPlugin.log(e); 124 } 125 } catch (final InterruptedException e) { 126 } 127 } 128 129 private List exclude(List javaElements, IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 130 if (monitor == null) 131 monitor= new NullProgressMonitor(); 132 try { 133 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Excluding, javaElements.size() + 4); 134 135 List existingEntries= ClasspathModifier.getExistingEntries(project); 136 List resources= new ArrayList (); 137 for (int i= 0; i < javaElements.size(); i++) { 138 IJavaElement javaElement= (IJavaElement) javaElements.get(i); 139 IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 140 CPListElement entry= ClasspathModifier.getClasspathEntry(existingEntries, root); 141 142 IResource resource= ClasspathModifier.exclude(javaElement, entry, project, new SubProgressMonitor(monitor, 1)); 143 if (resource != null) { 144 resources.add(resource); 145 } 146 } 147 148 ClasspathModifier.commitClassPath(existingEntries, project, new SubProgressMonitor(monitor, 4)); 149 150 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 151 delta.setNewEntries((CPListElement[])existingEntries.toArray(new CPListElement[existingEntries.size()])); 152 informListeners(delta); 153 154 return resources; 155 } finally { 156 monitor.done(); 157 } 158 } 159 160 protected boolean canHandle(IStructuredSelection elements) { 161 if (elements.size() == 0) 162 return false; 163 164 for (Iterator iter= elements.iterator(); iter.hasNext();) { 165 Object element= iter.next(); 166 if (element instanceof IPackageFragment) { 167 IPackageFragment fragment= (IPackageFragment)element; 168 if (ClasspathModifier.isDefaultFragment(fragment)) 169 return false; 170 171 if (((IPackageFragmentRoot)fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT)).isArchive()) 172 return false; 173 } else if (element instanceof ICompilationUnit) { 174 } else { 175 return false; 176 } 177 } 178 return true; 179 } 180 181 } 182 | Popular Tags |