X

Geertjan's Blog

  • May 16, 2010

org.openide.nodes.FilterNode (Part 2)

Geertjan Wielenga
Product Manager
The question of the day comes from Jesper Sahner: "I have a node representing a folder on disk. How can I easily select/omit specific file types?"

My response:


  1. Follow all the steps here:

    https://blogs.oracle.com/geertjan/entry/netbeans_apis_outside_of_the


  2. Then add this file to the module:
    public class FileFilterNodeChildren extends FilterNode.Children{
    public FileFilterNodeChildren(Node original) {
    super(original);
    }
    @Override
    protected Node[] createNodes(Node object) {
    List<Node> result = new ArrayList<Node>();
    for (Node node : super.createNodes(object)) {
    if (accept(node)) {
    result.add(node);
    }
    }
    return result.toArray(new Node[0]);
    }
    private boolean accept(Node node) {
    if (node.getDisplayName().endsWith(".doc") || node.getDisplayName().endsWith(".xls")){
    return true;
    } else {
    return false;
    }
    }
    }

That's all. Similar story to part 1 which also discusses FilterNode.


Join the discussion

Comments ( 2 )
  • Eduardo Costa Monday, May 17, 2010

    I guess line 12 must be "result.add(new FilterNode(node))" - since the children nodes came from another node tree, using it on your children will raise an exception.


  • guest Wednesday, June 13, 2012

    I think is safer to accept files by its MIME type.

    To do that you can use node.getLookup().lookup(DataObject.class).getPrimaryFile(), which is initialized by default in nodes creates by aDataObject.getNodeDelegate().

    So you can do something like that:

    private boolean accept(Node node) {

    FileObject file = null;

    DataObject dataObject = node.getLookup().lookup(DataObject.class);

    if (dataObject != null) {

    file = dataObject.getPrimaryFile();

    }

    else {

    file = node.getLookup().lookup(FileObject.class);

    }

    return file != null && (file.isFolder() || file.getMIMEType().equals(ActionGuiConstants.DM_MIME_TYPE));

    }


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.