fix tree snapshot for changing sources/authors
authorNiki Roo <niki@nikiroo.be>
Wed, 8 Apr 2020 15:47:23 +0000 (17:47 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 8 Apr 2020 15:47:23 +0000 (17:47 +0200)
src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java
src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java
src/be/nikiroo/fanfix_swing/gui/utils/TreeSnapshot.java

index e9e8edc018f8f4e7880affb2c6c10bc10eaa32a7..24b0d1f71bc21851347d1dedca7f9f7a7268f969 100644 (file)
@@ -98,7 +98,20 @@ public abstract class BasicTab<T> 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<Map<String, List<String>>, Integer> worker = new SwingWorker<Map<String, List<String>>, Integer>() {
                        @Override
                        protected Map<String, List<String>> doInBackground() throws Exception {
@@ -205,8 +218,7 @@ public abstract class BasicTab<T> 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);
                        }
index 6abb46433499bac4fd4fc82b4762c1a30e0b3219..63907dc65767ada5ad72670177e5aa55b9b2fa75 100644 (file)
@@ -38,6 +38,10 @@ public class SourceTab extends BasicTab<Map<String, List<String>>> {
 
        @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);
index e202544d7627f0c30cdce2844e22a458ce8de820..ad734a1448f3e2de3c678a2b462e76b71a8dca86 100644 (file)
@@ -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);
        }