stories order by name
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / browser / SourceTab.java
1 package be.nikiroo.fanfix_swing.gui.browser;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.swing.tree.DefaultMutableTreeNode;
12
13 import be.nikiroo.fanfix.Instance;
14
15 public 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) {
27 data.clear();
28 try {
29 Map<String, List<String>> sourcesGrouped = Instance.getInstance()
30 .getLibrary().getSourcesGrouped();
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) {
47 if (key.trim().isEmpty()) {
48 return "[*]"; // Root node
49 }
50
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
71 protected int loadData(DefaultMutableTreeNode root,
72 Map<String, List<String>> sourcesGrouped, String filter) {
73 int count = 0;
74 List<String> sources = new ArrayList<String>(sourcesGrouped.keySet());
75 sort(sources);
76 for (String source : sources) {
77 if (checkFilter(filter, source)
78 || checkFilter(filter, sourcesGrouped.get(source))) {
79 List<String> children = sourcesGrouped.get(source);
80 boolean hasChildren = (children.size() > 1)
81 || (children.size() == 1
82 && !children.get(0).trim().isEmpty());
83 DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(
84 ">" + source + (hasChildren ? "/" : ""));
85 root.add(sourceNode);
86 sort(children);
87 for (String subSource : children) {
88 if (checkFilter(filter, source)
89 || checkFilter(filter, subSource)) {
90 count = count + 1;
91 if (subSource.isEmpty()
92 && sourcesGrouped.get(source).size() > 1) {
93 sourceNode.add(
94 new DefaultMutableTreeNode(" " + source));
95 } else if (!subSource.isEmpty()) {
96 sourceNode.add(new DefaultMutableTreeNode(
97 " " + source + "/" + subSource));
98 }
99 }
100 }
101 }
102 }
103
104 return count;
105 }
106 }