protected DataNode<DataNodeBook> extractData() throws IOException {
List<? extends DataNode<DataNodeBook>> children = null;
- children = new DataTreeSources().loadData().getChildren();
+ children = new DataTreeSources(false).loadData().getChildren();
DataNode<DataNodeBook> sources = new DataNode<DataNodeBook>(
children, new DataNodeBook(Type.SOURCE, "Sources", true,
!children.isEmpty()));
- children = new DataTreeAuthors().loadData().getChildren();
+ children = new DataTreeAuthors(false).loadData().getChildren();
DataNode<DataNodeBook> authors = new DataNode<DataNodeBook>(
children, new DataNodeBook(Type.AUTHOR, "Authors", true,
!children.isEmpty()));
- children = new DataTreeTag().loadData().getChildren();
+ children = new DataTreeTag(false).loadData().getChildren();
DataNode<DataNodeBook> tags = new DataNode<DataNodeBook>(
children, new DataNodeBook(Type.TAG, "Tags", true,
!children.isEmpty()));
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;
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;
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");
});
}
- @SuppressWarnings("rawtypes")
private void unselect() {
for (int i = 0; i < tabs.getTabCount(); i++) {
if (i == tabs.getSelectedIndex())
}
}
- 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());
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) {
+++ /dev/null
-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<List<String>> {
- public AuthorTab(int index, String listenerCommand) {
- super(index, listenerCommand);
- }
-
- @Override
- protected List<String> createEmptyData() {
- return new ArrayList<String>();
- }
-
- @Override
- protected void fillData(List<String> data) {
- data.clear();
- try {
- List<String> 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<String> authors,
- String filter) {
- for (String author : authors) {
- if (checkFilter(filter, author)) {
- DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(
- author);
- root.add(sourceNode);
- }
- }
-
- return authors.size();
- }
-}
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;
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<T> extends ListenerPanel {
+public class BasicTab extends ListenerPanel {
private int totalCount = 0;
private List<String> selectedElements = new ArrayList<String>();
- private T data;
private String baseTitle;
private String listenerCommand;
private int index;
private JTree tree;
private DefaultMutableTreeNode root;
+ private DataTree<DataNodeBook> data;
private SearchBar searchBar;
- public BasicTab(int index, String listenerCommand) {
+ public BasicTab(DataTree<DataNodeBook> 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());
TreePath[] paths = tree.getSelectionPaths();
if (paths != null) {
for (TreePath path : paths) {
- String key = path.getLastPathComponent().toString();
- elements.add(keyToElement(key));
- }
- }
-
- List<String> selectedElements = new ArrayList<String>();
- 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);
}
return oldString.equals(newString);
}
};
- SwingWorker<Map<String, List<String>>, Integer> worker = new SwingWorker<Map<String, List<String>>, Integer>() {
+ SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
- protected Map<String, List<String>> doInBackground()
- throws Exception {
- fillData(data);
+ protected Void doInBackground() throws Exception {
+ data.loadData();
return null;
}
protected void done() {
try {
get();
- } catch (Exception e) {
- // TODO: error
- }
- root.removeAllChildren();
- totalCount = loadData(root, data, searchBar.getText());
- ((DefaultTreeModel) tree.getModel()).reload();
+ DataNode<DataNodeBook> 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
}
}
};
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<String> 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<String> values) {
- Collections.sort(values, new Comparator<String>() {
- @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);
return renderer;
}
+
+ private MutableTreeNode node2node(DefaultMutableTreeNode root,
+ DataNode<DataNodeBook> node) {
+ if (root == null) {
+ root = new DefaultMutableTreeNode(node.getUserData());
+ }
+
+ root.removeAllChildren();
+
+ for (DataNode<DataNodeBook> child : node.getChildren()) {
+ root.add(node2node(null, child));
+ }
+
+ return root;
+ }
}
+++ /dev/null
-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<Map<String, List<String>>> {
- public SourceTab(int index, String listenerCommand) {
- super(index, listenerCommand);
- }
-
- @Override
- protected Map<String, List<String>> createEmptyData() {
- return new HashMap<String, List<String>>();
- }
-
- @Override
- protected void fillData(Map<String, List<String>> data) {
- data.clear();
- try {
- Map<String, List<String>> 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<String, List<String>> sourcesGrouped, String filter) {
- int count = 0;
- List<String> sources = new ArrayList<String>(sourcesGrouped.keySet());
- sort(sources);
- for (String source : sources) {
- if (checkFilter(filter, source)
- || checkFilter(filter, sourcesGrouped.get(source))) {
- List<String> 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;
- }
-}
+++ /dev/null
-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<List<String>> {
- public TagsTab(int index, String listenerCommand) {
- super(index, listenerCommand);
- }
-
- @Override
- protected List<String> createEmptyData() {
- return new ArrayList<String>();
- }
-
- @Override
- protected void fillData(List<String> data) {
- data.clear();
- try {
- MetaResultList metas = Instance.getInstance().getLibrary()
- .getList();
- // TODO: getTagList, getAuthorList... ?
- for (MetaData meta : metas.getMetas()) {
- List<String> 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<String> tags,
- String filter) {
- for (String tag : tags) {
- if (checkFilter(filter, tag)) {
- DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(
- tag);
- root.add(sourceNode);
- }
- }
-
- return tags.size();
- }
-}
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());
@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().getAuthorsGrouped();
-
+
if (authorsGrouped.size() == 1) {
List<String> authors = authorsGrouped.values().iterator().next();
return getNodeFlat(authors, Type.AUTHOR);
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) {
- // TODO
- return userData.toString().toLowerCase().contains(filter.toLowerCase());
+ 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);
}
Type type) throws IOException {
List<DataNode<DataNodeBook>> nodes = new ArrayList<DataNode<DataNodeBook>>();
- // TODO: getResult() -> getTagList, getAuthorList... ?
- List<String> authors = Instance.getInstance().getLibrary().getAuthors();
- for (String author : authors) {
+ for (String data : flatData) {
nodes.add(new DataNode<DataNodeBook>(null,
- new DataNodeBook(type, author)));
+ new DataNodeBook(type, data)));
}
return new DataNode<DataNodeBook>(nodes,
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());
@Override
protected DataNode<DataNodeBook> extractData() throws IOException {
- List<String> tagList = new ArrayList<String>();
- MetaResultList metas = Instance.getInstance().getLibrary().getList();
- // TODO: getTagList, getAuthorList... ? including grouped?
- for (MetaData meta : metas.getMetas()) {
- List<String> 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);
}
}