Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/html/HTMLTextPresenter.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 7926b636c282 |
children |
rev | line source |
---|---|
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; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
26 import dwtx.dwtxhelper.Collection; |
162 | 27 import dwtx.dwtxhelper.StringReader; |
129 | 28 |
29 import dwt.custom.StyleRange; | |
30 import dwt.graphics.Drawable; | |
31 import dwt.graphics.GC; | |
32 import dwt.widgets.Display; | |
33 import dwtx.jface.internal.text.link.contentassist.LineBreakingReader; | |
34 import dwtx.jface.text.DefaultInformationControl; | |
35 import dwtx.jface.text.Region; | |
36 import dwtx.jface.text.TextPresentation; | |
37 | |
38 | |
39 /** | |
40 * <p> | |
41 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p> | |
42 */ | |
158 | 43 public class HTMLTextPresenter : DefaultInformationControl_IInformationPresenter, DefaultInformationControl_IInformationPresenterExtension { |
129 | 44 |
159 | 45 private static String LINE_DELIM_; |
46 private static String LINE_DELIM() { | |
47 if( LINE_DELIM_ is null ){ | |
48 LINE_DELIM_ = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
49 } | |
50 return LINE_DELIM_; | |
51 } | |
129 | 52 |
53 private int fCounter; | |
54 private bool fEnforceUpperLineLimit; | |
55 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
56 public this(bool enforceUpperLineLimit) { |
162 | 57 // super(); |
129 | 58 fEnforceUpperLineLimit= enforceUpperLineLimit; |
59 } | |
60 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
61 public this() { |
129 | 62 this(true); |
63 } | |
64 | |
65 protected Reader createReader(String hoverInfo, TextPresentation presentation) { | |
66 return new HTML2TextReader(new StringReader(hoverInfo), presentation); | |
67 } | |
68 | |
69 protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) { | |
70 | |
71 int yoursStart= offset; | |
72 int yoursEnd= offset + insertLength -1; | |
73 yoursEnd= Math.max(yoursStart, yoursEnd); | |
74 | |
75 Iterator e= presentation.getAllStyleRangeIterator(); | |
76 while (e.hasNext()) { | |
77 | |
134 | 78 StyleRange range= cast(StyleRange) e.next(); |
129 | 79 |
80 int myStart= range.start; | |
81 int myEnd= range.start + range.length -1; | |
82 myEnd= Math.max(myStart, myEnd); | |
83 | |
84 if (myEnd < yoursStart) | |
85 continue; | |
86 | |
87 if (myStart < yoursStart) | |
88 range.length += insertLength; | |
89 else | |
90 range.start += insertLength; | |
91 } | |
92 } | |
93 | |
94 private void append(StringBuffer buffer, String string, TextPresentation presentation) { | |
95 | |
96 int length= string.length(); | |
97 buffer.append(string); | |
98 | |
99 if (presentation !is null) | |
100 adaptTextPresentation(presentation, fCounter, length); | |
101 | |
102 fCounter += length; | |
103 } | |
104 | |
105 private String getIndent(String line) { | |
106 int length= line.length(); | |
107 | |
108 int i= 0; | |
162 | 109 while (i < length && Character.isWhitespace(line.getRelativeCodePoint(i,0))) |
110 i += line.getRelativeCodePointOffset(i,1); | |
129 | 111 |
162 | 112 return (i is length ? line : line.substring(0, i)) ~ " "; //$NON-NLS-1$ |
129 | 113 } |
114 | |
115 /** | |
116 * {@inheritDoc} | |
158 | 117 * |
129 | 118 * @see dwtx.jface.text.DefaultInformationControl.IInformationPresenter#updatePresentation(dwt.widgets.Display, java.lang.String, dwtx.jface.text.TextPresentation, int, int) |
119 * @deprecated | |
120 */ | |
121 public String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { | |
134 | 122 return updatePresentation(cast(Drawable)display, hoverInfo, presentation, maxWidth, maxHeight); |
129 | 123 } |
124 | |
125 /* | |
126 * @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int) | |
127 * @since 3.2 | |
128 */ | |
129 public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) { | |
130 | |
131 if (hoverInfo is null) | |
132 return null; | |
133 | |
134 GC gc= new GC(drawable); | |
135 try { | |
136 | |
137 StringBuffer buffer= new StringBuffer(); | |
162 | 138 int maxNumberOfLines= cast(int) Math.round(maxHeight / gc.getFontMetrics().getHeight()); |
129 | 139 |
140 fCounter= 0; | |
141 LineBreakingReader reader= new LineBreakingReader(createReader(hoverInfo, presentation), gc, maxWidth); | |
142 | |
143 bool lastLineFormatted= false; | |
144 String lastLineIndent= null; | |
145 | |
146 String line=reader.readLine(); | |
147 bool lineFormatted= reader.isFormattedLine(); | |
148 bool firstLineProcessed= false; | |
149 | |
150 while (line !is null) { | |
151 | |
152 if (fEnforceUpperLineLimit && maxNumberOfLines <= 0) | |
153 break; | |
154 | |
155 if (firstLineProcessed) { | |
156 if (!lastLineFormatted) | |
157 append(buffer, LINE_DELIM, null); | |
158 else { | |
159 append(buffer, LINE_DELIM, presentation); | |
160 if (lastLineIndent !is null) | |
161 append(buffer, lastLineIndent, presentation); | |
162 } | |
163 } | |
164 | |
165 append(buffer, line, null); | |
166 firstLineProcessed= true; | |
167 | |
168 lastLineFormatted= lineFormatted; | |
169 if (!lineFormatted) | |
170 lastLineIndent= null; | |
171 else if (lastLineIndent is null) | |
172 lastLineIndent= getIndent(line); | |
173 | |
174 line= reader.readLine(); | |
175 lineFormatted= reader.isFormattedLine(); | |
176 | |
177 maxNumberOfLines--; | |
178 } | |
179 | |
180 if (line !is null) { | |
181 append(buffer, LINE_DELIM, lineFormatted ? presentation : null); | |
182 append(buffer, HTMLMessages.getString("HTMLTextPresenter.ellipse"), presentation); //$NON-NLS-1$ | |
183 } | |
184 | |
185 return trim(buffer, presentation); | |
186 | |
187 } catch (IOException e) { | |
188 | |
189 // ignore TODO do something else? | |
190 return null; | |
191 | |
192 } finally { | |
193 gc.dispose(); | |
194 } | |
195 } | |
196 | |
197 private String trim(StringBuffer buffer, TextPresentation presentation) { | |
198 | |
199 int length= buffer.length(); | |
200 | |
201 int end= length -1; | |
202 | |
162 | 203 while (end >= 0 && Character.isWhitespace(buffer.slice().getRelativeCodePoint( end, -1 ))) |
204 end += buffer.slice().getRelativeCodePointOffset( end, -1 ); | |
205 | |
206 if (end <= -1) | |
129 | 207 return ""; //$NON-NLS-1$ |
208 | |
162 | 209 if (end < buffer.slice().getAbsoluteCodePointOffset( length, -1 )) |
210 buffer.truncate(buffer.slice().getAbsoluteCodePointOffset( end, 1)); | |
129 | 211 else |
212 end= length; | |
213 | |
214 int start= 0; | |
162 | 215 while (start < end && Character.isWhitespace(buffer.slice().getRelativeCodePoint(start, 0))) |
216 start += buffer.slice().getRelativeCodePointOffset( start, 1 ); | |
129 | 217 |
162 | 218 buffer.select(0, start); |
219 buffer.remove(); | |
129 | 220 presentation.setResultWindow(new Region(start, buffer.length())); |
221 return buffer.toString(); | |
222 } | |
223 } | |
224 |