forked from FXMisc/RichTextFX
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStyledTextArea.java
More file actions
135 lines (123 loc) · 6.26 KB
/
StyledTextArea.java
File metadata and controls
135 lines (123 loc) · 6.26 KB
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
package org.fxmisc.richtext;
import java.util.function.BiConsumer;
import javafx.beans.NamedArg;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.text.TextFlow;
import org.fxmisc.richtext.model.EditableStyledDocument;
import org.fxmisc.richtext.model.SegmentOps;
import org.fxmisc.richtext.model.SimpleEditableStyledDocument;
import org.fxmisc.richtext.model.StyledSegment;
import org.fxmisc.richtext.model.TextOps;
/**
* Text editing control. Accepts user input (keyboard, mouse) and
* provides API to assign style to text ranges. It is suitable for
* syntax highlighting and rich-text editors.
*
* <p>Subclassing is allowed to define the type of style, e.g. inline
* style or style classes.</p>
*
* <p>Note: Scroll bars no longer appear when the content spans outside
* of the viewport. To add scroll bars, the area needs to be wrapped in
* a {@link org.fxmisc.flowless.VirtualizedScrollPane}. For example, </p>
* <pre>
* {@code
* // shows area without scroll bars
* InlineCssTextArea area = new InlineCssTextArea();
*
* // add scroll bars that will display as needed
* VirtualizedScrollPane<InlineCssTextArea> vsPane = new VirtualizedScrollPane(area);
*
* Parent parent = //;
* parent.getChildren().add(vsPane)
* }
* </pre>
*
* <h3>Overriding keyboard shortcuts</h3>
*
* {@code StyledTextArea} uses {@code KEY_TYPED} handler to handle ordinary
* character input and {@code KEY_PRESSED} handler to handle control key
* combinations (including Enter and Tab). To add or override some keyboard
* shortcuts, while keeping the rest in place, you would combine the default
* event handler with a new one that adds or overrides some of the default
* key combinations. This is how to bind {@code Ctrl+S} to the {@code save()}
* operation:
* <pre>
* {@code
* import static javafx.scene.input.KeyCode.*;
* import static javafx.scene.input.KeyCombination.*;
* import static org.fxmisc.wellbehaved.event.EventPattern.*;
* import static org.fxmisc.wellbehaved.event.InputMap.*;
*
* import org.fxmisc.wellbehaved.event.Nodes;
*
* Nodes.addInputMap(area, consume(keyPressed(S, CONTROL_DOWN), event -> save()));
* }
* </pre>
*
* @param <S> type of style that can be applied to text.
*/
public class StyledTextArea<PS, S> extends GenericStyledArea<PS, String, S> {
public StyledTextArea(@NamedArg("initialParagraphStyle") PS initialParagraphStyle,
@NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle,
@NamedArg("initialTextStyle") S initialTextStyle,
@NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle,
@NamedArg("document") EditableStyledDocument<PS, String, S> document,
@NamedArg("segmentOps") TextOps<String, S> segmentOps,
@NamedArg("preserveStyle") boolean preserveStyle) {
super(initialParagraphStyle, applyParagraphStyle,
initialTextStyle, document, segmentOps, preserveStyle,
seg -> createStyledTextNode(seg, applyStyle)
);
}
public StyledTextArea(@NamedArg("initialParagraphStyle") PS initialParagraphStyle,
@NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle,
@NamedArg("initialTextStyle") S initialTextStyle,
@NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle,
@NamedArg("document") EditableStyledDocument<PS, String, S> document,
@NamedArg("preserveStyle") boolean preserveStyle) {
this(initialParagraphStyle, applyParagraphStyle,
initialTextStyle, applyStyle,
document, SegmentOps.styledTextOps(), preserveStyle);
}
public StyledTextArea(@NamedArg("initialParagraphStyle") PS initialParagraphStyle,
@NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle,
@NamedArg("initialTextStyle") S initialTextStyle,
@NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle,
@NamedArg("document") EditableStyledDocument<PS, String, S> document) {
this(initialParagraphStyle, applyParagraphStyle,
initialTextStyle, applyStyle, document, true);
}
public StyledTextArea(@NamedArg("initialParagraphStyle") PS initialParagraphStyle,
@NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle,
@NamedArg("initialTextStyle") S initialTextStyle,
@NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle,
@NamedArg("preserveStyle") boolean preserveStyle) {
this(
initialParagraphStyle,
applyParagraphStyle,
initialTextStyle,
applyStyle,
new SimpleEditableStyledDocument<>(initialParagraphStyle, initialTextStyle),
preserveStyle);
}
public StyledTextArea(@NamedArg("initialParagraphStyle") PS initialParagraphStyle,
@NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle,
@NamedArg("initialTextStyle") S initialTextStyle,
@NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle) {
this(initialParagraphStyle, applyParagraphStyle,
initialTextStyle, applyStyle, true);
}
public static <S> Node createStyledTextNode(StyledSegment<String, S> seg,
BiConsumer<? super TextExt, S> applyStyle) {
return createStyledTextNode(seg.getSegment(), seg.getStyle(), applyStyle);
}
public static <S> Node createStyledTextNode(String text, S style,
BiConsumer<? super TextExt, S> applyStyle) {
TextExt t = new TextExt(text);
t.setTextOrigin(VPos.TOP);
t.getStyleClass().add("text");
applyStyle.accept(t, style);
return t;
}
}