KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 import org.eclipse.pde.internal.ui.editor.contentassist.display.HTMLTextPresenter;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.events.MouseTrackListener;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.text.IInformationControl;
23 import org.eclipse.jface.text.IInformationControlCreator;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextHover;
26 import org.eclipse.jface.text.ITextHoverExtension;
27 import org.eclipse.jface.text.ITextViewer;
28 import org.eclipse.jface.text.Region;
29
30 import org.eclipse.ui.editors.text.EditorsUI;
31
32 public abstract class PDETextHover implements ITextHoverExtension, ITextHover {
33     
34     public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
35         return new Region(offset, 0);
36     }
37
38     public IInformationControlCreator getHoverControlCreator() {
39         return getInformationControlCreator();
40     }
41
42     public static IInformationControlCreator getInformationControlCreator() {
43         return new IInformationControlCreator() {
44             public IInformationControl createInformationControl(Shell parent) {
45                 return new PDEDefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString());
46             }
47         };
48     }
49
50     /**
51      * @param infoControl
52      * @param control
53      * @param provider
54      */

55     public static void addHoverListenerToControl(
56             final IInformationControl infoControl, final Control control,
57             final IControlHoverContentProvider provider) {
58         
59         control.addMouseTrackListener(new MouseTrackListener() {
60             public void mouseEnter(MouseEvent e) {
61             }
62             public void mouseExit(MouseEvent e) {
63                 if (infoControl instanceof PDEDefaultInformationControl && ((PDEDefaultInformationControl)infoControl).isDisposed())
64                     return;
65                 infoControl.setVisible(false);
66             }
67             public void mouseHover(MouseEvent e) {
68                 if (infoControl instanceof PDEDefaultInformationControl && ((PDEDefaultInformationControl)infoControl).isDisposed())
69                     return;
70                 String JavaDoc text = provider.getHoverContent(control);
71                 if (text == null || text.trim().length() == 0)
72                     return;
73                 updateHover(infoControl, text);
74                 infoControl.setLocation(control.toDisplay(new Point(10, 25)));
75                 infoControl.setVisible(true);
76             }
77         });
78     }
79     
80     /**
81      * @param infoControl
82      * @param text
83      */

84     public static void updateHover(IInformationControl infoControl, String JavaDoc text) {
85         infoControl.setInformation(text);
86         Point p = infoControl.computeSizeHint();
87         infoControl.setSize(p.x, p.y);
88         if (text == null || text.trim().length() == 0)
89             infoControl.setVisible(false);
90     }
91     
92 }
93
Popular Tags