]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/java/hover/AbstractJavaEditorTextHover.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / java / hover / AbstractJavaEditorTextHover.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 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  *     Brock Janiczak <brockj@tpg.com.au> - [implementation] Streams not being closed in Javadoc views - https://bugs.eclipse.org/bugs/show_bug.cgi?id=214854
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.text.java.hover;
13
14 import org.eclipse.swt.widgets.Shell;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.DefaultInformationControl;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IInformationControl;
20 import org.eclipse.jface.text.IInformationControlCreator;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextHoverExtension;
23 import org.eclipse.jface.text.ITextHoverExtension2;
24 import org.eclipse.jface.text.ITextViewer;
25
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28
29 import org.eclipse.ui.editors.text.EditorsUI;
30
31 import org.eclipse.jdt.core.ICodeAssist;
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.ITypeRoot;
34 import org.eclipse.jdt.core.JavaModelException;
35
36 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
40 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
41 import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager;
42 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
43
44
45 /**
46  * Abstract class for providing hover information for Java elements.
47  *
48  * @since 2.1
49  */
50 public abstract class AbstractJavaEditorTextHover implements IJavaEditorTextHover, ITextHoverExtension, ITextHoverExtension2 {
51         private IEditorPart fEditor;
52
53         /*
54          * @see IJavaEditorTextHover#setEditor(IEditorPart)
55          */
56         public void setEditor(IEditorPart editor) {
57                 fEditor= editor;
58         }
59
60         protected IEditorPart getEditor() {
61                 return fEditor;
62         }
63
64         protected ICodeAssist getCodeAssist() {
65                 if (fEditor != null) {
66                         IEditorInput input= fEditor.getEditorInput();
67                         if (input instanceof IClassFileEditorInput) {
68                                 IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
69                                 return cfeInput.getClassFile();
70                         }
71
72                         WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
73                         return manager.getWorkingCopy(input, false);
74                 }
75
76                 return null;
77         }
78
79     /*
80          * @see org.eclipse.jface.text.ITextHoverExtension2#getHoverInfo2(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
81          * @since 3.4
82          */
83         public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
84                 return getHoverInfo(textViewer, hoverRegion);
85         }
86
87         /*
88          * @see ITextHover#getHoverRegion(ITextViewer, int)
89          */
90         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
91                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
92         }
93
94         /**
95          * Returns the Java elements at the given hover region.
96          *
97          * @param textViewer the text viewer
98          * @param hoverRegion the hover region
99          * @return the array with the Java elements or <code>null</code>
100          * @since 3.4
101          */
102         protected IJavaElement[] getJavaElementsAt(ITextViewer textViewer, IRegion hoverRegion) {
103                 /*
104                  * The region should be a word region an not of length 0.
105                  * This check is needed because codeSelect(...) also finds
106                  * the Java element if the offset is behind the word.
107                  */
108                 if (hoverRegion.getLength() == 0)
109                         return null;
110                 
111                 IDocument document= textViewer.getDocument();
112                 if (document != null && isInheritDoc(document, hoverRegion))
113                         return null;
114
115                 ICodeAssist resolve= getCodeAssist();
116                 if (resolve != null) {
117                         try {
118                                 return resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
119                         } catch (JavaModelException x) {
120                                 return null;
121                         }
122                 }
123                 return null;
124         }
125
126         /**
127          * Returns whether the word is "inheritDoc".
128          * 
129          * @param document the document
130          * @param wordRegion the word region
131          * @return <code>true</code> iff the word is "inheritDoc"
132          * @since 3.7
133          */
134         private static boolean isInheritDoc(IDocument document, IRegion wordRegion) {
135                 try {
136                         String word= document.get(wordRegion.getOffset(), wordRegion.getLength());
137                         return "inheritDoc".equals(word); //$NON-NLS-1$
138                 } catch (BadLocationException e) {
139                         return false;
140                 }
141         }
142
143         /*
144          * @see ITextHoverExtension#getHoverControlCreator()
145          * @since 3.0
146          */
147         public IInformationControlCreator getHoverControlCreator() {
148                 return new IInformationControlCreator() {
149                         public IInformationControl createInformationControl(Shell parent) {
150                                 return new DefaultInformationControl(parent, EditorsUI.getTooltipAffordanceString());
151                         }
152                 };
153         }
154
155         /**
156          * Delegate method for {@link JavaInformationProvider#getInformationPresenterControlCreator()}
157          * 
158          * @return the information control creator or null if none is available
159          * @since 3.4
160          */
161         public IInformationControlCreator getInformationPresenterControlCreator() {
162                 return new IInformationControlCreator() {
163                         public IInformationControl createInformationControl(Shell shell) {
164                                 return new DefaultInformationControl(shell, true);
165                         }
166                 };
167         }
168
169         protected ITypeRoot getEditorInputJavaElement() {
170                 IEditorPart editor= getEditor();
171                 if (editor != null)
172                         return EditorUtility.getEditorInputJavaElement(editor, false);
173                 return null;
174         }
175 }