misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPTextHover.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. 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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextHover;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.Region;
20 import org.eclipse.swt.graphics.Point;
21
22 /**
23  * Example implementation for an <code>ITextHover</code> 
24  * which hovers over PHP code.
25  */
26 public class PHPTextHover implements ITextHover {
27   private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
28   /* (non-Javadoc)
29    * Method declared on ITextHover
30    */
31   public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
32     if (hoverRegion != null) {
33       try {
34         if (hoverRegion.getLength() > -1) {
35           return textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
36         }
37       } catch (BadLocationException x) {
38       }
39     }
40     return "empty selection";
41   }
42
43   /* (non-Javadoc)
44    * Method declared on ITextHover
45    */
46   public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
47     Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
48 //  show the extracted word as a tooltip
49     if (selection!=null && selection.x <= offset && offset < selection.x + selection.y)
50       return new Region(selection.x, selection.y);
51     return new Region(offset, 0);
52   }
53 }