From: Niki Roo Date: Mon, 27 Apr 2020 20:20:55 +0000 (+0200) Subject: fix reload change bookspanel X-Git-Tag: fanfix-swing-1.0.0~6 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=8fc322aa071e32de2dea2319bc373eb4480ddd77;p=fanfix-swing.git fix reload change bookspanel --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 8f18237a..863c37d4 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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 sources; + public List authors; + public List tags; + + public Type type; + public String value; + + public ReloadData() { + this.mode = ReloadMode.NONE; + } + + public ReloadData(List sources, List authors, + List 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 books = new HashMap(); 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 sources, final List authors, final List 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, Void>() { @Override protected List 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) 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 { + 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(); diff --git a/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java b/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java index c923b6a6..d453bdef 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java @@ -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 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 elements = new ArrayList(); @@ -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);