private class WindowSystemMonitor implements PropertyChangeListener, ComponentListener { private Set openedSoFar = null; private TopComponent shouldBeShown; /** * Must be called before adding this listener to environment if in hope that * it will provide (initial) open/close events. */ private void enableOpenCloseEvents() { List list = Arrays.asList(SuggestionsScanner.openedTopComponents()); openedSoFar = new HashSet(list); shouldBeShown = null; } /** Reacts to changes */ public void propertyChange(PropertyChangeEvent ev) { String prop = ev.getPropertyName(); if (prop.equals(TopComponent.Registry.PROP_OPENED)) { if (allOpenedClientsCount > 0) { // determine what components have been closed, window system does not // provide any other listener to do it in more smart way List list = Arrays.asList(SuggestionsScanner.openedTopComponents()); Set actual = new HashSet(list); if (openedSoFar != null) { Iterator it = openedSoFar.iterator(); while(it.hasNext()) { TopComponent tc = (TopComponent) it.next(); if (actual.contains(tc) ) continue; handleTopComponentClosed(tc); } Iterator ita = actual.iterator(); while(ita.hasNext()) { TopComponent tc = (TopComponent) ita.next(); if (openedSoFar.contains(tc)) continue; assert shouldBeShown == null : "Multiple opened TCs in burst without showing them one-by-one is not handled"; // NOI18N // defer actual action to componentShown, We need to assure opened TC is // selected one. At this moment previous one is still selected. shouldBeShown = tc; } } openedSoFar = actual; } else { componentsChanged(); openedSoFar = null; } } } public void componentShown(ComponentEvent e) { if (shouldBeShown != null) { handleTopComponentOpened(shouldBeShown); shouldBeShown = null; } } public void componentHidden(ComponentEvent e) { //XXX it does not support both "current file" and "all opened" clients at same time if (allOpenedClientsCount == 0) { componentsChanged(); } } public void componentResized(ComponentEvent e) { // Don't care } public void componentMoved(ComponentEvent e) { // Don't care } }