KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaModelException;
18
19 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
20 import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
21
22 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
23 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
24 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
25 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
26 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
27 import org.eclipse.jdt.internal.ui.refactoring.ExtractConstantWizard;
28 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
29 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
30 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
31 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
32
33 /**
34  * Extracts an expression into a constant field and replaces all occurrences of
35  * the expression with the new constant.
36  *
37  * <p>
38  * This class may be instantiated; it is not intended to be subclassed.
39  * </p>
40  *
41  * @since 2.1
42  */

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

51     public ExtractConstantAction(JavaEditor editor) {
52         super(editor.getEditorSite());
53         setText(RefactoringMessages.ExtractConstantAction_label);
54         fEditor= editor;
55         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
56         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_CONSTANT_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         setEnabled(RefactoringAvailabilityTester.isExtractConstantAvailable(selection));
71     }
72     
73     /* (non-Javadoc)
74      * Method declared on SelectionDispatchAction
75      */

76     public void run(ITextSelection selection) {
77         if (!ActionUtil.isEditable(fEditor))
78             return;
79         try{
80             final ExtractConstantRefactoring refactoring= new ExtractConstantRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
81             new RefactoringStarter().activate(refactoring, new ExtractConstantWizard(refactoring), getShell(), RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringSaveHelper.SAVE_NOTHING);
82         } catch (JavaModelException e){
83             ExceptionHandler.handle(e, RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringMessages.NewTextRefactoringAction_exception);
84         }
85     }
86 }
87
Popular Tags