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.IClassFile; 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IMethod; 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 IntroduceIndirectionAction extends SelectionDispatchAction { 52 53 private JavaEditor fEditor; 54 55 59 public IntroduceIndirectionAction(JavaEditor editor) { 60 this(editor.getEditorSite()); 61 fEditor= editor; 62 setEnabled(true); 63 } 64 65 70 public IntroduceIndirectionAction(IWorkbenchSite site) { 71 super(site); 72 setText(RefactoringMessages.IntroduceIndirectionAction_title); 73 setToolTipText(RefactoringMessages.IntroduceIndirectionAction_tooltip); 74 setDescription(RefactoringMessages.IntroduceIndirectionAction_description); 75 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_INDIRECTION_ACTION); 76 } 77 78 80 83 public void selectionChanged(IStructuredSelection selection) { 84 try { 85 setEnabled(RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(selection)); 86 } catch (JavaModelException e) { 87 if (JavaModelUtil.isExceptionToBeLogged(e)) 88 JavaPlugin.log(e); 89 } 90 } 91 92 95 public void selectionChanged(ITextSelection selection) { 96 setEnabled(true); 97 } 98 99 102 public void selectionChanged(JavaTextSelection selection) { 103 try { 104 setEnabled(RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(selection)); 105 } catch (JavaModelException e) { 106 setEnabled(false); 107 } 108 } 109 110 113 public void run(IStructuredSelection selection) { 114 try { 115 Assert.isTrue(RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(selection)); 116 Object first= selection.getFirstElement(); 117 Assert.isTrue(first instanceof IMethod); 118 run((IMethod) first); 119 } catch (CoreException e) { 120 ExceptionHandler.handle(e, RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringMessages.IntroduceIndirectionAction_unknown_exception); 121 } 122 } 123 124 127 public void run(ITextSelection selection) { 128 try { 129 Object editorInput= SelectionConverter.getInput(fEditor); 130 if (editorInput instanceof ICompilationUnit) 131 run(selection.getOffset(), selection.getLength(), (ICompilationUnit) editorInput); 132 else if (editorInput instanceof IClassFile) 133 run(selection.getOffset(), selection.getLength(), (IClassFile) editorInput); 134 } catch (JavaModelException e) { 135 ExceptionHandler.handle(e, getShell(), RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringMessages.IntroduceIndirectionAction_unknown_exception); 136 } 137 } 138 139 private void run(int offset, int length, ICompilationUnit unit) throws JavaModelException { 140 if (!ActionUtil.isEditable(fEditor, getShell(), unit)) 141 return; 142 RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(unit, offset, length, getShell()); 143 } 144 145 private void run(int offset, int length, IClassFile file) throws JavaModelException { 146 if (!ActionUtil.isEditable(fEditor, getShell(), file)) 147 return; 148 RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(file, offset, length, getShell()); 149 } 150 151 private void run(IMethod method) throws JavaModelException { 152 if (!ActionUtil.isEditable(fEditor, getShell(), method)) 153 return; 154 RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(method, getShell()); 155 } 156 } 157 | Popular Tags |