reformat
[nikiroo-utils.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().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) {
41 if (key.trim().isEmpty()) {
42 return "[*]"; // Root node
43 }
44
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 List<String> children = sourcesGrouped.get(source);
70 boolean hasChildren = (children.size() > 1)
71 || (children.size() == 1 && !children.get(0).trim().isEmpty());
72 DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(">" + source + (hasChildren ? "/" : ""));
73 root.add(sourceNode);
74 for (String subSource : children) {
75 if (checkFilter(filter, source) || checkFilter(filter, subSource)) {
76 count = count + 1;
77 if (subSource.isEmpty() && sourcesGrouped.get(source).size() > 1) {
78 sourceNode.add(new DefaultMutableTreeNode(" " + source));
79 } else if (!subSource.isEmpty()) {
80 sourceNode.add(new DefaultMutableTreeNode(" " + source + "/" + subSource));
81 }
82 }
83 }
84 }
85 }
86
87 return count;
88 }
89 }