// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    JCheckBox check = new JCheckBox("JMenu: hover(show popup automatically) on cursor", true);
    JMenuBar bar = makeMenuBar();
    visitAll(bar, new MouseAdapter() {
      @Override public void mousePressed(MouseEvent e) {
        if (check.isSelected()) {
          ((AbstractButton) e.getComponent()).doClick();
        }
      }

      @Override public void mouseEntered(MouseEvent e) {
        if (check.isSelected()) {
          ((AbstractButton) e.getComponent()).doClick();
        }
      }
    });

    // EventQueue.invokeLater(() -> {
    //   Component c = SwingUtilities.getRoot(this);
    //   if (c instanceof JFrame) {
    //     ((JFrame) c).setJMenuBar(bar);
    //   }
    // });
    EventQueue.invokeLater(() -> getRootPane().setJMenuBar(bar));

    add(check, BorderLayout.SOUTH);
    setPreferredSize(new Dimension(320, 240));
  }

  private static JMenuBar makeMenuBar() {
    JMenuBar bar = new JMenuBar();

    JMenu menu = bar.add(new JMenu("File"));
    menu.add("Open");
    menu.add("Save");
    menu.add("Exit");

    menu = bar.add(new JMenu("Edit"));
    menu.add("Undo");
    menu.add("Redo");
    menu.addSeparator();
    menu.add("Cut");
    menu.add("Copy");
    menu.add("Paste");
    menu.add("Delete");

    menu = bar.add(new JMenu("Test"));
    menu.add("JMenuItem1");
    menu.add("JMenuItem2");
    JMenu sub = new JMenu("JMenu");
    sub.add("JMenuItem4");
    sub.add("JMenuItem5");
    menu.add(sub);
    menu.add("JMenuItem3");

    return bar;
  }

  private static void visitAll(Container p, MouseListener l) {
    for (Component comp: p.getComponents()) {
      if (comp instanceof JMenu) {
        ((JMenu) comp).addMouseListener(l);
      }
      // if (comp instanceof Container) {
      //   Container c = (Container) comp;
      //   if (c.getComponentCount() > 0) {
      //     visitAll(c, l);
      //   }
      //   if (c instanceof JMenu) {
      //     c.addMouseListener(l);
      //   }
      // }
    }
  }

  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGui();
      }
    });
  }

  public static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -478,24 +478,34 @@
 ();%0A    
-visitAll
+addListenerToJMenu
 (bar, ne
@@ -869,199 +869,8 @@
 );%0A%0A
-    // EventQueue.invokeLater(() -%3E %7B%0A    //   Component c = SwingUtilities.getRoot(this);%0A    //   if (c instanceof JFrame) %7B%0A    //     ((JFrame) c).setJMenuBar(bar);%0A    //   %7D%0A    // %7D);%0A
     
@@ -1689,28 +1689,43 @@
 oid 
-visitAll(Container p
+addListenerToJMenu(JToolBar toolBar
 , Mo
@@ -1744,16 +1744,19 @@
 l) %7B%0A   
+ //
  for (Co
@@ -1767,15 +1767,21 @@
 ent 
-comp: p
+menu: toolBar
 .get
@@ -1804,278 +1804,73 @@
     
-  if (comp instanceof JMenu) %7B%0A        ((JMenu) comp).addMouseListener(l);%0A      %7D%0A      // if (comp instanceof Container) %7B%0A      //   Container c = (Container) comp;%0A      //   if (c.getComponentCount() %3E 0) %7B%0A      //     visitAll(c, l);%0A      //   %7D%0A      //   if (c
+for (MenuElement menu: toolBar.getSubElements()) %7B%0A      if (menu
  ins
@@ -1896,16 +1896,24 @@
     
-//     c
+  ((JMenu) menu)
 .add
@@ -1939,24 +1939,8 @@
     
- //   %7D%0A      //
  %7D%0A 

use MenuElement#getSubElements() instead of Container#getComponents()