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