129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.html.HTMLTextPresenter;
|
|
14
|
131
|
15 import dwtx.jface.internal.text.html.HTML2TextReader; // packageimport
|
|
16 import dwtx.jface.internal.text.html.HTMLPrinter; // packageimport
|
|
17 import dwtx.jface.internal.text.html.BrowserInformationControl; // packageimport
|
|
18 import dwtx.jface.internal.text.html.SubstitutionTextReader; // packageimport
|
|
19 import dwtx.jface.internal.text.html.BrowserInput; // packageimport
|
|
20 import dwtx.jface.internal.text.html.SingleCharReader; // packageimport
|
|
21 import dwtx.jface.internal.text.html.BrowserInformationControlInput; // packageimport
|
|
22 import dwtx.jface.internal.text.html.HTMLMessages; // packageimport
|
|
23
|
|
24
|
129
|
25 import dwt.dwthelper.utils;
|
|
26
|
|
27 import java.io.IOException;
|
|
28 import java.io.Reader;
|
|
29 import java.io.StringReader;
|
|
30 import java.util.Iterator;
|
|
31
|
|
32 import dwt.custom.StyleRange;
|
|
33 import dwt.graphics.Drawable;
|
|
34 import dwt.graphics.GC;
|
|
35 import dwt.widgets.Display;
|
|
36 import dwtx.jface.internal.text.link.contentassist.LineBreakingReader;
|
|
37 import dwtx.jface.text.DefaultInformationControl;
|
|
38 import dwtx.jface.text.Region;
|
|
39 import dwtx.jface.text.TextPresentation;
|
|
40
|
|
41
|
|
42 /**
|
|
43 * <p>
|
|
44 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p>
|
|
45 */
|
|
46 public class HTMLTextPresenter : DefaultInformationControl.IInformationPresenter, DefaultInformationControl.IInformationPresenterExtension {
|
|
47
|
|
48 private static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
49
|
|
50 private int fCounter;
|
|
51 private bool fEnforceUpperLineLimit;
|
|
52
|
|
53 public HTMLTextPresenter(bool enforceUpperLineLimit) {
|
|
54 super();
|
|
55 fEnforceUpperLineLimit= enforceUpperLineLimit;
|
|
56 }
|
|
57
|
|
58 public HTMLTextPresenter() {
|
|
59 this(true);
|
|
60 }
|
|
61
|
|
62 protected Reader createReader(String hoverInfo, TextPresentation presentation) {
|
|
63 return new HTML2TextReader(new StringReader(hoverInfo), presentation);
|
|
64 }
|
|
65
|
|
66 protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
|
|
67
|
|
68 int yoursStart= offset;
|
|
69 int yoursEnd= offset + insertLength -1;
|
|
70 yoursEnd= Math.max(yoursStart, yoursEnd);
|
|
71
|
|
72 Iterator e= presentation.getAllStyleRangeIterator();
|
|
73 while (e.hasNext()) {
|
|
74
|
|
75 StyleRange range= (StyleRange) e.next();
|
|
76
|
|
77 int myStart= range.start;
|
|
78 int myEnd= range.start + range.length -1;
|
|
79 myEnd= Math.max(myStart, myEnd);
|
|
80
|
|
81 if (myEnd < yoursStart)
|
|
82 continue;
|
|
83
|
|
84 if (myStart < yoursStart)
|
|
85 range.length += insertLength;
|
|
86 else
|
|
87 range.start += insertLength;
|
|
88 }
|
|
89 }
|
|
90
|
|
91 private void append(StringBuffer buffer, String string, TextPresentation presentation) {
|
|
92
|
|
93 int length= string.length();
|
|
94 buffer.append(string);
|
|
95
|
|
96 if (presentation !is null)
|
|
97 adaptTextPresentation(presentation, fCounter, length);
|
|
98
|
|
99 fCounter += length;
|
|
100 }
|
|
101
|
|
102 private String getIndent(String line) {
|
|
103 int length= line.length();
|
|
104
|
|
105 int i= 0;
|
|
106 while (i < length && Character.isWhitespace(line.charAt(i))) ++i;
|
|
107
|
|
108 return (i is length ? line : line.substring(0, i)) + " "; //$NON-NLS-1$
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * {@inheritDoc}
|
|
113 *
|
|
114 * @see dwtx.jface.text.DefaultInformationControl.IInformationPresenter#updatePresentation(dwt.widgets.Display, java.lang.String, dwtx.jface.text.TextPresentation, int, int)
|
|
115 * @deprecated
|
|
116 */
|
|
117 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
|
|
118 return updatePresentation((Drawable)display, hoverInfo, presentation, maxWidth, maxHeight);
|
|
119 }
|
|
120
|
|
121 /*
|
|
122 * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
|
|
123 * @since 3.2
|
|
124 */
|
|
125 public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
|
|
126
|
|
127 if (hoverInfo is null)
|
|
128 return null;
|
|
129
|
|
130 GC gc= new GC(drawable);
|
|
131 try {
|
|
132
|
|
133 StringBuffer buffer= new StringBuffer();
|
|
134 int maxNumberOfLines= Math.round(maxHeight / gc.getFontMetrics().getHeight());
|
|
135
|
|
136 fCounter= 0;
|
|
137 LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth);
|
|
138
|
|
139 bool lastLineFormatted= false;
|
|
140 String lastLineIndent= null;
|
|
141
|
|
142 String line=reader.readLine();
|
|
143 bool lineFormatted= reader.isFormattedLine();
|
|
144 bool firstLineProcessed= false;
|
|
145
|
|
146 while (line !is null) {
|
|
147
|
|
148 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0)
|
|
149 break;
|
|
150
|
|
151 if (firstLineProcessed) {
|
|
152 if (!lastLineFormatted)
|
|
153 append(buffer, LINE_DELIM, null);
|
|
154 else {
|
|
155 append(buffer, LINE_DELIM, presentation);
|
|
156 if (lastLineIndent !is null)
|
|
157 append(buffer, lastLineIndent, presentation);
|
|
158 }
|
|
159 }
|
|
160
|
|
161 append(buffer, line, null);
|
|
162 firstLineProcessed= true;
|
|
163
|
|
164 lastLineFormatted= lineFormatted;
|
|
165 if (!lineFormatted)
|
|
166 lastLineIndent= null;
|
|
167 else if (lastLineIndent is null)
|
|
168 lastLineIndent= getIndent(line);
|
|
169
|
|
170 line= reader.readLine();
|
|
171 lineFormatted= reader.isFormattedLine();
|
|
172
|
|
173 maxNumberOfLines--;
|
|
174 }
|
|
175
|
|
176 if (line !is null) {
|
|
177 append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
|
|
178 append(buffer, HTMLMessages.getString("HTMLTextPresenter.ellipse"), presentation); //$NON-NLS-1$
|
|
179 }
|
|
180
|
|
181 return trim(buffer, presentation);
|
|
182
|
|
183 } catch (IOException e) {
|
|
184
|
|
185 // ignore TODO do something else?
|
|
186 return null;
|
|
187
|
|
188 } finally {
|
|
189 gc.dispose();
|
|
190 }
|
|
191 }
|
|
192
|
|
193 private String trim(StringBuffer buffer, TextPresentation presentation) {
|
|
194
|
|
195 int length= buffer.length();
|
|
196
|
|
197 int end= length -1;
|
|
198 while (end >= 0 && Character.isWhitespace(buffer.charAt(end)))
|
|
199 -- end;
|
|
200
|
|
201 if (end is -1)
|
|
202 return ""; //$NON-NLS-1$
|
|
203
|
|
204 if (end < length -1)
|
|
205 buffer.delete(end + 1, length);
|
|
206 else
|
|
207 end= length;
|
|
208
|
|
209 int start= 0;
|
|
210 while (start < end && Character.isWhitespace(buffer.charAt(start)))
|
|
211 ++ start;
|
|
212
|
|
213 buffer.delete(0, start);
|
|
214 presentation.setResultWindow(new Region(start, buffer.length()));
|
|
215 return buffer.toString();
|
|
216 }
|
|
217 }
|
|
218
|