From 27ea0066a1bac89f2520e6fd643106ba64747074 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 20 Apr 2020 18:11:14 +0200 Subject: [PATCH] reloadData and prepare second (well, third..) loadData mode on BooksPanel --- .../nikiroo/fanfix_swing/gui/BooksPanel.java | 54 +++++++++++++++++-- .../fanfix_swing/gui/BrowserPanel.java | 2 +- .../nikiroo/fanfix_swing/gui/MainFrame.java | 10 ++-- .../fanfix_swing/gui/browser/BasicTab.java | 14 +++-- .../fanfix_swing/gui/utils/ListenerPanel.java | 6 +-- 5 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 93f2998f..20b6886b 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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 sources, final List authors, + public void loadData(final List sources, final List authors, final List tags) { + synchronized (lastLoad) { + lastLoad[0] = "sources, authors, tags"; + lastLoad[1] = sources; + lastLoad[2] = authors; + lastLoad[3] = tags; + } + new SwingWorker, Void>() { @Override protected List 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 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 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) lastLoad[1], (List) lastLoad[2], + (List) 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) lastLoad[1]); + } else { + Instance.getInstance().getTraceHandler() + .error("Unknown last load type: " + lastLoad[0]); + } + } + + // is UI! private void filter() { data.filter(new Predicate() { @Override diff --git a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java index ac7eaff4..8ac78396 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java @@ -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(); diff --git a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java index 948f3ccd..f74db0ae 100644 --- a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java @@ -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()); } }); diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java index c7f1e424..a4d67518 100644 --- a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java @@ -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 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 extends ListenerPanel { snapshot.apply(); - fireActionPerformed(listenerCommand); + if (fireActionPerformed) { + fireActionPerformed(listenerCommand); + } } }; worker.execute(); diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/ListenerPanel.java b/src/be/nikiroo/fanfix_swing/gui/utils/ListenerPanel.java index 9e321fd9..9dc8dfe8 100644 --- a/src/be/nikiroo/fanfix_swing/gui/utils/ListenerPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/utils/ListenerPanel.java @@ -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 -- 2.27.0