From: Niki Roo Date: Sat, 25 Apr 2020 23:23:36 +0000 (+0200) Subject: deduplicate code X-Git-Tag: fanfix-swing-1.0.0~26 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=45e66035aa343d7db645b6dc14497e32ac26c98c;p=fanfix-swing.git deduplicate code --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java b/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java index cae9e0d3..4502e8d0 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BreadCrumbsPanel.java @@ -23,15 +23,15 @@ public class BreadCrumbsPanel extends BreadCrumbsBar { protected DataNode extractData() throws IOException { List> children = null; - children = new DataTreeSources().loadData().getChildren(); + children = new DataTreeSources(false).loadData().getChildren(); DataNode sources = new DataNode( children, new DataNodeBook(Type.SOURCE, "Sources", true, !children.isEmpty())); - children = new DataTreeAuthors().loadData().getChildren(); + children = new DataTreeAuthors(false).loadData().getChildren(); DataNode authors = new DataNode( children, new DataNodeBook(Type.AUTHOR, "Authors", true, !children.isEmpty())); - children = new DataTreeTag().loadData().getChildren(); + children = new DataTreeTag(false).loadData().getChildren(); DataNode tags = new DataNode( children, new DataNodeBook(Type.TAG, "Tags", true, !children.isEmpty())); diff --git a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java index d526e20b..c84e946e 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BrowserPanel.java @@ -16,10 +16,10 @@ 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.browser.AuthorTab; import be.nikiroo.fanfix_swing.gui.browser.BasicTab; -import be.nikiroo.fanfix_swing.gui.browser.SourceTab; -import be.nikiroo.fanfix_swing.gui.browser.TagsTab; +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.UiHelper; import be.nikiroo.utils.ui.ListenerPanel; @@ -62,9 +62,9 @@ public class BrowserPanel extends ListenerPanel { static public final String TAB_CHANGE = "tab_change"; private JTabbedPane tabs; - private SourceTab sourceTab; - private AuthorTab authorTab; - private TagsTab tagsTab; + private BasicTab sourceTab; + private BasicTab authorTab; + private BasicTab tagsTab; private boolean keepSelection; @@ -78,9 +78,12 @@ public class BrowserPanel extends ListenerPanel { tabs = new JTabbedPane(); int index = 0; - tabs.add(sourceTab = new SourceTab(index++, SOURCE_SELECTION)); - tabs.add(authorTab = new AuthorTab(index++, AUTHOR_SELECTION)); - tabs.add(tagsTab = new TagsTab(index++, TAGS_SELECTION)); + tabs.add(sourceTab = new BasicTab(new DataTreeSources(false), index++, + SOURCE_SELECTION)); + tabs.add(authorTab = new BasicTab(new DataTreeAuthors(true), index++, + AUTHOR_SELECTION)); + tabs.add(tagsTab = new BasicTab(new DataTreeTag(true), index++, + TAGS_SELECTION)); configureTab(tabs, sourceTab, "Sources", "Tooltip for Sources"); configureTab(tabs, authorTab, "Authors", "Tooltip for Authors"); @@ -120,7 +123,6 @@ public class BrowserPanel extends ListenerPanel { }); } - @SuppressWarnings("rawtypes") private void unselect() { for (int i = 0; i < tabs.getTabCount(); i++) { if (i == tabs.getSelectedIndex()) @@ -131,8 +133,7 @@ public class BrowserPanel extends ListenerPanel { } } - private void configureTab(JTabbedPane tabs, - @SuppressWarnings("rawtypes") BasicTab tab, String name, + private void configureTab(JTabbedPane tabs, BasicTab tab, String name, String tooltip) { tab.setBaseTitle(name); tabs.setTitleAt(tab.getIndex(), tab.getTitle()); @@ -140,8 +141,7 @@ public class BrowserPanel extends ListenerPanel { listenTabs(tabs, tab); } - private void listenTabs(final JTabbedPane tabs, - @SuppressWarnings("rawtypes") final BasicTab tab) { + private void listenTabs(final JTabbedPane tabs, final BasicTab tab) { tab.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/AuthorTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/AuthorTab.java deleted file mode 100644 index b986e4dd..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/browser/AuthorTab.java +++ /dev/null @@ -1,66 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.browser; - -import java.util.ArrayList; -import java.util.List; - -import javax.swing.tree.DefaultMutableTreeNode; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.StringIdGui; - -public class AuthorTab extends BasicTab> { - public AuthorTab(int index, String listenerCommand) { - super(index, listenerCommand); - } - - @Override - protected List createEmptyData() { - return new ArrayList(); - } - - @Override - protected void fillData(List data) { - data.clear(); - try { - List authors = Instance.getInstance().getLibrary() - .getAuthors(); - for (String author : authors) { - data.add(author); - } - - sort(data); - } catch (Exception e) { - // TODO - e.printStackTrace(); - } - } - - @Override - protected String keyToElement(String key) { - return key; - } - - @Override - protected String keyToDisplay(String key) { - if (key.trim().isEmpty()) { - key = Instance.getInstance().getTransGui() - .getString(StringIdGui.MENU_AUTHORS_UNKNOWN); - } - - return key; - } - - @Override - protected int loadData(DefaultMutableTreeNode root, List authors, - String filter) { - for (String author : authors) { - if (checkFilter(filter, author)) { - DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode( - author); - root.add(sourceNode); - } - } - - return authors.size(); - } -} diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java index 6edb0f3f..7e70253e 100644 --- a/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java +++ b/src/be/nikiroo/fanfix_swing/gui/browser/BasicTab.java @@ -5,10 +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.Comparator; import java.util.List; -import java.util.Map; import javax.swing.JTree; import javax.swing.SwingWorker; @@ -18,42 +15,48 @@ import javax.swing.plaf.basic.BasicTreeUI; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreePath; import be.nikiroo.fanfix_swing.gui.SearchBar; +import be.nikiroo.fanfix_swing.gui.utils.DataNodeBook; import be.nikiroo.fanfix_swing.images.IconGenerator; import be.nikiroo.fanfix_swing.images.IconGenerator.Icon; import be.nikiroo.fanfix_swing.images.IconGenerator.Size; +import be.nikiroo.utils.ui.DataNode; +import be.nikiroo.utils.ui.DataTree; import be.nikiroo.utils.ui.ListenerPanel; import be.nikiroo.utils.ui.TreeCellSpanner; import be.nikiroo.utils.ui.TreeSnapshot; import be.nikiroo.utils.ui.UIUtils; -public abstract class BasicTab extends ListenerPanel { +public class BasicTab extends ListenerPanel { private int totalCount = 0; private List selectedElements = new ArrayList(); - private T data; private String baseTitle; private String listenerCommand; private int index; private JTree tree; private DefaultMutableTreeNode root; + private DataTree data; private SearchBar searchBar; - public BasicTab(int index, String listenerCommand) { + public BasicTab(DataTree data, int index, + String listenerCommand) { setLayout(new BorderLayout()); + this.data = data; + this.index = index; this.listenerCommand = listenerCommand; - data = createEmptyData(); totalCount = 0; root = new DefaultMutableTreeNode(); - tree = new JTree(root); + tree.setUI(new BasicTreeUI()); TreeCellSpanner spanner = new TreeCellSpanner(tree, generateCellRenderer()); @@ -68,19 +71,19 @@ public abstract class BasicTab extends ListenerPanel { TreePath[] paths = tree.getSelectionPaths(); if (paths != null) { for (TreePath path : paths) { - String key = path.getLastPathComponent().toString(); - elements.add(keyToElement(key)); - } - } - - List selectedElements = new ArrayList(); - for (String element : elements) { - if (!selectedElements.contains(element)) { - selectedElements.add(element); + if (path.getLastPathComponent() instanceof DefaultMutableTreeNode) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) path + .getLastPathComponent(); + if (node.getUserObject() instanceof DataNodeBook) { + DataNodeBook book = (DataNodeBook) node + .getUserObject(); + elements.add(book.getPath()); + } + } } } - BasicTab.this.selectedElements = selectedElements; + BasicTab.this.selectedElements = elements; fireActionPerformed(BasicTab.this.listenerCommand); } @@ -122,11 +125,10 @@ public abstract class BasicTab extends ListenerPanel { return oldString.equals(newString); } }; - SwingWorker>, Integer> worker = new SwingWorker>, Integer>() { + SwingWorker worker = new SwingWorker() { @Override - protected Map> doInBackground() - throws Exception { - fillData(data); + protected Void doInBackground() throws Exception { + data.loadData(); return null; } @@ -134,18 +136,22 @@ public abstract class BasicTab extends ListenerPanel { protected void done() { try { get(); - } catch (Exception e) { - // TODO: error - } - root.removeAllChildren(); - totalCount = loadData(root, data, searchBar.getText()); - ((DefaultTreeModel) tree.getModel()).reload(); + DataNode filtered = data + .getRoot(searchBar.getText()); + + node2node(root, filtered); + totalCount = filtered.count() - 1; // root is counted + + ((DefaultTreeModel) tree.getModel()).reload(); - snapshot.apply(); + snapshot.apply(); - if (fireActionPerformed) { - fireActionPerformed(listenerCommand); + if (fireActionPerformed) { + fireActionPerformed(listenerCommand); + } + } catch (Exception e) { + // TODO: error } } }; @@ -193,58 +199,28 @@ public abstract class BasicTab extends ListenerPanel { tree.clearSelection(); } - protected boolean checkFilter(String filter, String value) { - return (filter == null || filter.isEmpty() - || value.toLowerCase().contains(filter.toLowerCase())); - } - - protected boolean checkFilter(String filter, List list) { - for (String value : list) { - if (checkFilter(filter, value)) - return true; - } - return false; - } - - protected abstract T createEmptyData(); - - // beware: you should update it OR clean/re-add it, but previous data may - // still be there - protected abstract void fillData(T data); - - protected abstract String keyToElement(String key); - - protected abstract String keyToDisplay(String key); - - protected abstract int loadData(DefaultMutableTreeNode root, T data, - String filter); - - protected void sort(List values) { - Collections.sort(values, new Comparator() { - @Override - public int compare(String o1, String o2) { - return ("" + o1).compareToIgnoreCase("" + o2); - } - }); - } - private TreeCellRenderer generateCellRenderer() { DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { + + String display = value == null ? "" : value.toString(); if (value instanceof DefaultMutableTreeNode) { - if (((DefaultMutableTreeNode) value).getLevel() > 1) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; + if (node.getLevel() > 1) { setLeafIcon(null); setLeafIcon(IconGenerator.get(Icon.empty, Size.x4)); } else { setLeafIcon(IconGenerator.get(Icon.empty, Size.x16)); } - } - String display = value == null ? "" : value.toString(); - display = keyToDisplay(display); + if (node.getUserObject() instanceof DataNodeBook) { + DataNodeBook book = (DataNodeBook) node.getUserObject(); + display = book.getDisplay(); + } + } return super.getTreeCellRendererComponent(tree, display, selected, expanded, leaf, row, hasFocus); @@ -257,4 +233,19 @@ public abstract class BasicTab extends ListenerPanel { return renderer; } + + private MutableTreeNode node2node(DefaultMutableTreeNode root, + DataNode node) { + if (root == null) { + root = new DefaultMutableTreeNode(node.getUserData()); + } + + root.removeAllChildren(); + + for (DataNode child : node.getChildren()) { + root.add(node2node(null, child)); + } + + return root; + } } diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java deleted file mode 100644 index a86420e5..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/browser/SourceTab.java +++ /dev/null @@ -1,106 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.browser; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.swing.tree.DefaultMutableTreeNode; - -import be.nikiroo.fanfix.Instance; - -public class SourceTab extends BasicTab>> { - public SourceTab(int index, String listenerCommand) { - super(index, listenerCommand); - } - - @Override - protected Map> createEmptyData() { - return new HashMap>(); - } - - @Override - protected void fillData(Map> data) { - data.clear(); - try { - Map> sourcesGrouped = Instance.getInstance() - .getLibrary().getSourcesGrouped(); - for (String group : sourcesGrouped.keySet()) { - data.put(group, sourcesGrouped.get(group)); - } - } catch (Exception e) { - // TODO - e.printStackTrace(); - } - } - - @Override - protected String keyToElement(String key) { - return key.substring(1); - } - - @Override - protected String keyToDisplay(String key) { - if (key.trim().isEmpty()) { - return "[*]"; // Root node - } - - // Get and remove type - String type = key.substring(0, 1); - key = key.substring(1); - - if (!type.equals(">")) { - // Only display the final name - int pos = key.toString().lastIndexOf("/"); - if (pos >= 0) { - key = key.toString().substring(pos + 1); - } - } - - if (key.toString().isEmpty()) { - key = " "; - } - - return key; - } - - @Override - protected int loadData(DefaultMutableTreeNode root, - Map> sourcesGrouped, String filter) { - int count = 0; - List sources = new ArrayList(sourcesGrouped.keySet()); - sort(sources); - for (String source : sources) { - if (checkFilter(filter, source) - || checkFilter(filter, sourcesGrouped.get(source))) { - List children = sourcesGrouped.get(source); - boolean hasChildren = (children.size() > 1) - || (children.size() == 1 - && !children.get(0).trim().isEmpty()); - DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode( - ">" + source + (hasChildren ? "/" : "")); - root.add(sourceNode); - sort(children); - for (String subSource : children) { - if (checkFilter(filter, source) - || checkFilter(filter, subSource)) { - count = count + 1; - if (subSource.isEmpty() - && sourcesGrouped.get(source).size() > 1) { - sourceNode.add( - new DefaultMutableTreeNode(" " + source)); - } else if (!subSource.isEmpty()) { - sourceNode.add(new DefaultMutableTreeNode( - " " + source + "/" + subSource)); - } - } - } - } - } - - return count; - } -} diff --git a/src/be/nikiroo/fanfix_swing/gui/browser/TagsTab.java b/src/be/nikiroo/fanfix_swing/gui/browser/TagsTab.java deleted file mode 100644 index 4224fb34..00000000 --- a/src/be/nikiroo/fanfix_swing/gui/browser/TagsTab.java +++ /dev/null @@ -1,78 +0,0 @@ -package be.nikiroo.fanfix_swing.gui.browser; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import javax.swing.tree.DefaultMutableTreeNode; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.StringIdGui; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.library.MetaResultList; - -public class TagsTab extends BasicTab> { - public TagsTab(int index, String listenerCommand) { - super(index, listenerCommand); - } - - @Override - protected List createEmptyData() { - return new ArrayList(); - } - - @Override - protected void fillData(List data) { - data.clear(); - try { - MetaResultList metas = Instance.getInstance().getLibrary() - .getList(); - // TODO: getTagList, getAuthorList... ? - for (MetaData meta : metas.getMetas()) { - List tags = meta.getTags(); - if (tags != null) { - for (String tag : tags) { - if (!data.contains(tag)) { - data.add(tag); - } - } - } - } - - sort(data); - } catch (Exception e) { - // TODO - e.printStackTrace(); - } - } - - @Override - protected String keyToElement(String key) { - return key; - } - - @Override - protected String keyToDisplay(String key) { - if (key.trim().isEmpty()) { - // TODO: new TAG_UNKNOWN needed - key = Instance.getInstance().getTransGui() - .getString(StringIdGui.MENU_AUTHORS_UNKNOWN); - } - - return key; - } - - @Override - protected int loadData(DefaultMutableTreeNode root, List tags, - String filter) { - for (String tag : tags) { - if (checkFilter(filter, tag)) { - DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode( - tag); - root.add(sourceNode); - } - } - - return tags.size(); - } -} diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java index 32c6431e..c6ff00f4 100644 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java +++ b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeAuthors.java @@ -11,6 +11,10 @@ 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.toString().toLowerCase().contains(filter.toLowerCase()); @@ -18,9 +22,15 @@ public class DataTreeAuthors extends DataTreeSources { @Override protected DataNode extractData() throws IOException { + if (isFlat()) { + return getNodeFlat( + Instance.getInstance().getLibrary().getList().getAuthors(), + Type.AUTHOR); + } + Map> authorsGrouped = Instance.getInstance() .getLibrary().getAuthorsGrouped(); - + if (authorsGrouped.size() == 1) { List authors = authorsGrouped.values().iterator().next(); return getNodeFlat(authors, Type.AUTHOR); diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java index 83683379..f4651960 100644 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java +++ b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeSources.java @@ -11,17 +11,33 @@ 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) { - // TODO - return userData.toString().toLowerCase().contains(filter.toLowerCase()); + 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); } @@ -29,11 +45,9 @@ public class DataTreeSources extends DataTree { Type type) throws IOException { List> nodes = new ArrayList>(); - // TODO: getResult() -> getTagList, getAuthorList... ? - List authors = Instance.getInstance().getLibrary().getAuthors(); - for (String author : authors) { + for (String data : flatData) { nodes.add(new DataNode(null, - new DataNodeBook(type, author))); + new DataNodeBook(type, data))); } return new DataNode(nodes, diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java index 6e240ad0..318e5fea 100644 --- a/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java +++ b/src/be/nikiroo/fanfix_swing/gui/utils/DataTreeTag.java @@ -11,6 +11,10 @@ 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.toString().toLowerCase().contains(filter.toLowerCase()); @@ -18,21 +22,8 @@ public class DataTreeTag extends DataTreeSources { @Override protected DataNode extractData() throws IOException { - List tagList = new ArrayList(); - MetaResultList metas = Instance.getInstance().getLibrary().getList(); - // TODO: getTagList, getAuthorList... ? including grouped? - for (MetaData meta : metas.getMetas()) { - List tags = meta.getTags(); - if (tags != null) { - for (String tag : tags) { - if (!tagList.contains(tag)) { - tagList.add(tag); - } - } - } - } - sort(tagList); - - return getNodeFlat(tagList, Type.TAG); + return getNodeFlat( + Instance.getInstance().getLibrary().getList().getTags(), + Type.TAG); } }