KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ConvertLocalToFieldAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
35  * Action to convert a local variable to a field.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * </p>
39  *
40  * @since 2.1
41  */

42 public class ConvertLocalToFieldAction extends SelectionDispatchAction {
43
44     private final JavaEditor fEditor;
45     
46     /**
47      * Note: This constructor is for internal use only. Clients should not call this constructor.
48      *
49      * @param editor the java editor
50      */

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     /* (non-Javadoc)
60      * Method declared on SelectionDispatchAction
61      */

62     public void selectionChanged(ITextSelection selection) {
63         setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
64     }
65     
66     /**
67      * Note: This method is for internal use only. Clients should not call this method.
68      */

69     public void selectionChanged(JavaTextSelection selection) {
70         try {
71             setEnabled(RefactoringAvailabilityTester.isPromoteTempAvailable(selection));
72         } catch (JavaModelException e) {
73             setEnabled(false);
74         }
75     }
76     
77     /* (non-Javadoc)
78      * Method declared on SelectionDispatchAction
79      */

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