From b8f563d9d84c3c2afb8120481dc3e4715b6c1e41 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Tue, 19 Nov 2019 08:23:28 +0200 Subject: [PATCH] RichTextDemo Bullet Lists (#850) Added Indent and BulletFactory and updated ParStyle to accommodate Indent --- .../richtext/demo/richtext/BulletFactory.java | 77 ++++++++++++++++++ .../fxmisc/richtext/demo/richtext/Indent.java | 7 ++ .../richtext/demo/richtext/ParStyle.java | 32 +++++++- .../richtext/demo/richtext/RichTextDemo.java | 17 ++-- .../richtext/demo/richtext/decreaseIndent.png | Bin 0 -> 644 bytes .../richtext/demo/richtext/increaseIndent.png | Bin 0 -> 627 bytes .../richtext/demo/richtext/rich-text.css | 4 +- 7 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/BulletFactory.java create mode 100644 richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/Indent.java create mode 100644 richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/decreaseIndent.png create mode 100644 richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/increaseIndent.png diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/BulletFactory.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/BulletFactory.java new file mode 100644 index 000000000..14e341da5 --- /dev/null +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/BulletFactory.java @@ -0,0 +1,77 @@ +package org.fxmisc.richtext.demo.richtext; + +import java.util.function.IntFunction; + +import org.fxmisc.richtext.GenericStyledArea; + +import javafx.geometry.Insets; +import javafx.scene.Node; +import javafx.scene.control.ContentDisplay; +import javafx.scene.control.Label; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Rectangle; + +public class BulletFactory implements IntFunction +{ + private GenericStyledArea area; + + public BulletFactory( GenericStyledArea area ) + { + this.area = area; + } + + @Override + public Node apply( int value ) + { + if ( value < 0 ) return null; + + ParStyle ps = area.getParagraph( value ).getParagraphStyle(); + if ( ! ps.indent.isPresent() ) return null; + + return createBullet( ps.indent.get() ); + } + + private Node createBullet(Indent in) { + Node result; + switch( in.level ) { + case 1 : { + Circle c = new Circle(2.5); + c.setFill(Color.BLACK); + c.setStroke(Color.BLACK); + result = c; + } + break; + + case 2 : { + Circle c = new Circle(2.5); + c.setFill(Color.WHITE); + c.setStroke(Color.BLACK); + result = c; + } + break; + + case 3 : { + Rectangle r = new Rectangle(5, 5); + r.setFill(Color.BLACK); + r.setStroke(Color.BLACK); + result = r; + } + break; + + default : { + Rectangle r = new Rectangle(5, 5); + r.setFill(Color.WHITE); + r.setStroke(Color.BLACK); + result = r; + } + break; + } + + Label l = new Label( " ", result ); + l.setPadding( new Insets( 0, 0, 0, in.level*in.width ) ); + l.setContentDisplay( ContentDisplay.LEFT ); + return new VBox( 0, l ); + } +} diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/Indent.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/Indent.java new file mode 100644 index 000000000..0dbf20aba --- /dev/null +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/Indent.java @@ -0,0 +1,7 @@ +package org.fxmisc.richtext.demo.richtext; + +public class Indent +{ + double width = 15; + int level = 1; +} diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/ParStyle.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/ParStyle.java index 6057bd639..5a696d2c6 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/ParStyle.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/ParStyle.java @@ -55,14 +55,20 @@ public ParStyle decode(DataInputStream is) throws IOException { final Optional alignment; final Optional backgroundColor; + final Optional indent; public ParStyle() { - this(Optional.empty(), Optional.empty()); + this(Optional.empty(), Optional.empty(), Optional.empty()); } public ParStyle(Optional alignment, Optional backgroundColor) { + this(alignment, backgroundColor, Optional.empty()); + } + + public ParStyle(Optional alignment, Optional backgroundColor, Optional indent) { this.alignment = alignment; this.backgroundColor = backgroundColor; + this.indent = indent; } @Override @@ -111,15 +117,33 @@ public String toCss() { public ParStyle updateWith(ParStyle mixin) { return new ParStyle( mixin.alignment.isPresent() ? mixin.alignment : alignment, - mixin.backgroundColor.isPresent() ? mixin.backgroundColor : backgroundColor); + mixin.backgroundColor.isPresent() ? mixin.backgroundColor : backgroundColor, + mixin.indent.isPresent() ? mixin.indent : indent ); } public ParStyle updateAlignment(TextAlignment alignment) { - return new ParStyle(Optional.of(alignment), backgroundColor); + return new ParStyle(Optional.of(alignment), backgroundColor, indent); } public ParStyle updateBackgroundColor(Color backgroundColor) { - return new ParStyle(alignment, Optional.of(backgroundColor)); + return new ParStyle(alignment, Optional.of(backgroundColor), indent); + } + + public ParStyle updateIndent(Indent indent) { + return new ParStyle(alignment, backgroundColor, Optional.ofNullable(indent)); + } + + public ParStyle increaseIndent() { + if ( indent.isPresent() ) indent.get().level++; + else return updateIndent( new Indent() ); + return this; + } + + public ParStyle decreaseIndent() { + if ( indent.isPresent() && --indent.get().level == 0 ) { + return updateIndent( null ); + } + return this; } } diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichTextDemo.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichTextDemo.java index 349c32f60..fc7b2dcf6 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichTextDemo.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichTextDemo.java @@ -87,6 +87,7 @@ public static void main(String[] args) { area.setStyleCodecs( ParStyle.CODEC, Codec.styledSegmentCodec(Codec.eitherCodec(Codec.STRING_CODEC, LinkedImage.codec()), TextStyle.CODEC)); + area.setParagraphGraphicFactory( new BulletFactory( area ) ); } private Stage mainStage; @@ -118,6 +119,8 @@ public void start(Stage primaryStage) { Button underlineBtn = createButton("underline", this::toggleUnderline, "Underline"); Button strikeBtn = createButton("strikethrough", this::toggleStrikethrough, "Strike Trough"); Button insertImageBtn = createButton("insertimage", this::insertImage, "Insert Image"); + Button increaseIndentBtn = createButton("increaseIndent", this::increaseIndent, "Increase indent"); + Button decreaseIndentBtn = createButton("decreaseIndent", this::decreaseIndent, "Decrease indent"); ToggleGroup alignmentGrp = new ToggleGroup(); ToggleButton alignLeftBtn = createToggleButton(alignmentGrp, "align-left", this::alignLeft, "Align left"); ToggleButton alignCenterBtn = createToggleButton(alignmentGrp, "align-center", this::alignCenter, "Align center"); @@ -282,6 +285,7 @@ undoBtn, redoBtn, new Separator(Orientation.VERTICAL), cutBtn, copyBtn, pasteBtn, new Separator(Orientation.VERTICAL), boldBtn, italicBtn, underlineBtn, strikeBtn, new Separator(Orientation.VERTICAL), alignLeftBtn, alignCenterBtn, alignRightBtn, alignJustifyBtn, new Separator(Orientation.VERTICAL), + increaseIndentBtn, decreaseIndentBtn, new Separator(Orientation.VERTICAL), insertImageBtn, new Separator(Orientation.VERTICAL), paragraphBackgroundPicker); @@ -309,11 +313,6 @@ private Node createNode(StyledSegment, TextStyle> se ); } - @Deprecated - private Button createButton(String styleClass, Runnable action) { - return createButton(styleClass, action, null); - } - private Button createButton(String styleClass, Runnable action, String toolTip) { Button button = new Button(); button.getStyleClass().add(styleClass); @@ -465,6 +464,14 @@ private void insertImage() { } } + private void increaseIndent() { + updateParagraphStyleInSelection( ps -> ps.increaseIndent() ); + } + + private void decreaseIndent() { + updateParagraphStyleInSelection( ps -> ps.decreaseIndent() ); + } + private void updateStyleInSelection(Function, TextStyle> mixinGetter) { IndexRange selection = area.getSelection(); if(selection.getLength() != 0) { diff --git a/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/decreaseIndent.png b/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/decreaseIndent.png new file mode 100644 index 0000000000000000000000000000000000000000..4c67f82e9ab8bfc7ff660b723ddaeced05f3f491 GIT binary patch literal 644 zcmeAS@N?(olHy`uVBq!ia0vp^Vj#@H3?x5i&EaHVU~~!a332`Z|9@y`XafVoeg=kw zgoI26hQkaD?Fl14B?yP*G7)23R~K zB&4vg5NJ+8K>>sWO0_XCtY=`D1v3F?$RtLFR@qAhUbR8+6_m|+wxp@ZPXuFsy}$};9+X?BL%)kj~+cXR(fov@-*7}>C>mrl0%+l zMm@`md-m+v^TMR(C8^KLvtK9(y;Kl>S&{Yf<;&OgMQ`7}eJ3yZuD#~{`}ZFdB|h}E zewy0*`Sa&5ic;ScrN4jw{`2S0KY#xGQ&c=q)*%86R?U(izhDMt78U_PK@}BCTYG!= zfUvOE)+I|;ty;Z$bP0 Hl+XkKR9g8l literal 0 HcmV?d00001 diff --git a/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/increaseIndent.png b/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/richtext/increaseIndent.png new file mode 100644 index 0000000000000000000000000000000000000000..af845bc52ec2e2f6eabb1c4fa58ba3ed42790026 GIT binary patch literal 627 zcmeAS@N?(olHy`uVBq!ia0vp^Vj#@H3?x5i&EaHVU^ETz332`Z|38BRgF}M@zqg!C zyNyhYvQdMPRks<7y{Le%jAfUpWsez~t*~OMx=y){XqdcNn@LbmP*G74x2uFjk6B1a z2#_5b8d^|L0AxcLKoQ9}C4+iHwLC2WUulbOGcH#Nr3_X53SA&uCRWbA*BmG=;wJ_) zP%2JIAx%v+PfNQ@S2tZ4sLQn71gOliO2&VtV{mXV(Abb(fsm=@K*OW@IOArE$4_EU zn#!J>oSZV9J!_F%_I&o-#U?-p6c!c&tu9{1QNC2Qe3^3ja`lR}JXPyBt2c(%u2XE; zBGrS{ z;K8FukDfk#`s~@WmoHzwef#$F=g&WX{`~XjkMu&pEx>TpC<*clW?*3v6jV{Mw6(W) z4+sluZC$ct)vDF2H*VYk1Sd}d&G_*7^S8f$?*+0O0QDU8ba4!+U`$q!<=CmzAmDUE zkC!7Tc#`nrM~~hJ9BNv|q0n(Zp)7CBp;c;1ItJTXxFtLoX1Z;(>x$vn9X$E*!D~{- zT-aB08#J~a)jjASbHb#ftA}Ijgk)#qXnh+U6^#p?t^A=8*Dv2ad|m&5quhyUWo(R@ z6E^hq_AcJOf2)!HkyBGxI^4JB=G@)WlN#xmqq(6sNMYHu-n~1e?3H(wo2>aFYGGKr za*5^{p5$&v=kT`1#%6ZYmf%SX7Irf%N)u4