source: trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/hover/ModelicaAnnotationHover.java

Last change on this file was 1839, checked in by masberg, 11 years ago

Move null check before first usage.

File size: 4.3 KB
Line 
1/*******************************************************************************
2 * Copyright (c) 2002-2007
3 * @author Adrian Pop [adrpo@ida.liu.se]
4 * All rights reserved.
5 *******************************************************************************/
6package org.modelica.mdt.ui.hover;
7
8import java.util.ArrayList;
9import java.util.Iterator;
10import java.util.List;
11
12import org.eclipse.core.resources.IMarker;
13import org.eclipse.core.runtime.CoreException;
14import org.eclipse.jface.text.source.DefaultAnnotationHover;
15import org.eclipse.jface.text.source.IAnnotationModel;
16import org.eclipse.jface.text.source.ISourceViewer;
17import org.eclipse.ui.texteditor.MarkerAnnotation;
18import org.modelica.mdt.internal.core.ErrorManager;
19
20/**
21 * Determines all markers for the given line and collects, concatenates, and formats
22 * returns their messages in HTML.
23 *
24 * @since 0.7.0
25 */
26
27public class ModelicaAnnotationHover extends DefaultAnnotationHover {
28
29//  /*
30//   * Formats a message as HTML text.
31//   */
32//  protected String formatSingleMessage(String message) {
33//      StringBuffer buffer= new StringBuffer();
34//      HTMLPrinter.addPageProlog(buffer);
35//      HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
36//      HTMLPrinter.addPageEpilog(buffer);
37//      return buffer.toString();
38//  }
39//
40//  /*
41//   * Formats several message as HTML text.
42//   */
43//  protected String formatMultipleMessages(List messages) {
44//      StringBuffer buffer= new StringBuffer();
45//      HTMLPrinter.addPageProlog(buffer);
46//      HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent("Multiple Markups at this line!"));
47//
48//      HTMLPrinter.startBulletList(buffer);
49//      Iterator e= messages.iterator();
50//      while (e.hasNext())
51//          HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
52//      HTMLPrinter.endBulletList(buffer);
53//
54//      HTMLPrinter.addPageEpilog(buffer);
55//      return buffer.toString();
56//  }
57
58    /**
59     * Gets the hoverInfo attribute of the AnnotationHover object
60     *
61     * @param sourceViewer  Description of the Parameter
62     * @param lineNumber    Description of the Parameter
63     * @return              The hoverInfo value
64     */
65    public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
66        /* if we have something, just return it */
67        String hoverInfo = super.getHoverInfo(sourceViewer, lineNumber);
68
69        if (hoverInfo != null) {
70            return hoverInfo;
71        }
72
73        List<IMarker> markers = this.getModelicaAnnotationsForLine(sourceViewer, lineNumber);
74
75        if (markers != null) {
76            if (markers.size() == 1) {
77                IMarker marker = markers.get(0);
78                String message = marker.getAttribute(IMarker.MESSAGE, (String) null);
79                if (message != null && message.trim().length() > 0) {
80                    return formatSingleMessage(message);
81                }
82            }
83            else {
84                List<String> messages = new ArrayList<String>();
85
86                for (IMarker marker : markers) {
87                    String message = marker.getAttribute(IMarker.MESSAGE, (String) null);
88                    if (message != null) {
89                        String trimmedMessage = message.trim();
90
91                        if (trimmedMessage.length() > 0) {
92                            messages.add(trimmedMessage);
93                        }
94                    }
95                    if (messages.size() == 1) {
96                        return this.formatSingleMessage(messages.get(0));
97                    }
98                    if (messages.size() > 1) {
99                        return this.formatMultipleMessages(messages);
100                    }
101                }
102            }
103        }
104
105        return null;
106    }
107
108    /**
109     * Gets the ModelicaAnnotationsForLine attribute of the AnnotationHover object
110     *
111     * @param viewer  Description of the Parameter
112     * @param line    Description of the Parameter
113     * @return        The jSPAnnotationsForLine value
114     */
115    protected List<IMarker> getModelicaAnnotationsForLine(ISourceViewer viewer, int line) {
116        IAnnotationModel model = viewer.getAnnotationModel();
117
118        if (model == null) {
119            return null;
120        }
121
122        List<IMarker> markers = new ArrayList<IMarker>();
123        Iterator<?> e = model.getAnnotationIterator();
124
125        while (e.hasNext()) {
126            Object o = e.next();
127
128            if (o instanceof MarkerAnnotation) {
129                MarkerAnnotation a = (MarkerAnnotation)o;
130
131                try {
132                    Integer ln = (Integer) a.getMarker().getAttribute(IMarker.LINE_NUMBER);
133                    if (ln.intValue() == line + 1) {
134                        markers.add(a.getMarker());
135                    }
136                }
137                catch (CoreException ex) {
138                    ErrorManager.logError(ex);
139                }
140            }
141        }
142        return markers;
143    }
144}
Note: See TracBrowser for help on using the repository browser.