]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementSuperImplementationHyperlink.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / javaeditor / JavaElementSuperImplementationHyperlink.java
1 /*******************************************************************************
2  * Copyright (c) 2010 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
12 package org.eclipse.jdt.internal.ui.javaeditor;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.viewers.StructuredSelection;
17
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.hyperlink.IHyperlink;
20
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.jdt.internal.corext.util.Messages;
25 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
26 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
27
28 import org.eclipse.jdt.ui.JavaElementLabels;
29 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32
33 /**
34  * Java element super implementation hyperlink.
35  * 
36  * @since 3.7
37  */
38 public class JavaElementSuperImplementationHyperlink implements IHyperlink {
39
40         private final SelectionDispatchAction fOpenAction;
41         private final IMethod fMethod;
42         private final boolean fQualify;
43         private IRegion fRegion;
44
45         /**
46          * Creates a new Java element super implementation hyperlink for methods.
47          * 
48          * @param region the region of the link
49          * @param openAction the action to use to open the java elements
50          * @param method the method to open
51          * @param qualify <code>true</code> if the hyperlink text should show a qualified name for
52          *            element
53          */
54         public JavaElementSuperImplementationHyperlink(IRegion region, SelectionDispatchAction openAction, IMethod method, boolean qualify) {
55                 Assert.isNotNull(openAction);
56                 Assert.isNotNull(region);
57                 Assert.isNotNull(method);
58
59                 fRegion= region;
60                 fOpenAction= openAction;
61                 fMethod= method;
62                 fQualify= qualify;
63         }
64
65         /* (non-Javadoc)
66          * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkRegion()
67          */
68         public IRegion getHyperlinkRegion() {
69                 return fRegion;
70         }
71
72         /* (non-Javadoc)
73          * @see org.eclipse.jface.text.hyperlink.IHyperlink#getTypeLabel()
74          */
75         public String getTypeLabel() {
76                 return null;
77         }
78
79         /* (non-Javadoc)
80          * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkText()
81          */
82         public String getHyperlinkText() {
83                 if (fQualify) {
84                         String methodLabel= JavaElementLabels.getElementLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED);
85                         return Messages.format(JavaEditorMessages.JavaElementSuperImplementationHyperlink_hyperlinkText_qualified, new Object[] { methodLabel });
86                 } else {
87                         return JavaEditorMessages.JavaElementSuperImplementationHyperlink_hyperlinkText;
88                 }
89         }
90
91         /* (non-Javadoc)
92          * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
93          */
94         public void open() {
95                 try {
96                         IMethod method= findSuperImplementation(fMethod);
97                         if (method != null)
98                                 fOpenAction.run(new StructuredSelection(method));
99                 } catch (JavaModelException e) {
100                         JavaPlugin.log(e);
101                         return;
102                 }
103         }
104
105         /**
106          * Finds the super implementation of the method.
107          * 
108          * @param method the method
109          * @return the super implementation of the method if any or <code>null</code>
110          * @throws JavaModelException if an exception occurs while creating the type hierarchy to find
111          *             the super implementation
112          */
113         static IMethod findSuperImplementation(IMethod method) throws JavaModelException {
114                 MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(method.getDeclaringType());
115                 return tester.findOverriddenMethod(method, false);
116         }
117
118 }