Commit 7d39c25b authored by Johannes Eder's avatar Johannes Eder

Impl of generic Checkbox column for TableTreeViewers

Issue-Ref: 4029
Issue-Url: https://af3-developer.fortiss.org/issues/4029Signed-off-by: Johannes Eder's avatarJohannes Eder <eder@fortiss.org>
parent 035376eb
DynamicTreeContentProviderBase.java 6760a6dc5721175b1dada8f30fd9da05f7bcc4b3 GREEN DynamicTreeContentProviderBase.java 6760a6dc5721175b1dada8f30fd9da05f7bcc4b3 GREEN
DynamicTreeItem.java 75dc5534b119ffdb3c10a65810c2a0f330b7955e GREEN DynamicTreeItem.java 75dc5534b119ffdb3c10a65810c2a0f330b7955e GREEN
DynamicTreeTableUIProviderBase.java ea7de1e0fd824b61b46010d2317f93422bab6144 GREEN DynamicTreeTableUIProviderBase.java 928ef4fd5d6171c43d31ea628ded7e338469f5a8 YELLOW
DynamicTreeTableViewer.java 431ac62cbd6ad7df25852fce1b5a62a05ba510e3 GREEN DynamicTreeTableViewer.java 5e58a31a63f8e56d8c8e69e2c4d095809bc46bf1 YELLOW
DynamicTreeUIProviderBase.java e9b68607683de279d0cb8712a28dc131c5c33ece GREEN DynamicTreeUIProviderBase.java e9b68607683de279d0cb8712a28dc131c5c33ece GREEN
DynamicTreeViewer.java 725f41f4fb4b6bfa813f010fb9083ab02eea164a GREEN DynamicTreeViewer.java 725f41f4fb4b6bfa813f010fb9083ab02eea164a GREEN
DynamicTreeViewerBase.java a2013538b62d86f6a09efdf2cd78babac2072484 GREEN DynamicTreeViewerBase.java a2013538b62d86f6a09efdf2cd78babac2072484 GREEN
......
...@@ -15,12 +15,16 @@ package org.fortiss.tooling.common.ui.javafx.control.treetableview; ...@@ -15,12 +15,16 @@ package org.fortiss.tooling.common.ui.javafx.control.treetableview;
import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX; 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.Node;
import javafx.scene.control.ContextMenu; import javafx.scene.control.ContextMenu;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem; import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell; import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.control.cell.TextFieldTreeTableCell; import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.input.Dragboard; import javafx.scene.input.Dragboard;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
...@@ -111,7 +115,7 @@ public abstract class DynamicTreeTableUIProviderBase<T> { ...@@ -111,7 +115,7 @@ public abstract class DynamicTreeTableUIProviderBase<T> {
* @param value * @param value
* the edited 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 // ignored, since not editable
} }
...@@ -150,6 +154,44 @@ public abstract class DynamicTreeTableUIProviderBase<T> { ...@@ -150,6 +154,44 @@ 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) {
if(!isEditable(i)) {
column.setEditable(false);
column.setOnEditCommit(null);
return;
}
column.setEditable(true);
// 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>>() {
@Override
public ObservableValue<Boolean>
call(TreeTableColumn.CellDataFeatures<T, Boolean> param) {
TreeItem<T> treeItem = param.getValue();
T value = treeItem.getValue();
String label = getLabel(value, i);
boolean b = Boolean.valueOf(label);
SimpleBooleanProperty booleanProp = new SimpleBooleanProperty(b);
// equal behavior to setOnEditCommit()
booleanProp.addListener(new ChangeListener<Boolean>() {
@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. */ /** Creates a cell factory for editable cells. */
private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>> private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>>
createEditableCellFactory(int colIndex) { createEditableCellFactory(int colIndex) {
...@@ -158,6 +200,14 @@ public abstract class DynamicTreeTableUIProviderBase<T> { ...@@ -158,6 +200,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. */ /** Custom table cell implementation to allow on key released validation. */
private class MyTextFieldTreeTableCell extends TextFieldTreeTableCell<T, String> { private class MyTextFieldTreeTableCell extends TextFieldTreeTableCell<T, String> {
/** The column index of this cell. */ /** The column index of this cell. */
......
...@@ -24,6 +24,7 @@ import javafx.scene.control.TreeTableColumn; ...@@ -24,6 +24,7 @@ import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableRow; import javafx.scene.control.TreeTableRow;
import javafx.scene.control.TreeTableView; import javafx.scene.control.TreeTableView;
import javafx.scene.control.TreeTableView.TreeTableViewSelectionModel; import javafx.scene.control.TreeTableView.TreeTableViewSelectionModel;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.input.TransferMode; import javafx.scene.input.TransferMode;
import javafx.util.Callback; import javafx.util.Callback;
...@@ -147,6 +148,23 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> { ...@@ -147,6 +148,23 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> {
return column; 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 * Adds a column to the table part of the view. The labels, context menus and icons are shown as
* defined in the {@link DynamicTreeTableUIProviderBase}. * 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