1 11 package org.eclipse.jdt.internal.ui.refactoring.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.widgets.Shell; 16 17 import org.eclipse.jface.dialogs.MessageDialog; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 20 import org.eclipse.jface.text.ITextSelection; 21 22 import org.eclipse.ui.IWorkbenchSite; 23 import org.eclipse.ui.PlatformUI; 24 25 import org.eclipse.jdt.core.IMethod; 26 import org.eclipse.jdt.core.ISourceRange; 27 import org.eclipse.jdt.core.ITypeRoot; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 33 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 34 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 35 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 36 37 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 38 39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 42 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 43 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 44 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 45 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 46 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 47 48 55 public class InlineMethodAction extends SelectionDispatchAction { 56 57 private JavaEditor fEditor; 58 59 63 public InlineMethodAction(JavaEditor editor) { 64 this(editor.getEditorSite()); 65 fEditor= editor; 66 setEnabled(SelectionConverter.canOperateOn(fEditor)); 67 } 68 69 public InlineMethodAction(IWorkbenchSite site) { 70 super(site); 71 setText(RefactoringMessages.InlineMethodAction_inline_Method); 72 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION); 73 } 74 75 77 80 public void selectionChanged(IStructuredSelection selection) { 81 try { 82 setEnabled(RefactoringAvailabilityTester.isInlineMethodAvailable(selection)); 83 } catch (JavaModelException e) { 84 if (JavaModelUtil.isExceptionToBeLogged(e)) 85 JavaPlugin.log(e); 86 } 87 } 88 89 92 public void run(IStructuredSelection selection) { 93 try { 94 Assert.isTrue(RefactoringAvailabilityTester.isInlineMethodAvailable(selection)); 95 IMethod method= (IMethod) selection.getFirstElement(); 96 ISourceRange nameRange= method.getNameRange(); 97 run(nameRange.getOffset(), nameRange.getLength(), method.getTypeRoot()); 98 } catch (JavaModelException e) { 99 ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_unexpected_exception); 100 } 101 } 102 103 106 public void selectionChanged(ITextSelection selection) { 107 setEnabled(true); 108 } 109 110 114 public void selectionChanged(JavaTextSelection selection) { 115 try { 116 setEnabled(RefactoringAvailabilityTester.isInlineMethodAvailable(selection)); 117 } catch (JavaModelException e) { 118 setEnabled(false); 119 } 120 } 121 122 125 public void run(ITextSelection selection) { 126 ITypeRoot typeRoot= SelectionConverter.getInputAsTypeRoot(fEditor); 127 if (typeRoot == null) 128 return; 129 if (! JavaElementUtil.isSourceAvailable(typeRoot)) 130 return; 131 run(selection.getOffset(), selection.getLength(), typeRoot); 132 } 133 134 private void run(int offset, int length, ITypeRoot typeRoot) { 135 if (!ActionUtil.isEditable(fEditor, getShell(), typeRoot)) 136 return; 137 try { 138 CompilationUnit compilationUnit= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null); 139 if (! RefactoringExecutionStarter.startInlineMethodRefactoring(typeRoot, compilationUnit, offset, length, getShell())) { 140 MessageDialog.openInformation(getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_no_method_invocation_or_declaration_selected); 141 } 142 } catch (JavaModelException e) { 143 ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_unexpected_exception); 144 } 145 } 146 147 public boolean tryInlineMethod(ITypeRoot typeRoot, CompilationUnit node, ITextSelection selection, Shell shell) { 148 try { 149 if (RefactoringExecutionStarter.startInlineMethodRefactoring(typeRoot, node, selection.getOffset(), selection.getLength(), shell)) { 150 return true; 151 } 152 } catch (JavaModelException exception) { 153 JavaPlugin.log(exception); 154 } 155 return false; 156 } 157 } 158 | Popular Tags |