DevFaqListenForOpenEvents

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 && 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);
                                        }
                                    });
                                }
                            }
                        }
                    }
                }
            }
        });
    }

} 

See also http://java.net/projects/nbwicketsupport/sources/nbwicketsupport/content/WicketSuite/WicketCore/src/org/netbeans/modules/web/wicket/installer/Installer.java

Taken from nbdev-mailing list - thanks to Geertjan.

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