]>
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 java.io.CharConversionException; | |
14 | ||
15 | import org.eclipse.jface.dialogs.MessageDialog; | |
16 | import org.eclipse.jface.viewers.IStructuredSelection; | |
17 | ||
18 | import org.eclipse.jface.text.ITextSelection; | |
19 | ||
20 | import org.eclipse.ui.IWorkbenchSite; | |
21 | import org.eclipse.ui.PlatformUI; | |
22 | ||
23 | import org.eclipse.jdt.core.ICompilationUnit; | |
24 | import org.eclipse.jdt.core.IType; | |
25 | import org.eclipse.jdt.core.JavaModelException; | |
26 | ||
27 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; | |
28 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; | |
29 | import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; | |
30 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; | |
31 | ||
32 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
33 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
34 | import org.eclipse.jdt.internal.ui.actions.ActionUtil; | |
35 | import org.eclipse.jdt.internal.ui.actions.SelectionConverter; | |
36 | import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | |
37 | import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; | |
38 | import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; | |
39 | import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions; | |
40 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
41 | ||
42 | /** | |
43 | * Tries to use a super type of a class where possible. | |
44 | * | |
45 | * <p> | |
46 | * This class may be instantiated; it is not intended to be subclassed. | |
47 | * </p> | |
48 | * | |
49 | * @since 2.1 | |
50 | * | |
51 | * @noextend This class is not intended to be subclassed by clients. | |
52 | */ | |
53 | // Note: The disclaimer about instantiating and subclassing got added in 3.1. | |
54 | // Don't make this class final or remove a constructor! | |
55 | public class UseSupertypeAction extends SelectionDispatchAction{ | |
56 | ||
57 | private JavaEditor fEditor; | |
58 | ||
59 | /** | |
60 | * Note: This constructor is for internal use only. Clients should not call this constructor. | |
61 | * @param editor the java editor | |
62 | * | |
63 | * @noreference This constructor is not intended to be referenced by clients. | |
64 | */ | |
65 | public UseSupertypeAction(JavaEditor editor) { | |
66 | this(editor.getEditorSite()); | |
67 | fEditor= editor; | |
68 | setEnabled(SelectionConverter.canOperateOn(fEditor)); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Creates a new <code>UseSupertypeAction</code>. The action requires | |
73 | * that the selection provided by the site's selection provider is of type <code> | |
74 | * org.eclipse.jface.viewers.IStructuredSelection</code>. | |
75 | * | |
76 | * @param site the site providing context information for this action | |
77 | */ | |
78 | public UseSupertypeAction(IWorkbenchSite site) { | |
79 | super(site); | |
80 | setText(RefactoringMessages.UseSupertypeAction_use_Supertype); | |
81 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.USE_SUPERTYPE_ACTION); | |
82 | } | |
83 | ||
84 | //---- structured selection --------------------------------------------------- | |
85 | ||
86 | /* | |
87 | * @see SelectionDispatchAction#selectionChanged(IStructuredSelection) | |
88 | */ | |
89 | @Override | |
90 | public void selectionChanged(IStructuredSelection selection) { | |
91 | try { | |
92 | setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)); | |
93 | } catch (JavaModelException e) { | |
94 | // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253 | |
95 | if (!(e.getException() instanceof CharConversionException) && JavaModelUtil.isExceptionToBeLogged(e)) | |
96 | JavaPlugin.log(e); | |
97 | setEnabled(false);// no UI - happens on selection changes | |
98 | } | |
99 | } | |
100 | ||
101 | /* | |
102 | * @see SelectionDispatchAction#run(IStructuredSelection) | |
103 | */ | |
104 | @Override | |
105 | public void run(IStructuredSelection selection) { | |
106 | try { | |
107 | if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)) { | |
108 | IType singleSelectedType= getSingleSelectedType(selection); | |
109 | if (! ActionUtil.isEditable(getShell(), singleSelectedType)) | |
110 | return; | |
111 | RefactoringExecutionStarter.startUseSupertypeRefactoring(singleSelectedType, getShell()); | |
112 | } | |
113 | } catch (JavaModelException e) { | |
114 | ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); | |
115 | } | |
116 | } | |
117 | ||
118 | private static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException{ | |
119 | if (selection.isEmpty() || selection.size() != 1) | |
120 | return null; | |
121 | ||
122 | Object first= selection.getFirstElement(); | |
123 | if (first instanceof IType) | |
124 | return (IType)first; | |
125 | if (first instanceof ICompilationUnit) | |
126 | return JavaElementUtil.getMainType((ICompilationUnit)first); | |
127 | return null; | |
128 | } | |
129 | ||
130 | //---- text selection ------------------------------------------------------ | |
131 | ||
132 | /* | |
133 | * @see SelectionDispatchAction#selectionChanged(ITextSelection) | |
134 | */ | |
135 | @Override | |
136 | public void selectionChanged(ITextSelection selection) { | |
137 | setEnabled(true); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Note: This method is for internal use only. Clients should not call this method. | |
142 | * | |
143 | * @param selection the Java text selection | |
144 | * @noreference This method is not intended to be referenced by clients. | |
145 | */ | |
146 | @Override | |
147 | public void selectionChanged(JavaTextSelection selection) { | |
148 | try { | |
149 | setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)); | |
150 | } catch (JavaModelException e) { | |
151 | setEnabled(false); | |
152 | } | |
153 | } | |
154 | ||
155 | /* | |
156 | * @see SelectionDispatchAction#run(ITextSelection) | |
157 | */ | |
158 | @Override | |
159 | public void run(ITextSelection selection) { | |
160 | try { | |
161 | if (!ActionUtil.isProcessable(fEditor)) | |
162 | return; | |
163 | IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor); | |
164 | if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(type)) { | |
165 | if (!ActionUtil.isEditable(getShell(), type)) | |
166 | return; | |
167 | RefactoringExecutionStarter.startUseSupertypeRefactoring(type, getShell()); | |
168 | } else { | |
169 | MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.UseSupertypeAction_to_activate); | |
170 | } | |
171 | } catch (JavaModelException e) { | |
172 | ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); | |
173 | } | |
174 | } | |
175 | } |