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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java.hover;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.information.IInformationProvider;
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;
25 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
27 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
30 public class JavaInformationProvider implements IInformationProvider {
32 class EditorWatcher implements IPartListener {
35 * @see IPartListener#partOpened(IWorkbenchPart)
37 public void partOpened(IWorkbenchPart part) {
41 * @see IPartListener#partDeactivated(IWorkbenchPart)
43 public void partDeactivated(IWorkbenchPart part) {
47 * @see IPartListener#partClosed(IWorkbenchPart)
49 public void partClosed(IWorkbenchPart part) {
50 if (part == fEditor) {
51 fEditor.getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener);
57 * @see IPartListener#partActivated(IWorkbenchPart)
59 public void partActivated(IWorkbenchPart part) {
63 public void partBroughtToTop(IWorkbenchPart part) {
68 protected IEditorPart fEditor;
69 protected IPartListener fPartListener;
71 protected String fCurrentPerspective;
72 protected IJavaEditorTextHover fImplementation;
75 public JavaInformationProvider(IEditorPart editor) {
79 if (fEditor != null) {
81 fPartListener= new EditorWatcher();
82 IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
83 window.getPartService().addPartListener(fPartListener);
89 protected void update() {
91 IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow();
92 IWorkbenchPage page= window.getActivePage();
95 IPerspectiveDescriptor perspective= page.getPerspective();
96 if (perspective != null) {
97 String perspectiveId= perspective.getId();
99 if (fCurrentPerspective == null || fCurrentPerspective != perspectiveId) {
100 fCurrentPerspective= perspectiveId;
102 fImplementation= new JavaTypeHover();
103 fImplementation.setEditor(fEditor);
110 * @see IInformationProvider#getSubject(ITextViewer, int)
112 public IRegion getSubject(ITextViewer textViewer, int offset) {
114 if (textViewer != null)
115 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
121 * @see IInformationProvider#getInformation(ITextViewer, IRegion)
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)