reloadData and prepare second (well, third..) loadData mode on BooksPanel
authorNiki Roo <niki@nikiroo.be>
Mon, 20 Apr 2020 16:11:14 +0000 (18:11 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 20 Apr 2020 16:11:14 +0000 (18:11 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java
src/be/nikiroo/fanfix_swing/gui/MainFrame.java
src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java
src/be/nikiroo/fanfix_swing/gui/utils/ListenerPanel.java

index 93f2998fd6f3d416d8f143195743a6a7449d06f4..20b6886b34021bf9672a3e3aa5dd6fe8b0cea962 100644 (file)
@@ -47,6 +47,8 @@ public class BooksPanel extends ListenerPanel {
        private DelayWorker bookCoverUpdater;
        private String filter = "";
 
+       private Object[] lastLoad = new Object[4];
+
        public BooksPanel(boolean listMode) {
                setLayout(new BorderLayout());
 
@@ -72,8 +74,15 @@ public class BooksPanel extends ListenerPanel {
        // null or empty -> all sources
        // sources hierarchy supported ("source/" will includes all "source" and
        // "source/*")
-       public void load(final List<String> sources, final List<String> authors,
+       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;
+               }
+
                new SwingWorker<List<BookInfo>, Void>() {
                        @Override
                        protected List<BookInfo> doInBackground() throws Exception {
@@ -90,7 +99,7 @@ public class BooksPanel extends ListenerPanel {
                        @Override
                        protected void done() {
                                try {
-                                       load(get());
+                                       loadData(get());
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                } catch (ExecutionException e) {
@@ -101,7 +110,22 @@ public class BooksPanel extends ListenerPanel {
                }.execute();
        }
 
-       public void load(List<BookInfo> bookInfos) {
+       public void loadData(final BookInfo.Type type, final String value) {
+               synchronized (lastLoad) {
+                       lastLoad[0] = "type";
+                       lastLoad[1] = type;
+                       lastLoad[2] = value;
+               }
+
+               // TODO todo todo
+       }
+
+       public void loadData(List<BookInfo> bookInfos) {
+               synchronized (lastLoad) {
+                       lastLoad[0] = "bookinfos";
+                       lastLoad[1] = bookInfos;
+               }
+
                data.clearItems();
                data.addAllItems(bookInfos);
                bookCoverUpdater.clear();
@@ -109,6 +133,30 @@ public class BooksPanel extends ListenerPanel {
                filter();
        }
 
+       public void reloadData() {
+               Object[] lastLoad;
+               synchronized (this.lastLoad) {
+                       lastLoad = this.lastLoad.clone();
+               }
+
+               if (lastLoad[0] == null) {
+                       return; // nothing was loaded yet
+               }
+
+               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 {
+                       Instance.getInstance().getTraceHandler()
+                                       .error("Unknown last load type: " + lastLoad[0]);
+               }
+       }
+
+       // is UI!
        private void filter() {
                data.filter(new Predicate<BookInfo>() {
                        @Override
index ac7eaff4d32bd794dac53cf0ee6fcc451f6ae468..8ac78396d6b2cfb2c991441704aff5be76c16b66 100644 (file)
@@ -213,7 +213,7 @@ public class BrowserPanel extends ListenerPanel {
        }
 
        /**
-        * Reload all the data from the 3 tabs.
+        * Reload all the data from the 3 tabs (without firing an action).
         */
        public void reloadData() {
                sourceTab.reloadData();
index 948f3ccd3eb3160b1cba0a93abf9b1933453460c..f74db0ae1846a3d674a56a2bf7e14ac17310800b 100644 (file)
@@ -52,7 +52,7 @@ public class MainFrame extends JFrame {
                browser.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               books.load(browser.getSelectedSources(),
+                               books.loadData(browser.getSelectedSources(),
                                                browser.getSelectedAuthors(),
                                                browser.getSelectedTags());
                                details.setBook(browser.getHighlight());
@@ -100,9 +100,7 @@ public class MainFrame extends JFrame {
                                        @Override
                                        public void run() {
                                                browser.reloadData();
-                                               books.load(browser.getSelectedSources(),
-                                                               browser.getSelectedAuthors(),
-                                                               browser.getSelectedTags());
+                                               books.reloadData();
                                                details.setBook(browser.getHighlight());
                                        }
                                });
@@ -117,9 +115,7 @@ public class MainFrame extends JFrame {
                                        @Override
                                        public void run() {
                                                browser.reloadData();
-                                               books.load(browser.getSelectedSources(),
-                                                               browser.getSelectedAuthors(),
-                                                               browser.getSelectedTags());
+                                               books.reloadData();
                                                details.setBook(browser.getHighlight());
                                        }
                                });
index c7f1e424421e6a09aede3527b29c8f8d54eba388..a4d675187443e7af2eb2ffdcb5ea97637ad9a7d3 100644 (file)
@@ -5,7 +5,6 @@ import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
@@ -94,14 +93,19 @@ public abstract class BasicTab<T> extends ListenerPanel {
                searchBar.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               reloadData();
+                               reloadData(true);
                        }
                });
 
-               reloadData();
+               reloadData(true);
        }
 
+       // does NOT send a change event
        public void reloadData() {
+               reloadData(false);
+       }
+
+       private void reloadData(final boolean fireActionPerformed) {
                final TreeSnapshot snapshot = new TreeSnapshot(tree) {
                        @Override
                        protected boolean isSamePath(TreePath oldPath, TreePath newPath) {
@@ -140,7 +144,9 @@ public abstract class BasicTab<T> extends ListenerPanel {
 
                                snapshot.apply();
 
-                               fireActionPerformed(listenerCommand);
+                               if (fireActionPerformed) {
+                                       fireActionPerformed(listenerCommand);
+                               }
                        }
                };
                worker.execute();
index 9e321fd972f163a5dd781943bdada94486380e1b..9dc8dfe80fcdd29e570355b55f5943c361e05c7e 100644 (file)
@@ -7,8 +7,6 @@ import java.util.Queue;
 
 import javax.swing.JPanel;
 
-import be.nikiroo.fanfix_swing.gui.SearchBar;
-
 /**
  * A {@link JPanel} with the default {@link ActionListener} add/remove/fire
  * methods.
@@ -54,7 +52,7 @@ public class ListenerPanel extends JPanel {
 
        /**
         * Adds the specified action listener to receive action events from this
-        * {@link SearchBar}.
+        * {@link ListenerPanel}.
         *
         * @param listener
         *            the action listener to be added
@@ -71,7 +69,7 @@ public class ListenerPanel extends JPanel {
 
        /**
         * Removes the specified action listener so that it no longer receives
-        * action events from this {@link SearchBar}.
+        * action events from this {@link ListenerPanel}.
         *
         * @param listener
         *            the action listener to be removed