]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.internal.corext.refactoring.RefactoringAvailabilityTester; | |
18 | import org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring; | |
19 | ||
20 | import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper; | |
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.actions.RefactoringStarter; | |
29 | import org.eclipse.jdt.internal.ui.refactoring.code.ExtractMethodWizard; | |
30 | ||
31 | /** | |
32 | * Extracts the code selected inside a compilation unit editor into a new method. | |
33 | * Necessary arguments, exceptions and returns values are computed and an | |
34 | * appropriate method signature is generated. | |
35 | * | |
36 | * <p> | |
37 | * This class may be instantiated; it is not intended to be subclassed. | |
38 | * </p> | |
39 | * | |
40 | * @since 2.0 | |
41 | * | |
42 | * @noextend This class is not intended to be subclassed by clients. | |
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 | * @noreference This method is not intended to be referenced by clients. | |
54 | */ | |
55 | public ExtractMethodAction(JavaEditor editor) { | |
56 | super(editor.getEditorSite()); | |
57 | setText(RefactoringMessages.ExtractMethodAction_label); | |
58 | fEditor= editor; | |
59 | setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); | |
60 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_METHOD_ACTION); | |
61 | } | |
62 | ||
63 | /* (non-Javadoc) | |
64 | * Method declared on SelectionDispatchAction | |
65 | */ | |
66 | @Override | |
67 | public void selectionChanged(ITextSelection selection) { | |
68 | setEnabled(selection.getLength() == 0 ? false : fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Note: This method is for internal use only. Clients should not call this method. | |
73 | * @param selection the Java text selection (internal type) | |
74 | * | |
75 | * @noreference This method is not intended to be referenced by clients. | |
76 | */ | |
77 | @Override | |
78 | public void selectionChanged(JavaTextSelection selection) { | |
79 | setEnabled(RefactoringAvailabilityTester.isExtractMethodAvailable(selection)); | |
80 | } | |
81 | ||
82 | /* (non-Javadoc) | |
83 | * Method declared on SelectionDispatchAction | |
84 | */ | |
85 | @Override | |
86 | public void run(ITextSelection selection) { | |
87 | if (!ActionUtil.isEditable(fEditor)) | |
88 | return; | |
89 | ExtractMethodRefactoring refactoring= new ExtractMethodRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength()); | |
90 | new RefactoringStarter().activate(new ExtractMethodWizard(refactoring), getShell(), RefactoringMessages.ExtractMethodAction_dialog_title, RefactoringSaveHelper.SAVE_NOTHING); | |
91 | } | |
92 | } |