KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.CoreException;
14
15 import org.eclipse.jface.text.ITextSelection;
16
17 import org.eclipse.ui.PlatformUI;
18
19 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
20 import org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring;
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.RefactoringMessages;
28 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
29 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
30 import org.eclipse.jdt.internal.ui.refactoring.code.ExtractMethodWizard;
31 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
32
33 /**
34  * Extracts the code selected inside a compilation unit editor into a new method.
35  * Necessary arguments, exceptions and returns values are computed and an
36  * appropriate method signature is generated.
37  *
38  * <p>
39  * This class may be instantiated; it is not intended to be subclassed.
40  * </p>
41  *
42  * @since 2.0
43  */

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

53     public ExtractMethodAction(JavaEditor editor) {
54         super(editor.getEditorSite());
55         setText(RefactoringMessages.ExtractMethodAction_label);
56         fEditor= editor;
57         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
58         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_METHOD_ACTION);
59     }
60
61     /* (non-Javadoc)
62      * Method declared on SelectionDispatchAction
63      */

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

71     public void selectionChanged(JavaTextSelection selection) {
72         setEnabled(RefactoringAvailabilityTester.isExtractMethodAvailable(selection));
73     }
74     
75     /* (non-Javadoc)
76      * Method declared on SelectionDispatchAction
77      */

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