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