Skip to content

Commit 03612e1

Browse files
author
Andy Goryachev
committed
8372438: SimpleViewOnlyStyledModel: non-text paragraphs
Reviewed-by: zelmidaoui, lkostyra
1 parent 9e1c70c commit 03612e1

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

modules/jfx.incubator.richtext/src/main/java/jfx/incubator/scene/control/richtext/model/SimpleViewOnlyStyledModel.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public SimpleViewOnlyStyledModel addSegment(String text) {
120120
public SimpleViewOnlyStyledModel addWithInlineAndStyleNames(String text, String style, String... css) {
121121
Objects.requireNonNull(text);
122122
StyleAttributeMap a = StyleAttributeMap.fromStyles(style, css);
123-
Paragraph p = lastParagraph();
123+
Paragraph p = textParagraph();
124124
p.addSegment(text, a);
125125
return this;
126126
}
@@ -136,7 +136,7 @@ public SimpleViewOnlyStyledModel addWithInlineAndStyleNames(String text, String
136136
public SimpleViewOnlyStyledModel addWithStyleNames(String text, String... css) {
137137
Objects.requireNonNull(text);
138138
StyleAttributeMap a = StyleAttributeMap.fromStyles(null, css);
139-
Paragraph p = lastParagraph();
139+
Paragraph p = textParagraph();
140140
p.addSegment(text, a);
141141
return this;
142142
}
@@ -152,7 +152,7 @@ public SimpleViewOnlyStyledModel addWithStyleNames(String text, String... css) {
152152
public SimpleViewOnlyStyledModel addWithInlineStyle(String text, String style) {
153153
Objects.requireNonNull(text);
154154
StyleAttributeMap a = StyleAttributeMap.fromInlineStyle(style);
155-
Paragraph p = lastParagraph();
155+
Paragraph p = textParagraph();
156156
p.addSegment(text, a);
157157
return this;
158158
}
@@ -168,7 +168,7 @@ public SimpleViewOnlyStyledModel addWithInlineStyle(String text, String style) {
168168
public SimpleViewOnlyStyledModel addSegment(String text, StyleAttributeMap a) {
169169
// TODO split into paragraphs if \n is found, or check for \n ?
170170
Objects.requireNonNull(a);
171-
Paragraph p = lastParagraph();
171+
Paragraph p = textParagraph();
172172
p.addSegment(text, a);
173173
return this;
174174
}
@@ -182,7 +182,7 @@ public SimpleViewOnlyStyledModel addSegment(String text, StyleAttributeMap a) {
182182
* @return this model instance
183183
*/
184184
public SimpleViewOnlyStyledModel highlight(int start, int length, Color c) {
185-
Paragraph p = lastParagraph();
185+
Paragraph p = textParagraph();
186186
p.addHighlight(start, length, c);
187187
return this;
188188
}
@@ -198,7 +198,7 @@ public SimpleViewOnlyStyledModel highlight(int start, int length, Color c) {
198198
* @since 25
199199
*/
200200
public SimpleViewOnlyStyledModel highlight(int start, int length, String ... css) {
201-
Paragraph p = lastParagraph();
201+
Paragraph p = textParagraph();
202202
p.addHighlight(start, length, css);
203203
return this;
204204
}
@@ -212,7 +212,7 @@ public SimpleViewOnlyStyledModel highlight(int start, int length, String ... css
212212
* @return this model instance
213213
*/
214214
public SimpleViewOnlyStyledModel addWavyUnderline(int start, int length, Color c) {
215-
Paragraph p = lastParagraph();
215+
Paragraph p = textParagraph();
216216
p.addSquiggly(start, length, c);
217217
return this;
218218
}
@@ -228,19 +228,24 @@ public SimpleViewOnlyStyledModel addWavyUnderline(int start, int length, Color c
228228
* @since 25
229229
*/
230230
public SimpleViewOnlyStyledModel addWavyUnderline(int start, int length, String ... css) {
231-
Paragraph p = lastParagraph();
231+
Paragraph p = textParagraph();
232232
p.addSquiggly(start, length, css);
233233
return this;
234234
}
235235

236-
private Paragraph lastParagraph() {
236+
// returns the last text paragraph, adding one if necessary
237+
private Paragraph textParagraph() {
237238
int sz = paragraphs.size();
238-
if (sz == 0) {
239-
Paragraph p = new Paragraph();
240-
paragraphs.add(p);
241-
return p;
239+
if (sz > 0) {
240+
Paragraph p = paragraphs.get(sz - 1);
241+
if (p.isTextParagraph()) {
242+
return p;
243+
}
244+
// not a text paragraph, create new
242245
}
243-
return paragraphs.get(sz - 1);
246+
Paragraph p = new Paragraph();
247+
paragraphs.add(p);
248+
return p;
244249
}
245250

246251
/**
@@ -293,7 +298,7 @@ public SimpleViewOnlyStyledModel addParagraph(Supplier<Region> generator) {
293298
* @return this model instance
294299
*/
295300
public SimpleViewOnlyStyledModel addNodeSegment(Supplier<Node> generator) {
296-
Paragraph p = lastParagraph();
301+
Paragraph p = textParagraph();
297302
p.addInlineNode(generator);
298303
return this;
299304
}
@@ -345,7 +350,7 @@ public StyleAttributeMap getStyleAttributeMap(StyleResolver r, TextPos pos) {
345350
* @return this model instance
346351
*/
347352
public SimpleViewOnlyStyledModel setParagraphAttributes(StyleAttributeMap a) {
348-
Paragraph p = lastParagraph();
353+
Paragraph p = textParagraph();
349354
p.setParagraphAttributes(a);
350355
return this;
351356
}
@@ -359,6 +364,10 @@ static class Paragraph {
359364
public Paragraph() {
360365
}
361366

367+
public boolean isTextParagraph() {
368+
return true;
369+
}
370+
362371
public static Paragraph of(Supplier<Region> paragraphGenerator) {
363372
return new Paragraph() {
364373
@Override
@@ -376,6 +385,11 @@ public void export(int start, int end, StyledOutput out) throws IOException {
376385
StyledSegment seg = StyledSegment.ofRegion(paragraphGenerator);
377386
out.consume(seg);
378387
}
388+
389+
@Override
390+
public boolean isTextParagraph() {
391+
return false;
392+
}
379393
};
380394
}
381395

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.jfx.incubator.scene.control.richtext.model;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import javafx.scene.layout.Region;
30+
import org.junit.jupiter.api.Test;
31+
import jfx.incubator.scene.control.richtext.model.SimpleViewOnlyStyledModel;
32+
33+
/**
34+
* Tests SimpleViewOnlyStyledModel.
35+
*/
36+
public class TestSimpleViewOnlyStyledModel {
37+
@Test
38+
public void addTextAfterRegion() {
39+
SimpleViewOnlyStyledModel m = new SimpleViewOnlyStyledModel();
40+
m.addParagraph(() -> new Region());
41+
m.addNodeSegment(() -> new Region());
42+
assertEquals(2, m.size());
43+
}
44+
45+
@Test
46+
public void addTextAfterRegionAfterText() {
47+
SimpleViewOnlyStyledModel m = new SimpleViewOnlyStyledModel();
48+
m.addNodeSegment(() -> new Region());
49+
m.addParagraph(() -> new Region());
50+
m.addSegment("text");
51+
assertEquals(3, m.size());
52+
}
53+
}

0 commit comments

Comments
 (0)