3e38663a3b10798f219255b5f9393100a48440df
[phpeclipse.git] /
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: XMLAnnotationHover.java,v 1.2 2006-10-21 23:14:13 pombredanne Exp $
12  */
13 package net.sourceforge.phpeclipse.xml.ui.internal.text;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
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.Annotation;
23 import org.eclipse.jface.text.source.IAnnotationHover;
24 import org.eclipse.jface.text.source.IAnnotationModel;
25 import org.eclipse.jface.text.source.ISourceViewer;
26
27 /**
28  * Implements simple annotation hover to show the associated messages.
29  */
30 public class XMLAnnotationHover implements IAnnotationHover {
31         /*
32          * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer,
33          *      int)
34          */
35         public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
36                 List annotations = getAnnotationsForLine(sourceViewer, lineNumber);
37                 if (annotations != null) {
38                         List messages = new ArrayList();
39
40                         Iterator e = annotations.iterator();
41                         while (e.hasNext()) {
42                                 Annotation annotation = (Annotation) e.next();
43
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 = new XMLAnnotationIterator(model, true);
95                 while (e.hasNext()) {
96                         Annotation a = (Annotation) e.next();
97
98                         Position position = model.getPosition(a);
99                         if (position != null) {
100                                 try {
101                                         int annotationLine = document.getLineOfOffset(position
102                                                         .getOffset());
103                                         if (annotationLine == line) {
104                                                 retVal.add(a);
105                                         }
106                                 } catch (BadLocationException e1) {
107                                         // ignore
108                                 }
109                         }
110                 }
111
112                 return retVal;
113         }
114 }