]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 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.core.runtime.CoreException; | |
14 | ||
15 | import org.eclipse.jface.viewers.IStructuredSelection; | |
16 | ||
17 | import org.eclipse.jface.text.ITextSelection; | |
18 | ||
19 | import org.eclipse.ui.IWorkbenchSite; | |
20 | ||
21 | import org.eclipse.jdt.core.IJavaElement; | |
22 | import org.eclipse.jdt.core.IType; | |
23 | import org.eclipse.jdt.core.JavaModelException; | |
24 | ||
25 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; | |
26 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; | |
27 | ||
28 | import org.eclipse.jdt.internal.ui.actions.ActionUtil; | |
29 | import org.eclipse.jdt.internal.ui.actions.SelectionConverter; | |
30 | import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | |
31 | import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; | |
32 | import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; | |
33 | import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions; | |
34 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
35 | ||
36 | /** | |
37 | * Extracts selected fields into a new class and replaces the fields with a new field to the new class. | |
38 | * * | |
39 | * <p> | |
40 | * This class may be instantiated; it is not intended to be subclassed. | |
41 | * </p> | |
42 | * | |
43 | * @since 3.4 | |
44 | * | |
45 | * @noextend This class is not intended to be subclassed by clients. | |
46 | */ | |
47 | public class ExtractClassAction extends SelectionDispatchAction { | |
48 | private JavaEditor fEditor; | |
49 | ||
50 | /** | |
51 | * Note: This constructor is for internal use only. Clients should not call this constructor. | |
52 | * @param editor the java editor | |
53 | * | |
54 | * @noreference This constructor is not intended to be referenced by clients. | |
55 | */ | |
56 | public ExtractClassAction(JavaEditor editor) { | |
57 | this(editor.getEditorSite()); | |
58 | fEditor= editor; | |
59 | setEnabled(SelectionConverter.canOperateOn(fEditor)); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Creates a new <code>ExtractClassAction</code>. The action requires | |
64 | * that the selection provided by the site's selection provider is of type <code> | |
65 | * org.eclipse.jface.viewers.IStructuredSelection</code>. | |
66 | * | |
67 | * @param site the site providing context information for this action | |
68 | */ | |
69 | public ExtractClassAction(IWorkbenchSite site) { | |
70 | super(site); | |
71 | setText(RefactoringMessages.ExtractClassAction_action_text); | |
72 | } | |
73 | ||
74 | //---- structured selection ------------------------------------------- | |
75 | ||
76 | /* | |
77 | * @see SelectionDispatchAction#selectionChanged(IStructuredSelection) | |
78 | */ | |
79 | @Override | |
80 | public void selectionChanged(IStructuredSelection selection) { | |
81 | try { | |
82 | IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection); | |
83 | setEnabled(RefactoringAvailabilityTester.isExtractClassAvailable(singleSelectedType)); | |
84 | } catch (JavaModelException e) { | |
85 | setEnabled(false); | |
86 | } | |
87 | } | |
88 | ||
89 | /* | |
90 | * @see SelectionDispatchAction#run(IStructuredSelection) | |
91 | */ | |
92 | @Override | |
93 | public void run(IStructuredSelection selection) { | |
94 | try { | |
95 | IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection); | |
96 | if (RefactoringAvailabilityTester.isExtractClassAvailable(singleSelectedType)) { | |
97 | if (!ActionUtil.isEditable(getShell(), singleSelectedType)) | |
98 | return; | |
99 | RefactoringExecutionStarter.startExtractClassRefactoring(singleSelectedType, getShell()); | |
100 | } | |
101 | } catch (CoreException e) { | |
102 | ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); | |
103 | } | |
104 | } | |
105 | ||
106 | /* | |
107 | * @see SelectionDispatchAction#selectionChanged(ITextSelection) | |
108 | */ | |
109 | @Override | |
110 | public void selectionChanged(ITextSelection selection) { | |
111 | setEnabled(true); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Note: This method is for internal use only. Clients should not call this method. | |
116 | * @param selection the changed selection | |
117 | * | |
118 | * @noreference This method is not intended to be referenced by clients. | |
119 | */ | |
120 | @Override | |
121 | public void selectionChanged(JavaTextSelection selection) { | |
122 | try { | |
123 | IJavaElement element= selection.resolveEnclosingElement(); | |
124 | if (element != null) { | |
125 | IType type= (IType) element.getAncestor(IJavaElement.TYPE); | |
126 | setEnabled(RefactoringAvailabilityTester.isExtractClassAvailable(type)); | |
127 | } else { | |
128 | setEnabled(false); | |
129 | } | |
130 | } catch (JavaModelException e) { | |
131 | setEnabled(false); | |
132 | } | |
133 | } | |
134 | ||
135 | /* | |
136 | * @see SelectionDispatchAction#run(ITextSelection) | |
137 | */ | |
138 | @Override | |
139 | public void run(ITextSelection selection) { | |
140 | try { | |
141 | if (!ActionUtil.isEditable(fEditor)) | |
142 | return; | |
143 | IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor); | |
144 | if (RefactoringAvailabilityTester.isExtractClassAvailable(type)) { | |
145 | RefactoringExecutionStarter.startExtractClassRefactoring(type, getShell()); | |
146 | } | |
147 | } catch (CoreException e) { | |
148 | ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); | |
149 | } | |
150 | } | |
151 | } |