Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
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) { | |
b63e7e0b | 23 | data.clear(); |
3cdf3fd8 | 24 | try { |
32ed6089 NR |
25 | Map<String, List<String>> sourcesGrouped = Instance.getInstance() |
26 | .getLibrary().getSourcesGrouped(); | |
3cdf3fd8 NR |
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) { | |
77e5ecd4 NR |
43 | if (key.trim().isEmpty()) { |
44 | return "[*]"; // Root node | |
45 | } | |
46 | ||
3cdf3fd8 NR |
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 | |
32ed6089 NR |
67 | protected int loadData(DefaultMutableTreeNode root, |
68 | Map<String, List<String>> sourcesGrouped, String filter) { | |
3cdf3fd8 NR |
69 | int count = 0; |
70 | for (String source : sourcesGrouped.keySet()) { | |
32ed6089 NR |
71 | if (checkFilter(filter, source) |
72 | || checkFilter(filter, sourcesGrouped.get(source))) { | |
d6c8579c NR |
73 | List<String> children = sourcesGrouped.get(source); |
74 | boolean hasChildren = (children.size() > 1) | |
32ed6089 NR |
75 | || (children.size() == 1 |
76 | && !children.get(0).trim().isEmpty()); | |
77 | DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode( | |
78 | ">" + source + (hasChildren ? "/" : "")); | |
3cdf3fd8 | 79 | root.add(sourceNode); |
d6c8579c | 80 | for (String subSource : children) { |
32ed6089 NR |
81 | if (checkFilter(filter, source) |
82 | || checkFilter(filter, subSource)) { | |
3cdf3fd8 | 83 | count = count + 1; |
32ed6089 NR |
84 | if (subSource.isEmpty() |
85 | && sourcesGrouped.get(source).size() > 1) { | |
86 | sourceNode.add( | |
87 | new DefaultMutableTreeNode(" " + source)); | |
3cdf3fd8 | 88 | } else if (!subSource.isEmpty()) { |
32ed6089 NR |
89 | sourceNode.add(new DefaultMutableTreeNode( |
90 | " " + source + "/" + subSource)); | |
3cdf3fd8 NR |
91 | } |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | return count; | |
98 | } | |
99 | } |