1 11 package org.eclipse.jdt.internal.ui.refactoring.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jface.preference.IPreferenceStore; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 19 import org.eclipse.jface.text.ITextSelection; 20 21 import org.eclipse.ui.IWorkbenchSite; 22 23 import org.eclipse.jdt.core.Flags; 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IField; 26 import org.eclipse.jdt.core.IJavaElement; 27 import org.eclipse.jdt.core.IJavaProject; 28 import org.eclipse.jdt.core.ILocalVariable; 29 import org.eclipse.jdt.core.IMethod; 30 import org.eclipse.jdt.core.IPackageFragment; 31 import org.eclipse.jdt.core.IPackageFragmentRoot; 32 import org.eclipse.jdt.core.IType; 33 import org.eclipse.jdt.core.ITypeParameter; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 37 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 39 40 import org.eclipse.jdt.ui.PreferenceConstants; 41 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 45 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 46 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 47 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 48 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 49 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 50 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameLinkedMode; 51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 52 53 public class RenameJavaElementAction extends SelectionDispatchAction { 54 55 private JavaEditor fEditor; 56 57 public RenameJavaElementAction(IWorkbenchSite site) { 58 super(site); 59 } 60 61 public RenameJavaElementAction(JavaEditor editor) { 62 this(editor.getEditorSite()); 63 fEditor= editor; 64 setEnabled(SelectionConverter.canOperateOn(fEditor)); 65 } 66 67 69 public void selectionChanged(IStructuredSelection selection) { 70 try { 71 if (selection.size() == 1) { 72 setEnabled(canEnable(selection)); 73 return; 74 } 75 } catch (JavaModelException e) { 76 if (JavaModelUtil.isExceptionToBeLogged(e)) 78 JavaPlugin.log(e); 79 } catch (CoreException e) { 80 JavaPlugin.log(e); 81 } 82 setEnabled(false); 83 } 84 85 private static boolean canEnable(IStructuredSelection selection) throws CoreException { 86 IJavaElement element= getJavaElement(selection); 87 if (element == null) 88 return false; 89 return isRenameAvailable(element); 90 } 91 92 private static IJavaElement getJavaElement(IStructuredSelection selection) { 93 if (selection.size() != 1) 94 return null; 95 Object first= selection.getFirstElement(); 96 if (! (first instanceof IJavaElement)) 97 return null; 98 return (IJavaElement)first; 99 } 100 101 public void run(IStructuredSelection selection) { 102 IJavaElement element= getJavaElement(selection); 103 if (element == null) 104 return; 105 try { 106 run(element, false); 107 } catch (CoreException e){ 108 ExceptionHandler.handle(e, RefactoringMessages.RenameJavaElementAction_name, RefactoringMessages.RenameJavaElementAction_exception); 109 } 110 } 111 112 114 public void selectionChanged(ITextSelection selection) { 115 if (selection instanceof JavaTextSelection) { 116 try { 117 IJavaElement[] elements= ((JavaTextSelection)selection).resolveElementAtOffset(); 118 if (elements.length == 1) { 119 setEnabled(isRenameAvailable(elements[0])); 120 } else { 121 setEnabled(false); 122 } 123 } catch (CoreException e) { 124 setEnabled(false); 125 } 126 } else { 127 setEnabled(true); 128 } 129 } 130 131 public void run(ITextSelection selection) { 132 RenameLinkedMode activeLinkedMode= RenameLinkedMode.getActiveLinkedMode(); 133 if (activeLinkedMode != null) { 134 if (activeLinkedMode.isCaretInLinkedPosition()) { 135 activeLinkedMode.startFullDialog(); 136 return; 137 } else { 138 activeLinkedMode.cancel(); 139 } 140 } 141 142 try { 143 IJavaElement element= getJavaElementFromEditor(); 144 if (element != null && isRenameAvailable(element)) { 145 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 146 run(element, store.getBoolean(PreferenceConstants.REFACTOR_LIGHTWEIGHT)); 147 return; 148 } 149 } catch (CoreException e) { 150 ExceptionHandler.handle(e, RefactoringMessages.RenameJavaElementAction_name, RefactoringMessages.RenameJavaElementAction_exception); 151 } 152 MessageDialog.openInformation(getShell(), RefactoringMessages.RenameJavaElementAction_name, RefactoringMessages.RenameJavaElementAction_not_available); 153 } 154 155 public boolean canRunInEditor() { 156 if (RenameLinkedMode.getActiveLinkedMode() != null) 157 return true; 158 159 try { 160 IJavaElement element= getJavaElementFromEditor(); 161 if (element == null) 162 return false; 163 164 return isRenameAvailable(element); 165 } catch (JavaModelException e) { 166 if (JavaModelUtil.isExceptionToBeLogged(e)) 167 JavaPlugin.log(e); 168 } catch (CoreException e) { 169 JavaPlugin.log(e); 170 } 171 return false; 172 } 173 174 private IJavaElement getJavaElementFromEditor() throws JavaModelException { 175 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); 176 if (elements == null || elements.length != 1) 177 return null; 178 return elements[0]; 179 } 180 181 183 private void run(IJavaElement element, boolean lightweight) throws CoreException { 184 if (! ActionUtil.isEditable(fEditor, getShell(), element)) 186 return; 187 if (ActionUtil.mustDisableJavaModelAction(getShell(), element)) 189 return; 190 191 if (lightweight && fEditor instanceof CompilationUnitEditor && ! (element instanceof IPackageFragment)) { 192 new RenameLinkedMode(element, (CompilationUnitEditor) fEditor).start(); 193 } else { 194 RefactoringExecutionStarter.startRenameRefactoring(element, getShell()); 195 } 196 } 197 198 private static boolean isRenameAvailable(IJavaElement element) throws CoreException { 199 switch (element.getElementType()) { 200 case IJavaElement.JAVA_PROJECT: 201 return RefactoringAvailabilityTester.isRenameAvailable((IJavaProject) element); 202 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 203 return RefactoringAvailabilityTester.isRenameAvailable((IPackageFragmentRoot) element); 204 case IJavaElement.PACKAGE_FRAGMENT: 205 return RefactoringAvailabilityTester.isRenameAvailable((IPackageFragment) element); 206 case IJavaElement.COMPILATION_UNIT: 207 return RefactoringAvailabilityTester.isRenameAvailable((ICompilationUnit) element); 208 case IJavaElement.TYPE: 209 return RefactoringAvailabilityTester.isRenameAvailable((IType) element); 210 case IJavaElement.METHOD: 211 final IMethod method= (IMethod) element; 212 if (method.isConstructor()) 213 return RefactoringAvailabilityTester.isRenameAvailable(method.getDeclaringType()); 214 else 215 return RefactoringAvailabilityTester.isRenameAvailable(method); 216 case IJavaElement.FIELD: 217 final IField field= (IField) element; 218 if (Flags.isEnum(field.getFlags())) 219 return RefactoringAvailabilityTester.isRenameEnumConstAvailable(field); 220 else 221 return RefactoringAvailabilityTester.isRenameFieldAvailable(field); 222 case IJavaElement.TYPE_PARAMETER: 223 return RefactoringAvailabilityTester.isRenameAvailable((ITypeParameter) element); 224 case IJavaElement.LOCAL_VARIABLE: 225 return RefactoringAvailabilityTester.isRenameAvailable((ILocalVariable) element); 226 } 227 return false; 228 } 229 } 230 | Popular Tags |