From 1d6be6f3bcf2e39872d1ce0ed4a3bd95189d5944 Mon Sep 17 00:00:00 2001 From: Dirk Lemmermann Date: Tue, 25 Feb 2020 11:04:54 +0100 Subject: [PATCH] Code cleanup and javadocs for rich text area. --- .../com/dlsc/gemsfx/demo/RichTextAreaApp.java | 64 ------------------- .../dlsc/gemsfx/richtextarea/RTElement.java | 43 +++++++++++-- .../richtextarea/RTElementContainer.java | 4 +- .../dlsc/gemsfx/richtextarea/RTHeading.java | 4 +- .../com/dlsc/gemsfx/richtextarea/RTImage.java | 4 +- .../com/dlsc/gemsfx/richtextarea/RTLink.java | 8 +-- .../com/dlsc/gemsfx/richtextarea/RTList.java | 12 ++-- .../com/dlsc/gemsfx/richtextarea/RTTable.java | 8 +-- .../dlsc/gemsfx/richtextarea/RTTableCell.java | 12 ++-- .../gemsfx/richtextarea/RTTableContainer.java | 4 +- .../dlsc/gemsfx/richtextarea/RTTableRow.java | 4 +- .../gemsfx/richtextarea/RTTextElement.java | 22 +++---- .../gemsfx/richtextarea/RichTextArea.java | 29 ++++++++- .../dlsc/gemsfx/skins/RichTextAreaSkin.java | 5 ++ 14 files changed, 108 insertions(+), 115 deletions(-) delete mode 100644 gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/RichTextAreaApp.java diff --git a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/RichTextAreaApp.java b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/RichTextAreaApp.java deleted file mode 100644 index 00867cdc..00000000 --- a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/RichTextAreaApp.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.dlsc.gemsfx.demo; - -import com.dlsc.gemsfx.richtextarea.RTDocument; -import com.dlsc.gemsfx.richtextarea.RTHeading; -import com.dlsc.gemsfx.richtextarea.RTLink; -import com.dlsc.gemsfx.richtextarea.RTList; -import com.dlsc.gemsfx.richtextarea.RTListItem; -import com.dlsc.gemsfx.richtextarea.RTParagraph; -import com.dlsc.gemsfx.richtextarea.RTText; -import com.dlsc.gemsfx.richtextarea.RichTextArea; -import javafx.application.Application; -import javafx.scene.Scene; -import javafx.stage.Stage; - -public class RichTextAreaApp extends Application { - - @Override - public void start(Stage stage) { - RichTextArea area = new RichTextArea(); - area.setDocument( - RTDocument.create( - RTHeading.create("Heading 1"), - RTParagraph.create( - RTText.create("This is the first paragraph. "), - RTText.create("Some text comes here before the link that "), - RTLink.create("points to the website ", "https://www.dlsc.com"), - RTText.create("of DLSC Software & Consulting.") - ), - RTParagraph.create( - RTText.create("Here comes the second paragraph.") - ), - RTParagraph.create(), - RTHeading.create("Heading 2"), - RTParagraph.create( - RTText.create("Some text for the first paragraph after heading 2."), - RTList.create( - RTListItem.create("List item 1"), - RTListItem.create("List item 2"), - RTListItem.create("List item 3", - RTList.create( - RTListItem.create("Sub item A"), - RTListItem.create("Sub item B"), - RTListItem.create("Sub item C"), - RTListItem.create("Sub item D") - ) - ), - RTListItem.create("List item 4") - ) - ) - ) - ); - Scene scene = new Scene(area); - stage.setTitle("RichTextArea Demo"); - stage.setScene(scene); - stage.setWidth(1000); - stage.setHeight(850); - stage.centerOnScreen(); - stage.show(); - } - - public static void main(String[] args) { - launch(); - } -} diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElement.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElement.java index d4704359..1e07027e 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElement.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElement.java @@ -7,33 +7,62 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; -public class RTElement { +public abstract class RTElement { + + protected RTElement() { + } private final ListProperty styleClass = new SimpleListProperty<>(this, "styleClass", FXCollections.observableArrayList()); - public ObservableList getStyleClass() { + public final ObservableList getStyleClass() { return styleClass.get(); } - public ListProperty styleClassProperty() { + public final ListProperty styleClassProperty() { return styleClass; } - public void withStyleClasses(String... styleClass) { + public final void withStyleClasses(String... styleClass) { this.styleClass.setAll(styleClass); } private final StringProperty id = new SimpleStringProperty(this, "id"); - public String getId() { + public final String getId() { return id.get(); } - public StringProperty idProperty() { + public final StringProperty idProperty() { return id; } - public void setId(String id) { + public final void setId(String id) { this.id.set(id); } + + public final SELF withId(String id) { + setId(id); + return (SELF) this; + } + + // style + + private final StringProperty style = new SimpleStringProperty(this, "style"); + + public final String getStyle() { + return style.get(); + } + + public final StringProperty styleProperty() { + return style; + } + + public final void setStyle(String style) { + this.style.set(style); + } + + public final SELF withStyle(String style) { + setStyle(style); + return (SELF) this; + } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElementContainer.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElementContainer.java index 0dc388aa..bcfb58b9 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElementContainer.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTElementContainer.java @@ -11,12 +11,12 @@ public abstract class RTElementContainer extend protected RTElementContainer() { } - public SELF withElements(RTElement... elements) { + public final SELF withElements(RTElement... elements) { this.elements.setAll(elements); return (SELF) this; } - public ObservableList getElements() { + public final ObservableList getElements() { return elements.getReadOnlyProperty(); } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTHeading.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTHeading.java index b22ae388..f042f4cc 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTHeading.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTHeading.java @@ -11,12 +11,12 @@ public static RTHeading create(String text) { return new RTHeading().withText(text); } - public RTHeading withLevel(int level) { + public final RTHeading withLevel(int level) { this.level = level; return this; } - public int getLevel() { + public final int getLevel() { return level; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTImage.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTImage.java index 270f0899..216a4e7a 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTImage.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTImage.java @@ -13,11 +13,11 @@ public static RTImage create() { return new RTImage(); } - public Image getImage() { + public final Image getImage() { return image; } - public RTImage withImage(Image image) { + public final RTImage withImage(Image image) { this.image = image; return this; } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTLink.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTLink.java index 49b58ac9..8416933e 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTLink.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTLink.java @@ -16,22 +16,22 @@ public static RTLink create(String text, String url) { return create(text).withTarget(url); } - public RTLink withTarget(String url) { + public final RTLink withTarget(String url) { this.target.set(url); return this; } private final StringProperty target = new SimpleStringProperty(this, "target"); - public String getTarget() { + public final String getTarget() { return target.get(); } - public StringProperty targetProperty() { + public final StringProperty targetProperty() { return target; } - public void setTarget(String target) { + public final void setTarget(String target) { this.target.set(target); } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTList.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTList.java index 0ff9cef1..b6659246 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTList.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTList.java @@ -25,30 +25,30 @@ public static RTList create(RTListItem... items) { return new RTList().withItems(items); } - public RTList withItems(RTListItem... items) { + public final RTList withItems(RTListItem... items) { this.items.setAll(items); return this; } - public RTList withStart(int start) { + public final RTList withStart(int start) { this.start = start; return this; } - public RTList withType(Type type) { + public final RTList withType(Type type) { this.type = type; return this; } - public int getStart() { + public final int getStart() { return start; } - public Type getType() { + public final Type getType() { return type; } - public ObservableList getItems() { + public final ObservableList getItems() { return items.get(); } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTable.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTable.java index 823f24aa..40b2ec55 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTable.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTable.java @@ -12,21 +12,21 @@ public static RTTable create() { return new RTTable(); } - public RTTable withHead(RTTableHead head) { + public final RTTable withHead(RTTableHead head) { this.head = head; return this; } - public RTTable withBody(RTTableBody body) { + public final RTTable withBody(RTTableBody body) { this.body = body; return this; } - public RTTableHead getHead() { + public final RTTableHead getHead() { return head; } - public RTTableBody getBody() { + public final RTTableBody getBody() { return body; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableCell.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableCell.java index 7ff80c32..61be3549 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableCell.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableCell.java @@ -15,30 +15,30 @@ public static RTTableCell create() { return new RTTableCell(); } - public RTTableCell withRowSpan(int span) { + public final RTTableCell withRowSpan(int span) { this.rowSpan = rowSpan; return this; } - public RTTableCell withColSpan(int span) { + public final RTTableCell withColSpan(int span) { this.colSpan = colSpan; return this; } - public RTTableCell withAlignment(Pos alignment) { + public final RTTableCell withAlignment(Pos alignment) { this.alignment = alignment; return this; } - public int getRowSpan() { + public final int getRowSpan() { return rowSpan; } - public int getColSpan() { + public final int getColSpan() { return colSpan; } - public Pos getAlignment() { + public final Pos getAlignment() { return alignment; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableContainer.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableContainer.java index fab6e9bb..645d7942 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableContainer.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableContainer.java @@ -8,12 +8,12 @@ public abstract class RTTableContainer extends RT private final ObservableList rows = FXCollections.observableArrayList(); - public SELF withRows(RTTableRow... row) { + public final SELF withRows(RTTableRow... row) { rows.setAll(row); return (SELF) this; } - public ObservableList getRows() { + public final ObservableList getRows() { return rows; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableRow.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableRow.java index 7da24430..56a37a39 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableRow.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTableRow.java @@ -14,12 +14,12 @@ public static RTTableRow create() { return new RTTableRow(); } - public RTTableRow withCells(RTTableCell... cells) { + public final RTTableRow withCells(RTTableCell... cells) { this.cells.setAll(cells); return this; } - public ObservableList getCells() { + public final ObservableList getCells() { return cells; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTextElement.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTextElement.java index fb9517e7..1c95518b 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTextElement.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RTTextElement.java @@ -30,53 +30,53 @@ public final void setText(String text) { this.text.set(text); } - public SELF withText(String text) { + public final SELF withText(String text) { setText(text); return (SELF) this; } - public SELF withSubscript(boolean on) { + public final SELF withSubscript(boolean on) { this.subscript = on; return (SELF) this; } - public SELF withSuperscript(boolean on) { + public final SELF withSuperscript(boolean on) { this.superscript = on; return (SELF) this; } - public SELF withItalic(boolean on) { + public final SELF withItalic(boolean on) { this.italic = on; return (SELF) this; } - public SELF withBold(boolean on) { + public final SELF withBold(boolean on) { this.bold = on; return (SELF) this; } - public SELF withTextFill(Color color) { + public final SELF withTextFill(Color color) { this.textFill = color; return (SELF) this; } - public boolean isSubscript() { + public final boolean isSubscript() { return subscript; } - public boolean isSuperscript() { + public final boolean isSuperscript() { return superscript; } - public boolean isItalic() { + public final boolean isItalic() { return italic; } - public boolean isBold() { + public final boolean isBold() { return bold; } - public Color getTextFill() { + public final Color getTextFill() { return textFill; } } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RichTextArea.java b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RichTextArea.java index bcf41a14..9d210bb4 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RichTextArea.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/richtextarea/RichTextArea.java @@ -10,6 +10,12 @@ import java.util.function.Consumer; +/** + * A text area capable to displaying rich text based on a custom rich text + * model (see {@link RTDocument}). + * + * The styling of the text elements can be done via CSS. + */ public class RichTextArea extends Control { public RichTextArea() { @@ -29,11 +35,16 @@ protected Skin createDefaultSkin() { private final StringProperty placeholderText = new SimpleStringProperty(this, "placeholderText", "No content"); + /** + * A text that will be displayed if not document / content has been set on the area. + * + * @return the placeholder text + */ public final StringProperty placeholderTextProperty() { return placeholderText; } - public void setPlaceholderText(String text) { + public final void setPlaceholderText(String text) { placeholderText.set(text); } @@ -43,11 +54,17 @@ public final String getPlaceholderText() { private final ObjectProperty> hyperlinkHandler = new SimpleObjectProperty<>(this, "hyperlinkHandler"); - public Consumer getHyperlinkHandler() { + public final Consumer getHyperlinkHandler() { return hyperlinkHandler.get(); } - public ObjectProperty> hyperlinkHandlerProperty() { + /** + * A handler used for hyperlinks when the user clicks on them. This passed in consumer then + * has to decide what to do with the given link information. + * + * @return the hyperlink handler + */ + public final ObjectProperty> hyperlinkHandlerProperty() { return hyperlinkHandler; } @@ -61,6 +78,12 @@ public final RTDocument getDocument() { return document.get(); } + /** + * The model of the rich text area. A document consists of many nested + * elements. + * + * @return the document model + */ public final ObjectProperty documentProperty() { return document; } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/RichTextAreaSkin.java b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/RichTextAreaSkin.java index 35f6a4d9..a8b6383a 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/RichTextAreaSkin.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/RichTextAreaSkin.java @@ -247,6 +247,7 @@ private void createTableBody(RTTableBody tableBody, GridPane gridPane, int row) private void createHeading(RTHeading heading, Section section) { Label label = new Label(heading.getText()); label.getStyleClass().addAll("text", "heading"); + label.setStyle(heading.getStyle()); section.addChild(label); } @@ -266,6 +267,7 @@ private void createLink(RTLink link, Section section) { private void createParagraph(RTParagraph paragraph, Section section) { Section newSection = new Section(); newSection.getStyleClass().add("paragraph"); + newSection.setStyle(paragraph.getStyle()); if (paragraph.getElements().isEmpty()) { newSection.getStyleClass().add("gap"); @@ -284,6 +286,8 @@ private void createParagraph(RTParagraph paragraph, Section section) { private javafx.scene.text.Text createText(RTTextElement text, Section section) { javafx.scene.text.Text uiText = new javafx.scene.text.Text(text.getText()); + uiText.setStyle(text.getStyle()); + if (text.isItalic()) { uiText.getStyleClass().add("italic"); } @@ -315,6 +319,7 @@ private javafx.scene.text.Text createText(RTTextElement text, Section section) { private void createList(RTList list, Section section) { GridPane gridPane = new GridPane(); gridPane.getStyleClass().add("list-block"); + gridPane.setStyle(list.getStyle()); if (list.getType().equals(Type.ORDERED)) { gridPane.getStyleClass().add("ordered-list");