001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.pde.internal.ui.editor.contentassist.display.HTMLTextPresenter;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.MouseEvent;
016: import org.eclipse.swt.events.MouseTrackListener;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Shell;
020:
021: import org.eclipse.jface.text.IInformationControl;
022: import org.eclipse.jface.text.IInformationControlCreator;
023: import org.eclipse.jface.text.IRegion;
024: import org.eclipse.jface.text.ITextHover;
025: import org.eclipse.jface.text.ITextHoverExtension;
026: import org.eclipse.jface.text.ITextViewer;
027: import org.eclipse.jface.text.Region;
028:
029: import org.eclipse.ui.editors.text.EditorsUI;
030:
031: public abstract class PDETextHover implements ITextHoverExtension,
032: ITextHover {
033:
034: public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
035: return new Region(offset, 0);
036: }
037:
038: public IInformationControlCreator getHoverControlCreator() {
039: return getInformationControlCreator();
040: }
041:
042: public static IInformationControlCreator getInformationControlCreator() {
043: return new IInformationControlCreator() {
044: public IInformationControl createInformationControl(
045: Shell parent) {
046: return new PDEDefaultInformationControl(parent,
047: SWT.NONE, new HTMLTextPresenter(true),
048: EditorsUI.getTooltipAffordanceString());
049: }
050: };
051: }
052:
053: /**
054: * @param infoControl
055: * @param control
056: * @param provider
057: */
058: public static void addHoverListenerToControl(
059: final IInformationControl infoControl,
060: final Control control,
061: final IControlHoverContentProvider provider) {
062:
063: control.addMouseTrackListener(new MouseTrackListener() {
064: public void mouseEnter(MouseEvent e) {
065: }
066:
067: public void mouseExit(MouseEvent e) {
068: if (infoControl instanceof PDEDefaultInformationControl
069: && ((PDEDefaultInformationControl) infoControl)
070: .isDisposed())
071: return;
072: infoControl.setVisible(false);
073: }
074:
075: public void mouseHover(MouseEvent e) {
076: if (infoControl instanceof PDEDefaultInformationControl
077: && ((PDEDefaultInformationControl) infoControl)
078: .isDisposed())
079: return;
080: String text = provider.getHoverContent(control);
081: if (text == null || text.trim().length() == 0)
082: return;
083: updateHover(infoControl, text);
084: infoControl.setLocation(control.toDisplay(new Point(10,
085: 25)));
086: infoControl.setVisible(true);
087: }
088: });
089: }
090:
091: /**
092: * @param infoControl
093: * @param text
094: */
095: public static void updateHover(IInformationControl infoControl,
096: String text) {
097: infoControl.setInformation(text);
098: Point p = infoControl.computeSizeHint();
099: infoControl.setSize(p.x, p.y);
100: if (text == null || text.trim().length() == 0)
101: infoControl.setVisible(false);
102: }
103:
104: }
|