From: Niki Roo Date: Wed, 8 Apr 2020 15:47:23 +0000 (+0200) Subject: fix tree snapshot for changing sources/authors X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=commitdiff_plain;h=77e5ecd4fc25c052340fc00cc974972b2323d3d1 fix tree snapshot for changing sources/authors --- diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java index e9e8edc..24b0d1f 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 6abb464..63907dc 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 e202544..ad734a1 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); }