1 12 package org.eclipse.jdt.internal.ui.actions; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.Arrays ; 16 import java.util.Collection ; 17 import java.util.HashSet ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.MultiStatus; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 28 import org.eclipse.jface.text.ITextSelection; 29 30 import org.eclipse.ui.IWorkbenchSite; 31 32 import org.eclipse.jdt.core.ICompilationUnit; 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.IPackageFragment; 36 import org.eclipse.jdt.core.IPackageFragmentRoot; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 40 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 41 import org.eclipse.jdt.internal.corext.util.Messages; 42 43 import org.eclipse.jdt.ui.JavaUI; 44 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 45 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.browsing.LogicalPackage; 48 import org.eclipse.jdt.internal.ui.fix.ICleanUp; 49 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 50 import org.eclipse.jdt.internal.ui.util.ElementValidator; 51 52 public abstract class CleanUpAction extends SelectionDispatchAction { 53 54 private JavaEditor fEditor; 55 56 public CleanUpAction(IWorkbenchSite site) { 57 super(site); 58 } 59 60 67 public CleanUpAction(JavaEditor editor) { 68 this(editor.getEditorSite()); 69 fEditor= editor; 70 setEnabled(getCompilationUnit(fEditor) != null); 71 } 72 73 76 protected abstract String getActionName(); 77 78 84 protected abstract ICleanUp[] createCleanUps(ICompilationUnit[] units); 85 86 95 protected void performRefactoring(ICompilationUnit[] units, ICleanUp[] cleanUps) throws JavaModelException, InvocationTargetException { 96 RefactoringExecutionStarter.startCleanupRefactoring(units, cleanUps, getShell(), false, getActionName()); 97 } 98 99 public void run(ITextSelection selection) { 100 ICompilationUnit cu= getCompilationUnit(fEditor); 101 if (cu != null) { 102 run(cu); 103 } 104 } 105 106 public void run(IStructuredSelection selection) { 107 ICompilationUnit[] cus= getCompilationUnits(selection); 108 if (cus.length == 0) { 109 MessageDialog.openInformation(getShell(), getActionName(), ActionMessages.CleanUpAction_EmptySelection_description); 110 } else if (cus.length == 1) { 111 run(cus[0]); 112 } else { 113 runOnMultiple(cus); 114 } 115 } 116 117 public void selectionChanged(ITextSelection selection) { 118 setEnabled(getCompilationUnit(fEditor) != null); 119 } 120 121 public void selectionChanged(IStructuredSelection selection) { 122 setEnabled(isEnabled(selection)); 123 } 124 125 private boolean isEnabled(IStructuredSelection selection) { 126 Object [] selected= selection.toArray(); 127 for (int i= 0; i < selected.length; i++) { 128 try { 129 if (selected[i] instanceof IJavaElement) { 130 IJavaElement elem= (IJavaElement)selected[i]; 131 if (elem.exists()) { 132 switch (elem.getElementType()) { 133 case IJavaElement.TYPE: 134 return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; case IJavaElement.COMPILATION_UNIT: 136 return true; 137 case IJavaElement.IMPORT_CONTAINER: 138 return true; 139 case IJavaElement.PACKAGE_FRAGMENT: 140 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 141 IPackageFragmentRoot root= (IPackageFragmentRoot)elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 142 return (root.getKind() == IPackageFragmentRoot.K_SOURCE); 143 case IJavaElement.JAVA_PROJECT: 144 return true; 146 } 147 } 148 } else if (selected[i] instanceof LogicalPackage) { 149 return true; 150 } 151 } catch (JavaModelException e) { 152 if (!e.isDoesNotExist()) { 153 JavaPlugin.log(e); 154 } 155 } 156 } 157 return false; 158 } 159 160 private void run(ICompilationUnit cu) { 161 if (!ActionUtil.isEditable(fEditor, getShell(), cu)) 162 return; 163 164 ICleanUp[] cleanUps= createCleanUps(new ICompilationUnit[] { 165 cu 166 }); 167 if (cleanUps == null) 168 return; 169 170 if (!ElementValidator.check(cu, getShell(), getActionName(), fEditor != null)) 171 return; 172 173 try { 174 performRefactoring(new ICompilationUnit[] { 175 cu 176 }, cleanUps); 177 } catch (InvocationTargetException e) { 178 JavaPlugin.log(e); 179 if (e.getCause() instanceof CoreException) 180 showUnexpectedError((CoreException)e.getCause()); 181 } catch (JavaModelException e) { 182 showUnexpectedError(e); 183 } 184 } 185 186 private void runOnMultiple(final ICompilationUnit[] cus) { 187 ICleanUp[] cleanUps= createCleanUps(cus); 188 if (cleanUps == null) 189 return; 190 191 MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, ActionMessages.CleanUpAction_MultiStateErrorTitle, null); 192 for (int i= 0; i < cus.length; i++) { 193 ICompilationUnit cu= cus[i]; 194 195 if (!ActionUtil.isOnBuildPath(cu)) { 196 String cuLocation= cu.getPath().makeRelative().toString(); 197 String message= Messages.format(ActionMessages.CleanUpAction_CUNotOnBuildpathMessage, cuLocation); 198 status.add(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null)); 199 } 200 } 201 if (!status.isOK()) { 202 ErrorDialog.openError(getShell(), getActionName(), null, status); 203 return; 204 } 205 206 try { 207 performRefactoring(cus, cleanUps); 208 } catch (InvocationTargetException e) { 209 JavaPlugin.log(e); 210 if (e.getCause() instanceof CoreException) 211 showUnexpectedError((CoreException)e.getCause()); 212 } catch (JavaModelException e) { 213 showUnexpectedError(e); 214 } 215 } 216 217 private void showUnexpectedError(CoreException e) { 218 String message2= Messages.format(ActionMessages.CleanUpAction_UnexpectedErrorMessage, e.getStatus().getMessage()); 219 IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message2, null); 220 ErrorDialog.openError(getShell(), getActionName(), null, status); 221 } 222 223 public ICompilationUnit[] getCompilationUnits(IStructuredSelection selection) { 224 HashSet result= new HashSet (); 225 Object [] selected= selection.toArray(); 226 for (int i= 0; i < selected.length; i++) { 227 try { 228 if (selected[i] instanceof IJavaElement) { 229 IJavaElement elem= (IJavaElement)selected[i]; 230 if (elem.exists()) { 231 switch (elem.getElementType()) { 232 case IJavaElement.TYPE: 233 if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) { 234 result.add(elem.getParent()); 235 } 236 break; 237 case IJavaElement.COMPILATION_UNIT: 238 result.add(elem); 239 break; 240 case IJavaElement.IMPORT_CONTAINER: 241 result.add(elem.getParent()); 242 break; 243 case IJavaElement.PACKAGE_FRAGMENT: 244 collectCompilationUnits((IPackageFragment)elem, result); 245 break; 246 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 247 collectCompilationUnits((IPackageFragmentRoot)elem, result); 248 break; 249 case IJavaElement.JAVA_PROJECT: 250 IPackageFragmentRoot[] roots= ((IJavaProject)elem).getPackageFragmentRoots(); 251 for (int k= 0; k < roots.length; k++) { 252 collectCompilationUnits(roots[k], result); 253 } 254 break; 255 } 256 } 257 } else if (selected[i] instanceof LogicalPackage) { 258 IPackageFragment[] packageFragments= ((LogicalPackage)selected[i]).getFragments(); 259 for (int k= 0; k < packageFragments.length; k++) { 260 IPackageFragment pack= packageFragments[k]; 261 if (pack.exists()) { 262 collectCompilationUnits(pack, result); 263 } 264 } 265 } 266 } catch (JavaModelException e) { 267 if (JavaModelUtil.isExceptionToBeLogged(e)) 268 JavaPlugin.log(e); 269 } 270 } 271 return (ICompilationUnit[])result.toArray(new ICompilationUnit[result.size()]); 272 } 273 274 private void collectCompilationUnits(IPackageFragment pack, Collection result) throws JavaModelException { 275 result.addAll(Arrays.asList(pack.getCompilationUnits())); 276 } 277 278 private void collectCompilationUnits(IPackageFragmentRoot root, Collection result) throws JavaModelException { 279 if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { 280 IJavaElement[] children= root.getChildren(); 281 for (int i= 0; i < children.length; i++) { 282 collectCompilationUnits((IPackageFragment)children[i], result); 283 } 284 } 285 } 286 287 private static ICompilationUnit getCompilationUnit(JavaEditor editor) { 288 IJavaElement element= JavaUI.getEditorInputJavaElement(editor.getEditorInput()); 289 if (!(element instanceof ICompilationUnit)) 290 return null; 291 292 return (ICompilationUnit)element; 293 } 294 295 } 296 | Popular Tags |