001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import org.eclipse.jface.text.IInformationControlCreator;
013: import org.eclipse.jface.text.IRegion;
014: import org.eclipse.jface.text.ITextHover;
015: import org.eclipse.jface.text.ITextViewer;
016: import org.eclipse.jface.text.information.IInformationProvider;
017: import org.eclipse.jface.text.information.IInformationProviderExtension2;
018: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
019: import org.eclipse.ui.IPartListener;
020: import org.eclipse.ui.IPerspectiveDescriptor;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.IWorkbenchPart;
023:
024: public class SourceInformationProvider implements IInformationProvider,
025: IInformationProviderExtension2, IPartListener {
026:
027: public static final int F_NO_IMP = 0;
028: public static final int F_XML_IMP = 1;
029: public static final int F_MANIFEST_IMP = 2;
030:
031: protected PDESourcePage fSourcePage;
032: protected String fCurrentPerspective;
033: protected ITextHover fImplementation;
034: protected int fImpType;
035:
036: public void partOpened(IWorkbenchPart part) {
037: }
038:
039: public void partDeactivated(IWorkbenchPart part) {
040: }
041:
042: public void partClosed(IWorkbenchPart part) {
043: if (fSourcePage != null && part == fSourcePage.getEditor()
044: && fImpType != F_NO_IMP)
045: fSourcePage.getSite().getWorkbenchWindow().getPartService()
046: .removePartListener(this );
047: }
048:
049: public void partActivated(IWorkbenchPart part) {
050: update();
051: }
052:
053: public void partBroughtToTop(IWorkbenchPart part) {
054: update();
055: }
056:
057: private IInformationControlCreator fPresenterControlCreator;
058:
059: public SourceInformationProvider(PDESourcePage editor,
060: IInformationControlCreator creator, int impType) {
061: fSourcePage = editor;
062: fPresenterControlCreator = creator;
063: fImpType = impType;
064: if (fSourcePage != null && fImpType != F_NO_IMP) {
065: fSourcePage.getSite().getWorkbenchWindow().getPartService()
066: .addPartListener(this );
067: update();
068: }
069: }
070:
071: protected void update() {
072: IWorkbenchPage page = fSourcePage.getSite()
073: .getWorkbenchWindow().getActivePage();
074: if (page != null) {
075: IPerspectiveDescriptor perspective = page.getPerspective();
076: if (perspective != null) {
077: String perspectiveId = perspective.getId();
078: if (fCurrentPerspective == null
079: || fCurrentPerspective != perspectiveId) {
080: fCurrentPerspective = perspectiveId;
081: switch (fImpType) {
082: case F_MANIFEST_IMP:
083: fImplementation = new ManifestTextHover(
084: fSourcePage);
085: break;
086: case F_XML_IMP:
087: fImplementation = new PluginXMLTextHover(
088: fSourcePage);
089: break;
090: }
091: }
092: }
093: }
094: }
095:
096: public IRegion getSubject(ITextViewer textViewer, int offset) {
097: if (textViewer != null)
098: return PDEWordFinder.findWord(textViewer.getDocument(),
099: offset);
100: return null;
101: }
102:
103: public String getInformation(ITextViewer textViewer, IRegion subject) {
104: if (fImplementation != null) {
105: String s = fImplementation
106: .getHoverInfo(textViewer, subject);
107: if (s != null && s.trim().length() > 0)
108: return s;
109: }
110: return null;
111: }
112:
113: public IInformationControlCreator getInformationPresenterControlCreator() {
114: return fPresenterControlCreator;
115: }
116:
117: }
|