1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 18 import org.eclipse.jface.text.ITextSelection; 19 20 import org.eclipse.ui.IWorkbenchSite; 21 import org.eclipse.ui.PlatformUI; 22 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.ITypeRoot; 26 import org.eclipse.jdt.core.JavaModelException; 27 28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 31 32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 33 import org.eclipse.jdt.internal.ui.JavaPlugin; 34 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 35 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 37 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 38 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 40 41 51 public class ReplaceInvocationsAction extends SelectionDispatchAction { 52 53 private JavaEditor fEditor; 54 55 59 public ReplaceInvocationsAction(JavaEditor editor) { 60 this(editor.getEditorSite()); 61 fEditor= editor; 62 setEnabled(true); 63 } 64 65 70 public ReplaceInvocationsAction(IWorkbenchSite site) { 71 super(site); 72 setText(RefactoringMessages.ReplaceInvocationsAction_label); 73 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REPLACE_INVOCATIONS_ACTION); 74 } 75 76 78 81 public void selectionChanged(IStructuredSelection selection) { 82 try { 83 setEnabled(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection)); 84 } catch (JavaModelException e) { 85 if (JavaModelUtil.isExceptionToBeLogged(e)) 86 JavaPlugin.log(e); 87 } 88 } 89 90 93 public void selectionChanged(ITextSelection selection) { 94 setEnabled(true); 95 } 96 97 100 public void selectionChanged(JavaTextSelection selection) { 101 try { 102 setEnabled(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection)); 103 } catch (JavaModelException e) { 104 setEnabled(false); 105 } 106 } 107 108 111 public void run(IStructuredSelection selection) { 112 try { 113 Assert.isTrue(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection)); 114 Object first= selection.getFirstElement(); 115 Assert.isTrue(first instanceof IMethod); 116 IMethod method= (IMethod) first; 117 if (ActionUtil.isProcessable(getShell(), method)) 118 RefactoringExecutionStarter.startReplaceInvocationsRefactoring(method, getShell()); 119 } catch (CoreException e) { 120 handleException(e); 121 } 122 } 123 124 private void handleException(CoreException e) { 125 ExceptionHandler.handle(e, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringMessages.ReplaceInvocationsAction_unavailable); 126 } 127 128 131 public void run(ITextSelection selection) { 132 try { 133 IJavaElement editorInput= SelectionConverter.getInput(fEditor); 134 if ((editorInput instanceof ITypeRoot) 135 && ActionUtil.isProcessable(getShell(), editorInput)) { 136 ITypeRoot typeRoot= (ITypeRoot) editorInput; 137 RefactoringExecutionStarter.startReplaceInvocationsRefactoring(typeRoot, selection.getOffset(), selection.getLength(), getShell()); 138 } 139 } catch (JavaModelException e) { 140 handleException(e); 141 } 142 } 143 } 144 | Popular Tags |