Skip to content

Commit

Permalink
RichTextDemo Bullet Lists (#850)
Browse files Browse the repository at this point in the history
Added Indent and BulletFactory and updated ParStyle to accommodate Indent
  • Loading branch information
Jugen authored Nov 19, 2019
1 parent b8c87ee commit b8f563d
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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<Node>
{
private GenericStyledArea<ParStyle,?,?> area;

public BulletFactory( GenericStyledArea<ParStyle,?,?> 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 );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.fxmisc.richtext.demo.richtext;

public class Indent
{
double width = 15;
int level = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ public ParStyle decode(DataInputStream is) throws IOException {

final Optional<TextAlignment> alignment;
final Optional<Color> backgroundColor;
final Optional<Indent> indent;

public ParStyle() {
this(Optional.empty(), Optional.empty());
this(Optional.empty(), Optional.empty(), Optional.empty());
}

public ParStyle(Optional<TextAlignment> alignment, Optional<Color> backgroundColor) {
this(alignment, backgroundColor, Optional.empty());
}

public ParStyle(Optional<TextAlignment> alignment, Optional<Color> backgroundColor, Optional<Indent> indent) {
this.alignment = alignment;
this.backgroundColor = backgroundColor;
this.indent = indent;
}

@Override
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -309,11 +313,6 @@ private Node createNode(StyledSegment<Either<String, LinkedImage>, 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);
Expand Down Expand Up @@ -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<StyleSpans<TextStyle>, TextStyle> mixinGetter) {
IndexRange selection = area.getSelection();
if(selection.getLength() != 0) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
.button.underline { -fx-background-image: url(underline.png); }
.button.strikethrough { -fx-background-image: url(strikethrough.png); }
.button.insertimage { -fx-background-image: url(insertimage.png); }
.button.increaseIndent { -fx-background-image: url(increaseIndent.png); }
.button.decreaseIndent { -fx-background-image: url(decreaseIndent.png); }

.toggle-button.align-left { -fx-background-image: url(align-left.png); }
.toggle-button.align-center { -fx-background-image: url(align-center.png); }
.toggle-button.align-right { -fx-background-image: url(align-right.png); }
.toggle-button.align-justify { -fx-background-image: url(align-justify.png); }
.toggle-button.align-justify { -fx-background-image: url(align-justify.png); }

0 comments on commit b8f563d

Please sign in to comment.