*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / text / XMLTextHover.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: XMLTextHover.java,v 1.1 2004-09-02 18:28:03 jsurfer Exp $
12  */
13 package net.sourceforge.phpeclipse.xml.ui.internal.text;
14
15 import java.util.Iterator;
16
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextHover;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.Position;
21 import org.eclipse.jface.text.source.Annotation;
22 import org.eclipse.jface.text.source.IAnnotationModel;
23
24
25 /**
26  * Implements simple annotation hover to show the associated messages.
27  */
28 public class XMLTextHover implements ITextHover {
29         /**
30          * This hovers annotation model.
31          */
32         private IAnnotationModel model;
33
34         /**
35          * Creates a new annotation hover.
36          * 
37          * @param model this hover's annotation model
38          */
39         public XMLTextHover(IAnnotationModel model)  {
40                 this.model = model;
41         }
42         
43         /*
44          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
45          */
46         public String getHoverInfo(ITextViewer textViewer, IRegion region) {
47                 Iterator e = new XMLAnnotationIterator(model, true);
48                 while (e.hasNext()) {
49                         Annotation a = (Annotation) e.next();
50
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 XMLWordFinder.findWord(textViewer.getDocument(), offset);
68         }
69 }