Skip to content
Snippets Groups Projects
Commit 07bb0147 authored by Tiziano Munaro's avatar Tiziano Munaro
Browse files

[UI] Refactor support for editable columns within a `DynamicTreeViewer`

* The `addColumn` method now features a third parameter: a Boolean
`readOnly`. If true `true` the column is read-only, otherwise the cell's
content can be edited.
* There is no more `addColumn` method which takes an `EventHandler`.
However, as it now returns the `TreeTableColumn` handlers can be added
in the client code.

Issue-Ref: 3437
Issue-Url: https://af3-developer.fortiss.org/issues/3437



Signed-off-by: default avatarTiziano Munaro <munaro@fortiss.org>
parent 4e984f6d
No related branches found
No related tags found
1 merge request!823437
DynamicTreeContentProviderBase.java dff437afeaf7486af05460fa54eca4fa61d7eae6 GREEN
DynamicTreeItem.java afc105cf5acf3d2506d89e0892555100c234ce5b GREEN
DynamicTreeTableUIProviderBase.java fd9fce19a65eb1006ceacb0d869bbe90a8c578b3 GREEN
DynamicTreeTableViewer.java b06182c30665f54942703a5e9986822a4d8dd59a RED
DynamicTreeTableViewer.java 4f278387dd90542adc07bf0da12e92d4eaad79c4 YELLOW
DynamicTreeUIProviderBase.java 56fe4df4577b35f1e5e6e4c4be189b706c852d52 GREEN
DynamicTreeViewer.java da5e24ae57777a482d8e12c8262513d8143bfa93 GREEN
DynamicTreeViewerBase.java 47124c847de322a0ae26eb7a114f85ce4bd02d7e GREEN
......
......@@ -15,17 +15,17 @@
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.control.treetableview;
import static javafx.scene.control.cell.TextFieldTreeTableCell.forTreeTableColumn;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellEditEvent;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.util.Callback;
/**
* A JavaFX {@link TreeTableView} based on an {@link DynamicTreeContentProviderBase} and
......@@ -89,10 +89,11 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> {
}
/**
* Creates non-editable column displaying the labels as defined by the
* {@link DynamicTreeTableUIProviderBase} used as a basis for specific column types.
* Adds a column to the table part of the view. The labels, context menus and icons are shown as
* defined in the {@link DynamicTreeTableUIProviderBase}.
*/
private TreeTableColumn<T, String> createColumn(String headerLabel, int prefWidth) {
public TreeTableColumn<T, String> addColumn(String headerLabel, int prefWidth,
boolean readOnly) {
int num = view.getColumns().size();
TreeTableColumn<T, String> column = new TreeTableColumn<>(headerLabel);
......@@ -102,55 +103,38 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> {
return new SimpleObjectProperty<String>(uiProvider.getLabel(data, num));
});
view.getColumns().add(column);
return column;
}
/**
* Adds a non-editable column to the table part of the view. The labels, context menus and icons
* are shown as defined in the {@link DynamicTreeTableUIProviderBase}.
*/
// TODO(AD): Please add a flag to make the column read-only or read-writable.
public void addColumn(String headerLabel, int prefWidth) {
TreeTableColumn<T, String> column = createColumn(headerLabel, prefWidth);
column.setCellFactory(param -> {
TreeTableCell<T, String> cell = new TreeTableCell<T, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
ContextMenu menu = null;
Node icon = null;
int index = view.getColumns().size() - 1;
if(!empty && item != null) {
T data = this.getTreeTableRow().getItem();
menu = uiProvider.createContextMenu(data, index);
icon = uiProvider.getIconNode(data, index);
Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>> cellFactory;
if(readOnly) {
// Read only cell with the icon and context menu as specified in the UI provider
cellFactory = param -> {
TreeTableCell<T, String> cell = new TreeTableCell<T, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
ContextMenu menu = null;
Node icon = null;
int index = view.getColumns().size() - 1;
if(!empty && item != null) {
T data = this.getTreeTableRow().getItem();
menu = uiProvider.createContextMenu(data, index);
icon = uiProvider.getIconNode(data, index);
}
this.setContextMenu(menu);
this.setGraphic(icon);
}
this.setContextMenu(menu);
this.setGraphic(icon);
}
};
cell.textProperty().bind(cell.itemProperty());
return cell;
};
cell.textProperty().bind(cell.itemProperty());
return cell;
});
}
} else {
// Editable text field
cellFactory = forTreeTableColumn();
}
column.setCellFactory(cellFactory);
/**
* Adds an editable column to the table part of the view. The labels defined in the
* {@link DynamicTreeTableUIProviderBase} are used as predefined values. The
* {@link EventHandler} is executed after a cell has been successfully edited.
*/
// TODO(AD): This method is redundant and oversimplified (due to the limitation to a text field
// editor). It would be better to change the method "addColumn(...)" to return the created
// column. The client code would be pretty much the same with that change.
public void addColumn(String headerLabel, int prefWidth,
EventHandler<CellEditEvent<T, String>> eventHandler) {
TreeTableColumn<T, String> column = createColumn(headerLabel, prefWidth);
view.getColumns().add(column);
column.setCellFactory(TextFieldTreeTableCell.<T>forTreeTableColumn());
column.setOnEditCommit(eventHandler);
return column;
}
/** Returns the underlying {@link TreeTableView}. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment