X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FDataTree.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FDataTree.java;h=e941a71d2ac00a238c0c6fd6a67932b7f08ca080;hb=60ee255638ef6e151a18ee1a93dd9a6c0e9ee1a7;hp=0000000000000000000000000000000000000000;hpb=727b9fb39d3386f14f66a33de80de04f1f58734c;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/ui/DataTree.java b/src/be/nikiroo/utils/ui/DataTree.java new file mode 100644 index 0000000..e941a71 --- /dev/null +++ b/src/be/nikiroo/utils/ui/DataTree.java @@ -0,0 +1,88 @@ +package be.nikiroo.utils.ui; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.MutableTreeNode; + +public abstract class DataTree { + private DataNode data; + + public DataNode loadData() throws IOException { + return this.data = extractData(); + } + + public DataNode getRoot() { + return getRoot(null); + } + + public DataNode getRoot(String filter) { + return filterNode(data, filter); + } + + protected abstract DataNode extractData() throws IOException; + + // filter cannot be null nor empty + protected abstract boolean checkFilter(String filter, E userData); + + protected boolean checkFilter(DataNode node, String filter) { + if (filter == null || filter.isEmpty()) { + return true; + } + + if (checkFilter(filter, node.getUserData())) + return true; + + for (DataNode child : node.getChildren()) { + if (checkFilter(child, filter)) + return true; + } + + return false; + } + + protected void sort(List values) { + Collections.sort(values, new Comparator() { + @Override + public int compare(String o1, String o2) { + return ("" + o1).compareToIgnoreCase("" + o2); + } + }); + } + + // note: we always send TAHT node, but filter children + private DataNode filterNode(DataNode source, String filter) { + List> children = new ArrayList>(); + for (DataNode child : source.getChildren()) { + if (checkFilter(child, filter)) { + children.add(filterNode(child, filter)); + } + } + + return new DataNode(children, source.getUserData()); + } + + // TODO: not in this class: + + public void loadInto(DefaultMutableTreeNode root, String filter) { + DataNode filtered = getRoot(filter); + for (DataNode child : filtered.getChildren()) { + root.add(nodeToNode(child)); + } + } + + private MutableTreeNode nodeToNode(DataNode node) { + // TODO: node.toString + DefaultMutableTreeNode otherNode = new DefaultMutableTreeNode( + node.toString()); + for (DataNode child : node.getChildren()) { + otherNode.add(nodeToNode(child)); + } + + return otherNode; + } +}