75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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.ui.internal.forms.widgets.FormTextModel;
|
|
14
|
|
15 import dwtx.ui.internal.forms.widgets.Paragraph;
|
|
16 import dwtx.ui.internal.forms.widgets.IFocusSelectable;
|
|
17 import dwtx.ui.internal.forms.widgets.ParagraphSegment;
|
|
18 import dwtx.ui.internal.forms.widgets.IHyperlinkSegment;
|
|
19 import dwtx.ui.internal.forms.widgets.ControlSegment;
|
|
20 import dwtx.ui.internal.forms.widgets.ImageSegment;
|
|
21 import dwtx.ui.internal.forms.widgets.ObjectSegment;
|
78
|
22 import dwtx.ui.internal.forms.widgets.SWTUtil;
|
75
|
23
|
|
24 import dwt.DWT;
|
|
25 import dwtx.ui.forms.HyperlinkSettings;
|
|
26
|
|
27 import dwt.dwthelper.utils;
|
|
28 import dwt.dwthelper.InputStream;
|
|
29
|
|
30 static import tango.text.xml.Document;
|
90
|
31 static import tango.io.Buffer;
|
|
32
|
75
|
33 import tango.util.collection.ArraySeq;
|
|
34 public class FormTextModel {
|
|
35 // private static const DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
|
|
36 // .newInstance();
|
|
37
|
|
38 alias tango.text.xml.Document.Document!(char) Document;
|
|
39 alias Document.NodeImpl Node;
|
|
40
|
|
41 private bool whitespaceNormalized = true;
|
|
42
|
|
43 private alias ArraySeq!(Paragraph) TArraySeqParagraph;
|
|
44 private TArraySeqParagraph paragraphs;
|
|
45
|
|
46 private IFocusSelectable[] selectableSegments;
|
|
47
|
|
48 private int selectedSegmentIndex = -1;
|
|
49
|
|
50 private int savedSelectedLinkIndex = -1;
|
|
51
|
|
52 private HyperlinkSettings hyperlinkSettings;
|
|
53
|
|
54 public static const String BOLD_FONT_ID = "f.____bold"; //$NON-NLS-1$
|
|
55
|
|
56 //private static final int TEXT_ONLY_LINK = 1;
|
|
57
|
|
58 //private static final int IMG_ONLY_LINK = 2;
|
|
59
|
|
60 //private static final int TEXT_AND_IMAGES_LINK = 3;
|
|
61
|
|
62 public this() {
|
|
63 reset();
|
|
64 }
|
|
65
|
|
66 /*
|
|
67 * @see ITextModel#getParagraphs()
|
|
68 */
|
|
69 public Paragraph[] getParagraphs() {
|
|
70 if (paragraphs is null)
|
|
71 return new Paragraph[0];
|
|
72 return paragraphs
|
|
73 .toArray();
|
|
74 }
|
|
75
|
|
76 public String getAccessibleText() {
|
|
77 if (paragraphs is null)
|
|
78 return ""; //$NON-NLS-1$
|
|
79 StringBuffer sbuf = new StringBuffer();
|
|
80 for (int i = 0; i < paragraphs.size(); i++) {
|
|
81 Paragraph paragraph = cast(Paragraph) paragraphs.get(i);
|
|
82 String text = paragraph.getAccessibleText();
|
|
83 sbuf.append(text);
|
|
84 }
|
|
85 return sbuf.toString();
|
|
86 }
|
90
|
87
|
75
|
88 /*
|
|
89 * @see ITextModel#parse(String)
|
|
90 */
|
|
91 public void parseTaggedText(String taggedText, bool expandURLs) {
|
|
92 if (taggedText is null) {
|
|
93 reset();
|
|
94 return;
|
|
95 }
|
90
|
96 _parseTaggedText(taggedText, expandURLs);
|
75
|
97 }
|
|
98
|
|
99 public void parseInputStream(InputStream is_, bool expandURLs) {
|
90
|
100 auto buf = new tango.io.Buffer.Buffer( 1024 );
|
|
101 {
|
|
102 int l;
|
|
103 byte[1024] a = void;
|
|
104 while((l = is_.read(a)) > 0 ){
|
|
105 buf.append( a[ 0 .. l ] );
|
|
106 }
|
|
107 }
|
|
108 _parseTaggedText( cast(char[]) buf.slice(), expandURLs );
|
|
109 // documentBuilderFactory.setNamespaceAware(true);
|
|
110 // documentBuilderFactory.setIgnoringComments(true);
|
|
111 //
|
|
112 // reset();
|
|
113 // try {
|
|
114 // DocumentBuilder parser = documentBuilderFactory
|
|
115 // .newDocumentBuilder();
|
|
116 // InputSource source = new InputSource(is_);
|
|
117 // Document doc = parser.parse(source);
|
|
118 // processDocument(doc, expandURLs);
|
|
119 // } catch (ParserConfigurationException e) {
|
|
120 // DWT.error(DWT.ERROR_INVALID_ARGUMENT, e);
|
|
121 // } catch (SAXException e) {
|
|
122 // DWT.error(DWT.ERROR_INVALID_ARGUMENT, e);
|
|
123 // } catch (IOException e) {
|
|
124 // DWT.error(DWT.ERROR_IO, e);
|
|
125 // }
|
|
126 }
|
|
127 private void _parseTaggedText( char[] text, bool expandURLs ){
|
75
|
128 reset();
|
90
|
129 // try {
|
|
130 // auto doc = tango.text.xml.Document.Document!(char)();
|
|
131 // doc.parse(text);
|
|
132 // processDocument(doc, expandURLs);
|
|
133 // } catch (XmlException e) {
|
|
134 // DWT.error(DWT.ERROR_INVALID_ARGUMENT, e);
|
|
135 // } catch (IOException e) {
|
|
136 // DWT.error(DWT.ERROR_IO, e);
|
|
137 // }
|
75
|
138 }
|
|
139
|
90
|
140 // private void processDocument(tango.text.xml.Document.Document!(char) doc, bool expandURLs) {
|
|
141 // auto root = doc.query.root.dup;
|
|
142 // auto children = root.childs.dup;
|
|
143 // processSubnodes(paragraphs, children, expandURLs);
|
|
144 // }
|
75
|
145
|
90
|
146 /+ private void processSubnodes(TArraySeqParagraph plist, NodeList children, bool expandURLs) {
|
|
147 //o for (int i = 0; i < children.getLength(); i++) {
|
|
148 //o Node child = children.item(i);
|
|
149 foreach( child; children ){
|
|
150 //o if (child.getNodeType() is Node.TEXT_NODE) {
|
|
151 if (child.type is XmlNodeType.Data) {
|
75
|
152 // Make an implicit paragraph
|
|
153 String text = getSingleNodeText(child);
|
90
|
154 if ( !isIgnorableWhiteSpace(text, true)) {
|
75
|
155 Paragraph p = new Paragraph(true);
|
90
|
156 // p.parseRegularText(text, expandURLs, true,
|
|
157 // getHyperlinkSettings(), null);
|
|
158 // plist.add(p);
|
|
159 // }
|
|
160 // } else if (child.getNodeType() is Node.ELEMENT_NODE) {
|
|
161 // String tag = child.getNodeName().toLowerCase();
|
|
162 // if (tag.equals("p")) { //$NON-NLS-1$
|
|
163 // Paragraph p = processParagraph(child, expandURLs);
|
|
164 // if (p !is null)
|
|
165 // plist.add(p);
|
|
166 // } else if (tag.equals("li")) { //$NON-NLS-1$
|
|
167 // Paragraph p = processListItem(child, expandURLs);
|
|
168 // if (p !is null)
|
|
169 // plist.add(p);
|
75
|
170 }
|
|
171 }
|
|
172 }
|
90
|
173 }+/
|
|
174 /++
|
75
|
175
|
|
176 private Paragraph processParagraph(Node paragraph, bool expandURLs) {
|
|
177 NodeList children = paragraph.getChildNodes();
|
|
178 NamedNodeMap atts = paragraph.getAttributes();
|
|
179 Node addSpaceAtt = atts.getNamedItem("addVerticalSpace"); //$NON-NLS-1$
|
|
180 bool addSpace = true;
|
|
181
|
|
182 if (addSpaceAtt is null)
|
|
183 addSpaceAtt = atts.getNamedItem("vspace"); //$NON-NLS-1$
|
|
184
|
|
185 if (addSpaceAtt !is null) {
|
|
186 String value = addSpaceAtt.getNodeValue();
|
|
187 addSpace = value.equalsIgnoreCase("true"); //$NON-NLS-1$
|
|
188 }
|
|
189 Paragraph p = new Paragraph(addSpace);
|
|
190
|
|
191 processSegments(p, children, expandURLs);
|
|
192 return p;
|
|
193 }
|
|
194
|
|
195 private Paragraph processListItem(Node listItem, bool expandURLs) {
|
|
196 NodeList children = listItem.getChildNodes();
|
|
197 NamedNodeMap atts = listItem.getAttributes();
|
|
198 Node addSpaceAtt = atts.getNamedItem("addVerticalSpace");//$NON-NLS-1$
|
|
199 Node styleAtt = atts.getNamedItem("style");//$NON-NLS-1$
|
|
200 Node valueAtt = atts.getNamedItem("value");//$NON-NLS-1$
|
|
201 Node indentAtt = atts.getNamedItem("indent");//$NON-NLS-1$
|
|
202 Node bindentAtt = atts.getNamedItem("bindent");//$NON-NLS-1$
|
|
203 int style = BulletParagraph.CIRCLE;
|
|
204 int indent = -1;
|
|
205 int bindent = -1;
|
|
206 String text = null;
|
|
207 bool addSpace = true;
|
|
208
|
|
209 if (addSpaceAtt !is null) {
|
|
210 String value = addSpaceAtt.getNodeValue();
|
|
211 addSpace = value.equalsIgnoreCase("true"); //$NON-NLS-1$
|
|
212 }
|
|
213 if (styleAtt !is null) {
|
|
214 String value = styleAtt.getNodeValue();
|
|
215 if (value.equalsIgnoreCase("text")) { //$NON-NLS-1$
|
|
216 style = BulletParagraph.TEXT;
|
|
217 } else if (value.equalsIgnoreCase("image")) { //$NON-NLS-1$
|
|
218 style = BulletParagraph.IMAGE;
|
|
219 } else if (value.equalsIgnoreCase("bullet")) { //$NON-NLS-1$
|
|
220 style = BulletParagraph.CIRCLE;
|
|
221 }
|
|
222 }
|
|
223 if (valueAtt !is null) {
|
|
224 text = valueAtt.getNodeValue();
|
|
225 if (style is BulletParagraph.IMAGE)
|
|
226 text = "i." + text; //$NON-NLS-1$
|
|
227 }
|
|
228 if (indentAtt !is null) {
|
|
229 String value = indentAtt.getNodeValue();
|
|
230 try {
|
|
231 indent = Integer.parseInt(value);
|
|
232 } catch (NumberFormatException e) {
|
|
233 }
|
|
234 }
|
|
235 if (bindentAtt !is null) {
|
|
236 String value = bindentAtt.getNodeValue();
|
|
237 try {
|
|
238 bindent = Integer.parseInt(value);
|
|
239 } catch (NumberFormatException e) {
|
|
240 }
|
|
241 }
|
|
242
|
|
243 BulletParagraph p = new BulletParagraph(addSpace);
|
|
244 p.setIndent(indent);
|
|
245 p.setBulletIndent(bindent);
|
|
246 p.setBulletStyle(style);
|
|
247 p.setBulletText(text);
|
|
248
|
|
249 processSegments(p, children, expandURLs);
|
|
250 return p;
|
|
251 }
|
|
252
|
|
253 private void processSegments(Paragraph p, NodeList children,
|
|
254 bool expandURLs) {
|
|
255 for (int i = 0; i < children.getLength(); i++) {
|
|
256 Node child = children.item(i);
|
|
257 ParagraphSegment segment = null;
|
|
258
|
|
259 if (child.getNodeType() is Node.TEXT_NODE) {
|
|
260 String value = getSingleNodeText(child);
|
|
261
|
|
262 if (value !is null && !isIgnorableWhiteSpace(value, false)) {
|
|
263 p.parseRegularText(value, expandURLs, true,
|
|
264 getHyperlinkSettings(), null);
|
|
265 }
|
|
266 } else if (child.getNodeType() is Node.ELEMENT_NODE) {
|
|
267 String name = child.getNodeName();
|
|
268 if (name.equalsIgnoreCase("img")) { //$NON-NLS-1$
|
|
269 segment = processImageSegment(child);
|
|
270 } else if (name.equalsIgnoreCase("a")) { //$NON-NLS-1$
|
|
271 segment = processHyperlinkSegment(child,
|
|
272 getHyperlinkSettings());
|
|
273 } else if (name.equalsIgnoreCase("span")) { //$NON-NLS-1$
|
|
274 processTextSegment(p, expandURLs, child);
|
|
275 } else if (name.equalsIgnoreCase("b")) { //$NON-NLS-1$
|
|
276 String text = getNodeText(child);
|
|
277 String fontId = BOLD_FONT_ID;
|
|
278 p.parseRegularText(text, expandURLs, true,
|
|
279 getHyperlinkSettings(), fontId);
|
|
280 } else if (name.equalsIgnoreCase("br")) { //$NON-NLS-1$
|
|
281 segment = new BreakSegment();
|
|
282 } else if (name.equalsIgnoreCase("control")) { //$NON-NLS-1$
|
|
283 segment = processControlSegment(child);
|
|
284 }
|
|
285 }
|
|
286 if (segment !is null) {
|
|
287 p.addSegment(segment);
|
|
288 }
|
|
289 }
|
|
290 }
|
90
|
291 ++/
|
75
|
292 private bool isIgnorableWhiteSpace(String text, bool ignoreSpaces) {
|
90
|
293 for (int i = 0; i < text.length; i++) {
|
75
|
294 char c = text.charAt(i);
|
|
295 if (ignoreSpaces && c is ' ')
|
|
296 continue;
|
|
297 if (c is '\n' || c is '\r' || c is '\f')
|
|
298 continue;
|
|
299 return false;
|
|
300 }
|
|
301 return true;
|
|
302 }
|
90
|
303 /++
|
75
|
304 private ImageSegment processImageSegment(Node image) {
|
|
305 ImageSegment segment = new ImageSegment();
|
|
306 processObjectSegment(segment, image, "i."); //$NON-NLS-1$
|
|
307 return segment;
|
|
308 }
|
|
309
|
|
310 private ControlSegment processControlSegment(Node control) {
|
|
311 ControlSegment segment = new ControlSegment();
|
|
312 processObjectSegment(segment, control, "o."); //$NON-NLS-1$
|
|
313 Node fill = control.getAttributes().getNamedItem("fill"); //$NON-NLS-1$
|
|
314 if (fill !is null) {
|
|
315 String value = fill.getNodeValue();
|
|
316 bool doFill = value.equalsIgnoreCase("true"); //$NON-NLS-1$
|
|
317 segment.setFill(doFill);
|
|
318 }
|
|
319 try {
|
|
320 Node width = control.getAttributes().getNamedItem("width"); //$NON-NLS-1$
|
|
321 if (width !is null) {
|
|
322 String value = width.getNodeValue();
|
|
323 int doWidth = Integer.parseInt(value);
|
|
324 segment.setWidth(doWidth);
|
|
325 }
|
|
326 Node height = control.getAttributes().getNamedItem("height"); //$NON-NLS-1$
|
|
327 if (height !is null) {
|
|
328 String value = height.getNodeValue();
|
|
329 int doHeight = Integer.parseInt(value);
|
|
330 segment.setHeight(doHeight);
|
|
331 }
|
|
332 }
|
|
333 catch (NumberFormatException e) {
|
|
334 // ignore invalid width or height
|
|
335 }
|
|
336 return segment;
|
|
337 }
|
|
338
|
|
339 private void processObjectSegment(ObjectSegment segment, Node object, String prefix) {
|
|
340 NamedNodeMap atts = object.getAttributes();
|
|
341 Node id = atts.getNamedItem("href"); //$NON-NLS-1$
|
|
342 Node align_ = atts.getNamedItem("align"); //$NON-NLS-1$
|
|
343 if (id !is null) {
|
|
344 String value = id.getNodeValue();
|
|
345 segment.setObjectId(prefix + value);
|
|
346 }
|
|
347 if (align_ !is null) {
|
|
348 String value = align_.getNodeValue().toLowerCase();
|
|
349 if (value.equals("top")) //$NON-NLS-1$
|
|
350 segment.setVerticalAlignment(ImageSegment.TOP);
|
|
351 else if (value.equals("middle")) //$NON-NLS-1$
|
|
352 segment.setVerticalAlignment(ImageSegment.MIDDLE);
|
|
353 else if (value.equals("bottom")) //$NON-NLS-1$
|
|
354 segment.setVerticalAlignment(ImageSegment.BOTTOM);
|
|
355 }
|
|
356 }
|
78
|
357 ++/
|
75
|
358 private void appendText(String value, StringBuffer buf, int[] spaceCounter) {
|
|
359 if (!whitespaceNormalized)
|
|
360 buf.append(value);
|
|
361 else {
|
78
|
362 for (int j = 0; j < value.length; j++) {
|
75
|
363 char c = value.charAt(j);
|
|
364 if (c is ' ' || c is '\t') {
|
|
365 // space
|
|
366 if (++spaceCounter[0] is 1) {
|
|
367 buf.append(c);
|
|
368 }
|
|
369 } else if (c is '\n' || c is '\r' || c is '\f') {
|
|
370 // new line
|
|
371 if (++spaceCounter[0] is 1) {
|
|
372 buf.append(' ');
|
|
373 }
|
|
374 } else {
|
|
375 // other characters
|
|
376 spaceCounter[0] = 0;
|
|
377 buf.append(c);
|
|
378 }
|
|
379 }
|
|
380 }
|
|
381 }
|
|
382
|
|
383 private String getNormalizedText(String text) {
|
|
384 int[] spaceCounter = new int[1];
|
|
385 StringBuffer buf = new StringBuffer();
|
|
386
|
|
387 if (text is null)
|
|
388 return null;
|
|
389 appendText(text, buf, spaceCounter);
|
|
390 return buf.toString();
|
|
391 }
|
90
|
392
|
|
393 private String getSingleNodeText(tango.text.xml.Document.Document!(char).NodeImpl node) {
|
|
394 return getNormalizedText(node.value());
|
|
395 }
|
78
|
396 /++
|
75
|
397
|
|
398 private String getNodeText(Node node) {
|
|
399 NodeList children = node.getChildNodes();
|
|
400 StringBuffer buf = new StringBuffer();
|
|
401 int[] spaceCounter = new int[1];
|
|
402
|
|
403 for (int i = 0; i < children.getLength(); i++) {
|
|
404 Node child = children.item(i);
|
|
405 if (child.getNodeType() is Node.TEXT_NODE) {
|
|
406 String value = child.getNodeValue();
|
|
407 appendText(value, buf, spaceCounter);
|
|
408 }
|
|
409 }
|
|
410 return buf.toString().trim();
|
|
411 }
|
|
412
|
|
413 private ParagraphSegment processHyperlinkSegment(Node link,
|
|
414 HyperlinkSettings settings) {
|
|
415 NamedNodeMap atts = link.getAttributes();
|
|
416 String href = null;
|
|
417 bool wrapAllowed = true;
|
|
418 String boldFontId = null;
|
|
419
|
|
420 Node hrefAtt = atts.getNamedItem("href"); //$NON-NLS-1$
|
|
421 if (hrefAtt !is null) {
|
|
422 href = hrefAtt.getNodeValue();
|
|
423 }
|
|
424 Node boldAtt = atts.getNamedItem("bold"); //$NON-NLS-1$
|
|
425 if (boldAtt !is null) {
|
|
426 boldFontId = BOLD_FONT_ID;
|
|
427 }
|
|
428 Node nowrap = atts.getNamedItem("nowrap"); //$NON-NLS-1$
|
|
429 if (nowrap !is null) {
|
|
430 String value = nowrap.getNodeValue();
|
|
431 if (value !is null && value.equalsIgnoreCase("true")) //$NON-NLS-1$
|
|
432 wrapAllowed = false;
|
|
433 }
|
|
434 Object status = checkChildren(link);
|
|
435 if ( auto child = cast(Node)status ) {
|
|
436 ImageHyperlinkSegment segment = new ImageHyperlinkSegment();
|
|
437 segment.setHref(href);
|
|
438 segment.setWordWrapAllowed(wrapAllowed);
|
|
439 Node alt = child.getAttributes().getNamedItem("alt"); //$NON-NLS-1$
|
|
440 if (alt !is null)
|
|
441 segment.setTooltipText(alt.getNodeValue());
|
|
442 Node text = child.getAttributes().getNamedItem("text"); //$NON-NLS-1$
|
|
443 if (text !is null)
|
|
444 segment.setText(text.getNodeValue());
|
|
445 processObjectSegment(segment, child, "i."); //$NON-NLS-1$
|
|
446 return segment;
|
|
447 } else if ( auto textObj = cast(ArrayWrapperString)status ) {
|
|
448 String text = textObj.array;
|
|
449 TextHyperlinkSegment segment = new TextHyperlinkSegment(text,
|
|
450 settings, null);
|
|
451 segment.setHref(href);
|
|
452 segment.setFontId(boldFontId);
|
|
453 Node alt = atts.getNamedItem("alt"); //$NON-NLS-1$
|
|
454 if (alt !is null)
|
|
455 segment.setTooltipText(alt.getNodeValue());
|
|
456 segment.setWordWrapAllowed(wrapAllowed);
|
|
457 return segment;
|
|
458 } else {
|
|
459 AggregateHyperlinkSegment parent = new AggregateHyperlinkSegment();
|
|
460 parent.setHref(href);
|
|
461 NodeList children = link.getChildNodes();
|
|
462 for (int i = 0; i < children.getLength(); i++) {
|
|
463 Node child = children.item(i);
|
|
464 if (child.getNodeType() is Node.TEXT_NODE) {
|
|
465 String value = child.getNodeValue();
|
|
466 TextHyperlinkSegment ts = new TextHyperlinkSegment(
|
|
467 getNormalizedText(value), settings, null);
|
|
468 Node alt = atts.getNamedItem("alt"); //$NON-NLS-1$
|
|
469 if (alt !is null)
|
|
470 ts.setTooltipText(alt.getNodeValue());
|
|
471 ts.setWordWrapAllowed(wrapAllowed);
|
|
472 parent.add(ts);
|
|
473 } else if (child.getNodeType() is Node.ELEMENT_NODE) {
|
|
474 String name = child.getNodeName();
|
|
475 if (name.equalsIgnoreCase("img")) { //$NON-NLS-1$
|
|
476 ImageHyperlinkSegment is_ = new ImageHyperlinkSegment();
|
|
477 processObjectSegment(is_, child, "i."); //$NON-NLS-1$
|
|
478 Node alt = child.getAttributes().getNamedItem("alt"); //$NON-NLS-1$
|
|
479 if (alt !is null)
|
|
480 is_.setTooltipText(alt.getNodeValue());
|
|
481 parent.add(is_);
|
|
482 is_.setWordWrapAllowed(wrapAllowed);
|
|
483 }
|
|
484 }
|
|
485 }
|
|
486 return parent;
|
|
487 }
|
|
488 }
|
|
489
|
|
490 private Object checkChildren(Node node) {
|
|
491 bool text = false;
|
|
492 Node imgNode = null;
|
|
493 //int status = 0;
|
|
494
|
|
495 NodeList children = node.getChildNodes();
|
|
496 for (int i = 0; i < children.getLength(); i++) {
|
|
497 Node child = children.item(i);
|
|
498 if (child.getNodeType() is Node.TEXT_NODE)
|
|
499 text = true;
|
|
500 else if (child.getNodeType() is Node.ELEMENT_NODE
|
|
501 && child.getNodeName().equalsIgnoreCase("img")) { //$NON-NLS-1$
|
|
502 imgNode = child;
|
|
503 }
|
|
504 }
|
|
505 if (text && imgNode is null)
|
|
506 return getNodeText(node);
|
|
507 else if (!text && imgNode !is null)
|
|
508 return imgNode;
|
|
509 else return null;
|
|
510 }
|
|
511
|
|
512 private void processTextSegment(Paragraph p, bool expandURLs,
|
|
513 Node textNode) {
|
|
514 String text = getNodeText(textNode);
|
|
515
|
|
516 NamedNodeMap atts = textNode.getAttributes();
|
|
517 Node font = atts.getNamedItem("font"); //$NON-NLS-1$
|
|
518 Node color = atts.getNamedItem("color"); //$NON-NLS-1$
|
|
519 bool wrapAllowed=true;
|
|
520 Node nowrap = atts.getNamedItem("nowrap"); //$NON-NLS-1$
|
|
521 if (nowrap !is null) {
|
|
522 String value = nowrap.getNodeValue();
|
|
523 if (value !is null && value.equalsIgnoreCase("true")) //$NON-NLS-1$
|
|
524 wrapAllowed = false;
|
|
525 }
|
|
526 String fontId = null;
|
|
527 String colorId = null;
|
|
528 if (font !is null) {
|
|
529 fontId = "f." + font.getNodeValue(); //$NON-NLS-1$
|
|
530 }
|
|
531 if (color !is null) {
|
|
532 colorId = "c." + color.getNodeValue(); //$NON-NLS-1$
|
|
533 }
|
|
534 p.parseRegularText(text, expandURLs, wrapAllowed, getHyperlinkSettings(), fontId,
|
|
535 colorId);
|
|
536 }
|
78
|
537 ++/
|
75
|
538 public void parseRegularText(String regularText, bool convertURLs) {
|
|
539 reset();
|
|
540
|
|
541 if (regularText is null)
|
|
542 return;
|
|
543
|
|
544 regularText = getNormalizedText(regularText);
|
|
545
|
|
546 Paragraph p = new Paragraph(true);
|
|
547 paragraphs.append(p);
|
|
548 int pstart = 0;
|
|
549
|
78
|
550 for (int i = 0; i < regularText.length; i++) {
|
75
|
551 char c = regularText.charAt(i);
|
|
552 if (p is null) {
|
|
553 p = new Paragraph(true);
|
|
554 paragraphs.append(p);
|
|
555 }
|
|
556 if (c is '\n') {
|
|
557 String text = regularText.substring(pstart, i);
|
|
558 pstart = i + 1;
|
|
559 p.parseRegularText(text, convertURLs, true, getHyperlinkSettings(),
|
|
560 null);
|
|
561 p = null;
|
|
562 }
|
|
563 }
|
|
564 if (p !is null) {
|
|
565 // no new line
|
|
566 String text = regularText.substring(pstart);
|
|
567 p.parseRegularText(text, convertURLs, true, getHyperlinkSettings(), null);
|
|
568 }
|
|
569 }
|
|
570
|
|
571 public HyperlinkSettings getHyperlinkSettings() {
|
|
572 // #132723 cannot have null settings
|
78
|
573 if (hyperlinkSettings is null)
|
75
|
574 hyperlinkSettings = new HyperlinkSettings(SWTUtil.getStandardDisplay());
|
|
575 return hyperlinkSettings;
|
|
576 }
|
|
577
|
|
578 public void setHyperlinkSettings(HyperlinkSettings settings) {
|
|
579 this.hyperlinkSettings = settings;
|
|
580 }
|
|
581
|
|
582 private void reset() {
|
|
583 if (paragraphs is null)
|
|
584 paragraphs = new TArraySeqParagraph;
|
|
585 paragraphs.clear();
|
|
586 selectedSegmentIndex = -1;
|
|
587 savedSelectedLinkIndex = -1;
|
|
588 selectableSegments = null;
|
|
589 }
|
|
590
|
|
591 IFocusSelectable[] getFocusSelectableSegments() {
|
|
592 if (selectableSegments !is null || paragraphs is null)
|
|
593 return selectableSegments;
|
|
594 IFocusSelectable[] result;
|
|
595 for (int i = 0; i < paragraphs.size(); i++) {
|
|
596 Paragraph p = cast(Paragraph) paragraphs.get(i);
|
|
597 ParagraphSegment[] segments = p.getSegments();
|
|
598 for (int j = 0; j < segments.length; j++) {
|
|
599 if (null !is cast(IFocusSelectable)segments[j] )
|
78
|
600 result ~= cast(IFocusSelectable)segments[j];
|
75
|
601 }
|
|
602 }
|
|
603 selectableSegments = result;
|
|
604 return selectableSegments;
|
|
605 }
|
|
606
|
|
607 public IHyperlinkSegment getHyperlink(int index) {
|
|
608 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
609 if (selectables.length>index) {
|
|
610 IFocusSelectable link = selectables[index];
|
|
611 if (auto l = cast(IHyperlinkSegment)link )
|
|
612 return l;
|
|
613 }
|
|
614 return null;
|
|
615 }
|
|
616
|
|
617 public IHyperlinkSegment findHyperlinkAt(int x, int y) {
|
|
618 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
619 for (int i = 0; i < selectables.length; i++) {
|
|
620 IFocusSelectable segment = selectables[i];
|
|
621 if ( auto link = cast(IHyperlinkSegment)segment ) {
|
|
622 if (link.contains(x, y))
|
|
623 return link;
|
|
624 }
|
|
625 }
|
|
626 return null;
|
|
627 }
|
|
628
|
|
629 public int getHyperlinkCount() {
|
|
630 return getFocusSelectableSegments().length;
|
|
631 }
|
|
632
|
|
633 public int indexOf(IHyperlinkSegment link) {
|
|
634 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
635 for (int i = 0; i < selectables.length; i++) {
|
|
636 IFocusSelectable segment = selectables[i];
|
|
637 if (auto l = cast(IHyperlinkSegment)segment ) {
|
|
638 if (link is l)
|
|
639 return i;
|
|
640 }
|
|
641 }
|
|
642 return -1;
|
|
643 }
|
|
644
|
|
645 public ParagraphSegment findSegmentAt(int x, int y) {
|
|
646 for (int i = 0; i < paragraphs.size(); i++) {
|
|
647 Paragraph p = cast(Paragraph) paragraphs.get(i);
|
|
648 ParagraphSegment segment = p.findSegmentAt(x, y);
|
|
649 if (segment !is null)
|
|
650 return segment;
|
|
651 }
|
|
652 return null;
|
|
653 }
|
|
654
|
|
655 public void clearCache(String fontId) {
|
|
656 for (int i = 0; i < paragraphs.size(); i++) {
|
|
657 Paragraph p = cast(Paragraph) paragraphs.get(i);
|
|
658 p.clearCache(fontId);
|
|
659 }
|
|
660 }
|
|
661
|
|
662 public IFocusSelectable getSelectedSegment() {
|
|
663 if (selectableSegments is null || selectedSegmentIndex is -1)
|
|
664 return null;
|
|
665 return selectableSegments[selectedSegmentIndex];
|
|
666 }
|
|
667
|
|
668 public int getSelectedSegmentIndex() {
|
|
669 return selectedSegmentIndex;
|
|
670 }
|
|
671
|
|
672 public bool linkExists(IHyperlinkSegment link) {
|
78
|
673 if (selectableSegments is null)
|
75
|
674 return false;
|
|
675 for (int i=0; i<selectableSegments.length; i++) {
|
|
676 if (selectableSegments[i] is link)
|
|
677 return true;
|
|
678 }
|
|
679 return false;
|
|
680 }
|
|
681
|
|
682 public bool traverseFocusSelectableObjects(bool next) {
|
|
683 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
684 if (selectables is null)
|
|
685 return false;
|
|
686 int size = selectables.length;
|
|
687 if (next) {
|
|
688 selectedSegmentIndex++;
|
|
689 } else
|
|
690 selectedSegmentIndex--;
|
|
691
|
|
692 if (selectedSegmentIndex < 0 || selectedSegmentIndex > size - 1) {
|
|
693 selectedSegmentIndex = -1;
|
|
694 }
|
|
695 return selectedSegmentIndex !is -1;
|
|
696 }
|
|
697
|
|
698 public IFocusSelectable getNextFocusSegment(bool next) {
|
|
699 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
700 if (selectables is null)
|
|
701 return null;
|
|
702 int nextIndex = next?selectedSegmentIndex+1:selectedSegmentIndex-1;
|
|
703
|
|
704 if (nextIndex < 0 || nextIndex > selectables.length - 1) {
|
|
705 return null;
|
|
706 }
|
|
707 return selectables[nextIndex];
|
|
708 }
|
|
709
|
|
710 public bool restoreSavedLink() {
|
90
|
711 if (savedSelectedLinkIndex !is -1) {
|
75
|
712 selectedSegmentIndex = savedSelectedLinkIndex;
|
|
713 return true;
|
|
714 }
|
|
715 return false;
|
|
716 }
|
|
717
|
|
718 public void selectLink(IHyperlinkSegment link) {
|
|
719 if (link is null) {
|
|
720 savedSelectedLinkIndex = selectedSegmentIndex;
|
|
721 selectedSegmentIndex = -1;
|
|
722 }
|
|
723 else {
|
|
724 select(link);
|
|
725
|
|
726 }
|
|
727 }
|
|
728
|
|
729 public void select(IFocusSelectable selectable) {
|
|
730 IFocusSelectable[] selectables = getFocusSelectableSegments();
|
|
731 selectedSegmentIndex = -1;
|
|
732 if (selectables is null)
|
|
733 return;
|
|
734 for (int i = 0; i < selectables.length; i++) {
|
78
|
735 if ((cast(Object)selectables[i]).opEquals(cast(Object)selectable)) {
|
75
|
736 selectedSegmentIndex = i;
|
|
737 break;
|
|
738 }
|
|
739 }
|
|
740 }
|
|
741
|
|
742 public bool hasFocusSegments() {
|
|
743 IFocusSelectable[] segments = getFocusSelectableSegments();
|
|
744 if (segments.length > 0)
|
|
745 return true;
|
|
746 return false;
|
|
747 }
|
|
748
|
|
749 public void dispose() {
|
|
750 paragraphs = null;
|
|
751 selectedSegmentIndex = -1;
|
|
752 savedSelectedLinkIndex = -1;
|
|
753 selectableSegments = null;
|
|
754 }
|
|
755
|
|
756 /**
|
|
757 * @return Returns the whitespaceNormalized.
|
|
758 */
|
|
759 public bool isWhitespaceNormalized() {
|
|
760 return whitespaceNormalized;
|
|
761 }
|
|
762
|
|
763 /**
|
|
764 * @param whitespaceNormalized
|
|
765 * The whitespaceNormalized to set.
|
|
766 */
|
|
767 public void setWhitespaceNormalized(bool whitespaceNormalized) {
|
|
768 this.whitespaceNormalized = whitespaceNormalized;
|
|
769 }
|
|
770 }
|