098883ab4ad667a3d1592add51b46a8e784a4e28
[phpeclipse.git] /
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
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.information.IInformationProvider;
17
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IPartListener;
20 import org.eclipse.ui.IPerspectiveDescriptor;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.IWorkbenchWindow;
24
25 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
26
27 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
28
29
30 public class JavaInformationProvider implements IInformationProvider {
31
32         class EditorWatcher implements IPartListener {
33                 
34                 /**
35                  * @see IPartListener#partOpened(IWorkbenchPart)
36                  */
37                 public void partOpened(IWorkbenchPart part) {
38                 }
39                 
40                 /**
41                  * @see IPartListener#partDeactivated(IWorkbenchPart)
42                  */
43                 public void partDeactivated(IWorkbenchPart part) {
44                 }
45                 
46                 /**
47                  * @see IPartListener#partClosed(IWorkbenchPart)
48                  */
49                 public void partClosed(IWorkbenchPart part) {
50                         if (part == fEditor) {
51                                 fEditor.getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener);
52                                 fPartListener= null;
53                         }
54                 }
55                 
56                 /**
57                  * @see IPartListener#partActivated(IWorkbenchPart)
58                  */
59                 public void partActivated(IWorkbenchPart part) {
60                         update();
61                 }
62                 
63                 public void partBroughtToTop(IWorkbenchPart part) {
64                         update();
65                 }       
66         }
67         
68         protected IEditorPart fEditor;
69         protected IPartListener fPartListener;
70         
71         protected String fCurrentPerspective;
72         protected IJavaEditorTextHover fImplementation;
73         
74         
75         public JavaInformationProvider(IEditorPart editor) {
76                 
77                 fEditor= editor;
78                 
79                 if (fEditor != null) {
80                         
81                         fPartListener= new EditorWatcher();
82                         IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
83                         window.getPartService().addPartListener(fPartListener);
84                         
85                         update();
86                 }
87         }
88         
89         protected void update() {
90                 
91                 IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
92                 IWorkbenchPage page= window.getActivePage();
93                 if (page != null) {
94                         
95                         IPerspectiveDescriptor perspective= page.getPerspective();
96                         if (perspective != null)  {
97                                 String perspectiveId= perspective.getId();
98                                 
99                                 if (fCurrentPerspective == null || fCurrentPerspective != perspectiveId) {
100                                         fCurrentPerspective= perspectiveId;
101                 
102                                         fImplementation= new JavaTypeHover();
103                                         fImplementation.setEditor(fEditor);
104                                 }
105                         }
106                 }
107         }
108         
109         /*
110          * @see IInformationProvider#getSubject(ITextViewer, int)
111          */
112         public IRegion getSubject(ITextViewer textViewer, int offset) {
113                 
114                 if (textViewer != null)
115                         return JavaWordFinder.findWord(textViewer.getDocument(), offset);
116                 
117                 return null;    
118         }
119
120         /*
121          * @see IInformationProvider#getInformation(ITextViewer, IRegion)
122          */
123         public String getInformation(ITextViewer textViewer, IRegion subject) {
124                 if (fImplementation != null) {
125                         String s= fImplementation.getHoverInfo(textViewer, subject);
126                         if (s != null && s.trim().length() > 0)
127                                 return s;
128                 }
129                 
130                 return null;
131         }
132 }