Method: printAnnotations(List)

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.editor.core;
23:
24: import java.util.ArrayList;
25: import java.util.Iterator;
26: import java.util.List;
27:
28: import org.eclipse.core.resources.IMarker;
29: import org.eclipse.core.runtime.CoreException;
30: import org.eclipse.jface.text.BadLocationException;
31: import org.eclipse.jface.text.IDocument;
32: import org.eclipse.jface.text.Position;
33: import org.eclipse.jface.text.source.Annotation;
34: import org.eclipse.jface.text.source.DefaultAnnotationHover;
35: import org.eclipse.jface.text.source.IAnnotationModel;
36: import org.eclipse.jface.text.source.ISourceViewer;
37: import org.eclipse.ui.texteditor.MarkerAnnotation;
38:
39: public class VdmAnnotationHover extends DefaultAnnotationHover
40: {
41:
42:         /**
43:          * Creates a new HTML annotation hover.
44:          *
45:          * @param showLineNumber
46:          * <code>true</code> if the line number should be shown when no annotation is found
47:          * @since 3.4
48:          */
49:         public VdmAnnotationHover(boolean showLineNumber)
50:         {
51:                 super(showLineNumber);
52:         }
53:
54:         // /*
55:         // * Formats a message as HTML text.
56:         // */
57:         // protected String formatSingleMessage(String message) {
58:         // StringBuffer buffer= new StringBuffer();
59:         // HTMLPrinter.addPageProlog(buffer);
60:         // HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
61:         // HTMLPrinter.addPageEpilog(buffer);
62:         // return buffer.toString();
63:         // }
64:         //
65:         // /*
66:         // * Formats several message as HTML text.
67:         // */
68:         // protected String formatMultipleMessages(List messages) {
69:         // StringBuffer buffer= new StringBuffer();
70:         // HTMLPrinter.addPageProlog(buffer);
71:         // HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent("Multiple markers found in this line"));
72:         //
73:         // HTMLPrinter.startBulletList(buffer);
74:         // Iterator e= messages.iterator();
75:         // while (e.hasNext())
76:         // HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
77:         // HTMLPrinter.endBulletList(buffer);
78:         //
79:         // HTMLPrinter.addPageEpilog(buffer);
80:         // return buffer.toString();
81:         // }
82:
83:         private List<Annotation> getAnnotations(ISourceViewer viewer, int lineNumber)
84:         {
85:                 IAnnotationModel model = viewer.getAnnotationModel();
86:                 IDocument document = viewer.getDocument();
87:                 return getAnnotationsAtLine(model, document, lineNumber);
88:         }
89:
90:         private List<Annotation> getAnnotationsAtLine(IAnnotationModel model,
91:                         IDocument document, int lineNumber)
92:         {
93:                 List<Annotation> result = new ArrayList<Annotation>();
94:                 @SuppressWarnings("unchecked")
95:                 Iterator<Annotation> it = model.getAnnotationIterator();
96:
97:                 while (it.hasNext())
98:                 {
99:                         Annotation ann = it.next();
100:
101:                         Position pos = model.getPosition(ann);
102:                         
103:                         if(pos==null)
104:                         {
105:                                 continue;
106:                         }
107:
108:                         try
109:                         {
110:                                 if (document.getLineOfOffset(pos.offset) == lineNumber)
111:                                 {
112:                                         result.add(ann);
113:                                 }
114:                         } catch (BadLocationException e)
115:                         {
116:                                 // TODO Auto-generated catch block
117:                                 e.printStackTrace();
118:                         }
119:                 }
120:                 return result;
121:         }
122:
123:         public String getHoverInfo(ISourceViewer viewer, int lineNumber)
124:         {
125:                 List<Annotation> annotations = getAnnotations(viewer, lineNumber);
126:                 return printAnnotations(annotations);
127:         }
128:
129:         private String printAnnotations(List<Annotation> annotations)
130:         {
131:
132:                 List<String> hoverResult = new ArrayList<String>();
133:
134:•                for (Annotation ann : annotations)
135:                 {
136:•                        if (ann instanceof MarkerAnnotation)
137:                         {
138:
139:                                 MarkerAnnotation mAnn = (MarkerAnnotation) ann;
140:                                 IMarker m = mAnn.getMarker();
141:
142:                                 try
143:                                 {
144:                                         String a = (String) m.getAttribute(IMarker.MESSAGE);
145:                                         hoverResult.add(a + " ");
146:                                 } catch (CoreException e)
147:                                 {
148:                                         // TODO Auto-generated catch block
149:                                         e.printStackTrace();
150:                                 }
151:                         }
152:                 }
153:
154:•                if (hoverResult.size() == 0)
155:                 {
156:                         return null;
157:                 }
158:
159:•                if (hoverResult.size() == 1)
160:                 {
161:                         return hoverResult.get(0);
162:                 } else
163:                 {
164:                         StringBuilder s = new StringBuilder();
165:                         s.append("Multiple markers found on this line:\n");
166:•                        for (String string : hoverResult)
167:                         {
168:                                 s.append(" - ");
169:                                 s.append(string);
170:                                 s.append("\n");
171:                         }
172:                         return s.toString();
173:                 }
174:
175:         }
176:
177: }