KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > AbstractJavaEditorTextHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.ui.text.java.hover;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.core.runtime.FileLocator;
19 import org.eclipse.core.runtime.Platform;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.internal.text.html.HTMLPrinter;
26 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
27 import org.eclipse.jface.resource.JFaceResources;
28
29 import org.eclipse.jface.text.DefaultInformationControl;
30 import org.eclipse.jface.text.IInformationControl;
31 import org.eclipse.jface.text.IInformationControlCreator;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.jface.text.ITextHoverExtension;
34 import org.eclipse.jface.text.ITextViewer;
35
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IEditorPart;
38
39 import org.eclipse.ui.editors.text.EditorsUI;
40
41 import org.eclipse.jdt.core.ICodeAssist;
42 import org.eclipse.jdt.core.IJavaElement;
43 import org.eclipse.jdt.core.JavaModelException;
44
45 import org.eclipse.jdt.ui.PreferenceConstants;
46 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
47
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
50 import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager;
51 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
52
53 import org.osgi.framework.Bundle;
54
55 /**
56  * Abstract class for providing hover information for Java elements.
57  *
58  * @since 2.1
59  */

60 public abstract class AbstractJavaEditorTextHover implements IJavaEditorTextHover, ITextHoverExtension {
61     /**
62      * The style sheet (css).
63      * @since 3.2
64      */

65     private static String JavaDoc fgStyleSheet;
66     private IEditorPart fEditor;
67
68     /*
69      * @see IJavaEditorTextHover#setEditor(IEditorPart)
70      */

71     public void setEditor(IEditorPart editor) {
72         fEditor= editor;
73     }
74
75     protected IEditorPart getEditor() {
76         return fEditor;
77     }
78
79     protected ICodeAssist getCodeAssist() {
80         if (fEditor != null) {
81             IEditorInput input= fEditor.getEditorInput();
82             if (input instanceof IClassFileEditorInput) {
83                 IClassFileEditorInput cfeInput= (IClassFileEditorInput) input;
84                 return cfeInput.getClassFile();
85             }
86
87             WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
88             return manager.getWorkingCopy(input, false);
89         }
90
91         return null;
92     }
93
94     /*
95      * @see ITextHover#getHoverRegion(ITextViewer, int)
96      */

97     public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
98         return JavaWordFinder.findWord(textViewer.getDocument(), offset);
99     }
100
101     /*
102      * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
103      */

104     public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
105         
106         /*
107          * The region should be a word region an not of length 0.
108          * This check is needed because codeSelect(...) also finds
109          * the Java element if the offset is behind the word.
110          */

111         if (hoverRegion.getLength() == 0)
112             return null;
113         
114         ICodeAssist resolve= getCodeAssist();
115         if (resolve != null) {
116             try {
117                 IJavaElement[] result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength());
118                 if (result == null)
119                     return null;
120
121                 int nResults= result.length;
122                 if (nResults == 0)
123                     return null;
124
125                 return getHoverInfo(result);
126
127             } catch (JavaModelException x) {
128                 return null;
129             }
130         }
131         return null;
132     }
133
134     /**
135      * Provides hover information for the given Java elements.
136      *
137      * @param javaElements the Java elements for which to provide hover information
138      * @return the hover information string
139      * @since 2.1
140      */

141     protected String JavaDoc getHoverInfo(IJavaElement[] javaElements) {
142         return null;
143     }
144
145     /*
146      * @see ITextHoverExtension#getHoverControlCreator()
147      * @since 3.0
148      */

149     public IInformationControlCreator getHoverControlCreator() {
150         return new IInformationControlCreator() {
151             public IInformationControl createInformationControl(Shell parent) {
152                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString());
153             }
154         };
155     }
156
157     protected static String JavaDoc getStyleSheet() {
158         if (fgStyleSheet == null)
159             fgStyleSheet= loadStyleSheet();
160         String JavaDoc css= fgStyleSheet;
161         if (css != null) {
162             FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0];
163             css= HTMLPrinter.convertTopLevelFont(css, fontData);
164         }
165
166         return css;
167     }
168     
169     private static String JavaDoc loadStyleSheet() {
170         Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId());
171         URL JavaDoc styleSheetURL= bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$
172
if (styleSheetURL != null) {
173             try {
174                 styleSheetURL= FileLocator.toFileURL(styleSheetURL);
175                 BufferedReader JavaDoc reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(styleSheetURL.openStream()));
176                 StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(200);
177                 String JavaDoc line= reader.readLine();
178                 while (line != null) {
179                     buffer.append(line);
180                     buffer.append('\n');
181                     line= reader.readLine();
182                 }
183                 return buffer.toString();
184             } catch (IOException JavaDoc ex) {
185                 JavaPlugin.log(ex);
186                 return ""; //$NON-NLS-1$
187
}
188         }
189         return null;
190     }
191 }
192
Popular Tags