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