Geertjan's Blog

  • September 1, 2009

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

Geertjan Wielenga
Product Manager
New and improved version of what I posted in my blog yesterday, thanks to discussions with Toni Epple, with whom I find myself in the Holiday Inn in Geneva, Switzerland, the evening before the next NetBeans Platform Certified Training, this time in Switzerland,
public class Installer extends ModuleInstall {
public void restored() {
WindowManager.getDefault().invokeWhenUIReady(new DemoRunnable());
private class DemoRunnable implements Runnable {
public void run() {//Set property change listener on registry of WindowManager:
WindowManager.getDefault().getRegistry().addPropertyChangeListener(new PropertyChangeListener() {
@Override//When a change occurs...
public void propertyChange(PropertyChangeEvent evt) {//Get the events with "opened" as property name,
//i.e., a TopComponent has been opened:

if (evt.getPropertyName().equals("opened")) {//Get the current list of opened TopComponents:
HashSet<TopComponent> newHashSet = (HashSet<TopComponent>) evt.getNewValue();//Get the previous list of opened TopComponents:
HashSet<TopComponent>oldHashSet = (HashSet<TopComponent>) evt.getOldValue();//Iterate through the new list:
for (Iterator<TopComponent> it = newHashSet.iterator(); it.hasNext();) {//Get the current next TopComponent in the iteration;
TopComponent topComponent = it.next();//Check that the old set does not contain the current next TopComponent:
if (!oldHashSet.contains(topComponent)) {//If the old set does not contain the current TopComponent from the new set,
//we have found the TopComponent that has just been opened,
//so let's get the DataObject from its Lookup:

DataObject dObj = topComponent.getLookup().lookup(DataObject.class);
if (dObj != null) {//Get the FileObject:
FileObject currentFile = dObj.getPrimaryFile();
if (currentFile != null) {//Get the mime type:
String mimeType = dObj.getPrimaryFile().getMIMEType();
FileObject matchingFile = null;//Use the appropriate internal method in the Wicket plugin
//(thanks Tim) for finding the matching file,
//either Java or HTML:

if (mimeType.equals("text/html")) {
matchingFile = JavaForMarkupQuery.find(currentFile);
} else if (mimeType.equals("text/x-java")) {
matchingFile = MarkupForJavaQuery.find(currentFile);
if (matchingFile != null) {
try {//Open the found file:
DataObject matchingDobj = DataObject.find(matchingFile);
OpenCookie oc = matchingDobj.getCookie(OpenCookie.class);
} catch (DataObjectNotFoundException ex) {
}//Now the file that the user tried to open,
//together with its matching file,
//should both be open in the editor mode.


The above is applicable to the Wicket plugin, where opening a Java file now results in the opening of the related HTML file, and vice versa, thanks to the code above.

Join the discussion

Comments ( 4 )
  • Mikael Gueck Tuesday, September 1, 2009

    That's horrible, horrible code.

  • Peter Franken Tuesday, September 1, 2009

    That's a horrible, horrible comment.

  • Sven Reimers Wednesday, September 2, 2009

    How about

    newHashSet.removeAll(oldHashSet); ?

    This should leave only new components in newHashSet (those that were not in beforehand) and how about


    instead of


    since currentFile was already assigned.

    Makes the code perhaps it a bit less horrible (but still won't win a price for beauty).

    Couldn't resist ;-)

  • Jaroslav Tulach Monday, September 7, 2009

    The return value of getOpened() is Set<TopComponent>. How could you guys come up with a cast to HashSet?

    HashSet<TopComponent> newHashSet = (HashSet<TopComponent>) evt.getNewValue();

    A kind of "empiristic" programming?

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