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 / CssAnnotationHover.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: CssAnnotationHover.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.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.Position;
23 import org.eclipse.jface.text.source.Annotation;
24 import org.eclipse.jface.text.source.IAnnotationHover;
25 import org.eclipse.jface.text.source.IAnnotationModel;
26 import org.eclipse.jface.text.source.ISourceViewer;
27
28
29 /**
30  * Implements simple annotation hover to show the associated messages.
31  */
32 public class CssAnnotationHover implements IAnnotationHover {
33         /*
34          * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
35          */
36         public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
37                 List annotations = getAnnotationsForLine(sourceViewer, lineNumber);
38                 if (annotations != null) {
39                         List messages = new ArrayList();
40
41                         Iterator e = annotations.iterator();
42                         while (e.hasNext()) {
43                                 Annotation annotation = (Annotation) e.next();
44                                 String message = annotation.getText();
45                                 if (message != null) {
46                                         message = message.trim();
47                                         if (message.length() > 0) {
48                                                 messages.add(message);
49                                         }
50                                 }
51                         }
52
53                         if (messages.size() == 1) {
54                                 return (String) messages.get(0);
55                         }
56
57                         if (messages.size() > 1) {
58                                 return formatMessages(messages);
59                         }
60                 }
61
62                 return null;
63         }
64
65         /**
66          * Formats multiple annotation messages for display.
67          */
68         private String formatMessages(List messages) {
69                 StringBuffer buffer = new StringBuffer();
70
71                 Iterator e = messages.iterator();
72                 while (e.hasNext())  {
73                         buffer.append("- "); //$NON-NLS-1$
74                         buffer.append(e.next());
75                         buffer.append('\n');
76                 }
77
78                 return buffer.toString();
79         }
80
81         /**
82          * Returns annotations for the ruler's line of activity.
83          */
84         private List getAnnotationsForLine(ISourceViewer viewer, int line) {
85                 IDocument document = viewer.getDocument();
86
87                 IAnnotationModel model = viewer.getAnnotationModel();
88                 if (model == null) {
89                         return null;
90                 }
91
92                 List retVal = new ArrayList();
93
94                 Iterator e = model.getAnnotationIterator();
95                 while (e.hasNext()) {
96                         Annotation a = (Annotation) e.next();
97                         Position position = model.getPosition(a);
98                         if (position != null) {
99                                 try {
100                                         int annotationLine = document
101                                                         .getLineOfOffset(position.getOffset());
102                                         if (annotationLine == line) {
103                                                 retVal.add(a);
104                                         }
105                                 } catch (BadLocationException e1) {
106                                         // ignore
107                                 }
108                         }
109                 }
110
111                 return retVal;
112         }
113 }