public void propertyChange(final PropertyChangeEvent e) {
   if (myAlarm.getActiveRequestCount() == 0) {
     myInitialFocusedWindow = (Window) e.getOldValue();
     final MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath();
     if (selectedPath.length == 0) { // there is no visible popup
       return;
     }
     Component firstComponent = null;
     for (final MenuElement menuElement : selectedPath) {
       final Component component = menuElement.getComponent();
       if (component instanceof JMenuBar) {
         firstComponent = component;
         break;
       } else if (component instanceof JPopupMenu) {
         firstComponent = ((JPopupMenu) component).getInvoker();
         break;
       }
     }
     if (firstComponent == null) {
       return;
     }
     final Window window = SwingUtilities.getWindowAncestor(firstComponent);
     if (window != myInitialFocusedWindow) { // focused window doesn't have popup
       return;
     }
   }
   myLastFocusedWindow = (Window) e.getNewValue();
   myAlarm.cancelAllRequests();
   myAlarm.addRequest(myClearSelectedPathRunnable, 150);
 }
  private boolean isComponentPartOfCurrentMenu(MenuElement root, Component c) {
    MenuElement children[];
    int i, d;

    if (root == null) return false;

    if (root.getComponent() == c) return true;
    else {
      children = root.getSubElements();
      for (i = 0, d = children.length; i < d; i++) {
        if (isComponentPartOfCurrentMenu(children[i], c)) return true;
      }
    }
    return false;
  }