KickJava   Java API By Example, From Geeks To Geeks.

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


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.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18
19 import org.eclipse.jface.text.ITextSelection;
20
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.jdt.core.Flags;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29
30 import org.eclipse.jdt.internal.corext.util.Messages;
31 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
32 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
33
34 import org.eclipse.jdt.ui.JavaUI;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
39 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
40 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43
44 /**
45  * The action opens a Java editor on the selected method's super implementation.
46  * <p>
47  * The action is applicable to selections containing elements of type <code>
48  * IMethod</code>.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 2.0
55  */

56 public class OpenSuperImplementationAction extends SelectionDispatchAction {
57
58     private JavaEditor fEditor;
59
60     /**
61      * Creates a new <code>OpenSuperImplementationAction</code>. The action requires
62      * that the selection provided by the site's selection provider is of type <code>
63      * org.eclipse.jface.viewers.IStructuredSelection</code>.
64      *
65      * @param site the site providing context information for this action
66      */

67     public OpenSuperImplementationAction(IWorkbenchSite site) {
68         super(site);
69         setText(ActionMessages.OpenSuperImplementationAction_label);
70         setDescription(ActionMessages.OpenSuperImplementationAction_description);
71         setToolTipText(ActionMessages.OpenSuperImplementationAction_tooltip);
72         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_SUPER_IMPLEMENTATION_ACTION);
73     }
74     
75     /**
76      * Creates a new <code>OpenSuperImplementationAction</code>. The action requires
77      * that the selection provided by the given selection provider is of type <code>
78      * org.eclipse.jface.viewers.IStructuredSelection</code>.
79      *
80      * @param site the site providing context information for this action
81      * @param provider a special selection provider which is used instead
82      * of the site's selection provider or <code>null</code> to use the site's
83      * selection provider
84      *
85      * @since 3.2
86      * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This API will be
87      * removed after 3.2 M5.
88      */

89     public OpenSuperImplementationAction(IWorkbenchSite site, ISelectionProvider provider) {
90         this(site);
91         setSpecialSelectionProvider(provider);
92     }
93     
94
95     
96     /**
97      * Note: This constructor is for internal use only. Clients should not call this constructor.
98      * @param editor the Java editor
99      */

100     public OpenSuperImplementationAction(JavaEditor editor) {
101         this(editor.getEditorSite());
102         fEditor= editor;
103         setEnabled(SelectionConverter.canOperateOn(fEditor));
104     }
105     
106     /* (non-Javadoc)
107      * Method declared on SelectionDispatchAction.
108      */

109     public void selectionChanged(ITextSelection selection) {
110     }
111
112     /* (non-Javadoc)
113      * Method declared on SelectionDispatchAction.
114      */

115     public void selectionChanged(IStructuredSelection selection) {
116         IMethod method= getMethod(selection);
117         
118         setEnabled(method != null && checkMethod(method));
119     }
120     
121     /* (non-Javadoc)
122      * Method declared on SelectionDispatchAction.
123      */

124     public void run(ITextSelection selection) {
125         if (!ActionUtil.isProcessable(fEditor))
126             return;
127         IJavaElement element= elementAtOffset();
128         if (element == null || !(element instanceof IMethod)) {
129             MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OpenSuperImplementationAction_not_applicable);
130             return;
131         }
132         run((IMethod) element);
133     }
134     
135     /* (non-Javadoc)
136      * Method declared on SelectionDispatchAction.
137      */

138     public void run(IStructuredSelection selection) {
139         run(getMethod(selection));
140     }
141     
142     /*
143      * No Javadoc since the method isn't meant to be public but is
144      * since the beginning
145      */

146     public void run(IMethod method) {
147         if (method == null)
148             return;
149         if (!ActionUtil.isProcessable(getShell(), method))
150             return;
151         
152         if (!checkMethod(method)) {
153             MessageDialog.openInformation(getShell(), getDialogTitle(),
154                 Messages.format(ActionMessages.OpenSuperImplementationAction_no_super_implementation, method.getElementName()));
155             return;
156         }
157
158         try {
159             IMethod impl= findSuperImplementation(method);
160             if (impl != null) {
161                 JavaUI.openInEditor(impl, true, true);
162             }
163         } catch (CoreException e) {
164             ExceptionHandler.handle(e, getDialogTitle(), ActionMessages.OpenSuperImplementationAction_error_message);
165         }
166     }
167     
168     private IMethod findSuperImplementation(IMethod method) throws JavaModelException {
169         MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(method.getDeclaringType());
170         return tester.findOverriddenMethod(method, false);
171     }
172     
173     
174     private IMethod getMethod(IStructuredSelection selection) {
175         if (selection.size() != 1)
176             return null;
177         Object JavaDoc element= selection.getFirstElement();
178         if (element instanceof IMethod) {
179             return (IMethod) element;
180         }
181         return null;
182     }
183     
184     private boolean checkMethod(IMethod method) {
185         try {
186             int flags= method.getFlags();
187             if (!Flags.isStatic(flags) && !Flags.isPrivate(flags)) {
188                 IType declaringType= method.getDeclaringType();
189                 if (SuperTypeHierarchyCache.hasInCache(declaringType)) {
190                     if (findSuperImplementation(method) == null) {
191                         return false;
192                     }
193                 }
194                 return true;
195             }
196         } catch (JavaModelException e) {
197             if (!e.isDoesNotExist()) {
198                 JavaPlugin.log(e);
199             }
200         }
201         return false;
202     }
203     
204     private IJavaElement elementAtOffset() {
205         try {
206             return SelectionConverter.getElementAtOffset(fEditor);
207         } catch(JavaModelException e) {
208         }
209         return null;
210     }
211     
212     private static String JavaDoc getDialogTitle() {
213         return ActionMessages.OpenSuperImplementationAction_error_title;
214     }
215 }
216
Popular Tags