a8f3a8e864d3b20eb1717e774d2f7ddae81abb1b
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / java / hover / JavaInformationProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java.hover;
12
13 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
14 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
15
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.information.IInformationProvider;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IPartListener;
21 import org.eclipse.ui.IPerspectiveDescriptor;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25
26 public class JavaInformationProvider implements IInformationProvider {
27
28         class EditorWatcher implements IPartListener {
29
30                 /**
31                  * @see IPartListener#partOpened(IWorkbenchPart)
32                  */
33                 public void partOpened(IWorkbenchPart part) {
34                 }
35
36                 /**
37                  * @see IPartListener#partDeactivated(IWorkbenchPart)
38                  */
39                 public void partDeactivated(IWorkbenchPart part) {
40                 }
41
42                 /**
43                  * @see IPartListener#partClosed(IWorkbenchPart)
44                  */
45                 public void partClosed(IWorkbenchPart part) {
46                         if (part == fEditor) {
47                                 fEditor.getSite().getWorkbenchWindow().getPartService()
48                                                 .removePartListener(fPartListener);
49                                 fPartListener = null;
50                         }
51                 }
52
53                 /**
54                  * @see IPartListener#partActivated(IWorkbenchPart)
55                  */
56                 public void partActivated(IWorkbenchPart part) {
57                         update();
58                 }
59
60                 public void partBroughtToTop(IWorkbenchPart part) {
61                         update();
62                 }
63         }
64
65         protected IEditorPart fEditor;
66
67         protected IPartListener fPartListener;
68
69         protected String fCurrentPerspective;
70
71         protected IJavaEditorTextHover fImplementation;
72
73         public JavaInformationProvider(IEditorPart editor) {
74
75                 fEditor = editor;
76
77                 if (fEditor != null) {
78
79                         fPartListener = new EditorWatcher();
80                         IWorkbenchWindow window = fEditor.getSite().getWorkbenchWindow();
81                         window.getPartService().addPartListener(fPartListener);
82
83                         update();
84                 }
85         }
86
87         protected void update() {
88
89                 IWorkbenchWindow window = fEditor.getSite().getWorkbenchWindow();
90                 IWorkbenchPage page = window.getActivePage();
91                 if (page != null) {
92
93                         IPerspectiveDescriptor perspective = page.getPerspective();
94                         if (perspective != null) {
95                                 String perspectiveId = perspective.getId();
96
97                                 if (fCurrentPerspective == null
98                                                 || fCurrentPerspective != perspectiveId) {
99                                         fCurrentPerspective = perspectiveId;
100
101                                         fImplementation = new JavaTypeHover();
102                                         fImplementation.setEditor(fEditor);
103                                 }
104                         }
105                 }
106         }
107
108         /*
109          * @see IInformationProvider#getSubject(ITextViewer, int)
110          */
111         public IRegion getSubject(ITextViewer textViewer, int offset) {
112
113                 if (textViewer != null)
114                         return JavaWordFinder.findWord(textViewer.getDocument(), offset);
115
116                 return null;
117         }
118
119         /*
120          * @see IInformationProvider#getInformation(ITextViewer, IRegion)
121          */
122         public String getInformation(ITextViewer textViewer, IRegion subject) {
123                 if (fImplementation != null) {
124                         String s = fImplementation.getHoverInfo(textViewer, subject);
125                         if (s != null && s.trim().length() > 0)
126                                 return s;
127                 }
128
129                 return null;
130         }
131 }