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