X

Geertjan's Blog

  • August 31, 2009

Get Notified When A Window Opens/Closes (Part 1)

Geertjan Wielenga
Product Manager
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...");
}
}
}
});
}
}
}

Join the discussion

Comments ( 1 )
  • Jaroslav Tulach Tuesday, September 1, 2009

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.