ReheatMenus

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import org.openide.awt.MenuBar;
import org.openide.util.RequestProcessor;
import org.openide.windows.OnShowing;
import org.openide.windows.WindowManager;

/**
 * Adapted from {@code org.netbeans.core.ui.warmup.MenuWarmUpTask}.
 */
@OnShowing
public class ReheatMenus implements Runnable {
    static void launch() {
        EventQueue.invokeLater(new ReheatMenus());
    }
    private static final RequestProcessor RP = new RequestProcessor(ReheatMenus.class);
    private MenuBar bar;
    private Component[] comps;
    @Override public void run() {
        if (EventQueue.isDispatchThread()) {
            if (bar == null) {
                Frame main = WindowManager.getDefault().getMainWindow();
                assert main != null;
                if (main instanceof JFrame) {
                    JMenuBar menuBar = ((JFrame) main).getJMenuBar();
                    if (menuBar instanceof MenuBar) {
                        bar = (MenuBar) menuBar;
                        RP.post(this);
                    }
                }
            } else {
                comps = bar.getComponents();
                RP.post(this);
            }
        } else if (comps != null) {
            walkMenu(comps);
        } else {
            bar.waitFinished();
            EventQueue.invokeLater(this);
        }
    }
    private void walkMenu(Component[] items) {
        for (Component item : items) {
            if (!(item instanceof JMenu)) {
                continue;
            }
            try {
                Class<?> cls = item.getClass();
                Method m = cls.getDeclaredMethod("doInitialize");
                m.setAccessible(true);
                m.invoke(item);
                walkMenu(((JMenu) item).getMenuComponents());
            } catch (Exception x) {
                // most likely a NoSuchMethodException on a dynamic submenu
            }
        }
    }
}
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Categories
Archives
« June 2016
SunMonTueWedThuFriSat
   
16
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today