Search code examples
javanetbeansnetbeans-platformnetbeans-plugins

How to add a hook to close event for a particular editor in Netbeans?


I need to do something when an editor for a particular document is closed. I have a following code:

FileObject fobj = FileUtil.toFileObject(file);
final DataObject dobj = DataObject.find(fobj);
if (dobj != null) {
    EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
    StyledDocument doc = ec.openDocument();
    // Here I would like to add a listener for close event, for the editor window that was opened
}

Is there a way of doing this? Or can I at least hook a listener for global editor closing, so that I get notified of each editor window being closed? I guess in that case I would be somehow able to decide whether the given editor window is the one I am interested in.


Solution

  • By accident I stumbled about something which may help you:

    EditorCookie.Observable cookie = dataObject.getLookup().lookup(EditorCookie.Observable.class);
    cookie.addPropertyChangeListener(new PropertyChangeListener() {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
        // Do your stuff
      }
    });