[platform-dev] Re: How to perform some operation at the closing of file

  • From: Wade Chandler < >
  • To:
  • Cc: remya < >
  • Subject: [platform-dev] Re: How to perform some operation at the closing of file
  • Date: Thu, 29 Nov 2012 07:11:55 -0500

On 11/29/2012 12:50 AM, remya wrote:

I opened a file in editor using following code.

         FileObject fob = FileUtil.toFileObject(file.getAbsoluteFile());
          DataObject dob = DataObject.find(fob);
                 Openable oc = dob.getLookup().lookup(Openable.class);
                 if (oc != null) {
               Node  node = dob.getNodeDelegate();
I need to perform some operation at the closing of the file in editor. How can we understand that a file a closed in netbeans editor.
Please help.

I will give a couple possibilities. Too, I will give links first, and then I will explain:

Method 1: You defined the editor entirely, including the TopComponent with CloneableEditorSupport (may not be your case)


See your TopComponent classes componentClosed method. If you don't have one, then override it. Remember though, you may have clones. This would get you there without property editors if you have done your own editor and you don't need to listen to other editors.

Method 2: Listen for a closed editor by way of DataObject and CloneableEditorSupport:


Notice what CloneableEditorSupport states "Can be assigned as a cookie to any editable data object" and that it implements EditorCookie. Too, there is language in its addPropertyChangeListener documentation pointing to EditorCookie.Observable. So, I don't know if you will ever have a case where there isn't a CloneableEditorSupport or EditorCookie.Observable class found in the lookup or cookie set, but I think this may be the best way barring Method 1.

Based on your code, you can call dob.getLookup().lookup(EditorCookie.Observable.class) and then add a PropertyChangeListener to that. You want to listen to EditorCookie.Observable.PROP_OPENED_PANES. Remember, that unless you have created an editor and told it to specifically not be *cloneable* that the user may have multiple panes opened viewing the same file from different angles. When you have no opened panes, then you can say you are really closed.

Method 3: Listen for a closed editor and check the file name using EditorRegistry and BaseDocument


In your case, your have the file object and thus the file name, so you could use this method as well; here mostly for a complete demonstration, but works. Add a property listener to the EditorRegistry. Listen for the property EditorRegistry.COMPONENT_REMOVED_PROPERTY. Now, the user can actually have your file opened in multiple panes as I mentioned before, so we will have to check every opened pane.

In the property listener and if the correct property of course, call EditorRegistry.componentList() giving you a List<? extends JTextComponent>, then you can iterate over those and call getDocument(). If it follows normal NB conventions and isn't a completely customized editor, then this should be of the type BaseDocument which extends AbstractDocument. Notice BaseDocument has a property FILE_NAME_PROP accessible through BaseDocument.getProperty(). You will essentially have to look for the *missing* file name. That will tell you the closed file you are looking for.

Method 4 (Possibly: nothing guaranteed here, but would be interesting to see what happens...never tried): Listen to the DataObject.Registry


I'm not sure what happens on close in there, but you can easily put together some code to step through. Either way, the other options should get you there and are not as academic.

I found 4 just looking to see if the rest of the information wasn't already in the Dev FAQ. Once you get your code working, and for me to selfishly reel in another community contributor versus doing it myself, could you please add an entry to the Wiki:

That will help others. If so, thanks in advance!

I hope it helps,



Wade Chandler

Software Engineer and Consultant
NetBeans Contributor
NetBeans Dream Team Member


[platform-dev] How to perform some operation at the closing of file

remya 11/29/2012

[platform-dev] Re: How to perform some operation at the closing of file

Wade Chandler 11/29/2012

Project Features

Project Links

About this Project

Platform was started in November 2009, is owned by Antonin Nebuzelsky, and has 149 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
Please Confirm