DevFaqListenForSaveEvents

(Difference between revisions)
 
(5 intermediate revisions not shown)
Line 1: Line 1:
-
__NOTOC__
 
__NOTOC__
__NOTOC__
===How can I be notified when a file is modified and saved??===
===How can I be notified when a file is modified and saved??===
-
The [http://www.netbeans.org/download/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.Registry.html DataObject.Registry] in [http://www.netbeans.org/download/dev/javadoc/org-openide-loaders/allclasses-frame.html LoadersAPI] gives you a set of modified DataObjects. You can also add a listener and be notified when the set of modified objects changes.
+
The [http://bits.netbeans.org/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.Registry.html DataObject.Registry] in [http://bits.netbeans.org/dev/javadoc/org-openide-loaders LoadersAPI] gives you a set of modified DataObjects. You can also add a listener and be notified when the set of modified objects changes.
-
As this operates on DataObjects you will first need to get a DataObject for your FileObject using [http://www.netbeans.org/download/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.html#find(org.openide.filesystems.FileObject) DataObject.find()].
+
If you have a FileObject and want to listen for save events, you will need to get its DataObject by calling <tt>[http://bits.netbeans.org/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.html#find(org.openide.filesystems.FileObject) DataObject.find()]</tt>.
-
Or you can take the modified DataObjects and do dataObject.getPrimaryFile().
+
 
 +
To listen on a single DataObject for save events, simply add a <code>PropertyChangeListener</code> and listen for changes in <code>DataObject.PROP_MODIFIED</code>.
 +
 
 +
Note that listening for something to be saved is ''not the same as listening for any changes in the file'' - you are really listening only for (directly or indirectly) user-initiated save events, as in the user pressing CTRL-S when the file is modified and opened in the editor.  For notifications about any changes in a file, instead attach a <tt>[[DevFaqListenForChangesInNonExistentFile|FileChangeListener]]</tt> to the underlying <tt>[[DevFaqFileObject|FileObject]]</tt>.
 +
 
 +
==Track the currently modified files by DataObject Registry listening - Sample code==
 +
<source lang="java">
 +
DataObject.Registry registries = DataObject.getRegistry();
 +
registries.addChangeListener(new ChangeListener() {
 +
    public void stateChanged(ChangeEvent e) {
 +
        System.out.println("ChangedListener: o = " + e.getSource().getClass());
 +
        System.out.println("ChangedListener: o.source = " + e.getSource());
 +
    }
 +
});
 +
DataObject[] objects = registries.getModified();
 +
for (int i = 0; i < objects.length; i++) {
 +
    DataObject dataObj = objects[I];
 +
    System.out.println("data object name = " + dataObj.getName());
 +
    System.out.println("data object pimary file name = " + dataObj.getPrimaryFile().getName());
 +
    Set fss = dataObj.files();
 +
    Iterator iter = fss.iterator();
 +
    while (iter.hasNext()) {
 +
        FileObject fo = (FileObject) iter.next();
 +
        System.out.println("\tset file object: " + fo.getName());
 +
    }
 +
}
 +
</source>
 +
 
 +
==Libraries needed==
 +
* Datasystems API
 +
* File System API
 +
* Nodes API
 +
* Utilities API

Current revision as of 19:50, 24 July 2010

How can I be notified when a file is modified and saved??

The DataObject.Registry in LoadersAPI gives you a set of modified DataObjects. You can also add a listener and be notified when the set of modified objects changes.

If you have a FileObject and want to listen for save events, you will need to get its DataObject by calling DataObject.find().

To listen on a single DataObject for save events, simply add a PropertyChangeListener and listen for changes in DataObject.PROP_MODIFIED.

Note that listening for something to be saved is not the same as listening for any changes in the file - you are really listening only for (directly or indirectly) user-initiated save events, as in the user pressing CTRL-S when the file is modified and opened in the editor. For notifications about any changes in a file, instead attach a FileChangeListener to the underlying FileObject.

Track the currently modified files by DataObject Registry listening - Sample code

DataObject.Registry registries = DataObject.getRegistry();
registries.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        System.out.println("ChangedListener: o = " + e.getSource().getClass());
        System.out.println("ChangedListener: o.source = " + e.getSource());
    }
});
DataObject[] objects = registries.getModified();
for (int i = 0; i < objects.length; i++) {
    DataObject dataObj = objects[I];
    System.out.println("data object name = " + dataObj.getName());
    System.out.println("data object pimary file name = " + dataObj.getPrimaryFile().getName());
    Set fss = dataObj.files();
    Iterator iter = fss.iterator();
    while (iter.hasNext()) {
        FileObject fo = (FileObject) iter.next();
        System.out.println("\tset file object: " + fo.getName());
    }
}

Libraries needed

  • Datasystems API
  • File System API
  • Nodes API
  • Utilities API
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo