TransparenTech

An ISV and Software Consultancy

CocoaUIEnhancer - Connect the About, Preferences and Quit menus in Mac OS X Cocoa SWT and JFace applications

CocoaUIEnhancer is a Java class that can be used in SWT and JFace applications for connecting the Mac OS X Application 'About', 'Preferences' and 'Quit' menu items.

This class can be compiled without the SWT Cocoa bindings being in the classpath. It makes extensive use of Java reflection and so at compile time does not depend on any Cocoa internal classes.

This code was influenced by the CarbonUIEnhancer from Agynami with a modified implementation of the org.eclipse.ui.internal.cocoa.CocoaUIEnhancer.

Usage

First, you will need a method to determine if you are running on Mac OS X. You can use something like this:

    public static boolean isMac() {
        if ( System.getProperty( "os.name" ).equals( "Mac OS X" ) ) {
            return true;
        }
        return false;
    }

Then when running on Mac OS X, use the CocoaUIEnhancer to configure the About, Preferences and Quit menus. When not running on Mac OS X, add your Preferences, About and Quit menus as you normally do.

    if ( isMac() ) {
        CocoaUIEnhancer enhancer = new CocoaUIEnhancer( "MySWTApp" );
        enhancer.hookApplicationMenu( getShell().getDisplay(), myQuitListener, myAboutAction, myPreferencesAction );
    } else {
       // Add the Preferences, About and Quit menus.
    }

The version of CocoaUIEnhancer available here uses the JFace IAction interface for the About and Preferences menu. It would be trivial, however, to modify the code to use SWT Listeners for the About and Preferences menu if your application is SWT only.

Download

Download here: CocoaUIEnhancer.java

Contact

nick at transparentech.com

License

The CocoaUIEnhancer Java class is released under the Eclipse Public License (EPL).