Workaround BooleanStateAction in Mac OSX Global Menu

June 18th, 2012 | by Tonny Kohar |

If you are using BooleanStateAction and bitten by this bug #207477.
And interestingly this bug is start to creep into Ubuntu Unity Global Menu as well (using Java Ayatana plugins).

Below is the workaround. The idea is to maintain the JMenuItem reference ourselves and set the appropriate (checked/enabled) state.
So you can inherit from WorkaroundBooleanStateAction instead of original BooleanStateAction

public abstract class WorkaroundBooleanStateAction extends BooleanStateAction {
    // if needed do you own stuff
 
    /* XXX Netbeans Platform bug 207477
     * Workaround for Mac OSX Bug with BooleanStateAction enabled and selected
     * so keep track JMenuItem ourselves
     */
    private WeakHashMap<JMenuItem, WeakReference<JMenuItem>> menuItemsMap 
            = new WeakHashMap<JMenuItem, WeakReference<JMenuItem>>();
 
    @Override
    public JMenuItem getMenuPresenter() {
        if (Utilities.isWindows()) {
            return super.getMenuPresenter();
        }
        JMenuItem menuItem = super.getMenuPresenter();
        menuItemsMap.put(menuItem,new WeakReference<JMenuItem>(menuItem));
        return menuItem;
    }
 
    @Override
    public void setBooleanState(boolean value) {
        super.setBooleanState(value);
        if (!Utilities.isWindows()) {
            for (JMenuItem menuItem : menuItemsMap.keySet()) {
                menuItem.setSelected(value);
            }
        }
    }
 
    @Override
    public void setEnabled(boolean value) {
        super.setEnabled(value);
        if (!Utilities.isWindows()) {
            for (JMenuItem menuItem : menuItemsMap.keySet()) {
                menuItem.setEnabled(value);
            }
        }
    }
 
    // if needed do you own stuff
}

note: above is a hack (workaround) without modifying any platform code. It is not proper solution

Tags: ,

You must be logged in to post a comment.