intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / text / CssTextHover.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz 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  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssTextHover.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
15
16 import java.util.Iterator;
17
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextHover;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.source.Annotation;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24
25
26 /**
27  * Implements simple annotation hover to show the associated messages.
28  */
29 public class CssTextHover implements ITextHover {
30         /**
31          * This hovers annotation model.
32          */
33         private IAnnotationModel model;
34
35         /**
36          * Creates a new annotation hover.
37          * 
38          * @param model this hover's annotation model
39          */
40         public CssTextHover(IAnnotationModel model)  {
41                 this.model = model;
42         }
43
44         /*
45          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
46          */
47         public String getHoverInfo(ITextViewer textViewer, IRegion region) {
48                 Iterator e = model.getAnnotationIterator();
49                 while (e.hasNext()) {
50                         Annotation a = (Annotation) e.next();
51                         Position p = model.getPosition(a);
52                         if (p.overlapsWith(region.getOffset(), region.getLength())) {
53                                 String text = a.getText();
54                                 if ((text != null) && (text.trim().length() > 0)) {
55                                         return text;
56                                 }
57                         }
58                 }
59
60                 return null;
61         }
62
63         /*
64          * @see ITextHover#getHoverRegion(ITextViewer, int)
65          */
66         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
67                 return CssWordFinder.findWord(textViewer.getDocument(), offset);
68         }
69 }