Download NetBeans!

20080417 Thursday April 17, 2008

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.

Apr 17 2008, 12:14:38 AM PDT Permalink