Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API to set table columns width #282

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/assets/FullFeaturedDemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/assets/TableDemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/assets/rta_editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions rta/src/main/java/com/gluonhq/richtextarea/ParagraphTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ private HBox createGridBox(List<Node> fragments, List<Integer> positions, List<I
int r = decoration.getTableDecoration().getRows();
int c = decoration.getTableDecoration().getColumns();
TextAlignment[][] ta = decoration.getTableDecoration().getCellAlignment();
Double[] columnPrefWidth = decoration.getTableDecoration().getColumnsPrefWidth();
for (int j = 0; j < c; j++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(100.0 / (double) c);
cc.setPrefWidth(columnPrefWidth[j]);
grid.getColumnConstraints().add(cc);
}
int index = 0;
Expand All @@ -179,7 +180,7 @@ private HBox createGridBox(List<Node> fragments, List<Integer> positions, List<I
return (positions.get(tableIndex) <= p && p < positions.get(tableIndex + 1));
})
.collect(Collectors.toList()), background, pd);
layer.updatePrefWidth(100);
layer.updatePrefWidth(columnPrefWidth[j]);
layers.add(layer);
grid.add(layer, j, i);
prefHeight = Math.max(prefHeight, layer.prefHeight(100));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gluonhq.richtextarea.model;

/*
* Copyright (c) 2022, Gluon
* Copyright (c) 2022, 2023, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -35,15 +35,17 @@
/**
* TableDecoration is a {@link Decoration} that can be applied to a paragraph in order to place
* a table with a number of rows and columns where text can be added, with a given text alignment
* defined per table cell.
* defined per table cell, and a given preferred width per column.
*/
public class TableDecoration implements Decoration {

public static final String TABLE_SEPARATOR = "table_separator";
private static final double COLUMN_PREF_WIDTH = 100d;

private final int rows;
private final int columns;
private final TextAlignment[][] cellAlignment;
private final Double[] columnsPrefWidth;

public TableDecoration() {
this(0, 0, null);
Expand All @@ -54,6 +56,10 @@ public TableDecoration(int rows, int columns) {
}

public TableDecoration(int rows, int columns, TextAlignment[][] cellAlignment) {
this(rows, columns, cellAlignment, null);
}

public TableDecoration(int rows, int columns, TextAlignment[][] cellAlignment, Double[] columnsPrefWidth) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if its high time to create a builder for TableDecoration.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good time, indeed. As part of a new issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, I don't like the new constructor with an additional parameter. Once we add it, we can't simply remove it.

this.rows = rows;
this.columns = columns;
TextAlignment[][] defaultCellAlignment = new TextAlignment[rows][columns];
Expand All @@ -63,6 +69,9 @@ public TableDecoration(int rows, int columns, TextAlignment[][] cellAlignment) {
}
}
this.cellAlignment = cellAlignment != null ? cellAlignment : defaultCellAlignment;
Double[] defaultColumnsPrefWidth = new Double[columns];
Arrays.fill(defaultColumnsPrefWidth, COLUMN_PREF_WIDTH);
this.columnsPrefWidth = columnsPrefWidth != null ? columnsPrefWidth : defaultColumnsPrefWidth;
}

/**
Expand All @@ -88,12 +97,22 @@ public int getColumns() {
*
* By default, the text alignment is set to {@link TextAlignment#LEFT}.
*
* @return the {@link TextAlignment} for each cell
* @return an array with the {@link TextAlignment} for each cell
*/
public TextAlignment[][] getCellAlignment() {
return cellAlignment;
}

/**
* Returns an array with the preferred width for each column.
* By default, each column is set to 100 pixels
*
* @return an array with the pref width of each column
*/
public Double[] getColumnsPrefWidth() {
return columnsPrefWidth;
}

public static TableDecoration fromTableDecorationInsertingRow(TableDecoration tableDecoration, int row) {
int rows = tableDecoration.getRows();
int columns = tableDecoration.getColumns();
Expand All @@ -105,7 +124,7 @@ public static TableDecoration fromTableDecorationInsertingRow(TableDecoration ta
TextAlignment.LEFT : tableDecoration.getCellAlignment()[rowIndex][j];
}
}
return new TableDecoration(rows + 1, columns, newCellAlignment);
return new TableDecoration(rows + 1, columns, newCellAlignment, tableDecoration.getColumnsPrefWidth());
}

public static TableDecoration fromTableDecorationDeletingRow(TableDecoration tableDecoration, int row) {
Expand All @@ -123,7 +142,7 @@ public static TableDecoration fromTableDecorationDeletingRow(TableDecoration tab
newCellAlignment[rowIndex][j] = tableDecoration.getCellAlignment()[i][j];
}
}
return new TableDecoration(rows - 1, columns, newCellAlignment);
return new TableDecoration(rows - 1, columns, newCellAlignment, tableDecoration.getColumnsPrefWidth());
}

public static TableDecoration fromTableDecorationInsertingColumn(TableDecoration tableDecoration, int column) {
Expand All @@ -137,7 +156,13 @@ public static TableDecoration fromTableDecorationInsertingColumn(TableDecoration
TextAlignment.LEFT : tableDecoration.getCellAlignment()[i][colIndex];
}
}
return new TableDecoration(rows, columns + 1, newCellAlignment);
Double[] newColumnsPrefWidth = new Double[columns + 1];
for (int j = 0; j < columns + 1; j++) {
int colIndex = j > column ? j - 1 : j;
newColumnsPrefWidth[j] = j == column ?
COLUMN_PREF_WIDTH : tableDecoration.getColumnsPrefWidth()[colIndex];
}
return new TableDecoration(rows, columns + 1, newCellAlignment, newColumnsPrefWidth);
}

public static TableDecoration fromTableDecorationDeletingColumn(TableDecoration tableDecoration, int column) {
Expand All @@ -155,7 +180,17 @@ public static TableDecoration fromTableDecorationDeletingColumn(TableDecoration
newCellAlignment[i][colIndex] = tableDecoration.getCellAlignment()[i][j];
}
}
return new TableDecoration(rows, columns - 1, newCellAlignment);
Double[] newColumnsPrefWidth = new Double[columns + 1];
for (int j = 0; j < columns + 1; j++) {
int colIndex = j;
if (j == column) {
continue;
} else if (j > column) {
colIndex = j - 1;
}
newColumnsPrefWidth[colIndex] = tableDecoration.getColumnsPrefWidth()[j];
}
return new TableDecoration(rows, columns - 1, newCellAlignment, newColumnsPrefWidth);
}

@Override
Expand All @@ -165,18 +200,19 @@ public boolean equals(Object o) {
TableDecoration that = (TableDecoration) o;
return rows == that.rows &&
columns == that.columns &&
Arrays.deepEquals(cellAlignment, that.cellAlignment);
Arrays.deepEquals(cellAlignment, that.cellAlignment) &&
Arrays.deepEquals(columnsPrefWidth, that.columnsPrefWidth);
}

@Override
public int hashCode() {
int result = Objects.hash(rows, columns);
result = 31 * result + Arrays.deepHashCode(cellAlignment);
result = 31 * result + Arrays.deepHashCode(cellAlignment) + Arrays.deepHashCode(columnsPrefWidth);
return result;
}

@Override
public String toString() {
return "TabDec[" + rows + " x " + columns + "] - " + Arrays.deepToString(cellAlignment);
return "TabDec[" + rows + " x " + columns + "] - " + Arrays.deepToString(cellAlignment) + " - " + Arrays.deepToString(columnsPrefWidth);
}
}
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ To run this sample, using Java 17+, do as follows:
mvn javafx:run
```

![rta_editor.png](../.github/assets/rta_editor.png)
![rta_editor.png](../.github/assets/FullFeaturedDemo.png)
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
import static javafx.scene.text.FontPosture.REGULAR;
import static javafx.scene.text.FontWeight.BOLD;
import static javafx.scene.text.FontWeight.NORMAL;
import static javafx.scene.text.TextAlignment.CENTER;
import static javafx.scene.text.TextAlignment.JUSTIFY;
import static javafx.scene.text.TextAlignment.LEFT;
import static javafx.scene.text.TextAlignment.RIGHT;

/**
* This is an advance sample that shows how to create a rich text editor, by using
Expand All @@ -127,15 +131,17 @@ public class FullFeaturedDemo extends Application {
private final List<DecorationModel> decorations;

{
TextDecoration bold14 = TextDecoration.builder().presets().fontWeight(BOLD).fontSize(14).build();
TextDecoration preset = TextDecoration.builder().presets().build();
ParagraphDecoration center63 = ParagraphDecoration.builder().presets().alignment(TextAlignment.CENTER).topInset(6).bottomInset(3).build();
ParagraphDecoration justify22 = ParagraphDecoration.builder().presets().alignment(TextAlignment.JUSTIFY).topInset(2).bottomInset(2).build();
ParagraphDecoration right22 = ParagraphDecoration.builder().presets().alignment(TextAlignment.RIGHT).topInset(2).bottomInset(2).build();
ParagraphDecoration left535 = ParagraphDecoration.builder().presets().alignment(TextAlignment.LEFT).topInset(5).bottomInset(3).spacing(5).build();
ParagraphDecoration center42 = ParagraphDecoration.builder().presets().alignment(TextAlignment.CENTER).topInset(4).bottomInset(2).build();
TableDecoration tdec = new TableDecoration(2, 3, new TextAlignment[][]{{TextAlignment.CENTER, TextAlignment.LEFT, TextAlignment.RIGHT}, {TextAlignment.JUSTIFY, TextAlignment.RIGHT, TextAlignment.CENTER}});
ParagraphDecoration table = ParagraphDecoration.builder().presets().alignment(TextAlignment.CENTER).tableDecoration(tdec).build();
TextDecoration bold14 = TextDecoration.builder().presets().fontFamily("Arial").fontWeight(BOLD).fontSize(14).build();
TextDecoration preset = TextDecoration.builder().presets().fontFamily("Arial").build();
ParagraphDecoration center63 = ParagraphDecoration.builder().presets().alignment(CENTER).topInset(6).bottomInset(3).build();
ParagraphDecoration justify22 = ParagraphDecoration.builder().presets().alignment(JUSTIFY).topInset(2).bottomInset(2).build();
ParagraphDecoration right22 = ParagraphDecoration.builder().presets().alignment(RIGHT).topInset(2).bottomInset(2).build();
ParagraphDecoration left535 = ParagraphDecoration.builder().presets().alignment(LEFT).topInset(5).bottomInset(3).spacing(5).build();
ParagraphDecoration center42 = ParagraphDecoration.builder().presets().alignment(CENTER).topInset(4).bottomInset(2).build();
TableDecoration tdec = new TableDecoration(2, 3,
new TextAlignment[][]{{CENTER, LEFT, RIGHT}, {JUSTIFY, RIGHT, CENTER}},
new Double[] {100d, 50d, 150d});
ParagraphDecoration table = ParagraphDecoration.builder().presets().alignment(CENTER).tableDecoration(tdec).build();
decorations = List.of(
new DecorationModel(0, 21, bold14, center63),
new DecorationModel(21, 575, preset, justify22),
Expand Down Expand Up @@ -192,7 +198,7 @@ public Presets fromString(String s) {

ComboBox<String> fontFamilies = new ComboBox<>();
fontFamilies.getItems().setAll(Font.getFamilies());
fontFamilies.setValue("Arial");
fontFamilies.setValue("System");
fontFamilies.setPrefWidth(100);
new TextDecorateAction<>(editor, fontFamilies.valueProperty(), TextDecoration::getFontFamily, (builder, a) -> builder.fontFamily(a).build());

Expand Down Expand Up @@ -269,10 +275,10 @@ public Double fromString(String s) {

ToolBar paragraphToolbar = new ToolBar();
paragraphToolbar.getItems().setAll(
createToggleButton(LineAwesomeSolid.ALIGN_LEFT, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == TextAlignment.LEFT, (builder, a) -> builder.alignment(TextAlignment.LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_CENTER, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == TextAlignment.CENTER, (builder, a) -> builder.alignment(a ? TextAlignment.CENTER : TextAlignment.LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_RIGHT, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == TextAlignment.RIGHT, (builder, a) -> builder.alignment(a ? TextAlignment.RIGHT : TextAlignment.LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_JUSTIFY, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == TextAlignment.JUSTIFY, (builder, a) -> builder.alignment(a ? TextAlignment.JUSTIFY : TextAlignment.LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_LEFT, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == LEFT, (builder, a) -> builder.alignment(LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_CENTER, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == CENTER, (builder, a) -> builder.alignment(a ? CENTER : LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_RIGHT, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == RIGHT, (builder, a) -> builder.alignment(a ? RIGHT : LEFT).build())),
createToggleButton(LineAwesomeSolid.ALIGN_JUSTIFY, property -> new ParagraphDecorateAction<>(editor, property, d -> d.getAlignment() == JUSTIFY, (builder, a) -> builder.alignment(a ? JUSTIFY : LEFT).build())),
new Separator(Orientation.VERTICAL),
createSpinner("Spacing", p -> new ParagraphDecorateAction<>(editor, p, v -> (int) v.getSpacing(), (builder, a) -> builder.spacing(a).build())),
new Separator(Orientation.VERTICAL),
Expand Down Expand Up @@ -533,10 +539,10 @@ public static void main(String[] args) {

private enum Presets {

DEFAULT("Default",12, NORMAL, TextAlignment.LEFT),
HEADER1("Header 1", 32, BOLD, TextAlignment.CENTER),
HEADER2("Header 2", 24, BOLD, TextAlignment.LEFT),
HEADER3("Header 3", 19, BOLD, TextAlignment.LEFT);
DEFAULT("Default",12, NORMAL, LEFT),
HEADER1("Header 1", 32, BOLD, CENTER),
HEADER2("Header 2", 24, BOLD, LEFT),
HEADER3("Header 3", 19, BOLD, LEFT);

private final String name;
private final int fontSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private Document getDocument() {
alignment[i][0] = TextAlignment.CENTER;
alignment[i][1] = TextAlignment.LEFT;
}
TableDecoration tableDecoration = new TableDecoration(flags, 2, alignment);
TableDecoration tableDecoration = new TableDecoration(flags, 2, alignment, new Double[] {80d, 200d});
ParagraphDecoration table = ParagraphDecoration.builder().presets().alignment(TextAlignment.CENTER)
.tableDecoration(tableDecoration)
.topInset(5).rightInset(5).bottomInset(5).leftInset(5)
Expand Down