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