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;
13 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
14 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
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;
26 public class JavaInformationProvider implements IInformationProvider {
28 class EditorWatcher implements IPartListener {
31 * @see IPartListener#partOpened(IWorkbenchPart)
33 public void partOpened(IWorkbenchPart part) {
37 * @see IPartListener#partDeactivated(IWorkbenchPart)
39 public void partDeactivated(IWorkbenchPart part) {
43 * @see IPartListener#partClosed(IWorkbenchPart)
45 public void partClosed(IWorkbenchPart part) {
46 if (part == fEditor) {
47 fEditor.getSite().getWorkbenchWindow().getPartService()
48 .removePartListener(fPartListener);
54 * @see IPartListener#partActivated(IWorkbenchPart)
56 public void partActivated(IWorkbenchPart part) {
60 public void partBroughtToTop(IWorkbenchPart part) {
65 protected IEditorPart fEditor;
67 protected IPartListener fPartListener;
69 protected String fCurrentPerspective;
71 protected IJavaEditorTextHover fImplementation;
73 public JavaInformationProvider(IEditorPart editor) {
77 if (fEditor != null) {
79 fPartListener = new EditorWatcher();
80 IWorkbenchWindow window = fEditor.getSite().getWorkbenchWindow();
81 window.getPartService().addPartListener(fPartListener);
87 protected void update() {
89 IWorkbenchWindow window = fEditor.getSite().getWorkbenchWindow();
90 IWorkbenchPage page = window.getActivePage();
93 IPerspectiveDescriptor perspective = page.getPerspective();
94 if (perspective != null) {
95 String perspectiveId = perspective.getId();
97 if (fCurrentPerspective == null
98 || fCurrentPerspective != perspectiveId) {
99 fCurrentPerspective = perspectiveId;
101 fImplementation = new JavaTypeHover();
102 fImplementation.setEditor(fEditor);
109 * @see IInformationProvider#getSubject(ITextViewer, int)
111 public IRegion getSubject(ITextViewer textViewer, int offset) {
113 if (textViewer != null)
114 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
120 * @see IInformationProvider#getInformation(ITextViewer, IRegion)
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)