From 05a1fd8e8dccfba98c5988d0a18de2cb871984e6 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 23 Aug 2023 12:18:02 +0200 Subject: [PATCH] refactor cointreetable column sorting, add default sorting if table was empty --- .../sparrowwallet/sparrow/control/CoinTreeTable.java | 9 +++++++++ .../sparrow/control/TransactionsTreeTable.java | 10 +++------- .../sparrowwallet/sparrow/control/UtxosTreeTable.java | 10 +++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java index 911a25be..e292849e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java @@ -18,6 +18,7 @@ import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.control.Hyperlink; import javafx.scene.control.Label; +import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableView; import javafx.scene.layout.StackPane; @@ -133,4 +134,12 @@ public class CoinTreeTable extends TreeTableView { stackPane.setAlignment(Pos.CENTER); return stackPane; } + + public void setSortColumn(int columnIndex, TreeTableColumn.SortType sortType) { + if(columnIndex >= 0 && columnIndex < getColumns().size() && getSortOrder().isEmpty() && !getRoot().getChildren().isEmpty()) { + TreeTableColumn column = getColumns().get(columnIndex); + column.setSortType(sortType == null ? TreeTableColumn.SortType.DESCENDING : sortType); + getSortOrder().add(column); + } + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/TransactionsTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/TransactionsTreeTable.java index 1d6ea82a..5bf37533 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/TransactionsTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/TransactionsTreeTable.java @@ -50,8 +50,7 @@ public class TransactionsTreeTable extends CoinTreeTable { setPlaceholder(getDefaultPlaceholder(rootEntry.getWallet())); setEditable(true); setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY); - dateCol.setSortType(TreeTableColumn.SortType.DESCENDING); - getSortOrder().add(dateCol); + setSortColumn(0, TreeTableColumn.SortType.DESCENDING); } public void updateAll(WalletTransactionsEntry rootEntry) { @@ -61,16 +60,13 @@ public class TransactionsTreeTable extends CoinTreeTable { setRoot(rootItem); rootItem.setExpanded(true); - if(getColumns().size() > 0 && getSortOrder().isEmpty()) { - TreeTableColumn dateCol = getColumns().get(0); - getSortOrder().add(dateCol); - dateCol.setSortType(TreeTableColumn.SortType.DESCENDING); - } + setSortColumn(0, TreeTableColumn.SortType.DESCENDING); } public void updateHistory() { //Transaction entries should have already been updated using WalletTransactionsEntry.updateHistory, so only a resort required sort(); + setSortColumn(0, TreeTableColumn.SortType.DESCENDING); } public void updateLabel(Entry entry) { diff --git a/src/main/java/com/sparrowwallet/sparrow/control/UtxosTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/UtxosTreeTable.java index e790a9c8..f937e167 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/UtxosTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/UtxosTreeTable.java @@ -83,8 +83,7 @@ public class UtxosTreeTable extends CoinTreeTable { setPlaceholder(getDefaultPlaceholder(rootEntry.getWallet())); setEditable(true); setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY); - amountCol.setSortType(TreeTableColumn.SortType.DESCENDING); - getSortOrder().add(amountCol); + setSortColumn(getColumns().size() - 1, TreeTableColumn.SortType.DESCENDING); getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); } @@ -96,17 +95,14 @@ public class UtxosTreeTable extends CoinTreeTable { setRoot(rootItem); rootItem.setExpanded(true); - if(getColumns().size() > 0 && getSortOrder().isEmpty()) { - TreeTableColumn amountCol = getColumns().get(getColumns().size() - 1); - getSortOrder().add(amountCol); - amountCol.setSortType(TreeTableColumn.SortType.DESCENDING); - } + setSortColumn(getColumns().size() - 1, TreeTableColumn.SortType.DESCENDING); } public void updateHistory() { //Utxo entries should have already been updated, so only a resort required if(!getRoot().getChildren().isEmpty()) { sort(); + setSortColumn(getColumns().size() - 1, TreeTableColumn.SortType.DESCENDING); } }