X

Geertjan's Blog

  • April 17, 2008

org.openide.filesystems.FileChangeListener

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 1 )
  • Adam Thursday, April 17, 2008

    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?


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