1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.swt.widgets.Shell; 14 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 17 import org.eclipse.jface.text.ITextSelection; 18 19 import org.eclipse.ui.IWorkbenchSite; 20 import org.eclipse.ui.PlatformUI; 21 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 28 29 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 30 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 36 37 47 public class InlineTempAction extends SelectionDispatchAction { 48 49 private JavaEditor fEditor; 50 51 56 public InlineTempAction(JavaEditor editor) { 57 this(editor.getEditorSite()); 58 fEditor= editor; 59 setEnabled(SelectionConverter.canOperateOn(fEditor)); 60 } 61 62 InlineTempAction(IWorkbenchSite site) { 63 super(site); 64 setText(RefactoringMessages.InlineTempAction_label); 65 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION); 66 } 67 68 70 73 public void selectionChanged(ITextSelection selection) { 74 setEnabled(true); 75 } 76 77 81 public void selectionChanged(JavaTextSelection selection) { 82 try { 83 setEnabled(RefactoringAvailabilityTester.isInlineTempAvailable(selection)); 84 } catch (JavaModelException e) { 85 setEnabled(false); 86 } 87 } 88 89 92 public void run(ITextSelection selection) { 93 try{ 94 ICompilationUnit input= SelectionConverter.getInputAsCompilationUnit(fEditor); 95 if (!ActionUtil.isEditable(fEditor)) 96 return; 97 RefactoringExecutionStarter.startInlineTempRefactoring(input, null, selection, getShell()); 98 } catch (JavaModelException e){ 99 ExceptionHandler.handle(e, RefactoringMessages.InlineTempAction_inline_temp, RefactoringMessages.NewTextRefactoringAction_exception); 100 } 101 } 102 103 106 public void run(IStructuredSelection selection) { 107 } 109 110 113 public void selectionChanged(IStructuredSelection selection) { 114 setEnabled(false); 115 } 116 117 boolean tryInlineTemp(ICompilationUnit unit, CompilationUnit node, ITextSelection selection, Shell shell) { 118 try { 119 if (RefactoringExecutionStarter.startInlineTempRefactoring(unit, node, selection, shell)) { 120 return true; 121 } 122 } catch (JavaModelException exception) { 123 ExceptionHandler.handle(exception, RefactoringMessages.InlineTempAction_inline_temp, RefactoringMessages.NewTextRefactoringAction_exception); 124 } 125 return false; 126 } 127 } 128 | Popular Tags |