Monday August 31, 2009
Get Notified When A Window Opens/Closes (Part 1)
public class Installer extends ModuleInstall {
private Result<DataObject> res;
@Override
public void restored() {
WindowManager.getDefault().invokeWhenUIReady(new DemoRunnable());
}
private class DemoRunnable implements Runnable {
@Override
public void run() {
final TopComponent.Registry registry = TopComponent.getRegistry();
registry.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
TopComponent tc = registry.getActivated();
if (tc != null) {
res = tc.getLookup().lookupResult(DataObject.class);
EditorCookie ec = tc.getLookup().lookup(EditorCookie.class);
if (ec != null && ec.getDocument() != null && res.allInstances().iterator().hasNext()) {
FileObject fo = res.allInstances().iterator().next().getPrimaryFile();
StatusDisplayer.getDefault().setStatusText("Open: " + fo.getPath());
} else {
StatusDisplayer.getDefault().setStatusText("Closed...");
}
}
}
});
}
}
}
Aug 31 2009, 03:32:53 PM PDT Permalink
Rather than listening on TopComponent registry, I usually suggest to observer Utilities.actionsGlobalLookup() with A LookupListener.
Such code is simpler and also observes changes inside the TopComponents themselves (like changing selected node in a explorer) which the above example fails to achieve.
Posted by Jaroslav Tulach on September 01, 2009 at 12:40 AM PDT #


