KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > text > XMLAnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.ant.internal.ui.editor.AntEditorMessages;
20 import org.eclipse.jface.internal.text.html.HTMLPrinter;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.Position;
24 import org.eclipse.jface.text.source.Annotation;
25 import org.eclipse.jface.text.source.IAnnotationHover;
26 import org.eclipse.jface.text.source.IAnnotationModel;
27 import org.eclipse.jface.text.source.ISourceViewer;
28
29
30 /**
31  * Determines all markers for the given line and collects, concatenates, and formulates
32  * their messages.
33  */

34 public class XMLAnnotationHover implements IAnnotationHover {
35     
36     /**
37      * Returns the distance to the ruler line.
38      */

39     private int compareRulerLine(Position position, IDocument document, int line) {
40         
41         if (position.getOffset() > -1 && position.getLength() > -1) {
42             try {
43                 int xmlAnnotationLine= document.getLineOfOffset(position.getOffset());
44                 if (line == xmlAnnotationLine)
45                     return 1;
46                 if (xmlAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
47                     return 2;
48             } catch (BadLocationException x) {
49             }
50         }
51         
52         return 0;
53     }
54     
55     /**
56      * Returns one marker which includes the ruler's line of activity.
57      */

58     private List JavaDoc getXMLAnnotationsForLine(ISourceViewer viewer, int line) {
59         
60         IDocument document= viewer.getDocument();
61         IAnnotationModel model= viewer.getAnnotationModel();
62         
63         if (model == null)
64             return null;
65             
66         List JavaDoc exact= new ArrayList JavaDoc();
67         
68         Iterator JavaDoc e= model.getAnnotationIterator();
69         Map JavaDoc messagesAtPosition= new HashMap JavaDoc();
70         while (e.hasNext()) {
71             Object JavaDoc o= e.next();
72             if (o instanceof Annotation) {
73                 Annotation a= (Annotation)o;
74                 Position position= model.getPosition(a);
75                 if (position == null)
76                     continue;
77
78                 if (isDuplicateXMLAnnotation(messagesAtPosition, position, a.getText()))
79                     continue;
80
81                 switch (compareRulerLine(position, document, line)) {
82                     case 1:
83                         exact.add(a);
84                         break;
85                 }
86             }
87         }
88         
89         return exact;
90     }
91
92     private boolean isDuplicateXMLAnnotation(Map JavaDoc messagesAtPosition, Position position, String JavaDoc message) {
93         if (messagesAtPosition.containsKey(position)) {
94             Object JavaDoc value= messagesAtPosition.get(position);
95             if (message.equals(value))
96                 return true;
97
98             if (value instanceof List JavaDoc) {
99                 List JavaDoc messages= (List JavaDoc)value;
100                 if (messages.contains(message)) {
101                     return true;
102                 }
103                 messages.add(message);
104             } else {
105                 ArrayList JavaDoc messages= new ArrayList JavaDoc();
106                 messages.add(value);
107                 messages.add(message);
108                 messagesAtPosition.put(position, messages);
109             }
110         } else
111             messagesAtPosition.put(position, message);
112         return false;
113     }
114         
115     /* (non-Javadoc)
116      * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
117      */

118     public String JavaDoc getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
119         List JavaDoc xmlAnnotations= getXMLAnnotationsForLine(sourceViewer, lineNumber);
120         if (xmlAnnotations != null) {
121             
122             if (xmlAnnotations.size() == 1) {
123                 
124                 // optimization
125
Annotation xmlAnnotation= (Annotation)xmlAnnotations.get(0);
126                 String JavaDoc message= xmlAnnotation.getText();
127                 if (message != null && message.trim().length() > 0) {
128                     return formatSingleMessage(message);
129                 }
130                     
131             } else {
132                     
133                 List JavaDoc messages= new ArrayList JavaDoc(xmlAnnotations.size());
134                 Iterator JavaDoc e= xmlAnnotations.iterator();
135                 while (e.hasNext()) {
136                     Annotation xmlAnnotation= (Annotation)e.next();
137                     String JavaDoc message= xmlAnnotation.getText();
138                     if (message != null && message.trim().length() > 0) {
139                         messages.add(message.trim());
140                     }
141                 }
142                 
143                 if (messages.size() == 1) {
144                     return formatSingleMessage((String JavaDoc) messages.get(0));
145                 }
146                     
147                 if (messages.size() > 1) {
148                     return formatMultipleMessages(messages);
149                 }
150             }
151         }
152         
153         return null;
154     }
155     
156     /*
157      * Formats a message as HTML text.
158      */

159     private String JavaDoc formatSingleMessage(String JavaDoc message) {
160         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
161         HTMLPrinter.addPageProlog(buffer);
162         HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
163         HTMLPrinter.addPageEpilog(buffer);
164         return buffer.toString();
165     }
166     
167     /*
168      * Formats several message as HTML text.
169      */

170     private String JavaDoc formatMultipleMessages(List JavaDoc messages) {
171         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
172         HTMLPrinter.addPageProlog(buffer);
173         HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(AntEditorMessages.getString("AntAnnotationHover.multipleMarkersAtThisLine"))); //$NON-NLS-1$
174

175         HTMLPrinter.startBulletList(buffer);
176         Iterator JavaDoc e= messages.iterator();
177         while (e.hasNext())
178             HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String JavaDoc) e.next()));
179         HTMLPrinter.endBulletList(buffer);
180         
181         HTMLPrinter.addPageEpilog(buffer);
182         return buffer.toString();
183     }
184 }
185
Popular Tags