// setHandler creates a Proxy object from the passed OSXAdapter and adds it as an // ApplicationListener @SuppressWarnings({"rawtypes", "unchecked"}) public static void setHandler(OSXAdapter adapter) { try { Class applicationClass = Class.forName("com.apple.eawt.Application"); if (macOSXApplication == null) { macOSXApplication = applicationClass.getConstructor((Class[]) null).newInstance((Object[]) null); } Class applicationListenerClass = Class.forName("com.apple.eawt.ApplicationListener"); Method addListenerMethod = applicationClass.getDeclaredMethod( "addApplicationListener", new Class[] {applicationListenerClass}); // Create a proxy object around this handler that can be reflectively added as an Apple // ApplicationListener Object osxAdapterProxy = Proxy.newProxyInstance( OSXAdapter.class.getClassLoader(), new Class[] {applicationListenerClass}, adapter); addListenerMethod.invoke(macOSXApplication, new Object[] {osxAdapterProxy}); } catch (ClassNotFoundException cnfe) { LOG.debug( "This version of Mac OS X does not support the Apple EAWT. ApplicationEvent handling has been disabled."); } catch ( Exception ex) { // Likely a NoSuchMethodException or an IllegalAccessException loading/invoking // eawt.Application methods LOG.debug("Failed to access Mac OS X EAWT: " + ex.getMessage()); } }
@SuppressWarnings({"rawtypes", "unchecked"}) public static void setDockIconImage(Image image) { try { Class applicationClass = Class.forName("com.apple.eawt.Application"); if (macOSXApplication == null) { macOSXApplication = applicationClass.getConstructor((Class[]) null).newInstance((Object[]) null); } Method setDockIconImage = applicationClass.getDeclaredMethod("setDockIconImage", new Class[] {Image.class}); setDockIconImage.invoke(macOSXApplication, image); } catch (ClassNotFoundException e) { LOG.debug( "This version of Mac OS X does not support the Apple EAWT. ApplicationEvent handling has been disabled."); } catch (Exception e) { LOG.debug("Failed to access Mac OS X EAWT: " + e.getMessage()); } }
// It is important to mark the ApplicationEvent as handled and cancel the default behavior // This method checks for a boolean result from the proxy method and sets the event accordingly protected void setApplicationEventHandled(Object event, boolean handled) { if (event != null) { try { Method setHandledMethod = event.getClass().getDeclaredMethod("setHandled", new Class[] {boolean.class}); // If the target method returns a boolean, use that as a hint setHandledMethod.invoke(event, new Object[] {Boolean.valueOf(handled)}); } catch (Exception ex) { LOG.debug("Failed to handle an Mac OS X EAWT ApplicationEvent: " + ex.getMessage()); } } }
// Pass this method an Object and a Method equipped to display application options // They will be called when the Preferences menu item is selected from the application menu public static void setPreferencesHandler(Object target, Method prefsHandler) { boolean enablePrefsMenu = (target != null && prefsHandler != null); if (enablePrefsMenu) { setHandler(new OSXAdapter("handlePreferences", target, prefsHandler)); } // If we're setting a handler, enable the Preferences menu item by calling // com.apple.eawt.Application reflectively try { Method enablePrefsMethod = macOSXApplication .getClass() .getDeclaredMethod("setEnabledPreferencesMenu", new Class[] {boolean.class}); enablePrefsMethod.invoke(macOSXApplication, new Object[] {Boolean.valueOf(enablePrefsMenu)}); } catch (Exception ex) { LOG.debug("Failed to enable the Preferences entry in the Mac OS X application menu."); } }