diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings index 1d719596aadb4771c489581b90bd5a6dcde4f410..ab2968d5dad31d022886e0b813f8b51761c1e7fa 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings @@ -1,7 +1,7 @@ DynamicTreeContentProviderBase.java dff437afeaf7486af05460fa54eca4fa61d7eae6 GREEN DynamicTreeItem.java afc105cf5acf3d2506d89e0892555100c234ce5b GREEN -DynamicTreeTableUIProviderBase.java fd9fce19a65eb1006ceacb0d869bbe90a8c578b3 GREEN -DynamicTreeTableViewer.java 4f278387dd90542adc07bf0da12e92d4eaad79c4 GREEN +DynamicTreeTableUIProviderBase.java e1bd00b999db856f5b928f6edd1a11d6aa9f4a83 YELLOW +DynamicTreeTableViewer.java 4eb55f9b0b0bb3c9198fe192d43f3bcb29643f12 YELLOW DynamicTreeUIProviderBase.java 56fe4df4577b35f1e5e6e4c4be189b706c852d52 GREEN DynamicTreeViewer.java da5e24ae57777a482d8e12c8262513d8143bfa93 GREEN DynamicTreeViewerBase.java 47124c847de322a0ae26eb7a114f85ce4bd02d7e GREEN diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java index fd9fce19a65eb1006ceacb0d869bbe90a8c578b3..e1bd00b999db856f5b928f6edd1a11d6aa9f4a83 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java @@ -13,14 +13,19 @@ *******************************************************************************/ package org.fortiss.tooling.common.ui.javafx.control.treetableview; +import static javafx.scene.control.cell.TextFieldTreeTableCell.forTreeTableColumn; + import javafx.scene.Node; import javafx.scene.control.ContextMenu; +import javafx.scene.control.TreeTableCell; +import javafx.scene.control.TreeTableColumn; +import javafx.util.Callback; /** * This UI provider is responsible to return the label, the icon, and the context menu for each cell * in the {@link DynamicTreeTableViewer} based on the data object and the column. */ -public abstract class DynamicTreeTableUIProviderBase { +public abstract class DynamicTreeTableUIProviderBase<T> { /** * @param element * the element to be displayed in the current row @@ -28,7 +33,7 @@ public abstract class DynamicTreeTableUIProviderBase { * the current column * @return the label to be displayed in the given column */ - public String getLabel(Object element, int column) { + public String getLabel(T element, int column) { return ""; } @@ -39,7 +44,7 @@ public abstract class DynamicTreeTableUIProviderBase { * the current column * @return the node to be displayed as the icon in the given column */ - public Node getIconNode(Object element, int column) { + public Node getIconNode(T element, int column) { return null; } @@ -50,7 +55,80 @@ public abstract class DynamicTreeTableUIProviderBase { * the current column * @return the context menu in the given column */ - public ContextMenu createContextMenu(Object element, int column) { + public ContextMenu createContextMenu(T element, int column) { return null; } + + /** + * Returns whether the given column is editable. + * + * @param column + * the column index + * @return whether the column is editable + */ + public boolean isEditable(int column) { + return false; + } + + /** + * Updates the value after the given column was edited for the given element. + * + * @param element + * the element to be edited in the current row + * @param column + * the currently edited column + * @param value + * the edited value + */ + public void updateValue(T element, int column, String value) { + // ignored, since not editable + } + + /** Applies the editing support to the given column. */ + /* package */ final void applyToColumn(int i, TreeTableColumn<T, String> column) { + if(!isEditable(i)) { + column.setCellFactory(createReadOnlyCellFactory(i)); + column.setEditable(false); + column.setOnEditCommit(null); + return; + } + column.setCellFactory(createEditableCellFactory()); + column.setEditable(true); + column.setOnEditCommit(event -> { + T element = event.getRowValue().getValue(); + int colIndex = event.getTreeTablePosition().getColumn(); + String value = event.getNewValue(); + updateValue(element, colIndex, value); + }); + } + + /** Creates a cell factory for editable cells. */ + private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>> + createEditableCellFactory() { + return forTreeTableColumn(); + } + + /** Creates a cell factory for read-only cells. */ + private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>> + createReadOnlyCellFactory(int colIndex) { + return 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; + if(!empty && item != null) { + T data = this.getTreeTableRow().getItem(); + menu = createContextMenu(data, colIndex); + icon = getIconNode(data, colIndex); + } + this.setContextMenu(menu); + this.setGraphic(icon); + } + }; + cell.textProperty().bind(cell.itemProperty()); + return cell; + }; + } } diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableViewer.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableViewer.java index 4f278387dd90542adc07bf0da12e92d4eaad79c4..4eb55f9b0b0bb3c9198fe192d43f3bcb29643f12 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableViewer.java +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableViewer.java @@ -15,22 +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.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.TreeTableView; -import javafx.util.Callback; /** - * A JavaFX {@link TreeTableView} based on an {@link DynamicTreeContentProviderBase} and - * {@link DynamicTreeTableUIProviderBase}. This class is intended to be used as a wrapper for - * {@link TreeTableView} and its rather complex expert API. + * A JavaFX {@link TreeTableView} based on an {@link DynamicTreeContentProviderBase}, + * {@link DynamicTreeTableUIProviderBase}, and {@link DynamicTreeTableEditingSupportBase}. This + * class is intended to be used as a wrapper for {@link TreeTableView} and its rather complex expert + * API. * <P> * This class currently supports only a static tree-table layout, i.e., there is no way to alter the * columns unless the developer changes the underlying {@link TreeTableView} directly. Note that if @@ -42,12 +37,12 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> { /** The {@link TreeTableView} control to be managed. */ private final TreeTableView<T> view; /** The UI provider of this tree-table. */ - private final DynamicTreeTableUIProviderBase uiProvider; + private final DynamicTreeTableUIProviderBase<T> uiProvider; /** Constructor. */ public DynamicTreeTableViewer(TreeTableView<T> view, T root, boolean showRoot, int revealLevel, DynamicTreeContentProviderBase<T> contentProvider, - DynamicTreeTableUIProviderBase uiProvider) { + DynamicTreeTableUIProviderBase<T> uiProvider) { super(contentProvider); this.uiProvider = uiProvider; // construct view @@ -65,7 +60,7 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> { /** Constructor. */ public DynamicTreeTableViewer(T root, boolean showRoot, int revealLevel, DynamicTreeContentProviderBase<T> contentProvider, - DynamicTreeTableUIProviderBase uiProvider) { + DynamicTreeTableUIProviderBase<T> uiProvider) { this(new TreeTableView<T>(), root, showRoot, revealLevel, contentProvider, uiProvider); } @@ -92,51 +87,33 @@ public final class DynamicTreeTableViewer<T> extends DynamicTreeViewerBase<T> { * Adds a column to the table part of the view. The labels, context menus and icons are shown as * defined in the {@link DynamicTreeTableUIProviderBase}. */ - public TreeTableColumn<T, String> addColumn(String headerLabel, int prefWidth, - boolean readOnly) { + public TreeTableColumn<T, String> addColumn(String headerLabel, int prefWidth) { int num = view.getColumns().size(); TreeTableColumn<T, String> column = new TreeTableColumn<>(headerLabel); column.setPrefWidth(prefWidth); column.setCellValueFactory(param -> { - Object data = param.getValue().getValue(); + T data = param.getValue().getValue(); return new SimpleObjectProperty<String>(uiProvider.getLabel(data, num)); }); - 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); - } - }; - cell.textProperty().bind(cell.itemProperty()); - return cell; - }; - } else { - // Editable text field - cellFactory = forTreeTableColumn(); - } - column.setCellFactory(cellFactory); - + uiProvider.applyToColumn(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}. + * + * @deprecated use {@link DynamicTreeTableUIProviderBase} + */ + @Deprecated + public TreeTableColumn<T, String> addColumn(String headerLabel, int prefWidth, + boolean readOnly) { + return addColumn(headerLabel, prefWidth); + } + /** Returns the underlying {@link TreeTableView}. */ public TreeTableView<T> getControl() { return view;