fix filter re-add data problem
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / browser / SourceTab.java
1 package be.nikiroo.fanfix_swing.gui.browser;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.swing.tree.DefaultMutableTreeNode;
8
9 import be.nikiroo.fanfix.Instance;
10
11 public 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 data.clear();
24 try {
25 Map<String, List<String>> sourcesGrouped = Instance.getInstance()
26 .getLibrary().getSourcesGrouped();
27 for (String group : sourcesGrouped.keySet()) {
28 data.put(group, sourcesGrouped.get(group));
29 }
30 } catch (Exception e) {
31 // TODO
32 e.printStackTrace();
33 }
34 }
35
36 @Override
37 protected String keyToElement(String key) {
38 return key.substring(1);
39 }
40
41 @Override
42 protected String keyToDisplay(String key) {
43 if (key.trim().isEmpty()) {
44 return "[*]"; // Root node
45 }
46
47 // Get and remove type
48 String type = key.substring(0, 1);
49 key = key.substring(1);
50
51 if (!type.equals(">")) {
52 // Only display the final name
53 int pos = key.toString().lastIndexOf("/");
54 if (pos >= 0) {
55 key = key.toString().substring(pos + 1);
56 }
57 }
58
59 if (key.toString().isEmpty()) {
60 key = " ";
61 }
62
63 return key;
64 }
65
66 @Override
67 protected int loadData(DefaultMutableTreeNode root,
68 Map<String, List<String>> sourcesGrouped, String filter) {
69 int count = 0;
70 for (String source : sourcesGrouped.keySet()) {
71 if (checkFilter(filter, source)
72 || checkFilter(filter, sourcesGrouped.get(source))) {
73 List<String> children = sourcesGrouped.get(source);
74 boolean hasChildren = (children.size() > 1)
75 || (children.size() == 1
76 && !children.get(0).trim().isEmpty());
77 DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(
78 ">" + source + (hasChildren ? "/" : ""));
79 root.add(sourceNode);
80 for (String subSource : children) {
81 if (checkFilter(filter, source)
82 || checkFilter(filter, subSource)) {
83 count = count + 1;
84 if (subSource.isEmpty()
85 && sourcesGrouped.get(source).size() > 1) {
86 sourceNode.add(
87 new DefaultMutableTreeNode(" " + source));
88 } else if (!subSource.isEmpty()) {
89 sourceNode.add(new DefaultMutableTreeNode(
90 " " + source + "/" + subSource));
91 }
92 }
93 }
94 }
95 }
96
97 return count;
98 }
99 }