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.jface.operation.IRunnableContext; 24 import org.eclipse.jface.operation.IRunnableWithProgress; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 28 import org.eclipse.ui.IWorkbenchSite; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.part.ISetSelectionTarget; 31 32 import org.eclipse.jdt.core.IClasspathEntry; 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.IPackageFragmentRoot; 36 import org.eclipse.jdt.core.JavaModelException; 37 38 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 39 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 40 41 import org.eclipse.jdt.internal.ui.JavaPlugin; 42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 44 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute; 45 46 public class ResetAction extends BuildpathModifierAction { 49 50 private final IRunnableContext fContext; 51 52 public ResetAction(IWorkbenchSite site) { 53 this(site, null, PlatformUI.getWorkbench().getProgressService()); 54 } 55 56 public ResetAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 57 this(null, selectionTarget, context); 58 } 59 60 public ResetAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 61 super(site, selectionTarget, BuildpathModifierAction.RESET); 62 63 fContext= context; 64 65 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip); 66 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip); 67 } 68 69 72 public String getDetailedDescription() { 73 if (!isEnabled()) 74 return null; 75 76 Iterator iterator= getSelectedElements().iterator(); 77 Object p= iterator.next(); 78 while (iterator.hasNext()) { 79 Object q= iterator.next(); 80 if ( 81 (p instanceof CPListElementAttribute && !(q instanceof CPListElementAttribute)) || 82 (q instanceof CPListElementAttribute && !(p instanceof CPListElementAttribute)) 83 ) { 84 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Reset; 85 } 86 p= q; 87 } 88 if (p instanceof CPListElementAttribute) { 89 return NewWizardMessages.PackageExplorerActionGroup_FormText_SetOutputToDefault; 90 } else { 91 return NewWizardMessages.PackageExplorerActionGroup_FormText_ResetFilters; 92 } 93 } 94 95 98 public void run() { 99 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 100 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 101 try { 102 Object firstElement= getSelectedElements().get(0); 103 IJavaProject project= null; 104 if (firstElement instanceof IJavaProject) { 105 project= (IJavaProject)firstElement; 106 } else if (firstElement instanceof IPackageFragmentRoot) { 107 project= ((IPackageFragmentRoot)firstElement).getJavaProject(); 108 } else { 109 project= ((CPListElementAttribute)firstElement).getParent().getJavaProject(); 110 } 111 112 List result= reset(getSelectedElements(), project, monitor); 113 selectAndReveal(new StructuredSelection(result)); 114 } catch (CoreException e) { 115 throw new InvocationTargetException (e); 116 } 117 } 118 }; 119 try { 120 fContext.run(false, false, runnable); 121 } catch (InvocationTargetException e) { 122 if (e.getCause() instanceof CoreException) { 123 showExceptionDialog((CoreException)e.getCause(), ""); } else { 125 JavaPlugin.log(e); 126 } 127 } catch (InterruptedException e) { 128 } 129 } 130 131 private List reset(List selection, IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 132 if (monitor == null) 133 monitor= new NullProgressMonitor(); 134 try { 135 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Resetting, selection.size()); 136 List entries= ClasspathModifier.getExistingEntries(project); 137 List result= new ArrayList (); 138 for (int i= 0; i < selection.size(); i++) { 139 Object element= selection.get(i); 140 if (element instanceof IJavaElement) { 141 IJavaElement javaElement= (IJavaElement) element; 142 IPackageFragmentRoot root; 143 if (element instanceof IJavaProject) 144 root= project.getPackageFragmentRoot(project.getResource()); 145 else 146 root= (IPackageFragmentRoot) element; 147 CPListElement entry= ClasspathModifier.getClasspathEntry(entries, root); 148 ClasspathModifier.resetFilters(javaElement, entry, project, new SubProgressMonitor(monitor, 1)); 149 result.add(javaElement); 150 } else { 151 CPListElement selElement= ((CPListElementAttribute) element).getParent(); 152 CPListElement entry= ClasspathModifier.getClasspathEntry(entries, selElement); 153 CPListElementAttribute outputFolder= ClasspathModifier.resetOutputFolder(entry, project); 154 result.add(outputFolder); 155 } 156 } 157 158 ClasspathModifier.commitClassPath(entries, project, null); 159 160 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 161 delta.setNewEntries((CPListElement[])entries.toArray(new CPListElement[entries.size()])); 162 informListeners(delta); 163 164 return result; 165 } finally { 166 monitor.done(); 167 } 168 } 169 170 protected boolean canHandle(IStructuredSelection elements) { 171 try { 172 for (Iterator iterator= elements.iterator(); iterator.hasNext();) { 173 Object element= iterator.next(); 174 if (element instanceof IJavaProject) { 175 IJavaProject project= (IJavaProject)element; 176 if (!project.isOnClasspath(project)) 177 return false; 178 179 IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE); 180 if (entry.getInclusionPatterns().length == 0 && entry.getExclusionPatterns().length == 0) 181 return false; 182 183 return true; 184 } else if (element instanceof IPackageFragmentRoot) { 185 if (ClasspathModifier.filtersSet((IPackageFragmentRoot)element)) 186 return true; 187 } else if (element instanceof CPListElementAttribute) { 188 if (!ClasspathModifier.isDefaultOutputFolder((CPListElementAttribute)element)) 189 return true; 190 } else { 191 return false; 192 } 193 } 194 } catch (JavaModelException e) { 195 return false; 196 } 197 return false; 198 } 199 } | Popular Tags |