KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.internal.text.html.BrowserInformationControl;
18 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
19
20 import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
21 import org.eclipse.jface.text.DefaultInformationControl;
22 import org.eclipse.jface.text.IInformationControl;
23 import org.eclipse.jface.text.IInformationControlCreator;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.information.IInformationProvider;
27 import org.eclipse.jface.text.information.IInformationProviderExtension2;
28
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IPartListener;
31 import org.eclipse.ui.IPerspectiveDescriptor;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35
36 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
37
38 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
39
40
41 public class JavaInformationProvider implements IInformationProvider, IInformationProviderExtension2 {
42
43     
44     /**
45      * Control creator.
46      *
47      * @since 3.3
48      */

49     private static final class ControlCreator extends AbstractReusableInformationControlCreator {
50         /*
51          * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
52          */

53         public IInformationControl doCreateInformationControl(Shell parent) {
54             int shellStyle= SWT.RESIZE | SWT.TOOL;
55             int style= SWT.V_SCROLL | SWT.H_SCROLL;
56             if (BrowserInformationControl.isAvailable(parent))
57                 return new BrowserInformationControl(parent, shellStyle, style);
58             else
59                 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
60         }
61     }
62     
63
64     class EditorWatcher implements IPartListener {
65
66         /**
67          * @see IPartListener#partOpened(IWorkbenchPart)
68          */

69         public void partOpened(IWorkbenchPart part) {
70         }
71
72         /**
73          * @see IPartListener#partDeactivated(IWorkbenchPart)
74          */

75         public void partDeactivated(IWorkbenchPart part) {
76         }
77
78         /**
79          * @see IPartListener#partClosed(IWorkbenchPart)
80          */

81         public void partClosed(IWorkbenchPart part) {
82             if (part == fEditor) {
83                 fEditor.getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener);
84                 fPartListener= null;
85             }
86         }
87
88         /**
89          * @see IPartListener#partActivated(IWorkbenchPart)
90          */

91         public void partActivated(IWorkbenchPart part) {
92             update();
93         }
94
95         public void partBroughtToTop(IWorkbenchPart part) {
96             update();
97         }
98     }
99
100     protected IEditorPart fEditor;
101     protected IPartListener fPartListener;
102
103     protected String JavaDoc fCurrentPerspective;
104     protected IJavaEditorTextHover fImplementation;
105
106     /**
107      * The presentation control creator.
108      *
109      * @since 3.2
110      */

111     private IInformationControlCreator fPresenterControlCreator;
112     
113
114
115     public JavaInformationProvider(IEditorPart editor) {
116
117         fEditor= editor;
118
119         if (fEditor != null) {
120
121             fPartListener= new EditorWatcher();
122             IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
123             window.getPartService().addPartListener(fPartListener);
124
125             update();
126         }
127     }
128
129     protected void update() {
130
131         IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
132         IWorkbenchPage page= window.getActivePage();
133         if (page != null) {
134
135             IPerspectiveDescriptor perspective= page.getPerspective();
136             if (perspective != null) {
137                 String JavaDoc perspectiveId= perspective.getId();
138
139                 if (fCurrentPerspective == null || fCurrentPerspective != perspectiveId) {
140                     fCurrentPerspective= perspectiveId;
141
142                     fImplementation= new JavaTypeHover();
143                     fImplementation.setEditor(fEditor);
144                 }
145             }
146         }
147     }
148
149     /*
150      * @see IInformationProvider#getSubject(ITextViewer, int)
151      */

152     public IRegion getSubject(ITextViewer textViewer, int offset) {
153
154         if (textViewer != null)
155             return JavaWordFinder.findWord(textViewer.getDocument(), offset);
156
157         return null;
158     }
159
160     /*
161      * @see IInformationProvider#getInformation(ITextViewer, IRegion)
162      */

163     public String JavaDoc getInformation(ITextViewer textViewer, IRegion subject) {
164         if (fImplementation != null) {
165             String JavaDoc s= fImplementation.getHoverInfo(textViewer, subject);
166             if (s != null && s.trim().length() > 0) {
167                 return s;
168             }
169         }
170
171         return null;
172     }
173
174     /*
175      * @see IInformationProviderExtension2#getInformationPresenterControlCreator()
176      * @since 3.1
177      */

178     public IInformationControlCreator getInformationPresenterControlCreator() {
179         if (fPresenterControlCreator == null)
180             fPresenterControlCreator= new ControlCreator();
181         return fPresenterControlCreator;
182     }
183 }
184
Popular Tags