[Aprog]
[aprog.git] / Aprog / src / net / sourceforge / aprog / af / MacOSXTools.java
blob5396b54bfadf65dfc84ec3c650d3e9ced7af816f
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 package net.sourceforge.aprog.af;
27 import static net.sourceforge.aprog.tools.Tools.*;
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Proxy;
31 import java.util.logging.Level;
33 import net.sourceforge.aprog.tools.AbstractInvocationHandler;
34 import net.sourceforge.aprog.tools.IllegalInstantiationException;
35 import net.sourceforge.aprog.tools.Tools;
37 /**
39 * @author codistmonk (creation 2010-09-16)
41 public final class MacOSXTools {
43 /**
44 * @throws IllegalInstantiationException To prevent instantiation
46 private MacOSXTools() {
47 throw new IllegalInstantiationException();
50 public static final boolean MAC_OS_X = System.getProperty("os.name").equalsIgnoreCase("mac os x");
52 /**
54 * @param listenerMethodName
55 * <br>Not null
56 * <br>Shared
57 * <br>Range: { {@code "handleAbout"}, {@code "handlePreferences"}, {@code "handleQuit"} }
58 * @param objectOrClass
59 * <br>Not null
60 * <br>Shared
61 * @param methodName
62 * <br>Not null
63 * <br>Shared
64 * @param arguments
65 * <br>Not null
66 * <br>Shared
67 * @return {@code true} if a listener was successfully created and registered
69 @SuppressWarnings("unchecked")
70 public static final boolean registerMacOSXApplicationListener(final String listenerMethodName,
71 final Object objectOrClass, final String methodName, final Object... arguments) {
72 try {
73 final Class<?> applicationClass = Class.forName("com.apple.eawt.Application");
74 final Class<?> listenerClass = Class.forName("com.apple.eawt.ApplicationListener");
75 final Class<?> eventClass = Class.forName("com.apple.eawt.ApplicationEvent");
76 final Object application = invoke(applicationClass, "getApplication");
78 invoke(application, "addApplicationListener", Proxy.newProxyInstance(
79 getCallerClass().getClassLoader(),
80 array(listenerClass),
81 new AbstractInvocationHandler() {
83 @Override
84 public final Object invoke(final Object proxy,
85 final Method method, final Object[] proxyMethodArguments) throws Throwable {
86 if (method.getName().equals(listenerMethodName) &&
87 proxyMethodArguments.length == 1 && null != cast(eventClass, proxyMethodArguments[0])) {
88 Tools.invoke(proxyMethodArguments[0], "setHandled", true);
90 return Tools.invoke(objectOrClass, methodName, arguments);
93 return this.defaultInvoke(proxy, method, proxyMethodArguments);
96 }));
98 return true;
99 } catch (final Exception exception) {
100 getLoggerForThisMethod().log(Level.SEVERE, null, exception);
102 return false;
107 * Sets the application title that will be displayed in the Mac OS X Application Menu.
109 * <br>This must be invoked before loading any UI-related class (AWT, Swing, ...).
111 * @param applicationName
112 * <br>Not null
114 public static final void setApplicationName(final String applicationName) {
115 System.setProperty("com.apple.mrj.application.apple.menu.about.name", applicationName);
119 * @return
120 * <br>Maybe null
122 public static final String getApplicationName() {
123 return System.getProperty("com.apple.mrj.application.apple.menu.about.name");
127 * Calls {@code setUseScreenMenuBar(true)}.
129 public static final void useScreenMenuBar() {
130 setUseScreenMenuBar(true);
135 * @param useScreenMenuBar
136 * <br>Range: any boolean
138 public static final void setUseScreenMenuBar(final boolean useScreenMenuBar) {
139 System.setProperty("apple.laf.useScreenMenuBar", "" + useScreenMenuBar);
143 * @return
144 * <br>Range: any boolean
146 public static final boolean getUseScreenMenuBar() {
147 return "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
150 public static final void enableAboutMenu() {
151 invoke(invoke(getApplicationClass(), "getApplication"), "setEnabledAboutMenu", true);
154 public static final void enablePreferencesMenu() {
155 invoke(invoke(getApplicationClass(), "getApplication"), "setEnabledPreferencesMenu", true);
160 * @return
161 * <br>Maybe null
163 private static final Class<?> getApplicationClass() {
164 try {
165 return Class.forName("com.apple.eawt.Application");
166 } catch (final ClassNotFoundException ignoredException) {
167 suppressWarningUnused(ignoredException);
169 return null;