From: Niki Roo Date: Mon, 27 Apr 2020 15:41:50 +0000 (+0200) Subject: DataTree changes X-Git-Tag: fanfix-swing-1.0.0~13 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=ad5f0d05bf5d3ccff2732734ae6550673a722747;p=fanfix-swing.git DataTree changes --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java b/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java index 4502e8d0..4e2d63c8 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java @@ -1,17 +1,12 @@ package be.nikiroo.fanfix_swing.gui; import java.io.IOException; -import java.util.Arrays; -import java.util.List; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix_swing.gui.book.BookInfo; -import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type; import be.nikiroo.fanfix_swing.gui.utils.DataNodeBook; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeAuthors; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeSources; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeTag; +import be.nikiroo.fanfix_swing.gui.utils.DataTreeBooks; import be.nikiroo.utils.ui.BreadCrumbsBar; import be.nikiroo.utils.ui.DataNode; import be.nikiroo.utils.ui.DataTree; @@ -19,32 +14,18 @@ import be.nikiroo.utils.ui.DataTree; public class BreadCrumbsPanel extends BreadCrumbsBar { public BreadCrumbsPanel() { super(new DataTree() { + private DataTreeBooks dataTreeBooks = new DataTreeBooks(false, + false, false); + @Override protected DataNode extractData() throws IOException { - List> children = null; - - children = new DataTreeSources(false).loadData().getChildren(); - DataNode sources = new DataNode( - children, new DataNodeBook(Type.SOURCE, "Sources", true, - !children.isEmpty())); - children = new DataTreeAuthors(false).loadData().getChildren(); - DataNode authors = new DataNode( - children, new DataNodeBook(Type.AUTHOR, "Authors", true, - !children.isEmpty())); - children = new DataTreeTag(false).loadData().getChildren(); - DataNode tags = new DataNode( - children, new DataNodeBook(Type.TAG, "Tags", true, - !children.isEmpty())); - - return new DataNode( - Arrays.asList(sources, authors, tags), - new DataNodeBook(null, false)); + return dataTreeBooks.loadData(); } @Override protected boolean checkFilter(String filter, DataNodeBook userData) { - return userData.toString().contains(filter.toLowerCase()); + return dataTreeBooks.checkFilter(filter, userData); } }); } diff --git a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java index 12840f52..2e48398b 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java @@ -10,15 +10,14 @@ import java.util.List; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTabbedPane; +import javax.swing.SwingWorker; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix_swing.gui.book.BookInfo; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeAuthors; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeSources; -import be.nikiroo.fanfix_swing.gui.utils.DataTreeTag; +import be.nikiroo.fanfix_swing.gui.utils.DataTreeBooks; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; import be.nikiroo.utils.ui.ListenerPanel; @@ -60,6 +59,8 @@ public class BrowserPanel extends ListenerPanel { */ static public final String TAB_CHANGE = "tab_change"; + private DataTreeBooks dataTreeBooks; + private JTabbedPane tabs; private BrowserTab sourceTab; private BrowserTab authorTab; @@ -77,11 +78,12 @@ public class BrowserPanel extends ListenerPanel { tabs = new JTabbedPane(); int index = 0; - tabs.add(sourceTab = new BrowserTab(new DataTreeSources(false), index++, + dataTreeBooks = new DataTreeBooks(false, true, true); + tabs.add(sourceTab = new BrowserTab(dataTreeBooks.getSources(), index++, SOURCE_SELECTION)); - tabs.add(authorTab = new BrowserTab(new DataTreeAuthors(true), index++, + tabs.add(authorTab = new BrowserTab(dataTreeBooks.getAuthors(), index++, AUTHOR_SELECTION)); - tabs.add(tagsTab = new BrowserTab(new DataTreeTag(true), index++, + tabs.add(tagsTab = new BrowserTab(dataTreeBooks.getTags(), index++, TAGS_SELECTION)); configureTab(tabs, sourceTab, "Sources", "Tooltip for Sources"); @@ -120,6 +122,8 @@ public class BrowserPanel extends ListenerPanel { fireActionPerformed(TAB_CHANGE); } }); + + reloadData(true); } private void unselect() { @@ -215,8 +219,24 @@ public class BrowserPanel extends ListenerPanel { * Reload all the data from the 3 tabs (without firing an action). */ public void reloadData() { - sourceTab.reloadData(); - authorTab.reloadData(); - tagsTab.reloadData(); + reloadData(false); + } + + public void reloadData(final boolean fireActionPerformed) { + new SwingWorker() { + @Override + protected Void doInBackground() throws Exception { + dataTreeBooks.loadData(); + return null; + } + + @Override + protected void done() { + sourceTab.filter(fireActionPerformed); + authorTab.filter(fireActionPerformed); + tagsTab.filter(fireActionPerformed); + + } + }.execute(); } } diff --git a/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java b/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java index 243402b4..c923b6a6 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/BrowserTab.java @@ -5,9 +5,11 @@ import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.swing.JTree; +import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; @@ -95,19 +97,12 @@ public class BrowserTab extends ListenerPanel { searchBar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - reloadData(true); + filter(true); } }); - - reloadData(true); - } - - // does NOT send a change event - public void reloadData() { - reloadData(false); } - private void reloadData(final boolean fireActionPerformed) { + public void filter(final boolean fireActionPerformed) { final TreeSnapshot snapshot = new TreeSnapshot(tree) { @Override protected boolean isSamePath(TreePath oldPath, TreePath newPath) { @@ -124,37 +119,19 @@ public class BrowserTab extends ListenerPanel { return oldString.equals(newString); } }; - SwingWorker worker = new SwingWorker() { - @Override - protected Void doInBackground() throws Exception { - data.loadData(); - return null; - } - - @Override - protected void done() { - try { - get(); - DataNode filtered = data - .getRoot(searchBar.getText()); + DataNode filtered = data.getRoot(searchBar.getText()); - node2node(root, filtered); - totalCount = filtered.count() - 1; // root is counted + node2node(root, filtered); + totalCount = filtered.count() - 1; // root is counted - ((DefaultTreeModel) tree.getModel()).reload(); + ((DefaultTreeModel) tree.getModel()).reload(); - snapshot.apply(); + snapshot.apply(); - if (fireActionPerformed) { - fireActionPerformed(listenerCommand); - } - } catch (Exception e) { - // TODO: error - } - } - }; - worker.execute(); + if (fireActionPerformed) { + fireActionPerformed(listenerCommand); + } } /** diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java deleted file mode 100644 index a964a8e8..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java +++ /dev/null @@ -1,42 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.utils; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type; -import be.nikiroo.utils.ui.DataNode; -import be.nikiroo.utils.ui.DataTree; - -public class DataTreeAuthors extends DataTreeSources { - public DataTreeAuthors(boolean flat) { - super(flat); - } - - @Override - protected boolean checkFilter(String filter, DataNodeBook userData) { - return userData.getSubnameOrName().toLowerCase() - .contains(filter.toLowerCase()); - } - - @Override - protected DataNode extractData() throws IOException { - if (isFlat()) { - return getNodeFlat( - Instance.getInstance().getLibrary().getList().getAuthors(), - Type.AUTHOR); - } - - Map> authorsGrouped = Instance.getInstance() - .getLibrary().getList().getAuthorsGrouped(); - - if (authorsGrouped.size() == 1) { - List authors = authorsGrouped.values().iterator().next(); - return getNodeFlat(authors, Type.AUTHOR); - } - - return getNodeGrouped(authorsGrouped, Type.AUTHOR); - } -} diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeBooks.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeBooks.java new file mode 100644 index 00000000..ad0c5c39 --- /dev/null +++ b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeBooks.java @@ -0,0 +1,238 @@ +package be.nikiroo.fanfix_swing.gui.utils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.library.BasicLibrary; +import be.nikiroo.fanfix.library.MetaResultList; +import be.nikiroo.fanfix_swing.gui.book.BookInfo; +import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type; +import be.nikiroo.utils.ui.DataNode; +import be.nikiroo.utils.ui.DataTree; + +public class DataTreeBooks { + abstract private class DataTreeSort extends DataTree { + // make it PUBLIC instead of PROTECTED + @Override + abstract public boolean checkFilter(String filter, + DataNodeBook userData); + + @Override + public void sort(List values) { + super.sort(values); + } + + @Override + public DataNode loadData() throws IOException { + return loadData(true); + } + + public DataNode loadData(boolean reload) + throws IOException { + if (reload) { + reload(); + } + + return data = extractData(); + } + + abstract public void reload() throws IOException; + } + + private DataTreeSort sources; + private DataTreeSort authors; + private DataTreeSort tags; + + private MetaResultList list; + + // flat = force flat mode (no [A-B] groups) + public DataTreeBooks(final boolean flatSources, final boolean flatAuthors, + final boolean flatTags) { + list = new MetaResultList(null); + + sources = new DataTreeSort() { + @Override + public boolean checkFilter(String filter, DataNodeBook userData) { + return userData.getName().toLowerCase() + .contains(filter.toLowerCase()) + || userData.getSubname().toLowerCase() + .contains(filter.toLowerCase()); + } + + @Override + protected DataNode extractData() throws IOException { + if (flatSources) { + return getNodeFlat(list.getSources(), Type.SOURCE); + } + + Map> sourcesGrouped = list + .getSourcesGrouped(); + return getNodeGrouped(this, sourcesGrouped, Type.SOURCE); + } + + @Override + public void reload() throws IOException { + reloadList(); + } + }; + + authors = new DataTreeSort() { + @Override + public boolean checkFilter(String filter, DataNodeBook userData) { + return userData.getSubnameOrName().toLowerCase() + .contains(filter.toLowerCase()); + } + + @Override + protected DataNode extractData() throws IOException { + if (flatAuthors) { + return getNodeFlat(list.getAuthors(), Type.AUTHOR); + } + + Map> authorsGrouped = list + .getAuthorsGrouped(); + + if (authorsGrouped.size() == 1) { + List authors = authorsGrouped.values().iterator() + .next(); + return getNodeFlat(authors, Type.AUTHOR); + } + + return getNodeGrouped(this, authorsGrouped, Type.AUTHOR); + } + + @Override + public void reload() throws IOException { + reloadList(); + } + }; + + tags = new DataTreeSort() { + @Override + public boolean checkFilter(String filter, DataNodeBook userData) { + return userData.getSubnameOrName().toLowerCase() + .contains(filter.toLowerCase()); + } + + @Override + protected DataNode extractData() throws IOException { + if (flatTags) { + return getNodeFlat(list.getTags(), Type.TAG); + } + + return getNodeGrouped(this, list.getTagsGrouped(), Type.TAG); + } + + @Override + public void reload() throws IOException { + reloadList(); + } + }; + } + + public DataTree getSources() { + return sources; + } + + public DataTree getAuthors() { + return authors; + } + + public DataTree getTags() { + return tags; + } + + public DataNode loadData() throws IOException { + reloadList(); + + List> children = null; + + children = sources.loadData(false).getChildren(); + DataNode sources = new DataNode(children, + new DataNodeBook(Type.SOURCE, "Sources", true, + !children.isEmpty())); + + children = authors.loadData(false).getChildren(); + DataNode authors = new DataNode(children, + new DataNodeBook(Type.AUTHOR, "Authors", true, + !children.isEmpty())); + + children = tags.loadData(false).getChildren(); + DataNode tags = new DataNode(children, + new DataNodeBook(Type.TAG, "Tags", true, !children.isEmpty())); + + return new DataNode(Arrays.asList(sources, authors, tags), + new DataNodeBook(null, false)); + } + + public boolean checkFilter(String filter, DataNodeBook userData) { + if (userData.getType() != null) { + switch (userData.getType()) { + case SOURCE: + return sources.checkFilter(filter, userData); + case AUTHOR: + return authors.checkFilter(filter, userData); + case TAG: + return tags.checkFilter(filter, userData); + default: + break; + } + } + + return false; + } + + private void reloadList() throws IOException { + list = Instance.getInstance().getLibrary().getList(); + } + + private DataNode getNodeFlat(List flatData, + Type type) { + List> nodes = new ArrayList>(); + + for (String data : flatData) { + nodes.add(new DataNode(null, + new DataNodeBook(type, data))); + } + + return new DataNode(nodes, + new DataNodeBook(type, !nodes.isEmpty())); + } + + private DataNode getNodeGrouped(DataTreeSort tree, + Map> sourcesGrouped, Type type) { + List> nodes = new ArrayList>(); + + List sources = new ArrayList(sourcesGrouped.keySet()); + tree.sort(sources); + for (String source : sources) { + List children = sourcesGrouped.get(source); + boolean hasChildren = (children.size() > 1) || (children.size() == 1 + && !children.get(0).trim().isEmpty()); + + List> subnodes = new ArrayList>(); + if (hasChildren) { + tree.sort(children); + for (String subSource : children) { + boolean baseSubSource = subSource.isEmpty() + && children.size() > 1; + DataNodeBook book = new DataNodeBook(type, source, + subSource, false); + if (baseSubSource) + book.setDisplay("*"); + subnodes.add(new DataNode(null, book)); + } + } + + nodes.add(new DataNode(subnodes, + new DataNodeBook(type, source, "", hasChildren))); + } + + return new DataNode(nodes, + new DataNodeBook(type, !nodes.isEmpty())); + } +} diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java deleted file mode 100644 index f4651960..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java +++ /dev/null @@ -1,90 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.utils; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type; -import be.nikiroo.utils.ui.DataNode; -import be.nikiroo.utils.ui.DataTree; - -public class DataTreeSources extends DataTree { - private boolean flat; - - public DataTreeSources(boolean flat) { - this.flat = flat; - } - - protected boolean isFlat() { - return flat; - } - - @Override - protected boolean checkFilter(String filter, DataNodeBook userData) { - return userData.getName().toLowerCase().contains(filter.toLowerCase()) - || userData.getSubname().toLowerCase() - .contains(filter.toLowerCase()); - } - - @Override - protected DataNode extractData() throws IOException { - if (isFlat()) { - return getNodeFlat( - Instance.getInstance().getLibrary().getList().getSources(), - Type.SOURCE); - } - - Map> sourcesGrouped = Instance.getInstance() - .getLibrary().getSourcesGrouped(); - return getNodeGrouped(sourcesGrouped, Type.SOURCE); - } - - protected DataNode getNodeFlat(List flatData, - Type type) throws IOException { - List> nodes = new ArrayList>(); - - for (String data : flatData) { - nodes.add(new DataNode(null, - new DataNodeBook(type, data))); - } - - return new DataNode(nodes, - new DataNodeBook(type, !nodes.isEmpty())); - } - - protected DataNode getNodeGrouped( - Map> sourcesGrouped, Type type) - throws IOException { - List> nodes = new ArrayList>(); - - List sources = new ArrayList(sourcesGrouped.keySet()); - sort(sources); - for (String source : sources) { - List children = sourcesGrouped.get(source); - boolean hasChildren = (children.size() > 1) || (children.size() == 1 - && !children.get(0).trim().isEmpty()); - - List> subnodes = new ArrayList>(); - if (hasChildren) { - sort(children); - for (String subSource : children) { - boolean baseSubSource = subSource.isEmpty() - && children.size() > 1; - DataNodeBook book = new DataNodeBook(type, source, - subSource, false); - if (baseSubSource) - book.setDisplay("*"); - subnodes.add(new DataNode(null, book)); - } - } - - nodes.add(new DataNode(subnodes, - new DataNodeBook(type, source, "", hasChildren))); - } - - return new DataNode(nodes, - new DataNodeBook(type, !nodes.isEmpty())); - } -} diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java deleted file mode 100644 index f94c105d..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java +++ /dev/null @@ -1,36 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.utils; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.library.MetaResultList; -import be.nikiroo.fanfix_swing.gui.book.BookInfo.Type; -import be.nikiroo.utils.ui.DataNode; - -public class DataTreeTag extends DataTreeSources { - public DataTreeTag(boolean flat) { - super(flat); - } - - @Override - protected boolean checkFilter(String filter, DataNodeBook userData) { - return userData - .getSubnameOrName().toLowerCase().contains(filter.toLowerCase()); - } - - @Override - protected DataNode extractData() throws IOException { - if (isFlat()) { - return getNodeFlat( - Instance.getInstance().getLibrary().getList().getTags(), - Type.TAG); - } - - return getNodeGrouped( - Instance.getInstance().getLibrary().getList().getTagsGrouped(), - Type.TAG); - } -}