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;
public class BreadCrumbsPanel extends BreadCrumbsBar<DataNodeBook> {
public BreadCrumbsPanel() {
super(new DataTree<DataNodeBook>() {
+ private DataTreeBooks dataTreeBooks = new DataTreeBooks(false,
+ false, false);
+
@Override
protected DataNode<DataNodeBook> extractData() throws IOException {
- List<? extends DataNode<DataNodeBook>> children = null;
-
- children = new DataTreeSources(false).loadData().getChildren();
- DataNode<DataNodeBook> sources = new DataNode<DataNodeBook>(
- children, new DataNodeBook(Type.SOURCE, "Sources", true,
- !children.isEmpty()));
- children = new DataTreeAuthors(false).loadData().getChildren();
- DataNode<DataNodeBook> authors = new DataNode<DataNodeBook>(
- children, new DataNodeBook(Type.AUTHOR, "Authors", true,
- !children.isEmpty()));
- children = new DataTreeTag(false).loadData().getChildren();
- DataNode<DataNodeBook> tags = new DataNode<DataNodeBook>(
- children, new DataNodeBook(Type.TAG, "Tags", true,
- !children.isEmpty()));
-
- return new DataNode<DataNodeBook>(
- 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);
}
});
}
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;
*/
static public final String TAB_CHANGE = "tab_change";
+ private DataTreeBooks dataTreeBooks;
+
private JTabbedPane tabs;
private BrowserTab sourceTab;
private BrowserTab authorTab;
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");
fireActionPerformed(TAB_CHANGE);
}
});
+
+ reloadData(true);
}
private void unselect() {
* 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<Void, Void>() {
+ @Override
+ protected Void doInBackground() throws Exception {
+ dataTreeBooks.loadData();
+ return null;
+ }
+
+ @Override
+ protected void done() {
+ sourceTab.filter(fireActionPerformed);
+ authorTab.filter(fireActionPerformed);
+ tagsTab.filter(fireActionPerformed);
+
+ }
+ }.execute();
}
}
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;
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) {
return oldString.equals(newString);
}
};
- SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
- @Override
- protected Void doInBackground() throws Exception {
- data.loadData();
- return null;
- }
-
- @Override
- protected void done() {
- try {
- get();
- DataNode<DataNodeBook> filtered = data
- .getRoot(searchBar.getText());
+ DataNode<DataNodeBook> 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);
+ }
}
/**
+++ /dev/null
-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<DataNodeBook> extractData() throws IOException {
- if (isFlat()) {
- return getNodeFlat(
- Instance.getInstance().getLibrary().getList().getAuthors(),
- Type.AUTHOR);
- }
-
- Map<String, List<String>> authorsGrouped = Instance.getInstance()
- .getLibrary().getList().getAuthorsGrouped();
-
- if (authorsGrouped.size() == 1) {
- List<String> authors = authorsGrouped.values().iterator().next();
- return getNodeFlat(authors, Type.AUTHOR);
- }
-
- return getNodeGrouped(authorsGrouped, Type.AUTHOR);
- }
-}
--- /dev/null
+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<DataNodeBook> {
+ // make it PUBLIC instead of PROTECTED
+ @Override
+ abstract public boolean checkFilter(String filter,
+ DataNodeBook userData);
+
+ @Override
+ public void sort(List<String> values) {
+ super.sort(values);
+ }
+
+ @Override
+ public DataNode<DataNodeBook> loadData() throws IOException {
+ return loadData(true);
+ }
+
+ public DataNode<DataNodeBook> 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<DataNodeBook> extractData() throws IOException {
+ if (flatSources) {
+ return getNodeFlat(list.getSources(), Type.SOURCE);
+ }
+
+ Map<String, List<String>> 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<DataNodeBook> extractData() throws IOException {
+ if (flatAuthors) {
+ return getNodeFlat(list.getAuthors(), Type.AUTHOR);
+ }
+
+ Map<String, List<String>> authorsGrouped = list
+ .getAuthorsGrouped();
+
+ if (authorsGrouped.size() == 1) {
+ List<String> 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<DataNodeBook> 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<DataNodeBook> getSources() {
+ return sources;
+ }
+
+ public DataTree<DataNodeBook> getAuthors() {
+ return authors;
+ }
+
+ public DataTree<DataNodeBook> getTags() {
+ return tags;
+ }
+
+ public DataNode<DataNodeBook> loadData() throws IOException {
+ reloadList();
+
+ List<? extends DataNode<DataNodeBook>> children = null;
+
+ children = sources.loadData(false).getChildren();
+ DataNode<DataNodeBook> sources = new DataNode<DataNodeBook>(children,
+ new DataNodeBook(Type.SOURCE, "Sources", true,
+ !children.isEmpty()));
+
+ children = authors.loadData(false).getChildren();
+ DataNode<DataNodeBook> authors = new DataNode<DataNodeBook>(children,
+ new DataNodeBook(Type.AUTHOR, "Authors", true,
+ !children.isEmpty()));
+
+ children = tags.loadData(false).getChildren();
+ DataNode<DataNodeBook> tags = new DataNode<DataNodeBook>(children,
+ new DataNodeBook(Type.TAG, "Tags", true, !children.isEmpty()));
+
+ return new DataNode<DataNodeBook>(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<DataNodeBook> getNodeFlat(List<String> flatData,
+ Type type) {
+ List<DataNode<DataNodeBook>> nodes = new ArrayList<DataNode<DataNodeBook>>();
+
+ for (String data : flatData) {
+ nodes.add(new DataNode<DataNodeBook>(null,
+ new DataNodeBook(type, data)));
+ }
+
+ return new DataNode<DataNodeBook>(nodes,
+ new DataNodeBook(type, !nodes.isEmpty()));
+ }
+
+ private DataNode<DataNodeBook> getNodeGrouped(DataTreeSort tree,
+ Map<String, List<String>> sourcesGrouped, Type type) {
+ List<DataNode<DataNodeBook>> nodes = new ArrayList<DataNode<DataNodeBook>>();
+
+ List<String> sources = new ArrayList<String>(sourcesGrouped.keySet());
+ tree.sort(sources);
+ for (String source : sources) {
+ List<String> children = sourcesGrouped.get(source);
+ boolean hasChildren = (children.size() > 1) || (children.size() == 1
+ && !children.get(0).trim().isEmpty());
+
+ List<DataNode<DataNodeBook>> subnodes = new ArrayList<DataNode<DataNodeBook>>();
+ 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<DataNodeBook>(null, book));
+ }
+ }
+
+ nodes.add(new DataNode<DataNodeBook>(subnodes,
+ new DataNodeBook(type, source, "", hasChildren)));
+ }
+
+ return new DataNode<DataNodeBook>(nodes,
+ new DataNodeBook(type, !nodes.isEmpty()));
+ }
+}
+++ /dev/null
-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<DataNodeBook> {
- 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<DataNodeBook> extractData() throws IOException {
- if (isFlat()) {
- return getNodeFlat(
- Instance.getInstance().getLibrary().getList().getSources(),
- Type.SOURCE);
- }
-
- Map<String, List<String>> sourcesGrouped = Instance.getInstance()
- .getLibrary().getSourcesGrouped();
- return getNodeGrouped(sourcesGrouped, Type.SOURCE);
- }
-
- protected DataNode<DataNodeBook> getNodeFlat(List<String> flatData,
- Type type) throws IOException {
- List<DataNode<DataNodeBook>> nodes = new ArrayList<DataNode<DataNodeBook>>();
-
- for (String data : flatData) {
- nodes.add(new DataNode<DataNodeBook>(null,
- new DataNodeBook(type, data)));
- }
-
- return new DataNode<DataNodeBook>(nodes,
- new DataNodeBook(type, !nodes.isEmpty()));
- }
-
- protected DataNode<DataNodeBook> getNodeGrouped(
- Map<String, List<String>> sourcesGrouped, Type type)
- throws IOException {
- List<DataNode<DataNodeBook>> nodes = new ArrayList<DataNode<DataNodeBook>>();
-
- List<String> sources = new ArrayList<String>(sourcesGrouped.keySet());
- sort(sources);
- for (String source : sources) {
- List<String> children = sourcesGrouped.get(source);
- boolean hasChildren = (children.size() > 1) || (children.size() == 1
- && !children.get(0).trim().isEmpty());
-
- List<DataNode<DataNodeBook>> subnodes = new ArrayList<DataNode<DataNodeBook>>();
- 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<DataNodeBook>(null, book));
- }
- }
-
- nodes.add(new DataNode<DataNodeBook>(subnodes,
- new DataNodeBook(type, source, "", hasChildren)));
- }
-
- return new DataNode<DataNodeBook>(nodes,
- new DataNodeBook(type, !nodes.isEmpty()));
- }
-}
+++ /dev/null
-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<DataNodeBook> extractData() throws IOException {
- if (isFlat()) {
- return getNodeFlat(
- Instance.getInstance().getLibrary().getList().getTags(),
- Type.TAG);
- }
-
- return getNodeGrouped(
- Instance.getInstance().getLibrary().getList().getTagsGrouped(),
- Type.TAG);
- }
-}