1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.jface.text.ITextSelection; 14 15 import org.eclipse.ui.PlatformUI; 16 17 import org.eclipse.jdt.core.ICompilationUnit; 18 import org.eclipse.jdt.core.JavaModelException; 19 20 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 21 import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring; 22 23 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 24 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 25 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 26 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 27 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 28 import org.eclipse.jdt.internal.ui.refactoring.PromoteTempWizard; 29 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 30 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper; 31 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter; 32 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 33 34 42 public class ConvertLocalToFieldAction extends SelectionDispatchAction { 43 44 private final JavaEditor fEditor; 45 46 51 public ConvertLocalToFieldAction(JavaEditor editor) { 52 super(editor.getEditorSite()); 53 setText(RefactoringMessages.ConvertLocalToField_label); 54 fEditor= editor; 55 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 56 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.PROMOTE_TEMP_TO_FIELD_ACTION); 57 } 58 59 62 public void selectionChanged(ITextSelection selection) { 63 setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 64 } 65 66 69 public void selectionChanged(JavaTextSelection selection) { 70 try { 71 setEnabled(RefactoringAvailabilityTester.isPromoteTempAvailable(selection)); 72 } catch (JavaModelException e) { 73 setEnabled(false); 74 } 75 } 76 77 80 public void run(ITextSelection selection) { 81 if (!ActionUtil.isEditable(fEditor)) 82 return; 83 try{ 84 ICompilationUnit cunit= SelectionConverter.getInputAsCompilationUnit(fEditor); 85 final PromoteTempToFieldRefactoring refactoring= new PromoteTempToFieldRefactoring(cunit, selection.getOffset(), selection.getLength()); 86 new RefactoringStarter().activate(refactoring, new PromoteTempWizard(refactoring), getShell(), RefactoringMessages.ConvertLocalToField_title, RefactoringSaveHelper.SAVE_NOTHING); 87 } catch (JavaModelException e){ 88 ExceptionHandler.handle(e, RefactoringMessages.ConvertLocalToField_title, RefactoringMessages.NewTextRefactoringAction_exception); 89 } 90 } 91 } 92 | Popular Tags |