forked from eclipse-platform/eclipse.platform.ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormText.java
1698 lines (1581 loc) · 51.1 KB
/
FormText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Martin Donnelly (m2a3@eircom.net) - patch (see Bugzilla #145997)
*******************************************************************************/
package org.eclipse.ui.forms.widgets;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.accessibility.ACC;
import org.eclipse.swt.accessibility.Accessible;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleControlAdapter;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.forms.HyperlinkSettings;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.internal.forms.Messages;
import org.eclipse.ui.internal.forms.widgets.ControlSegment;
import org.eclipse.ui.internal.forms.widgets.FormFonts;
import org.eclipse.ui.internal.forms.widgets.FormTextModel;
import org.eclipse.ui.internal.forms.widgets.FormUtil;
import org.eclipse.ui.internal.forms.widgets.IFocusSelectable;
import org.eclipse.ui.internal.forms.widgets.IHyperlinkSegment;
import org.eclipse.ui.internal.forms.widgets.ImageSegment;
import org.eclipse.ui.internal.forms.widgets.Locator;
import org.eclipse.ui.internal.forms.widgets.Paragraph;
import org.eclipse.ui.internal.forms.widgets.ParagraphSegment;
import org.eclipse.ui.internal.forms.widgets.SelectionData;
import org.eclipse.ui.internal.forms.widgets.TextSegment;
/**
* This class is a read-only text control that is capable of rendering wrapped
* text. Text can be rendered as-is or by parsing the formatting XML tags.
* Independently, words that start with http:// can be converted into hyperlinks
* on the fly.
* <p>
* When configured to use formatting XML, the control requires the root element
* <code>form</code> to be used and requires any ampersand (&) characters in the text to
* be replaced by the entity <b>&amp;</b>. The following tags can be children of the
* <code>form</code> element:
* </p>
* <ul>
* <li><b>p </b>- for defining paragraphs. The following attributes are
* allowed:
* <ul>
* <li><b>vspace </b>- if set to 'false', no vertical space will be added
* (default is 'true')</li>
* </ul>
* </li>
* <li><b>li </b>- for defining list items. The following attributes are
* allowed:
* <ul>
* <li><b>vspace </b>- the same as with the <b>p </b> tag</li>
* <li><b>style </b>- could be 'bullet' (default), 'text' and 'image'</li>
* <li><b>value </b>- not used for 'bullet'. For text, it is the value of the
* text that is rendered as a bullet. For image, it is the href of the image to
* be rendered as a bullet.</li>
* <li><b>indent </b>- the number of pixels to indent the text in the list item
* </li>
* <li><b>bindent </b>- the number of pixels to indent the bullet itself</li>
* </ul>
* </li>
* </ul>
* <p>
* Text in paragraphs and list items will be wrapped according to the width of
* the control. The following tags can appear as children of either <b>p </b> or
* <b>li </b> elements:
* <ul>
* <li><b>img </b>- to render an image. Element accepts attribute 'href' that
* is a key to the <code>Image</code> set using 'setImage' method. Vertical
* position of image relative to surrounding text is optionally controlled by
* the attribute <b>align</b> that can have values <b>top</b>, <b>middle</b>
* and <b>bottom</b></li>
* <li><b>a </b>- to render a hyperlink. Element accepts attribute 'href' that
* will be provided to the hyperlink listeners via HyperlinkEvent object. The
* element also accepts 'nowrap' attribute (default is false). When set to
* 'true', the hyperlink will not be wrapped. Hyperlinks automatically created
* when 'http://' is encountered in text are not wrapped.</li>
* <li><b>b </b>- the enclosed text will use bold font.</li>
* <li><b>br </b>- forced line break (no attributes).</li>
* <li><b>span </b>- the enclosed text will have the color and font specified
* in the element attributes. Color is provided using 'color' attribute and is a
* key to the Color object set by 'setColor' method. Font is provided using
* 'font' attribute and is a key to the Font object set by 'setFont' method. As with
* hyperlinks, it is possible to block wrapping by setting 'nowrap' to true
* (false by default).
* </li>
* <li><b>control (new in 3.1)</b> - to place a control that is a child of the
* text control. Element accepts attribute 'href' that is a key to the Control
* object set using 'setControl' method. Optionally, attribute 'fill' can be set
* to <code>true</code> to make the control fill the entire width of the text.
* Form text is not responsible for creating or disposing controls, it only
* places them relative to the surrounding text. Similar to <b>img</b>,
* vertical position of the control can be set using the <b>align</b>
* attribute. In addition, <b>width</b> and <b>height</b> attributes can
* be used to force the dimensions of the control. If not used,
* the preferred control size will be used.
* </ul>
* <p>
* None of the elements can nest. For example, you cannot have <b>b </b> inside
* a <b>span </b>. This was done to keep everything simple and transparent.
* Since 3.1, an exception to this rule has been added to support nesting images
* and text inside the hyperlink tag (<b>a</b>). Image enclosed in the
* hyperlink tag acts as a hyperlink, can be clicked on and can accept and
* render selection focus. When both text and image is enclosed, selection and
* rendering will affect both as a single hyperlink.
* </p>
* <p>
* Since 3.1, it is possible to select text. Text selection can be
* programmatically accessed and also copied to clipboard. Non-textual objects
* (images, controls etc.) in the selection range are ignored.
* <p>
* Care should be taken when using this control. Form text is not an HTML
* browser and should not be treated as such. If you need complex formatting
* capabilities, use Browser widget. If you need editing capabilities and
* font/color styles of text segments is all you need, use StyleText widget.
* Finally, if all you need is to wrap text, use SWT Label widget and create it
* with SWT.WRAP style.
*
* @see FormToolkit
* @see TableWrapLayout
* @since 3.0
*/
public class FormText extends Canvas {
/**
* The object ID to be used when registering action to handle URL hyperlinks
* (those that should result in opening the web browser). Value is
* "urlHandler".
*/
public static final String URL_HANDLER_ID = "urlHandler"; //$NON-NLS-1$
/**
* Value of the horizontal margin (default is 0).
*/
public int marginWidth = 0;
/**
* Value of tue vertical margin (default is 1).
*/
public int marginHeight = 1;
// private fields
private static final boolean DEBUG_TEXT = false;//"true".equalsIgnoreCase(Platform.getDebugOption(FormUtil.DEBUG_TEXT));
private static final boolean DEBUG_TEXTSIZE = false;//"true".equalsIgnoreCase(Platform.getDebugOption(FormUtil.DEBUG_TEXTSIZE));
private static final boolean DEBUG_FOCUS = false;//"true".equalsIgnoreCase(Platform.getDebugOption(FormUtil.DEBUG_FOCUS));
private boolean hasFocus;
private boolean paragraphsSeparated = true;
private FormTextModel model;
private ListenerList<IHyperlinkListener> listeners;
private Hashtable<String, Object> resourceTable = new Hashtable<>();
private IHyperlinkSegment entered;
private IHyperlinkSegment armed;
private boolean mouseFocus = false;
private boolean controlFocusTransfer = false;
private boolean inSelection = false;
private SelectionData selData;
private static final String INTERNAL_MENU = "__internal_menu__"; //$NON-NLS-1$
private static final String CONTROL_KEY = "__segment__"; //$NON-NLS-1$
private class FormTextLayout extends Layout implements ILayoutExtension {
public FormTextLayout() {
}
@Override
public int computeMaximumWidth(Composite parent, boolean changed) {
return computeSize(parent, SWT.DEFAULT, SWT.DEFAULT, changed).x;
}
@Override
public int computeMinimumWidth(Composite parent, boolean changed) {
return computeSize(parent, 5, SWT.DEFAULT, true).x;
}
@Override
public Point computeSize(Composite composite, int wHint, int hHint,
boolean changed) {
long start = 0;
if (DEBUG_TEXT)
start = System.currentTimeMillis();
int innerWidth = wHint;
if (innerWidth != SWT.DEFAULT)
innerWidth -= marginWidth * 2;
Point textSize = computeTextSize(innerWidth);
int textWidth = textSize.x + 2 * marginWidth;
int textHeight = textSize.y + 2 * marginHeight;
Point result = new Point(textWidth, textHeight);
if (DEBUG_TEXT) {
long stop = System.currentTimeMillis();
System.out.println("FormText computeSize: " + (stop - start) //$NON-NLS-1$
+ "ms"); //$NON-NLS-1$
}
if (DEBUG_TEXTSIZE) {
System.out.println("FormText ("+model.getAccessibleText()+"), computeSize: wHint="+wHint+", result="+result); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return result;
}
private Point computeTextSize(int wHint) {
Paragraph[] paragraphs = model.getParagraphs();
GC gc = new GC(FormText.this);
gc.setFont(getFont());
Locator loc = new Locator();
int width = wHint != SWT.DEFAULT ? wHint : 0;
FontMetrics fm = gc.getFontMetrics();
int lineHeight = fm.getHeight();
boolean selectableInTheLastRow = false;
for (int i = 0; i < paragraphs.length; i++) {
Paragraph p = paragraphs[i];
if (i > 0 && getParagraphsSeparated()
&& p.getAddVerticalSpace())
loc.y += getParagraphSpacing(lineHeight);
loc.rowHeight = 0;
loc.indent = p.getIndent();
loc.x = p.getIndent();
ParagraphSegment[] segments = p.getSegments();
if (segments.length > 0) {
selectableInTheLastRow = false;
int pwidth = 0;
for (ParagraphSegment segment : segments) {
segment.advanceLocator(gc, wHint, loc, resourceTable, false);
if (wHint != SWT.DEFAULT) {
width = Math.max(width, loc.width);
} else {
pwidth = Math.max(pwidth, loc.width);
}
if (segment instanceof IFocusSelectable)
selectableInTheLastRow = true;
}
if (wHint == SWT.DEFAULT)
width = Math.max(width, pwidth);
loc.y += loc.rowHeight;
} else {
// empty new line
loc.y += lineHeight;
}
}
gc.dispose();
if (selectableInTheLastRow)
loc.y += 1;
return new Point(width, loc.y);
}
@Override
protected void layout(Composite composite, boolean flushCache) {
long start = 0;
if (DEBUG_TEXT) {
start = System.currentTimeMillis();
}
selData = null;
Rectangle carea = composite.getClientArea();
if (DEBUG_TEXTSIZE) {
System.out.println("FormText layout ("+model.getAccessibleText()+"), carea="+carea); //$NON-NLS-1$ //$NON-NLS-2$
}
GC gc = new GC(composite);
gc.setFont(getFont());
ensureBoldFontPresent(getFont());
gc.setForeground(getForeground());
gc.setBackground(getBackground());
Locator loc = new Locator();
loc.marginWidth = marginWidth;
loc.marginHeight = marginHeight;
loc.y = marginHeight;
FontMetrics fm = gc.getFontMetrics();
int lineHeight = fm.getHeight();
Paragraph[] paragraphs = model.getParagraphs();
IHyperlinkSegment selectedLink = getSelectedLink();
for (int i = 0; i < paragraphs.length; i++) {
Paragraph p = paragraphs[i];
if (i > 0 && paragraphsSeparated && p.getAddVerticalSpace())
loc.y += getParagraphSpacing(lineHeight);
loc.indent = p.getIndent();
loc.resetCaret();
loc.rowHeight = 0;
p.layout(gc, carea.width, loc, lineHeight, resourceTable,
selectedLink);
}
gc.dispose();
if (DEBUG_TEXT) {
long stop = System.currentTimeMillis();
System.out.println("FormText.layout: " + (stop - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
/**
* Contructs a new form text widget in the provided parent and using the
* styles.
* <p>
* The only valid style bit for <code>FormText</code> is <code>SWT.NO_FOCUS</code>.
* This will cause the widget to always refuse focus.
*
* @param parent
* form text parent control
* @param style
* the widget style
*/
public FormText(Composite parent, int style) {
super(parent, SWT.NO_BACKGROUND | SWT.WRAP | style);
setLayout(new FormTextLayout());
model = new FormTextModel();
addDisposeListener(e -> {
model.dispose();
disposeResourceTable(true);
});
addPaintListener(this::paint);
addListener(SWT.KeyDown, e -> {
if (e.character == '\r') {
activateSelectedLink();
return;
}
});
addListener(SWT.Traverse, e -> {
if (DEBUG_FOCUS)
System.out.println("Traversal: " + e); //$NON-NLS-1$
switch (e.detail) {
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
case SWT.TRAVERSE_ARROW_NEXT:
case SWT.TRAVERSE_ARROW_PREVIOUS:
e.doit = false;
return;
}
if (!model.hasFocusSegments()) {
e.doit = true;
return;
}
if (e.detail == SWT.TRAVERSE_TAB_NEXT)
e.doit = advance(true);
else if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)
e.doit = advance(false);
else if (e.detail != SWT.TRAVERSE_RETURN)
e.doit = true;
});
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (!hasFocus) {
hasFocus = true;
if (DEBUG_FOCUS) {
System.out.println("FormText: focus gained"); //$NON-NLS-1$
}
if (!mouseFocus && !controlFocusTransfer) {
handleFocusChange();
}
}
}
@Override
public void focusLost(FocusEvent e) {
if (DEBUG_FOCUS) {
System.out.println("FormText: focus lost"); //$NON-NLS-1$
}
if (hasFocus) {
hasFocus = false;
if (!controlFocusTransfer)
handleFocusChange();
}
}
});
addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(MouseEvent e) {
}
@Override
public void mouseDown(MouseEvent e) {
// select a link
handleMouseClick(e, true);
}
@Override
public void mouseUp(MouseEvent e) {
// activate a link
handleMouseClick(e, false);
}
});
addMouseTrackListener(new MouseTrackListener() {
@Override
public void mouseEnter(MouseEvent e) {
handleMouseMove(e);
}
@Override
public void mouseExit(MouseEvent e) {
if (entered != null) {
exitLink(entered, e.stateMask);
paintLinkHover(entered, false);
entered = null;
setCursor(null);
}
}
@Override
public void mouseHover(MouseEvent e) {
handleMouseHover(e);
}
});
addMouseMoveListener(this::handleMouseMove);
initAccessible();
ensureBoldFontPresent(getFont());
createMenu();
// we will handle traversal of controls, if any
setTabList(new Control[] {});
}
/**
* Test for focus.
*
* @return <samp>true </samp> if the widget has focus.
*/
public boolean getFocus() {
return hasFocus;
}
/**
* Test if the widget is currently processing the text it is about to
* render.
*
* @return <samp>true </samp> if the widget is still loading the text,
* <samp>false </samp> otherwise.
* @deprecated not used any more - returns <code>false</code>
*/
@Deprecated
public boolean isLoading() {
return false;
}
/**
* Returns the text that will be shown in the control while the real content
* is loading.
*
* @return loading text message
* @deprecated loading text is not used since 3.1
*/
@Deprecated
public String getLoadingText() {
return null;
}
/**
* Sets the text that will be shown in the control while the real content is
* loading. This is significant when content to render is loaded from the
* input stream that was created from a remote URL, and the time to load the
* entire content is nontrivial.
*
* @param loadingText
* loading text message
* @deprecated use setText(loadingText, false, false);
*/
@Deprecated
public void setLoadingText(String loadingText) {
setText(loadingText, false, false);
}
/**
* If paragraphs are separated, spacing will be added between them. Otherwise,
* new paragraphs will simply start on a new line with no spacing.
*
* @param value <code>true</code> if paragraphs are separated, <code>false
* </code> otherwise.
*/
public void setParagraphsSeparated(boolean value) {
paragraphsSeparated = value;
}
/**
* Tests if there is some inter-paragraph spacing.
*
* @return <samp>true </samp> if paragraphs are separated, <samp>false
* </samp> otherwise.
*/
public boolean getParagraphsSeparated() {
return paragraphsSeparated;
}
/**
* Registers the image referenced by the provided key.
* <p>
* For <samp>img </samp> tags, an object of a type <samp>Image </samp> must
* be registered using the key equivalent to the value of the <samp>href
* </samp> attribute used in the tag.
*
* @param key
* unique key that matches the value of the <samp>href </samp>
* attribute.
* @param image
* an object of a type <samp>Image </samp>.
*/
public void setImage(String key, Image image) {
resourceTable.put("i." + key, image); //$NON-NLS-1$
}
/**
* Registers the color referenced by the provided key.
* <p>
* For <samp>span </samp> tags, an object of a type <samp>Color </samp> must
* be registered using the key equivalent to the value of the <samp>color
* </samp> attribute.
*
* @param key
* unique key that matches the value of the <samp>color </samp>
* attribute.
* @param color
* an object of the type <samp>Color </samp> or <samp>null</samp>
* if the key needs to be cleared.
*/
public void setColor(String key, Color color) {
String fullKey = "c." + key; //$NON-NLS-1$
if (color == null)
resourceTable.remove(fullKey);
else
resourceTable.put(fullKey, color);
}
/**
* Registers the font referenced by the provided key.
* <p>
* For <samp>span </samp> tags, an object of a type <samp>Font </samp> must
* be registered using the key equivalent to the value of the <samp>font
* </samp> attribute.
*
* @param key
* unique key that matches the value of the <samp>font </samp>
* attribute.
* @param font
* an object of the type <samp>Font </samp> or <samp>null</samp>
* if the key needs to be cleared.
*/
public void setFont(String key, Font font) {
String fullKey = "f." + key; //$NON-NLS-1$
if (font == null)
resourceTable.remove(fullKey);
else
resourceTable.put(fullKey, font);
model.clearCache(fullKey);
}
/**
* Registers the control referenced by the provided key.
* <p>
* For <samp>control</samp> tags, an object of a type <samp>Control</samp>
* must be registered using the key equivalent to the value of the
* <samp>control</samp> attribute.
*
* @param key
* unique key that matches the value of the <samp>control</samp>
* attribute.
* @param control
* an object of the type <samp>Control</samp> or <samp>null</samp>
* if the existing control at the specified key needs to be
* removed.
* @since 3.1
*/
public void setControl(String key, Control control) {
String fullKey = "o." + key; //$NON-NLS-1$
if (control == null)
resourceTable.remove(fullKey);
else
resourceTable.put(fullKey, control);
}
/**
* Sets the font to use to render the default text (text that does not have
* special font property assigned). Bold font will be constructed from this
* font.
*
* @param font
* the default font to use
*/
@Override
public void setFont(Font font) {
super.setFont(font);
model.clearCache(null);
Font boldFont = (Font) resourceTable.get(FormTextModel.BOLD_FONT_ID);
if (boldFont != null) {
FormFonts.getInstance().markFinished(boldFont, getDisplay());
resourceTable.remove(FormTextModel.BOLD_FONT_ID);
}
ensureBoldFontPresent(getFont());
}
/**
* Sets the provided text. Text can be rendered as-is, or by parsing the
* formatting tags. Optionally, sections of text starting with http:// will
* be converted to hyperlinks.
*
* @param text
* the text to render
* @param parseTags
* if <samp>true </samp>, formatting tags will be parsed.
* Otherwise, text will be rendered as-is.
* @param expandURLs
* if <samp>true </samp>, URLs found in the untagged text will be
* converted into hyperlinks.
*/
public void setText(String text, boolean parseTags, boolean expandURLs) {
disposeResourceTable(false);
entered = null;
if (parseTags)
model.parseTaggedText(text, expandURLs);
else
model.parseRegularText(text, expandURLs);
hookControlSegmentFocus();
layout();
redraw();
}
/**
* Sets the contents of the stream. Optionally, URLs in untagged text can be
* converted into hyperlinks. The caller is responsible for closing the
* stream.
*
* @param is
* stream to render
* @param expandURLs
* if <samp>true </samp>, URLs found in untagged text will be
* converted into hyperlinks.
*/
public void setContents(InputStream is, boolean expandURLs) {
entered = null;
disposeResourceTable(false);
model.parseInputStream(is, expandURLs);
hookControlSegmentFocus();
layout();
redraw();
}
private void hookControlSegmentFocus() {
Paragraph[] paragraphs = model.getParagraphs();
if (paragraphs == null)
return;
Listener listener = e -> {
switch (e.type) {
case SWT.FocusIn:
if (!controlFocusTransfer)
syncControlSegmentFocus((Control) e.widget);
break;
case SWT.Traverse:
if (DEBUG_FOCUS)
System.out.println("Control traversal: " + e); //$NON-NLS-1$
switch (e.detail) {
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
case SWT.TRAVERSE_ARROW_NEXT:
case SWT.TRAVERSE_ARROW_PREVIOUS:
e.doit = false;
return;
}
Control c = (Control) e.widget;
ControlSegment segment = (ControlSegment) c.getData(CONTROL_KEY);
if (e.detail == SWT.TRAVERSE_TAB_NEXT)
e.doit = advanceControl(c, segment, true);
else if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)
e.doit = advanceControl(c, segment, false);
if (!e.doit)
e.detail = SWT.TRAVERSE_NONE;
break;
}
};
for (Paragraph p : paragraphs) {
ParagraphSegment[] segments = p.getSegments();
for (ParagraphSegment segment : segments) {
if (segment instanceof ControlSegment) {
ControlSegment cs = (ControlSegment) segment;
Control c = cs.getControl(resourceTable);
if (c != null) {
if (c.getData(CONTROL_KEY) == null) {
// first time - hook
c.setData(CONTROL_KEY, cs);
attachTraverseListener(c, listener);
}
}
}
}
}
}
private void attachTraverseListener(Control c, Listener listener) {
if (c instanceof Composite) {
Composite parent = (Composite) c;
Control[] children = parent.getChildren();
for (Control element : children) {
attachTraverseListener(element, listener);
}
if (c instanceof Canvas) {
// If Canvas, the control iteself can accept
// traverse events and should be monitored
c.addListener(SWT.Traverse, listener);
c.addListener(SWT.FocusIn, listener);
}
} else {
c.addListener(SWT.Traverse, listener);
c.addListener(SWT.FocusIn, listener);
}
}
/**
* If we click on the control randomly, our internal book-keeping will be
* off. We need to update the model and mark the control segment and
* currently selected. Hyperlink that may have had focus must also be
* exited.
*
* @param control
* the control that got focus
*/
private void syncControlSegmentFocus(Control control) {
ControlSegment cs = null;
while (control != null) {
cs = (ControlSegment) control.getData(CONTROL_KEY);
if (cs != null)
break;
control = control.getParent();
}
if (cs == null)
return;
IFocusSelectable current = model.getSelectedSegment();
// If the model and the control match, all is well
if (current == cs)
return;
IHyperlinkSegment oldLink = null;
if (current != null && current instanceof IHyperlinkSegment) {
oldLink = (IHyperlinkSegment) current;
exitLink(oldLink, SWT.NULL);
}
if (DEBUG_FOCUS)
System.out.println("Sync control: " + cs + ", oldLink=" + oldLink); //$NON-NLS-1$ //$NON-NLS-2$
model.select(cs);
if (oldLink != null)
paintFocusTransfer(oldLink, null);
// getAccessible().setFocus(model.getSelectedSegmentIndex());
}
private boolean advanceControl(Control c, ControlSegment segment,
boolean next) {
Composite parent = c.getParent();
if (parent == this) {
// segment-level control
IFocusSelectable nextSegment = model.getNextFocusSegment(next);
if (nextSegment != null) {
controlFocusTransfer = true;
super.forceFocus();
controlFocusTransfer = false;
model.select(segment);
return advance(next);
}
// nowhere to go
return setFocusToNextSibling(this, next);
}
if (setFocusToNextSibling(c, next))
return true;
// still here - must go one level up
segment = (ControlSegment) parent.getData(CONTROL_KEY);
return advanceControl(parent, segment, next);
}
private boolean setFocusToNextSibling(Control c, boolean next) {
Composite parent = c.getParent();
Control[] children = parent.getTabList();
for (int i = 0; i < children.length; i++) {
Control child = children[i];
if (child == c) {
// here
if (next) {
for (int j = i + 1; j < children.length; j++) {
Control nc = children[j];
if (nc.setFocus())
return false;
}
} else {
for (int j = i - 1; j >= 0; j--) {
Control pc = children[j];
if (pc.setFocus())
return false;
}
}
}
}
return false;
}
/**
* Controls whether whitespace inside paragraph and list items is normalized.
* Note that the new value will not affect the current text in the control, only
* subsequent calls to <code>setText</code> or <code>setContents</code>.
* <p>
* If normalized:
* </p>
* <ul>
* <li>all white space characters will be condensed into at most one when
* between words.</li>
* <li>new line characters will be ignored and replaced with one white space
* character</li>
* <li>white space characters after the opening tags and before the closing tags
* will be trimmed</li>
* </ul>
*
* @param value <code>true</code> if whitespace is normalized,
* <code>false</code> otherwise.
*/
public void setWhitespaceNormalized(boolean value) {
model.setWhitespaceNormalized(value);
}
/**
* Tests whether whitespace inside paragraph and list item is normalized.
*
* @see #setWhitespaceNormalized(boolean)
* @return <code>true</code> if whitespace is normalized,
* <code>false</code> otherwise.
*/
public boolean isWhitespaceNormalized() {
return model.isWhitespaceNormalized();
}
/**
* Disposes the internal menu if created and sets the menu provided as a
* parameter.
*
* @param menu
* the menu to associate with this text control
*/
@Override
public void setMenu(Menu menu) {
Menu currentMenu = super.getMenu();
if (currentMenu != null && INTERNAL_MENU.equals(currentMenu.getData())) {
// internal menu set
if (menu != null) {
currentMenu.dispose();
super.setMenu(menu);
}
} else
super.setMenu(menu);
}
private void createMenu() {
Menu menu = new Menu(this);
final MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
copyItem.setText(Messages.FormText_copy);
SelectionListener listener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (e.widget == copyItem) {
copy();
}
}
};
copyItem.addSelectionListener(listener);
menu.addMenuListener(new MenuListener() {
@Override
public void menuShown(MenuEvent e) {
copyItem.setEnabled(canCopy());
}
@Override
public void menuHidden(MenuEvent e) {
}
});
menu.setData(INTERNAL_MENU);
super.setMenu(menu);
}
/**
* Returns the hyperlink settings that are in effect for this control.
*
* @return current hyperlinks settings
*/
public HyperlinkSettings getHyperlinkSettings() {
return model.getHyperlinkSettings();
}
/**
* Sets the hyperlink settings to be used for this control. Settings will
* affect things like hyperlink color, rendering style, cursor etc.
*
* @param settings
* hyperlink settings for this control
*/
public void setHyperlinkSettings(HyperlinkSettings settings) {
model.setHyperlinkSettings(settings);
}
/**
* Adds a listener that will handle hyperlink events.
*
* @param listener
* the listener to add
*/
public void addHyperlinkListener(IHyperlinkListener listener) {
if (listeners == null)
listeners = new ListenerList<>();
listeners.add(listener);
}
/**
* Removes the hyperlink listener.
*
* @param listener
* the listener to remove
*/
public void removeHyperlinkListener(IHyperlinkListener listener) {
if (listeners == null)
return;
listeners.remove(listener);
}
/**
* Adds a selection listener. A Selection event is sent by the widget when
* the selection has changed.
* <p>
* <code>widgetDefaultSelected</code> is not called for FormText.
* </p>
*
* @param listener
* the listener
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT when listener is null</li>
* </ul>
* @since 3.1
*/
public void addSelectionListener(SelectionListener listener) {
addTypedListener(listener, SWT.Selection);
}
/**
* Removes the specified selection listener.