org.openide.nodes.FilterNode (Part 2)

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.

 

Comments:

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.

Posted by Eduardo Costa on May 16, 2010 at 09:35 PM PDT #

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));
}

Posted by guest on June 13, 2012 at 06:59 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today