KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > SourceInformationProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.pde.internal.ui.editor.text;
12
13
14 import org.eclipse.jface.text.IInformationControlCreator;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.text.ITextHover;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.information.IInformationProvider;
19 import org.eclipse.jface.text.information.IInformationProviderExtension2;
20 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
21 import org.eclipse.ui.IPartListener;
22 import org.eclipse.ui.IPerspectiveDescriptor;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchPart;
25
26
27 public class SourceInformationProvider implements IInformationProvider, IInformationProviderExtension2, IPartListener {
28
29     public static final int F_NO_IMP = 0;
30     public static final int F_XML_IMP = 1;
31     public static final int F_MANIFEST_IMP = 2;
32     
33     protected PDESourcePage fSourcePage;
34     protected String JavaDoc fCurrentPerspective;
35     protected ITextHover fImplementation;
36     protected int fImpType;
37     
38     public void partOpened(IWorkbenchPart part) {
39     }
40
41     public void partDeactivated(IWorkbenchPart part) {
42     }
43
44     public void partClosed(IWorkbenchPart part) {
45         if (fSourcePage != null && part == fSourcePage.getEditor() && fImpType != F_NO_IMP)
46             fSourcePage.getSite().getWorkbenchWindow().getPartService().removePartListener(this);
47     }
48
49     public void partActivated(IWorkbenchPart part) {
50         update();
51     }
52
53     public void partBroughtToTop(IWorkbenchPart part) {
54         update();
55     }
56     
57
58     private IInformationControlCreator fPresenterControlCreator;
59     
60     public SourceInformationProvider(PDESourcePage editor, IInformationControlCreator creator, int impType) {
61         fSourcePage = editor;
62         fPresenterControlCreator = creator;
63         fImpType = impType;
64         if (fSourcePage != null && fImpType != F_NO_IMP) {
65             fSourcePage.getSite().getWorkbenchWindow().getPartService().addPartListener(this);
66             update();
67         }
68     }
69
70     protected void update() {
71         IWorkbenchPage page = fSourcePage.getSite().getWorkbenchWindow().getActivePage();
72         if (page != null) {
73             IPerspectiveDescriptor perspective = page.getPerspective();
74             if (perspective != null) {
75                 String JavaDoc perspectiveId = perspective.getId();
76                 if (fCurrentPerspective == null || fCurrentPerspective != perspectiveId) {
77                     fCurrentPerspective = perspectiveId;
78                     switch (fImpType) {
79                     case F_MANIFEST_IMP:
80                         fImplementation = new ManifestTextHover(fSourcePage);
81                         break;
82                     case F_XML_IMP:
83                         fImplementation = new PluginXMLTextHover(fSourcePage);
84                         break;
85                     }
86                 }
87             }
88         }
89     }
90
91     public IRegion getSubject(ITextViewer textViewer, int offset) {
92         if (textViewer != null)
93             return PDEWordFinder.findWord(textViewer.getDocument(), offset);
94         return null;
95     }
96
97     public String JavaDoc getInformation(ITextViewer textViewer, IRegion subject) {
98         if (fImplementation != null) {
99             String JavaDoc s = fImplementation.getHoverInfo(textViewer, subject);
100             if (s != null && s.trim().length() > 0)
101                 return s;
102         }
103         return null;
104     }
105
106     public IInformationControlCreator getInformationPresenterControlCreator() {
107         return fPresenterControlCreator;
108     }
109
110 }
111
Popular Tags