1 | |
---|
2 | package org.modelica.mdt.ui.hover; |
---|
3 | |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.StringReader; |
---|
6 | import java.util.Iterator; |
---|
7 | |
---|
8 | import org.eclipse.swt.SWT; |
---|
9 | import org.eclipse.swt.custom.StyleRange; |
---|
10 | import org.eclipse.swt.graphics.Drawable; |
---|
11 | import org.eclipse.swt.graphics.Font; |
---|
12 | import org.eclipse.swt.graphics.FontData; |
---|
13 | import org.eclipse.swt.graphics.GC; |
---|
14 | import org.eclipse.swt.widgets.Display; |
---|
15 | |
---|
16 | import org.modelica.mdt.ui.hover.HTML2TextReader; |
---|
17 | import org.modelica.mdt.ui.hover.LineBreakingReader; |
---|
18 | import org.eclipse.jface.text.DefaultInformationControl; |
---|
19 | import org.eclipse.jface.text.Region; |
---|
20 | import org.eclipse.jface.text.TextPresentation; |
---|
21 | |
---|
22 | |
---|
23 | public class HTMLTextPresenter implements DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension { |
---|
24 | |
---|
25 | private static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
---|
26 | |
---|
27 | private int fCounter; |
---|
28 | private boolean fEnforceUpperLineLimit; |
---|
29 | |
---|
30 | /** |
---|
31 | * Enables using bold font in order not to clip the text. |
---|
32 | * <p> |
---|
33 | * <em>Enabling this is a hack.</em> |
---|
34 | * |
---|
35 | * @since 3.2 |
---|
36 | */ |
---|
37 | private boolean fUseBoldFont= false; |
---|
38 | |
---|
39 | public HTMLTextPresenter(boolean enforceUpperLineLimit) { |
---|
40 | super(); |
---|
41 | fEnforceUpperLineLimit= enforceUpperLineLimit; |
---|
42 | } |
---|
43 | |
---|
44 | public HTMLTextPresenter() { |
---|
45 | this(true); |
---|
46 | } |
---|
47 | |
---|
48 | protected HTML2TextReader createReader(String hoverInfo, TextPresentation presentation) { |
---|
49 | return new HTML2TextReader(new StringReader(hoverInfo), presentation); |
---|
50 | } |
---|
51 | |
---|
52 | protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { |
---|
53 | |
---|
54 | int yoursStart= offset; |
---|
55 | int yoursEnd= offset + insertLength -1; |
---|
56 | yoursEnd= Math.max(yoursStart, yoursEnd); |
---|
57 | |
---|
58 | Iterator<?> e= presentation.getAllStyleRangeIterator(); |
---|
59 | while (e.hasNext()) { |
---|
60 | |
---|
61 | StyleRange range= (StyleRange) e.next(); |
---|
62 | |
---|
63 | int myStart= range.start; |
---|
64 | int myEnd= range.start + range.length -1; |
---|
65 | myEnd= Math.max(myStart, myEnd); |
---|
66 | |
---|
67 | if (myEnd < yoursStart) |
---|
68 | continue; |
---|
69 | |
---|
70 | if (myStart < yoursStart) |
---|
71 | range.length += insertLength; |
---|
72 | else |
---|
73 | range.start += insertLength; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | private void append(StringBuffer buffer, String string, TextPresentation presentation) { |
---|
78 | |
---|
79 | int length= string.length(); |
---|
80 | buffer.append(string); |
---|
81 | |
---|
82 | if (presentation != null) |
---|
83 | adaptTextPresentation(presentation, fCounter, length); |
---|
84 | |
---|
85 | fCounter += length; |
---|
86 | } |
---|
87 | |
---|
88 | private String getIndent(String line) { |
---|
89 | int length= line.length(); |
---|
90 | |
---|
91 | int i= 0; |
---|
92 | while (i < length && Character.isWhitespace(line.charAt(i))) ++i; |
---|
93 | |
---|
94 | return (i == length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$ |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * @see IHoverInformationPresenter#updatePresentation(Display display, String, TextPresentation, int, int) |
---|
99 | */ |
---|
100 | public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { |
---|
101 | return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight); |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int) |
---|
106 | * @since 3.2 |
---|
107 | */ |
---|
108 | public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { |
---|
109 | |
---|
110 | if (hoverInfo == null) |
---|
111 | return null; |
---|
112 | |
---|
113 | GC gc= new GC(drawable); |
---|
114 | |
---|
115 | Font font= null; |
---|
116 | if (fUseBoldFont) { |
---|
117 | font= gc.getFont(); |
---|
118 | FontData[] fontData= font.getFontData(); |
---|
119 | for (int i= 0; i < fontData.length; i++) |
---|
120 | fontData[i].setStyle(SWT.BOLD); |
---|
121 | font= new Font(gc.getDevice(), fontData); |
---|
122 | gc.setFont(font); |
---|
123 | } |
---|
124 | |
---|
125 | try { |
---|
126 | |
---|
127 | StringBuffer buffer= new StringBuffer(); |
---|
128 | int maxNumberOfLines= Math.round((float)maxHeight / gc.getFontMetrics().getHeight()); |
---|
129 | |
---|
130 | fCounter= 0; |
---|
131 | LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth); |
---|
132 | |
---|
133 | boolean lastLineFormatted= false; |
---|
134 | String lastLineIndent= null; |
---|
135 | |
---|
136 | String line=reader.readLine(); |
---|
137 | boolean lineFormatted= reader.isFormattedLine(); |
---|
138 | boolean firstLineProcessed= false; |
---|
139 | |
---|
140 | while (line != null) { |
---|
141 | |
---|
142 | if (fEnforceUpperLineLimit && maxNumberOfLines <= 0) |
---|
143 | break; |
---|
144 | |
---|
145 | if (firstLineProcessed) { |
---|
146 | if (!lastLineFormatted) |
---|
147 | append(buffer, LINE_DELIM, null); |
---|
148 | else { |
---|
149 | append(buffer, LINE_DELIM, presentation); |
---|
150 | if (lastLineIndent != null) |
---|
151 | append(buffer, lastLineIndent, presentation); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | append(buffer, line, null); |
---|
156 | firstLineProcessed= true; |
---|
157 | |
---|
158 | lastLineFormatted= lineFormatted; |
---|
159 | if (!lineFormatted) |
---|
160 | lastLineIndent= null; |
---|
161 | else if (lastLineIndent == null) |
---|
162 | lastLineIndent= getIndent(line); |
---|
163 | |
---|
164 | line= reader.readLine(); |
---|
165 | lineFormatted= reader.isFormattedLine(); |
---|
166 | |
---|
167 | maxNumberOfLines--; |
---|
168 | } |
---|
169 | |
---|
170 | if (line != null && buffer.length() > 0) { |
---|
171 | append(buffer, LINE_DELIM, lineFormatted ? presentation : null); |
---|
172 | append(buffer, "", presentation); |
---|
173 | } |
---|
174 | |
---|
175 | return trim(buffer, presentation); |
---|
176 | |
---|
177 | } catch (IOException e) { |
---|
178 | |
---|
179 | e.printStackTrace(); |
---|
180 | return null; |
---|
181 | |
---|
182 | } finally { |
---|
183 | if (font != null) |
---|
184 | font.dispose(); |
---|
185 | gc.dispose(); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | private String trim(StringBuffer buffer, TextPresentation presentation) { |
---|
190 | |
---|
191 | int length= buffer.length(); |
---|
192 | |
---|
193 | int end= length -1; |
---|
194 | while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) |
---|
195 | -- end; |
---|
196 | |
---|
197 | if (end == -1) |
---|
198 | return ""; //$NON-NLS-1$ |
---|
199 | |
---|
200 | if (end < length -1) |
---|
201 | buffer.delete(end + 1, length); |
---|
202 | else |
---|
203 | end= length; |
---|
204 | |
---|
205 | int start= 0; |
---|
206 | while (start < end && Character.isWhitespace(buffer.charAt(start))) |
---|
207 | ++ start; |
---|
208 | |
---|
209 | buffer.delete(0, start); |
---|
210 | presentation.setResultWindow(new Region(start, buffer.length())); |
---|
211 | return buffer.toString(); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|