fix reload change bookspanel
authorNiki Roo <niki@nikiroo.be>
Mon, 27 Apr 2020 20:20:55 +0000 (22:20 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 27 Apr 2020 20:20:55 +0000 (22:20 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/BrowserTab.java

index 8f18237a192ca38ac9bd8fdaae80d2c34b396d31..863c37d4042282cc37ffa90d677a48e234bd91cd 100644 (file)
@@ -24,6 +24,7 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix_swing.gui.book.BookBlock;
 import be.nikiroo.fanfix_swing.gui.book.BookInfo;
+import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type;
 import be.nikiroo.fanfix_swing.gui.book.BookLine;
 import be.nikiroo.fanfix_swing.gui.book.BookPopup;
 import be.nikiroo.fanfix_swing.gui.book.BookPopup.Informer;
@@ -31,15 +32,47 @@ import be.nikiroo.utils.compat.JList6;
 import be.nikiroo.utils.compat.ListCellRenderer6;
 import be.nikiroo.utils.ui.DelayWorker;
 import be.nikiroo.utils.ui.ListModel;
-import be.nikiroo.utils.ui.ListSnapshot;
 import be.nikiroo.utils.ui.ListModel.Predicate;
+import be.nikiroo.utils.ui.ListSnapshot;
 import be.nikiroo.utils.ui.ListenerPanel;
-import be.nikiroo.utils.ui.TreeSnapshot;
 import be.nikiroo.utils.ui.UIUtils;
 
 public class BooksPanel extends ListenerPanel {
        static public final String INVALIDATE_CACHE = "invalidate_cache";
 
+       private enum ReloadMode {
+               NONE, STA, TYPE_VALUE
+       }
+
+       private class ReloadData {
+               public ReloadMode mode;
+
+               public List<String> sources;
+               public List<String> authors;
+               public List<String> tags;
+
+               public Type type;
+               public String value;
+
+               public ReloadData() {
+                       this.mode = ReloadMode.NONE;
+               }
+
+               public ReloadData(List<String> sources, List<String> authors,
+                               List<String> tags) {
+                       this.mode = ReloadMode.STA;
+                       this.sources = sources;
+                       this.authors = authors;
+                       this.tags = tags;
+               }
+
+               public ReloadData(Type type, String value) {
+                       this.mode = ReloadMode.TYPE_VALUE;
+                       this.type = type;
+                       this.value = value;
+               }
+       }
+
        private Map<BookInfo, BookLine> books = new HashMap<BookInfo, BookLine>();
        private boolean seeWordCount;
        private boolean listMode;
@@ -53,7 +86,7 @@ public class BooksPanel extends ListenerPanel {
        private BooksPanelActions actions;
        private BookPopup popup;
 
-       private Object[] lastLoad = new Object[4];
+       private ReloadData lastLoad = new ReloadData();
 
        public BooksPanel(boolean listMode) {
                setLayout(new BorderLayout());
@@ -83,12 +116,13 @@ public class BooksPanel extends ListenerPanel {
        public void loadData(final List<String> sources, final List<String> authors,
                        final List<String> tags) {
                synchronized (lastLoad) {
-                       lastLoad[0] = "sources, authors, tags";
-                       lastLoad[1] = sources;
-                       lastLoad[2] = authors;
-                       lastLoad[3] = tags;
+                       lastLoad = new ReloadData(sources, authors, tags);
                }
-               
+
+               if(sources.size()==0) {
+                       new Exception().printStackTrace();
+               }
+
                new SwingWorker<List<BookInfo>, Void>() {
                        @Override
                        protected List<BookInfo> doInBackground() throws Exception {
@@ -117,11 +151,9 @@ public class BooksPanel extends ListenerPanel {
        }
 
        // TODO
-       private void loadData(final BookInfo.Type type, final String value) {
+       private void loadData(final Type type, final String value) {
                synchronized (lastLoad) {
-                       lastLoad[0] = "type";
-                       lastLoad[1] = type;
-                       lastLoad[2] = value;
+                       lastLoad = new ReloadData(type, value);
                }
 
                // TODO todo todo
@@ -142,30 +174,31 @@ public class BooksPanel extends ListenerPanel {
        }
 
        public void reloadData() {
-               Object[] lastLoad;
+               ReloadData lastLoad;
                synchronized (this.lastLoad) {
-                       lastLoad = this.lastLoad.clone();
+                       lastLoad = this.lastLoad;
                }
 
                // Reset the popup menu items for for sources/author
                popup.reloadData();
 
-               if (lastLoad[0] == null) {
+               if (lastLoad.mode == ReloadMode.NONE) {
                        return; // nothing was loaded yet
                }
 
                ListSnapshot snapshot = new ListSnapshot(list);
 
-               if (lastLoad[0].toString().equals("sources, authors, tags")) {
-                       loadData((List<String>) lastLoad[1], (List<String>) lastLoad[2],
-                                       (List<String>) lastLoad[3]);
-               } else if (lastLoad[0].toString().equals("type")) {
-                       loadData((BookInfo.Type) lastLoad[1], (String) lastLoad[2]);
-               } else if (lastLoad[0].toString().equals("bookInfos")) {
-                       loadData((List<BookInfo>) lastLoad[1]);
-               } else {
+               switch (lastLoad.mode) {
+               case STA:
+                       loadData(lastLoad.sources, lastLoad.authors, lastLoad.tags);
+                       break;
+               case TYPE_VALUE:
+                       loadData(lastLoad.type, lastLoad.value);
+                       break;
+               default:
                        Instance.getInstance().getTraceHandler()
-                                       .error("Unknown last load type: " + lastLoad[0]);
+                                       .error("Unknown last load type: " + lastLoad.mode);
+                       break;
                }
 
                snapshot.apply();
index c923b6a6546486ff25739d6e7c9873bbd52bc6b1..d453bdef25ef04cba470a29b7d7ede20adba8446 100644 (file)
@@ -5,6 +5,7 @@ import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
@@ -40,6 +41,7 @@ public class BrowserTab extends ListenerPanel {
        private int index;
 
        private JTree tree;
+       private TreeSelectionListener treeListener;
        private DefaultMutableTreeNode root;
        private DataTree<DataNodeBook> data;
        private SearchBar searchBar;
@@ -58,14 +60,7 @@ public class BrowserTab extends ListenerPanel {
                root = new DefaultMutableTreeNode();
                tree = new JTree(root);
 
-               tree.setUI(new BasicTreeUI());
-               TreeCellSpanner spanner = new TreeCellSpanner(tree,
-                               generateCellRenderer());
-               tree.setCellRenderer(spanner);
-               tree.setRootVisible(false);
-               tree.setShowsRootHandles(false);
-
-               tree.addTreeSelectionListener(new TreeSelectionListener() {
+               treeListener = new TreeSelectionListener() {
                        @Override
                        public void valueChanged(TreeSelectionEvent e) {
                                List<String> elements = new ArrayList<String>();
@@ -84,11 +79,36 @@ public class BrowserTab extends ListenerPanel {
                                        }
                                }
 
-                               BrowserTab.this.selectedElements = elements;
+                               Collections.sort(elements);
+
+                               boolean same = false;
+                               if (BrowserTab.this.selectedElements.size() == elements
+                                               .size()) {
+                                       same = true;
+                                       for (int i = 0; i < elements.size(); i++) {
+                                               String newEl = elements.get(i);
+                                               String oldEl = BrowserTab.this.selectedElements.get(i);
+                                               if (!newEl.equals(oldEl)) {
+                                                       same = false;
+                                                       break;
+                                               }
+                                       }
+                               }
 
-                               fireActionPerformed(BrowserTab.this.listenerCommand);
+                               if (!same) {
+                                       BrowserTab.this.selectedElements = elements;
+                                       fireActionPerformed(BrowserTab.this.listenerCommand);
+                               }
                        }
-               });
+               };
+
+               tree.setUI(new BasicTreeUI());
+               TreeCellSpanner spanner = new TreeCellSpanner(tree,
+                               generateCellRenderer());
+               tree.setCellRenderer(spanner);
+               tree.setRootVisible(false);
+               tree.setShowsRootHandles(false);
+               tree.addTreeSelectionListener(treeListener);
 
                add(UIUtils.scroll(tree, false), BorderLayout.CENTER);
 
@@ -125,9 +145,14 @@ public class BrowserTab extends ListenerPanel {
                node2node(root, filtered);
                totalCount = filtered.count() - 1; // root is counted
 
+               tree.removeTreeSelectionListener(treeListener);
                ((DefaultTreeModel) tree.getModel()).reload();
-
                snapshot.apply();
+               tree.addTreeSelectionListener(treeListener);
+
+               // Try to fire it (it will not do anything if no selection changed)
+               treeListener.valueChanged(
+                               new TreeSelectionEvent(this, null, false, null, null));
 
                if (fireActionPerformed) {
                        fireActionPerformed(listenerCommand);