Skip to content

Commit b8f563d

Browse files
authored
RichTextDemo Bullet Lists (#850)
Added Indent and BulletFactory and updated ParStyle to accommodate Indent
1 parent b8c87ee commit b8f563d

File tree

7 files changed

+127
-10
lines changed

7 files changed

+127
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.fxmisc.richtext.demo.richtext;
2+
3+
import java.util.function.IntFunction;
4+
5+
import org.fxmisc.richtext.GenericStyledArea;
6+
7+
import javafx.geometry.Insets;
8+
import javafx.scene.Node;
9+
import javafx.scene.control.ContentDisplay;
10+
import javafx.scene.control.Label;
11+
import javafx.scene.layout.VBox;
12+
import javafx.scene.paint.Color;
13+
import javafx.scene.shape.Circle;
14+
import javafx.scene.shape.Rectangle;
15+
16+
public class BulletFactory implements IntFunction<Node>
17+
{
18+
private GenericStyledArea<ParStyle,?,?> area;
19+
20+
public BulletFactory( GenericStyledArea<ParStyle,?,?> area )
21+
{
22+
this.area = area;
23+
}
24+
25+
@Override
26+
public Node apply( int value )
27+
{
28+
if ( value < 0 ) return null;
29+
30+
ParStyle ps = area.getParagraph( value ).getParagraphStyle();
31+
if ( ! ps.indent.isPresent() ) return null;
32+
33+
return createBullet( ps.indent.get() );
34+
}
35+
36+
private Node createBullet(Indent in) {
37+
Node result;
38+
switch( in.level ) {
39+
case 1 : {
40+
Circle c = new Circle(2.5);
41+
c.setFill(Color.BLACK);
42+
c.setStroke(Color.BLACK);
43+
result = c;
44+
}
45+
break;
46+
47+
case 2 : {
48+
Circle c = new Circle(2.5);
49+
c.setFill(Color.WHITE);
50+
c.setStroke(Color.BLACK);
51+
result = c;
52+
}
53+
break;
54+
55+
case 3 : {
56+
Rectangle r = new Rectangle(5, 5);
57+
r.setFill(Color.BLACK);
58+
r.setStroke(Color.BLACK);
59+
result = r;
60+
}
61+
break;
62+
63+
default : {
64+
Rectangle r = new Rectangle(5, 5);
65+
r.setFill(Color.WHITE);
66+
r.setStroke(Color.BLACK);
67+
result = r;
68+
}
69+
break;
70+
}
71+
72+
Label l = new Label( " ", result );
73+
l.setPadding( new Insets( 0, 0, 0, in.level*in.width ) );
74+
l.setContentDisplay( ContentDisplay.LEFT );
75+
return new VBox( 0, l );
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.fxmisc.richtext.demo.richtext;
2+
3+
public class Indent
4+
{
5+
double width = 15;
6+
int level = 1;
7+
}

Diff for: richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/ParStyle.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,20 @@ public ParStyle decode(DataInputStream is) throws IOException {
5555

5656
final Optional<TextAlignment> alignment;
5757
final Optional<Color> backgroundColor;
58+
final Optional<Indent> indent;
5859

5960
public ParStyle() {
60-
this(Optional.empty(), Optional.empty());
61+
this(Optional.empty(), Optional.empty(), Optional.empty());
6162
}
6263

6364
public ParStyle(Optional<TextAlignment> alignment, Optional<Color> backgroundColor) {
65+
this(alignment, backgroundColor, Optional.empty());
66+
}
67+
68+
public ParStyle(Optional<TextAlignment> alignment, Optional<Color> backgroundColor, Optional<Indent> indent) {
6469
this.alignment = alignment;
6570
this.backgroundColor = backgroundColor;
71+
this.indent = indent;
6672
}
6773

6874
@Override
@@ -111,15 +117,33 @@ public String toCss() {
111117
public ParStyle updateWith(ParStyle mixin) {
112118
return new ParStyle(
113119
mixin.alignment.isPresent() ? mixin.alignment : alignment,
114-
mixin.backgroundColor.isPresent() ? mixin.backgroundColor : backgroundColor);
120+
mixin.backgroundColor.isPresent() ? mixin.backgroundColor : backgroundColor,
121+
mixin.indent.isPresent() ? mixin.indent : indent );
115122
}
116123

117124
public ParStyle updateAlignment(TextAlignment alignment) {
118-
return new ParStyle(Optional.of(alignment), backgroundColor);
125+
return new ParStyle(Optional.of(alignment), backgroundColor, indent);
119126
}
120127

121128
public ParStyle updateBackgroundColor(Color backgroundColor) {
122-
return new ParStyle(alignment, Optional.of(backgroundColor));
129+
return new ParStyle(alignment, Optional.of(backgroundColor), indent);
130+
}
131+
132+
public ParStyle updateIndent(Indent indent) {
133+
return new ParStyle(alignment, backgroundColor, Optional.ofNullable(indent));
134+
}
135+
136+
public ParStyle increaseIndent() {
137+
if ( indent.isPresent() ) indent.get().level++;
138+
else return updateIndent( new Indent() );
139+
return this;
140+
}
141+
142+
public ParStyle decreaseIndent() {
143+
if ( indent.isPresent() && --indent.get().level == 0 ) {
144+
return updateIndent( null );
145+
}
146+
return this;
123147
}
124148

125149
}

Diff for: richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichTextDemo.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static void main(String[] args) {
8787
area.setStyleCodecs(
8888
ParStyle.CODEC,
8989
Codec.styledSegmentCodec(Codec.eitherCodec(Codec.STRING_CODEC, LinkedImage.codec()), TextStyle.CODEC));
90+
area.setParagraphGraphicFactory( new BulletFactory( area ) );
9091
}
9192

9293
private Stage mainStage;
@@ -118,6 +119,8 @@ public void start(Stage primaryStage) {
118119
Button underlineBtn = createButton("underline", this::toggleUnderline, "Underline");
119120
Button strikeBtn = createButton("strikethrough", this::toggleStrikethrough, "Strike Trough");
120121
Button insertImageBtn = createButton("insertimage", this::insertImage, "Insert Image");
122+
Button increaseIndentBtn = createButton("increaseIndent", this::increaseIndent, "Increase indent");
123+
Button decreaseIndentBtn = createButton("decreaseIndent", this::decreaseIndent, "Decrease indent");
121124
ToggleGroup alignmentGrp = new ToggleGroup();
122125
ToggleButton alignLeftBtn = createToggleButton(alignmentGrp, "align-left", this::alignLeft, "Align left");
123126
ToggleButton alignCenterBtn = createToggleButton(alignmentGrp, "align-center", this::alignCenter, "Align center");
@@ -282,6 +285,7 @@ undoBtn, redoBtn, new Separator(Orientation.VERTICAL),
282285
cutBtn, copyBtn, pasteBtn, new Separator(Orientation.VERTICAL),
283286
boldBtn, italicBtn, underlineBtn, strikeBtn, new Separator(Orientation.VERTICAL),
284287
alignLeftBtn, alignCenterBtn, alignRightBtn, alignJustifyBtn, new Separator(Orientation.VERTICAL),
288+
increaseIndentBtn, decreaseIndentBtn, new Separator(Orientation.VERTICAL),
285289
insertImageBtn, new Separator(Orientation.VERTICAL),
286290
paragraphBackgroundPicker);
287291

@@ -309,11 +313,6 @@ private Node createNode(StyledSegment<Either<String, LinkedImage>, TextStyle> se
309313
);
310314
}
311315

312-
@Deprecated
313-
private Button createButton(String styleClass, Runnable action) {
314-
return createButton(styleClass, action, null);
315-
}
316-
317316
private Button createButton(String styleClass, Runnable action, String toolTip) {
318317
Button button = new Button();
319318
button.getStyleClass().add(styleClass);
@@ -465,6 +464,14 @@ private void insertImage() {
465464
}
466465
}
467466

467+
private void increaseIndent() {
468+
updateParagraphStyleInSelection( ps -> ps.increaseIndent() );
469+
}
470+
471+
private void decreaseIndent() {
472+
updateParagraphStyleInSelection( ps -> ps.decreaseIndent() );
473+
}
474+
468475
private void updateStyleInSelection(Function<StyleSpans<TextStyle>, TextStyle> mixinGetter) {
469476
IndexRange selection = area.getSelection();
470477
if(selection.getLength() != 0) {
Loading
Loading

Diff for: richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/rich-text.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
.button.underline { -fx-background-image: url(underline.png); }
1818
.button.strikethrough { -fx-background-image: url(strikethrough.png); }
1919
.button.insertimage { -fx-background-image: url(insertimage.png); }
20+
.button.increaseIndent { -fx-background-image: url(increaseIndent.png); }
21+
.button.decreaseIndent { -fx-background-image: url(decreaseIndent.png); }
2022

2123
.toggle-button.align-left { -fx-background-image: url(align-left.png); }
2224
.toggle-button.align-center { -fx-background-image: url(align-center.png); }
2325
.toggle-button.align-right { -fx-background-image: url(align-right.png); }
24-
.toggle-button.align-justify { -fx-background-image: url(align-justify.png); }
26+
.toggle-button.align-justify { -fx-background-image: url(align-justify.png); }

0 commit comments

Comments
 (0)