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

Grouping #488

Open
wants to merge 4 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package org.cirdles.topsoil.app.control.data;

import com.sun.javafx.scene.control.skin.TreeTableViewSkin;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.layout.Region;
import javafx.scene.text.TextAlignment;
import javafx.util.Callback;
import javafx.util.StringConverter;
import javafx.util.converter.DefaultStringConverter;
import org.cirdles.topsoil.app.control.undo.UndoAction;
Expand Down Expand Up @@ -108,6 +114,63 @@ public FXDataTableViewer(FXDataTable table) {
updateFractionDigitsForLeafColumns();
refreshCells();
});

// MULTI-SELECT
treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

treeTableView.setRowFactory(new Callback<TreeTableView<FXDataRow>, TreeTableRow<FXDataRow>>() {
@Override
public TreeTableRow<FXDataRow> call(TreeTableView<FXDataRow> param) {
final TreeTableRow<FXDataRow> row = new TreeTableRow<>();
final ContextMenu rowMenu = new ContextMenu();
ContextMenu tableMenu = treeTableView.getContextMenu();

MenuItem editItem = new MenuItem("Group");
editItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// todo: add popup for naming the group and specifying the plot color
// todo: prevent nested grouping
System.out.println("ActionEvent handled this group: " + treeTableView.getSelectionModel().getSelectedItems());
ObservableList<TreeItem<FXDataRow>> itemsForGrouping = treeTableView.getSelectionModel().getSelectedItems();
TreeItem<FXDataRow> firstItem = itemsForGrouping.get(0);

FXDataRow groupRow = new FXDataRow("group", true);
groupRow.setGroupProperty(true);
TreeItem<FXDataRow> groupItem = new TreeItem<FXDataRow>(groupRow);

// Copies content into new TreeItem objects instead of using groupItem.getChildren().addAll(itemsForGrouping)
// because the latter caused an issue with spacing
boolean isGroupable = true;
for (TreeItem<FXDataRow> item : itemsForGrouping) {
FXDataRow fxDataRowContent = item.getValue();

if (!fxDataRowContent.getGroupProperty()) {
groupItem.getChildren().add(new TreeItem<FXDataRow>(fxDataRowContent));
} else {
isGroupable = false;
break;
}
}

if (isGroupable) {
int indexOfFirstSelection = treeTableView.getSelectionModel().getSelectedIndices().get(0);
treeTableView.getRoot().getChildren().add(indexOfFirstSelection, groupItem);
treeTableView.getRoot().getChildren().removeAll(itemsForGrouping);
}

treeTableView.getSelectionModel().clearSelection();
}
});

rowMenu.getItems().addAll(editItem);
row.contextMenuProperty().bind(
Bindings.when(Bindings.isNotNull(row.itemProperty()))
.then(rowMenu)
.otherwise((ContextMenu) null));
return row;
}
});
}

//**********************************************//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

public class FXDataRow extends FXDataComponent<DataRow> implements DataRow {

private boolean isGroup = false;

private ListProperty<FXDataRow> childRows = new SimpleListProperty<>(
// Initializes the list property with an observable list that listens to changes in the properties of each
// row
Expand All @@ -36,6 +38,15 @@ public class FXDataRow extends FXDataComponent<DataRow> implements DataRow {
public final ReadOnlyListProperty<FXDataRow> childRowsProperty() {
return childRows;
}

public boolean getGroupProperty() {
return isGroup;
}

public void setGroupProperty(boolean newProperty) {
this.isGroup = newProperty;
}

@Override
public final ObservableList<FXDataRow> getChildren() {
return childRows.get();
Expand Down