Eclipse 3.x compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaAnnotationHover.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation 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  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
19 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
20 import net.sourceforge.phpeclipse.phpeditor.IJavaAnnotation;
21
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.source.Annotation;
26 import org.eclipse.jface.text.source.IAnnotationHover;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.jface.text.source.ISourceViewer;
29
30 // TODO: delete this class ? we use PHPAnnotationHover instead !
31 /**
32  * Determines all markers for the given line and collects, concatenates, and formates
33  * their messages.
34  */
35 public class JavaAnnotationHover implements IAnnotationHover {
36         
37         /**
38          * Returns the distance to the ruler line. 
39          */
40         protected int compareRulerLine(Position position, IDocument document, int line) {
41                 
42                 if (position.getOffset() > -1 && position.getLength() > -1) {
43                         try {
44                                 int javaAnnotationLine= document.getLineOfOffset(position.getOffset());
45                                 if (line == javaAnnotationLine)
46                                         return 1;
47                                 if (javaAnnotationLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
48                                         return 2;
49                         } catch (BadLocationException x) {
50                         }
51                 }
52                 
53                 return 0;
54         }
55         
56         /**
57          * Selects a set of markers from the two lists. By default, it just returns
58          * the set of exact matches.
59          */
60         protected List select(List exactMatch, List including) {
61                 return exactMatch;
62         }
63         
64         /**
65          * Returns one marker which includes the ruler's line of activity.
66          */
67         protected List getJavaAnnotationsForLine(ISourceViewer viewer, int line) {
68                 
69                 IDocument document= viewer.getDocument();
70                 IAnnotationModel model= viewer.getAnnotationModel();
71                 
72                 if (model == null)
73                         return null;
74                         
75                 List exact= new ArrayList();
76                 List including= new ArrayList();
77                 
78                 Iterator e= model.getAnnotationIterator();
79                 HashMap messagesAtPosition= new HashMap();
80                 while (e.hasNext()) {
81                         Object o= e.next();
82                         if (o instanceof IJavaAnnotation) {
83                                 IJavaAnnotation a= (IJavaAnnotation)o;
84                                 if (!a.hasOverlay()) {
85                                         Position position= model.getPosition((Annotation)a);
86                                         if (position == null)
87                                                 continue;
88
89                                         if (isDuplicateJavaAnnotation(messagesAtPosition, position, a.getMessage()))
90                                                 continue;
91         
92                                         switch (compareRulerLine(position, document, line)) {
93                                                 case 1:
94                                                         exact.add(a);
95                                                         break;
96                                                 case 2:
97                                                         including.add(a);
98                                                         break;
99                                         }
100                                 }
101                         }
102                 }
103                 
104                 return select(exact, including);
105         }
106
107         private boolean isDuplicateJavaAnnotation(Map messagesAtPosition, Position position, String message) {
108                 if (messagesAtPosition.containsKey(position)) {
109                         Object value= messagesAtPosition.get(position);
110                         if (message.equals(value))
111                                 return true;
112
113                         if (value instanceof List) {
114                                 List messages= (List)value;
115                                 if  (messages.contains(message))
116                                         return true;
117                                 else
118                                         messages.add(message);
119                         } else {
120                                 ArrayList messages= new ArrayList();
121                                 messages.add(value);
122                                 messages.add(message);
123                                 messagesAtPosition.put(position, messages);
124                         }
125                 } else
126                         messagesAtPosition.put(position, message);
127                 return false;
128         }
129                 
130         /*
131          * @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
132          */
133         public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
134                 List javaAnnotations= getJavaAnnotationsForLine(sourceViewer, lineNumber);
135                 if (javaAnnotations != null) {
136                         
137                         if (javaAnnotations.size() == 1) {
138                                 
139                                 // optimization
140                                 IJavaAnnotation javaAnnotation= (IJavaAnnotation) javaAnnotations.get(0);
141                                 String message= javaAnnotation.getMessage();
142                                 if (message != null && message.trim().length() > 0)
143                                         return formatSingleMessage(message);
144                                         
145                         } else {
146                                         
147                                 List messages= new ArrayList();
148                                 
149                                 Iterator e= javaAnnotations.iterator();
150                                 while (e.hasNext()) {
151                                         IJavaAnnotation javaAnnotation= (IJavaAnnotation) e.next();
152                                         String message= javaAnnotation.getMessage();
153                                         if (message != null && message.trim().length() > 0)
154                                                 messages.add(message.trim());
155                                 }
156                                 
157                                 if (messages.size() == 1)
158                                         return formatSingleMessage((String) messages.get(0));
159                                         
160                                 if (messages.size() > 1)
161                                         return formatMultipleMessages(messages);
162                         }
163                 }
164                 
165                 return null;
166         }
167         
168         /*
169          * Formats a message as HTML text.
170          */
171         private String formatSingleMessage(String message) {
172                 StringBuffer buffer= new StringBuffer();
173                 HTMLPrinter.addPageProlog(buffer);
174                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
175                 HTMLPrinter.addPageEpilog(buffer);
176                 return buffer.toString();
177         }
178         
179         /*
180          * Formats several message as HTML text.
181          */
182         private String formatMultipleMessages(List messages) {
183                 StringBuffer buffer= new StringBuffer();
184                 HTMLPrinter.addPageProlog(buffer);
185                 HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(PHPUIMessages.getString("JavaAnnotationHover.multipleMarkersAtThisLine"))); //$NON-NLS-1$
186                 
187                 HTMLPrinter.startBulletList(buffer);
188                 Iterator e= messages.iterator();
189                 while (e.hasNext())
190                         HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
191                 HTMLPrinter.endBulletList(buffer);      
192                 
193                 HTMLPrinter.addPageEpilog(buffer);
194                 return buffer.toString();
195         }
196 }