From 77e5ecd4fc25c052340fc00cc974972b2323d3d1 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 8 Apr 2020 17:47:23 +0200 Subject: [PATCH] fix tree snapshot for changing sources/authors --- .../fanfix_swing/gui/browser/BasicTab.java | 18 +++++++++++++++--- .../fanfix_swing/gui/browser/SourceTab.java | 4 ++++ .../fanfix_swing/gui/utils/TreeSnapshot.java | 7 ++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java index e9e8edc0..24b0d1f7 100644 --- a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java @@ -98,7 +98,20 @@ public abstract class BasicTab extends ListenerPanel { } public void reloadData() { - final TreeSnapshot snapshot = new TreeSnapshot(tree); + final TreeSnapshot snapshot = new TreeSnapshot(tree) { + @Override + protected boolean isSamePath(TreePath oldPath, TreePath newPath) { + String oldString = oldPath.toString(); + if (oldString.endsWith("/]")) + oldString = oldString.substring(0, oldString.length() - 2) + "]"; + + String newString = newPath.toString(); + if (newString.endsWith("/]")) + newString = newString.substring(0, newString.length() - 2) + "]"; + + return oldString.equals(newString); + } + }; SwingWorker>, Integer> worker = new SwingWorker>, Integer>() { @Override protected Map> doInBackground() throws Exception { @@ -205,8 +218,7 @@ public abstract class BasicTab extends ListenerPanel { } String display = value == null ? "" : value.toString(); - if (!display.isEmpty()) - display = keyToDisplay(display); + display = keyToDisplay(display); return super.getTreeCellRendererComponent(tree, display, selected, expanded, leaf, row, hasFocus); } diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java index 6abb4643..63907dc6 100644 --- a/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java @@ -38,6 +38,10 @@ public class SourceTab extends BasicTab>> { @Override protected String keyToDisplay(String key) { + if (key.trim().isEmpty()) { + return "[*]"; // Root node + } + // Get and remove type String type = key.substring(0, 1); key = key.substring(1); diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/TreeSnapshot.java b/src/be/nikiroo/fanfix_swing/gui/utils/TreeSnapshot.java index e202544d..ad734a14 100644 --- a/src/be/nikiroo/fanfix_swing/gui/utils/TreeSnapshot.java +++ b/src/be/nikiroo/fanfix_swing/gui/utils/TreeSnapshot.java @@ -54,7 +54,7 @@ public class TreeSnapshot { TreePath newPath = nodeToPath(newNode); if (newPath != null) { for (TreePath path : selectionPaths) { - if (newPath.toString().equals(path.toString())) { + if (isSamePath(path, newPath)) { newSlectionPaths.add(newPath); if (expanded.contains(path)) { newExpanded.add(newPath); @@ -72,6 +72,11 @@ public class TreeSnapshot { tree.setSelectionPaths(newSlectionPaths.toArray(new TreePath[0])); } + // You can override this + protected boolean isSamePath(TreePath oldPath, TreePath newPath) { + return newPath.toString().equals(oldPath.toString()); + } + private void forEach(JTree tree, NodeAction action) { forEach(tree.getModel(), tree.getModel().getRoot(), action); } -- 2.27.0