org.openide.filesystems.FileChangeListener

How to listen to changes to a file, via the related NetBeans FileObject, and change the DataNode's display name, tooltip, and icon as a result:

public class DemoDataNode extends DataNode implements FileChangeListener {

    //The objects we are dealing with:
    private static final String ICON1 = "/org/netbeans/demofiletype/demo-1.png";
    private static final String ICON2 = "/org/netbeans/demofiletype/demo-2.png";
    DemoDataObject obj;
    Date date;
    String displayName;
    String tooltip;
    Image icon;
    int count = 0;

    //Constructor:
    public DemoDataNode(DemoDataObject obj) {
        super(obj, Children.LEAF);
        this.obj = obj;
        date = new Date();
    }

    //Constructor for receiving the lookup:
    DemoDataNode(DemoDataObject obj, Lookup lookup) {
        super(obj, Children.LEAF, lookup);
        //Add file change listener to the FileObject:
        obj.getPrimaryFile().addFileChangeListener(this);
        //Set default icon:
        setIconBaseWithExtension(ICON1);
        //Set default tooltip:
        setShortDescription("Hello world!");
    }

    @Override
    public String getDisplayName() {
        if (null != displayName) {
            return displayName;
        }
        return super.getDisplayName();
    }

    @Override
    public String getShortDescription() {
        if (null != tooltip) {
            return tooltip;
        }
        return super.getShortDescription();
    }

    @Override
    public Image getIcon(int arg0) {
        if (null != icon) {
            return icon;
        }
        return super.getIcon(arg0);
    }

    //When the file changes...
    @Override
    public void fileChanged(FileEvent arg0) {

        //Increment the count:
        count = count + 1;
        //Depending on the modulus, switch the icon:
        if (count % 2 == 1) {
            icon = Utilities.loadImage(ICON1);
        } else {
            icon = Utilities.loadImage(ICON2);
        }
        
        //Get the milliseconds and format it:
        long mills = System.currentTimeMillis();
        DateFormat dateFormatter = DateFormat.getDateTimeInstance(
                DateFormat.LONG,
                DateFormat.LONG);
        String formatted = dateFormatter.format(mills);

        //Save the current display name:
        String oldDisplayName = displayName;

        //Save the current tooltip:
        String oldShortDescription = tooltip;

        //Set the new display name:
        displayName = "Change " + count + " (" + formatted + ")";

        //Set the new tooltip:
        tooltip = formatted;

        //Fire change events on the node,
        //which will immediately refresh it with the new values:
        fireDisplayNameChange(oldDisplayName, displayName);
        fireShortDescriptionChange(oldShortDescription, tooltip);
        fireIconChange();
    }

    @Override
    public void fileFolderCreated(FileEvent arg0) {}
    @Override
    public void fileDataCreated(FileEvent arg0) {}
    @Override
    public void fileDeleted(FileEvent arg0) {}
    @Override
    public void fileRenamed(FileRenameEvent arg0) {}
    @Override
    public void fileAttributeChanged(FileAttributeEvent arg0) {}

}

When you change a document associated with the DataNode and DataObject referred to here, and you then save the document, the display name, tooltip, and icon will change.

Comments:

Nice. While I've been writing NB-based prototype of our application at me work, I had one (constant) fear: may I store (hard) reference to DataObject inside node? Like you did in todays post. In most cases just getting event source (via weak listeners oc) was enough but I was unable to find "the proper way" to reference some dataobject from node.

Maybe you know: Is it safe to store hard reference? Or is it a (memory) leak?

Btw have you ever tried to map database entities (instances) to custom file system (FileObject represented entity, DataObject entity instance)? Amount of platform features that can be used is impressive (e.g. editor support) I've used custom DataObject based class but imho InstanceDataObject can be used. Do you have any experience? Is this already present somewhere inside NB?

Posted by Adam on April 17, 2008 at 04:22 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
23
24
25
26
27
28
29
30
   
       
Today