-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomSegmentDemo.java
More file actions
165 lines (116 loc) · 5.92 KB
/
CustomSegmentDemo.java
File metadata and controls
165 lines (116 loc) · 5.92 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
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
//package lightware.richtext;
import javafx.application.Platform;
import org.fxmisc.flowless.VirtualizedScrollPane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.fxmisc.richtext.model.ReadOnlyStyledDocument;
import org.fxmisc.richtext.model.ReadOnlyStyledDocumentBuilder;
import org.fxmisc.richtext.model.StyledSegment;
import org.fxmisc.richtext.model.TextOps;
import java.util.ArrayList;
import java.util.List;
/**
* This demo shows how to register custom objects with the RichTextFX editor.
* It creates a sample document with some text and a custom node.
*/
public class CustomSegmentDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Function<AbstractSegment,Node> nodeFactory = seg -> seg.createNode();
StyledSegmentTextArea textArea = new StyledSegmentTextArea();
// flip this boolean to execute one or the other
if (false) {
textArea.replaceText(0, 0, "");
// all in one paragraph
textArea.appendText("text ");
textArea.appendText("hyperlink ");
//textArea.append( new LabelSegment("label") );
textArea.append( new HyperlinkSegment("hyperlink1", "c:\\temp\\abc.html") );
textArea.append(new TextSegment(" end "));
textArea.appendText("hyperlink ");
textArea.append( new HyperlinkSegment("hyperlink2", "c:\\temp\\abc.html") );
textArea.appendText(" end ");
textArea.appendText("image ");
textArea.append( new ImageSegment("picture.jpg") );
textArea.appendText(" end ");
textArea.appendText("hyperlink ");
textArea.append( new HyperlinkSegment("hyperlink3", "c:\\temp\\abc.html") );
textArea.appendText(" end ");
}
else {
final TextOps<AbstractSegment, String> segmentOps = new MySegmentOps();
ReadOnlyStyledDocumentBuilder<String, AbstractSegment, String> builder =
new ReadOnlyStyledDocumentBuilder<>(segmentOps, "");
ReadOnlyStyledDocument<String, AbstractSegment, String> doc = null;
AbstractSegment segment = null;
String style = null;
StyledSegment<AbstractSegment, String> styledSegment = null;
List<StyledSegment<AbstractSegment, String>> styledSegments = new ArrayList<>();
// paragraphs
segment = new TextSegment("text paragraph");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
builder.addParagraph(styledSegments);
styledSegments = new ArrayList<>();
segment = new TextSegment("hyperlink paragraph");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new HyperlinkSegment("hyperlink1", "c:\\temp\\abc.html");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new TextSegment("end");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
builder.addParagraph(styledSegments);
styledSegments = new ArrayList<>();
segment = new TextSegment("hyperlink paragraph");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
// this hyperlink is corrupted:
// - mouse-hovering over it produces lots of calls to createNode (see stdout)
// - hovering does not cause the link to become underlined
// - clicking the link does not execute its handler
segment = new HyperlinkSegment("hyperlink2", "c:\\temp\\abc.html");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new TextSegment("end");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
builder.addParagraph(styledSegments);
styledSegments = new ArrayList<>();
segment = new TextSegment("image paragraph");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new ImageSegment("picture.jpg");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new TextSegment("end");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
builder.addParagraph(styledSegments);
styledSegments = new ArrayList<>();
segment = new TextSegment("hyperlink paragraph");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new HyperlinkSegment("hyperlink3", "c:\\temp\\abc.html");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
segment = new TextSegment("end");
styledSegment = new StyledSegment<>(segment, "");
styledSegments.add(styledSegment);
builder.addParagraph(styledSegments);
styledSegments = new ArrayList<>();
doc = builder.build();
textArea.replaceSelection(doc);
}
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(textArea)), 600, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Custom Object demo");
primaryStage.show();
}
}