KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > DefaultAnnotationHover


1 /*******************************************************************************
2  * Copyright (c) 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.jface.text.source;
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.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.source.projection.AnnotationBag;
23
24 /**
25  * Standard implementation of {@link org.eclipse.jface.text.source.IAnnotationHover}.
26  *
27  * @since 3.2
28  */

29 public class DefaultAnnotationHover implements IAnnotationHover {
30     
31     /*
32      * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
33      */

34     public String JavaDoc getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
35         List JavaDoc javaAnnotations= getAnnotationsForLine(sourceViewer, lineNumber);
36         if (javaAnnotations != null) {
37             
38             if (javaAnnotations.size() == 1) {
39                 
40                 // optimization
41
Annotation annotation= (Annotation) javaAnnotations.get(0);
42                 String JavaDoc message= annotation.getText();
43                 if (message != null && message.trim().length() > 0)
44                     return formatSingleMessage(message);
45                 
46             } else {
47                 
48                 List JavaDoc messages= new ArrayList JavaDoc();
49                 
50                 Iterator JavaDoc e= javaAnnotations.iterator();
51                 while (e.hasNext()) {
52                     Annotation annotation= (Annotation) e.next();
53                     String JavaDoc message= annotation.getText();
54                     if (message != null && message.trim().length() > 0)
55                         messages.add(message.trim());
56                 }
57                 
58                 if (messages.size() == 1)
59                     return formatSingleMessage((String JavaDoc) messages.get(0));
60                 
61                 if (messages.size() > 1)
62                     return formatMultipleMessages(messages);
63             }
64         }
65         return null;
66     }
67     
68     /**
69      * Tells whether the annotation should be included in
70      * the computation.
71      *
72      * @param annotation the annotation to test
73      * @return <code>true</code> if the annotation is included in the computation
74      */

75     protected boolean isIncluded(Annotation annotation) {
76         return true;
77     }
78     
79     /**
80      * Hook method to format the given single message.
81      * <p>
82      * Subclasses can change this to create a different
83      * format like HTML.
84      * </p>
85      *
86      * @param message the message to format
87      * @return the formatted message
88      */

89     protected String JavaDoc formatSingleMessage(String JavaDoc message) {
90         return message;
91     }
92     
93     /**
94      * Hook method to formats the given messages.
95      * <p>
96      * Subclasses can change this to create a different
97      * format like HTML.
98      * </p>
99      *
100      * @param messages the messages to format
101      * @return the formatted message
102      */

103     protected String JavaDoc formatMultipleMessages(List JavaDoc messages) {
104         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
105         buffer.append(JFaceTextMessages.getString("DefaultAnnotationHover.multipleMarkers")); //$NON-NLS-1$
106

107         Iterator JavaDoc e= messages.iterator();
108         while (e.hasNext()) {
109             buffer.append('\n');
110             String JavaDoc listItemText= (String JavaDoc) e.next();
111             buffer.append(JFaceTextMessages.getFormattedString("DefaultAnnotationHover.listItem", new String JavaDoc[] { listItemText })); //$NON-NLS-1$
112
}
113         return buffer.toString();
114     }
115     
116     private boolean isRulerLine(Position position, IDocument document, int line) {
117         if (position.getOffset() > -1 && position.getLength() > -1) {
118             try {
119                 return line == document.getLineOfOffset(position.getOffset());
120             } catch (BadLocationException x) {
121             }
122         }
123         return false;
124     }
125     
126     private IAnnotationModel getAnnotationModel(ISourceViewer viewer) {
127         if (viewer instanceof ISourceViewerExtension2) {
128             ISourceViewerExtension2 extension= (ISourceViewerExtension2) viewer;
129             return extension.getVisualAnnotationModel();
130         }
131         return viewer.getAnnotationModel();
132     }
133     
134     private boolean isDuplicateAnnotation(Map JavaDoc messagesAtPosition, Position position, String JavaDoc message) {
135         if (messagesAtPosition.containsKey(position)) {
136             Object JavaDoc value= messagesAtPosition.get(position);
137             if (message.equals(value))
138                 return true;
139             
140             if (value instanceof List JavaDoc) {
141                 List JavaDoc messages= (List JavaDoc)value;
142                 if (messages.contains(message))
143                     return true;
144
145                 messages.add(message);
146             } else {
147                 ArrayList JavaDoc messages= new ArrayList JavaDoc();
148                 messages.add(value);
149                 messages.add(message);
150                 messagesAtPosition.put(position, messages);
151             }
152         } else
153             messagesAtPosition.put(position, message);
154         return false;
155     }
156     
157     private boolean includeAnnotation(Annotation annotation, Position position, HashMap JavaDoc messagesAtPosition) {
158         if (!isIncluded(annotation))
159             return false;
160         
161         String JavaDoc text= annotation.getText();
162         return (text != null && !isDuplicateAnnotation(messagesAtPosition, position, text));
163     }
164     
165     private List JavaDoc getAnnotationsForLine(ISourceViewer viewer, int line) {
166         IAnnotationModel model= getAnnotationModel(viewer);
167         if (model == null)
168             return null;
169         
170         IDocument document= viewer.getDocument();
171         List JavaDoc javaAnnotations= new ArrayList JavaDoc();
172         HashMap JavaDoc messagesAtPosition= new HashMap JavaDoc();
173         Iterator JavaDoc iterator= model.getAnnotationIterator();
174         
175         while (iterator.hasNext()) {
176             Annotation annotation= (Annotation) iterator.next();
177             
178             Position position= model.getPosition(annotation);
179             if (position == null)
180                 continue;
181             
182             if (!isRulerLine(position, document, line))
183                 continue;
184             
185             if (annotation instanceof AnnotationBag) {
186                 AnnotationBag bag= (AnnotationBag) annotation;
187                 Iterator JavaDoc e= bag.iterator();
188                 while (e.hasNext()) {
189                     annotation= (Annotation) e.next();
190                     position= model.getPosition(annotation);
191                     if (position != null && includeAnnotation(annotation, position, messagesAtPosition))
192                         javaAnnotations.add(annotation);
193                 }
194                 continue;
195             }
196             
197             if (includeAnnotation(annotation, position, messagesAtPosition))
198                 javaAnnotations.add(annotation);
199         }
200         
201         return javaAnnotations;
202     }
203 }
204
Popular Tags