| 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 | // Get and remove type |
| 42 | String type = key.substring(0, 1); |
| 43 | key = key.substring(1); |
| 44 | |
| 45 | if (!type.equals(">")) { |
| 46 | // Only display the final name |
| 47 | int pos = key.toString().lastIndexOf("/"); |
| 48 | if (pos >= 0) { |
| 49 | key = key.toString().substring(pos + 1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (key.toString().isEmpty()) { |
| 54 | key = " "; |
| 55 | } |
| 56 | |
| 57 | return key; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected int loadData(DefaultMutableTreeNode root, Map<String, List<String>> sourcesGrouped, String filter) { |
| 62 | int count = 0; |
| 63 | for (String source : sourcesGrouped.keySet()) { |
| 64 | if (checkFilter(filter, source) || checkFilter(filter, sourcesGrouped.get(source))) { |
| 65 | boolean hasChildren = sourcesGrouped.get(source).size() > 1; |
| 66 | DefaultMutableTreeNode sourceNode = new DefaultMutableTreeNode(">" + source + (hasChildren ? "/" : "")); |
| 67 | root.add(sourceNode); |
| 68 | for (String subSource : sourcesGrouped.get(source)) { |
| 69 | if (checkFilter(filter, source) || checkFilter(filter, subSource)) { |
| 70 | count = count + 1; |
| 71 | if (subSource.isEmpty() && sourcesGrouped.get(source).size() > 1) { |
| 72 | sourceNode.add(new DefaultMutableTreeNode(" " + source)); |
| 73 | } else if (!subSource.isEmpty()) { |
| 74 | sourceNode.add(new DefaultMutableTreeNode(" " + source + "/" + subSource)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return count; |
| 82 | } |
| 83 | } |