stories order by name
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / browser / SourceTab.java
CommitLineData
3cdf3fd8
NR
1package be.nikiroo.fanfix_swing.gui.browser;
2
62c7e07e
NR
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.Comparator;
3cdf3fd8
NR
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11import javax.swing.tree.DefaultMutableTreeNode;
12
13import be.nikiroo.fanfix.Instance;
14
15public class SourceTab extends BasicTab<Map<String, List<String>>> {
16 public SourceTab(int index, String listenerCommand) {
17 super(index, listenerCommand);
18 }
19
20 @Override
21 protected Map<String, List<String>> createEmptyData() {
22 return new HashMap<String, List<String>>();
23 }
24
25 @Override
26 protected void fillData(Map<String, List<String>> data) {
b63e7e0b 27 data.clear();
3cdf3fd8 28 try {
32ed6089
NR
29 Map<String, List<String>> sourcesGrouped = Instance.getInstance()
30 .getLibrary().getSourcesGrouped();
3cdf3fd8
NR
31 for (String group : sourcesGrouped.keySet()) {
32 data.put(group, sourcesGrouped.get(group));
33 }
34 } catch (Exception e) {
35 // TODO
36 e.printStackTrace();
37 }
38 }
39
40 @Override
41 protected String keyToElement(String key) {
42 return key.substring(1);
43 }
44
45 @Override
46 protected String keyToDisplay(String key) {
77e5ecd4
NR
47 if (key.trim().isEmpty()) {
48 return "[*]"; // Root node
49 }
50
3cdf3fd8
NR
51 // Get and remove type
52 String type = key.substring(0, 1);
53 key = key.substring(1);
54
55 if (!type.equals(">")) {
56 // Only display the final name
57 int pos = key.toString().lastIndexOf("/");
58 if (pos >= 0) {
59 key = key.toString().substring(pos + 1);
60 }
61 }
62
63 if (key.toString().isEmpty()) {
64 key = " ";
65 }
66
67 return key;
68 }
69
70 @Override
32ed6089
NR
71 protected int loadData(DefaultMutableTreeNode root,
72 Map<String, List<String>> sourcesGrouped, String filter) {
3cdf3fd8 73 int count = 0;
62c7e07e
NR
74 List<String> sources = new ArrayList<String>(sourcesGrouped.keySet());
75 sort(sources);
76 for (String source : sources) {
32ed6089
NR
77 if (checkFilter(filter, source)
78 || checkFilter(filter, sourcesGrouped.get(source))) {
d6c8579c
NR
79 List<String> children = sourcesGrouped.get(source);
80 boolean hasChildren = (children.size() > 1)
32ed6089
NR
81 || (children.size() == 1
82 && !children.get(0).trim().isEmpty());
83 DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(
84 ">" + source + (hasChildren ? "/" : ""));
3cdf3fd8 85 root.add(sourceNode);
62c7e07e 86 sort(children);
d6c8579c 87 for (String subSource : children) {
32ed6089
NR
88 if (checkFilter(filter, source)
89 || checkFilter(filter, subSource)) {
3cdf3fd8 90 count = count + 1;
32ed6089
NR
91 if (subSource.isEmpty()
92 && sourcesGrouped.get(source).size() > 1) {
93 sourceNode.add(
94 new DefaultMutableTreeNode(" " + source));
3cdf3fd8 95 } else if (!subSource.isEmpty()) {
32ed6089
NR
96 sourceNode.add(new DefaultMutableTreeNode(
97 " " + source + "/" + subSource));
3cdf3fd8
NR
98 }
99 }
100 }
101 }
102 }
103
104 return count;
105 }
106}