Download NetBeans!

20090901 Tuesday September 01, 2009

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

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 {

    @Override
    public void restored() {
        WindowManager.getDefault().invokeWhenUIReady(new DemoRunnable());
    }

    private class DemoRunnable implements Runnable {
        @Override
        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);
                                                oc.open();
                                            } catch (DataObjectNotFoundException ex) {
                                                Exceptions.printStackTrace(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.

Sep 01 2009, 03:17:22 PM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/get_notified_when_a_window1
Comments:

That's horrible, horrible code.

Posted by Mikael Gueck on September 01, 2009 at 09:52 PM PDT #

That's a horrible, horrible comment.

Posted by Peter Franken on September 01, 2009 at 10:07 PM PDT #

How about

newHashSet.removeAll(oldHashSet); ?

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

currentFile.getMIMEType();

instead of

dObj.getPrimaryFile().getMIMEType();

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

Posted by Sven Reimers on September 02, 2009 at 03:56 PM PDT #

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?

Posted by Jaroslav Tulach on September 07, 2009 at 12:14 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed