Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 24

Download

How can I be notified when a file is opened?

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import org.openide.awt.StatusDisplayer;
import org.openide.filesystems.FileChangeAdapter;
import org.openide.filesystems.FileEvent;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.windows.OnShowing;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

@OnShowing
public class Installer implements Runnable {
    @Override
    public void run() {
WindowManager.getDefault().getRegistry().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("opened")) {
                    HashSet<TopComponent> newHashSet = (HashSet<TopComponent>) evt.getNewValue();
                    HashSet<TopComponent> oldHashSet = (HashSet<TopComponent>) evt.getOldValue();
                    for (TopComponent topComponent : newHashSet) {
                        if (!oldHashSet.contains(topComponent)) {
                            DataObject dObj = topComponent.getLookup().lookup(DataObject.class);
                            if (dObj != null) {
                                FileObject currentFile = dObj.getPrimaryFile();
                                if (currentFile != null &amp;&amp; currentFile.getMIMEType().equals("text/x-java")) {
StatusDisplayer.getDefault().setStatusText("Hurray! "
                                            + "Opened " + currentFile.getNameExt(), 1);
currentFile.addFileChangeListener(new FileChangeAdapter() {
                                        @Override
                                        public void fileChanged(FileEvent fe) {
StatusDisplayer.getDefault().setStatusText("Hurray! "
                                                    + "Saved " + fe.getFile().getNameExt(), 1);
                                        }
                                    });
                                }
                            }
                        }
                    }
                }
            }
        });
    }

}

Taken from nbdev-mailing list - thanks to Geertjan.