1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 21 import org.eclipse.core.resources.IResource; 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.part.ISetSelectionTarget; 29 30 import org.eclipse.jdt.core.ElementChangedEvent; 31 import org.eclipse.jdt.core.IClasspathEntry; 32 import org.eclipse.jdt.core.IElementChangedListener; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 37 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 38 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.JavaPluginImages; 42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 44 45 public class ResetAllAction extends BuildpathModifierAction { 47 48 private final HintTextGroup fProvider; 49 private final IRunnableContext fContext; 50 private IJavaProject fJavaProject; 51 private List fEntries; 52 private IPath fOutputLocation; 53 54 public ResetAllAction(HintTextGroup provider, IRunnableContext context, ISetSelectionTarget selectionTarget) { 55 super(null, selectionTarget, BuildpathModifierAction.RESET_ALL); 56 57 fProvider= provider; 58 fContext= context; 59 60 setImageDescriptor(JavaPluginImages.DESC_ELCL_CLEAR); 61 setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CLEAR); 62 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_label); 63 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); 64 setEnabled(false); 65 } 66 67 70 public String getDetailedDescription() { 71 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAll; 72 } 73 74 public void setBreakPoint(IJavaProject javaProject) { 75 fJavaProject= javaProject; 76 if (fJavaProject.exists()) { 77 try { 78 fEntries= ClasspathModifier.getExistingEntries(fJavaProject); 79 fOutputLocation= fJavaProject.getOutputLocation(); 80 } catch (JavaModelException e) { 81 JavaPlugin.log(e); 82 return; 83 } 84 setEnabled(true); 85 } else { 86 JavaCore.addElementChangedListener(new IElementChangedListener() { 87 88 public void elementChanged(ElementChangedEvent event) { 89 if (fJavaProject.exists()) { 90 try { 91 fEntries= ClasspathModifier.getExistingEntries(fJavaProject); 92 fOutputLocation= fJavaProject.getOutputLocation(); 93 } catch (JavaModelException e) { 94 JavaPlugin.log(e); 95 return; 96 } finally { 97 JavaCore.removeElementChangedListener(this); 98 } 99 setEnabled(true); 100 } 101 } 102 103 }, ElementChangedEvent.POST_CHANGE); 104 } 105 } 106 107 110 public void run() { 111 112 try { 113 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 114 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 115 116 monitor.beginTask("", 3); try { 118 if (!hasChange(fJavaProject)) 119 return; 120 121 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 122 123 ClasspathModifier.commitClassPath(fEntries, fJavaProject, monitor); 124 delta.setNewEntries((CPListElement[])fEntries.toArray(new CPListElement[fEntries.size()])); 125 126 fJavaProject.setOutputLocation(fOutputLocation, monitor); 127 delta.setDefaultOutputLocation(fOutputLocation); 128 129 for (Iterator iterator= fProvider.getCreatedResources().iterator(); iterator.hasNext();) { 130 IResource resource= (IResource)iterator.next(); 131 resource.delete(false, null); 132 delta.addDeletedResource(resource); 133 } 134 135 fProvider.resetCreatedResources(); 136 137 informListeners(delta); 138 139 selectAndReveal(new StructuredSelection(fJavaProject)); 140 } catch (JavaModelException e) { 141 showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); 142 } catch (CoreException e) { 143 showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); 144 } finally { 145 monitor.done(); 146 } 147 } 148 }; 149 fContext.run(false, false, runnable); 150 } catch (InvocationTargetException e) { 151 if (e.getCause() instanceof CoreException) { 152 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); 153 } else { 154 JavaPlugin.log(e); 155 } 156 } catch (InterruptedException e) { 157 } 158 } 159 160 161 164 protected boolean canHandle(IStructuredSelection elements) { 165 if (fJavaProject == null) 166 return false; 167 168 return true; 169 } 170 171 172 private boolean hasChange(IJavaProject project) throws JavaModelException { 174 if (!project.getOutputLocation().equals(fOutputLocation)) 175 return true; 176 177 IClasspathEntry[] currentEntries= project.getRawClasspath(); 178 if (currentEntries.length != fEntries.size()) 179 return true; 180 181 int i= 0; 182 for (Iterator iterator= fEntries.iterator(); iterator.hasNext();) { 183 CPListElement oldEntrie= (CPListElement)iterator.next(); 184 if (!oldEntrie.getClasspathEntry().equals(currentEntries[i])) 185 return true; 186 i++; 187 } 188 return false; 189 } 190 } 191 | Popular Tags |