Commit 433d5623 authored by Simon Barner's avatar Simon Barner

Merge branch '4029' into 'master'

[4029] Impl of generic Checkbox column for TableTreeViewers

See merge request !118
parents 035376eb c970122a
DynamicTreeContentProviderBase.java 6760a6dc5721175b1dada8f30fd9da05f7bcc4b3 GREEN
DynamicTreeItem.java 75dc5534b119ffdb3c10a65810c2a0f330b7955e GREEN
DynamicTreeTableUIProviderBase.java ea7de1e0fd824b61b46010d2317f93422bab6144 GREEN
DynamicTreeTableViewer.java 431ac62cbd6ad7df25852fce1b5a62a05ba510e3 GREEN
DynamicTreeTableUIProviderBase.java f78c0f8b52fbc939166b3f94f7f6006cc0f4d32b GREEN
DynamicTreeTableViewer.java 5e58a31a63f8e56d8c8e69e2c4d095809bc46bf1 GREEN
DynamicTreeUIProviderBase.java e9b68607683de279d0cb8712a28dc131c5c33ece GREEN
DynamicTreeViewer.java 725f41f4fb4b6bfa813f010fb9083ab02eea164a GREEN
DynamicTreeViewerBase.java a2013538b62d86f6a09efdf2cd78babac2072484 GREEN
......
......@@ -15,12 +15,16 @@ package org.fortiss.tooling.common.ui.javafx.control.treetableview;
import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyCode;
......@@ -111,7 +115,7 @@ public abstract class DynamicTreeTableUIProviderBase<T> {
* @param value
* the edited value
*/
public void updateValue(T element, int column, String value) {
public void updateValue(T element, int column, Object value) {
// ignored, since not editable
}
......@@ -150,6 +154,52 @@ public abstract class DynamicTreeTableUIProviderBase<T> {
});
}
/** Applies a checkbox editing support to the given checkbox column. */
/* package */ final void applyToCheckboxColumn(int i, TreeTableColumn<T, Boolean> column) {
column.setOnEditCommit(null);
column.setEditable(isEditable(i));
if(!isEditable(i)) {
return;
}
// setOnEditCommit() does not work for CheckBoxTreeTableCell. This is why a custom
// CellValueFactory has to be set here
column.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<T, Boolean>, //
ObservableValue<Boolean>>() {
/** {@inheritDoc} */
@Override
public ObservableValue<Boolean>
call(TreeTableColumn.CellDataFeatures<T, Boolean> param) {
TreeItem<T> treeItem = param.getValue();
T value = treeItem.getValue();
String label = getLabel(value, i).trim().toLowerCase();
// String to boolean transformations throw no error/exception that is why this has
// to be captured manually here.
if(!(label.equals("true") || label.equals("false"))) {
throw new RuntimeException(
"Wrong type in checkbox table cell. Expected boolean \"true\" or \"false\" but received \"" +
label + "\".");
}
boolean b = Boolean.valueOf(label);
SimpleBooleanProperty booleanProp = new SimpleBooleanProperty(b);
// equal behavior as setOnEditCommit()
booleanProp.addListener(new ChangeListener<Boolean>() {
/** {@inheritDoc} */
@Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
updateValue(value, i, newValue);
column.getTreeTableView().refresh();
}
});
return booleanProp;
}
});
column.setCellFactory(createEditableCheckboxCellFactory());
}
/** Creates a cell factory for editable cells. */
private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>>
createEditableCellFactory(int colIndex) {
......@@ -158,6 +208,14 @@ public abstract class DynamicTreeTableUIProviderBase<T> {
};
}
/** Creates a cell factory for editable cells. */
private Callback<TreeTableColumn<T, Boolean>, TreeTableCell<T, Boolean>>
createEditableCheckboxCellFactory() {
return param -> {
return new CheckBoxTreeTableCell<T, Boolean>();
};
}
/** Custom table cell implementation to allow on key released validation. */
private class MyTextFieldTreeTableCell extends TextFieldTreeTableCell<T, String> {
/** The column index of this cell. */
......
......@@ -24,6 +24,7 @@ import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableRow;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.TreeTableView.TreeTableViewSelectionModel;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.input.TransferMode;
import javafx.util.Callback;
......@@ -147,6 +148,23 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> {
return column;
}
/**
* Adds a boolean checkbox column to the table part of the view. The labels, context menus and
* icons are shown as defined in the {@link DynamicTreeTableUIProviderBase}.
*
* <b>IMPORTANT</b>: the getLabel method of the uiProvider must return the boolean string "true"
* or "false" in order to work correctly
*/
public TreeTableColumn<T, Boolean> addCheckboxColumn(String headerLabel, int prefWidth) {
int num = view.getColumns().size();
TreeTableColumn<T, Boolean> column = new TreeTableColumn<>(headerLabel);
column.setPrefWidth(prefWidth);
column.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(column));
uiProvider.applyToCheckboxColumn(num, column);
view.getColumns().add(column);
return column;
}
/**
* Adds a column to the table part of the view. The labels, context menus and icons are shown as
* defined in the {@link DynamicTreeTableUIProviderBase}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment