*** empty log message ***
[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 java.util.HashMap;
15 import java.util.Vector;
16
17 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
19
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextHover;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.Region;
24 import org.eclipse.swt.graphics.Point;
25
26 /**
27  * Example implementation for an <code>ITextHover</code> 
28  * which hovers over PHP code.
29  */
30 public class PHPTextHover implements ITextHover {
31   public static HashMap functionDescriptions = null;
32
33   private static PHPWordExtractor phpWordDetector = new PHPWordExtractor();
34   
35   /* (non-Javadoc)
36    * Method declared on ITextHover
37    */
38   public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
39     if (hoverRegion != null) {
40       try {
41         if (hoverRegion.getLength() > -1) {
42           String word = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
43           if (functionDescriptions == null) {
44             functionDescriptions = new HashMap();
45             //                  PHPSyntaxRdr syntaxRdr = new PHPSyntaxRdr();
46             //                  syntaxRdr.readInSyntax();
47
48             //                  Vector syntaxbuffer = syntaxRdr.getsyntaxdata();
49             Vector syntaxbuffer = PHPSyntaxRdr.getsyntaxdata();
50             String strbuffer = null;
51             PHPElement elbuffer = null;
52             while ((syntaxbuffer != null)
53               && (!syntaxbuffer.isEmpty() && 
54                  ((elbuffer = (PHPElement) syntaxbuffer.remove(0)) != null))) {
55               functionDescriptions.put(elbuffer.getName(), elbuffer.getHoverText());
56             }
57
58             //            functionDescriptions = new HashMap(997);
59             //            for (int i=0; i<PHPFunctionNames.FUNCTION_NAMES.length;i++) {
60             //              functionDescriptions.put(PHPFunctionNasmes.FUNCTION_NAMES[i],PHPFunctionDescription.FUNCTION_DESCRIPTION[i]);
61             //            }
62           }
63           return (String) functionDescriptions.get(word);
64         }
65         //      } catch (BadLocationException x) {
66       } catch (Exception x) {
67       }
68     }
69     return "empty selection";
70   }
71
72   /* (non-Javadoc)
73    * Method declared on ITextHover
74    */
75   public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
76     Point selection = PHPWordExtractor.findWord(textViewer.getDocument(), offset);
77     //  show the extracted word as a tooltip
78     if (selection != null && selection.x <= offset && offset < selection.x + selection.y)
79       return new Region(selection.x, selection.y);
80     return new Region(offset, 0);
81   }
82 }