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.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.NullProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 22 import org.eclipse.jface.operation.IRunnableContext; 23 import org.eclipse.jface.operation.IRunnableWithProgress; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.part.ISetSelectionTarget; 28 29 import org.eclipse.jdt.core.IClasspathEntry; 30 import org.eclipse.jdt.core.IJavaElement; 31 import org.eclipse.jdt.core.IJavaProject; 32 import org.eclipse.jdt.core.IPackageFragmentRoot; 33 import org.eclipse.jdt.core.JavaModelException; 34 35 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 36 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 40 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 41 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute; 42 43 public class ResetAllOutputFoldersAction extends BuildpathModifierAction { 44 45 private final IRunnableContext fContext; 46 private final IJavaProject fJavaProject; 47 48 49 public ResetAllOutputFoldersAction(IRunnableContext context, IJavaProject project, ISetSelectionTarget selectionTarget) { 50 this(null, selectionTarget, context, project); 51 } 52 53 public ResetAllOutputFoldersAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context, IJavaProject javaProject) { 54 super(site, selectionTarget, BuildpathModifierAction.RESET_ALL_OUTPUT_FOLDERS); 55 56 fContext= context; 57 fJavaProject= javaProject; 58 59 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip); 60 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip); 61 } 62 63 66 public String getDetailedDescription() { 67 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAllOutputFolders; 68 } 69 70 73 public void run() { 74 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 75 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 76 try { 77 resetOutputFolders(fJavaProject, monitor); 78 } catch (CoreException e) { 79 throw new InvocationTargetException (e); 80 } 81 } 82 }; 83 try { 84 fContext.run(false, false, runnable); 85 } catch (InvocationTargetException e) { 86 if (e.getCause() instanceof CoreException) { 87 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle); 88 } else { 89 JavaPlugin.log(e); 90 } 91 } catch (InterruptedException e) { 92 } 93 } 94 95 private List resetOutputFolders(IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 96 if (monitor == null) 97 monitor= new NullProgressMonitor(); 98 try { 99 IPackageFragmentRoot[] roots= project.getPackageFragmentRoots(); 100 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_ResetOutputFolder, roots.length + 10); 101 List entries= new ArrayList (); 102 for (int i= 0; i < roots.length; i++) { 103 monitor.worked(1); 104 if (roots[i].isArchive()) 105 continue; 106 IClasspathEntry entry= roots[i].getRawClasspathEntry(); 107 CPListElement element= CPListElement.createFromExisting(entry, project); 108 CPListElementAttribute outputFolder= new CPListElementAttribute(element, CPListElement.OUTPUT, element.getAttribute(CPListElement.OUTPUT), true); 109 entries.add(outputFolder); 110 } 111 return reset(entries, project, new SubProgressMonitor(monitor, 10)); 112 } finally { 113 monitor.done(); 114 } 115 } 116 117 private List reset(List selection, IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 118 if (monitor == null) 119 monitor= new NullProgressMonitor(); 120 try { 121 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Resetting, selection.size()); 122 List entries= ClasspathModifier.getExistingEntries(project); 123 List result= new ArrayList (); 124 for (int i= 0; i < selection.size(); i++) { 125 Object element= selection.get(i); 126 if (element instanceof IJavaElement) { 127 IJavaElement javaElement= (IJavaElement) element; 128 IPackageFragmentRoot root; 129 if (element instanceof IJavaProject) 130 root= project.getPackageFragmentRoot(project.getResource()); 131 else 132 root= (IPackageFragmentRoot) element; 133 CPListElement entry= ClasspathModifier.getClasspathEntry(entries, root); 134 ClasspathModifier.resetFilters(javaElement, entry, project, new SubProgressMonitor(monitor, 1)); 135 result.add(javaElement); 136 } else { 137 CPListElement selElement= ((CPListElementAttribute) element).getParent(); 138 CPListElement entry= ClasspathModifier.getClasspathEntry(entries, selElement); 139 CPListElementAttribute outputFolder= ClasspathModifier.resetOutputFolder(entry, project); 140 result.add(outputFolder); 141 } 142 } 143 144 ClasspathModifier.commitClassPath(entries, project, null); 145 146 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 147 delta.setNewEntries((CPListElement[])entries.toArray(new CPListElement[entries.size()])); 148 informListeners(delta); 149 150 return result; 151 } finally { 152 monitor.done(); 153 } 154 } 155 156 protected boolean canHandle(IStructuredSelection elements) { 157 return true; 158 } 159 } 160 | Popular Tags |