C
ced
Hi,
I want to filter a JTree using the following model FilteredTreeModel.
In my case, my tree contains files & folders and I want to display
files that have a specific extension (my filter accepts folder and
files with this extension).
The model works fine except that it will display empty folders...
I can't find an easy way to avoid these empty folders :-(
Can someone help me please ?
public class FilteredTreeModel extends DefaultTreeModel {
public Filter filter;
public FilteredTreeModel(DefaultMutableTreeNode root, Filter filter)
{
super(root);
this.filter = filter;
}
public Object getChild(Object parent, int index) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
if(filter == null) {
return node.getChildAt(index);
}
else {
int pos = 0;
for(int i = 0, cnt = 0; i < node.getChildCount(); i++) {
if(filter.accepts(((DefaultMutableTreeNode)
node.getChildAt(i)).getUserObject())) {
if (cnt++ == index) {
pos = i;
break;
}
}
}
return node.getChildAt(pos);
}
}
public int getChildCount(Object parent) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
if (filter == null) {
return node.getChildCount();
}
else {
int childCount = 0;
Enumeration children = node.children();
while (children.hasMoreElements()) {
if (filter.accepts(((DefaultMutableTreeNode)
children.nextElement()).getUserObject())) {
childCount++;
}
}
return childCount;
}
}
public void setFilter(Filter filter) {
if (!this.filter.equals(filter)) {
this.filter = filter;
Object[] path = {root};
int[] childIndices = new int[root.getChildCount()];
Object[] children = new Object[root.getChildCount()];
for (int i = 0; i < root.getChildCount(); i++) {
childIndices = i;
children = root.getChildAt(i);
}
fireTreeStructureChanged(this, path, childIndices, children);
}
}
}
I want to filter a JTree using the following model FilteredTreeModel.
In my case, my tree contains files & folders and I want to display
files that have a specific extension (my filter accepts folder and
files with this extension).
The model works fine except that it will display empty folders...
I can't find an easy way to avoid these empty folders :-(
Can someone help me please ?
public class FilteredTreeModel extends DefaultTreeModel {
public Filter filter;
public FilteredTreeModel(DefaultMutableTreeNode root, Filter filter)
{
super(root);
this.filter = filter;
}
public Object getChild(Object parent, int index) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
if(filter == null) {
return node.getChildAt(index);
}
else {
int pos = 0;
for(int i = 0, cnt = 0; i < node.getChildCount(); i++) {
if(filter.accepts(((DefaultMutableTreeNode)
node.getChildAt(i)).getUserObject())) {
if (cnt++ == index) {
pos = i;
break;
}
}
}
return node.getChildAt(pos);
}
}
public int getChildCount(Object parent) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
if (filter == null) {
return node.getChildCount();
}
else {
int childCount = 0;
Enumeration children = node.children();
while (children.hasMoreElements()) {
if (filter.accepts(((DefaultMutableTreeNode)
children.nextElement()).getUserObject())) {
childCount++;
}
}
return childCount;
}
}
public void setFilter(Filter filter) {
if (!this.filter.equals(filter)) {
this.filter = filter;
Object[] path = {root};
int[] childIndices = new int[root.getChildCount()];
Object[] children = new Object[root.getChildCount()];
for (int i = 0; i < root.getChildCount(); i++) {
childIndices = i;
children = root.getChildAt(i);
}
fireTreeStructureChanged(this, path, childIndices, children);
}
}
}