1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.support.shared;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21
22
23
24
25 public class NewPlatformApple {
26
27 public static void setup(final ShutdownHandler shutdownHandler, String appName) throws Exception {
28
29 System.setProperty("apple.laf.useScreenMenuBar", "true");
30 System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName);
31 System.setProperty("apple.awt.showGrowBox", "true");
32
33
34 Class appClass = Class.forName("com.apple.eawt.Application");
35 Object application = appClass.newInstance();
36 Class listenerClass = Class.forName("com.apple.eawt.ApplicationListener");
37 Method addAppListmethod = appClass.getDeclaredMethod("addApplicationListener", listenerClass);
38
39
40 Class adapterClass = Class.forName("com.apple.eawt.ApplicationAdapter");
41 Object listener = AppListenerProxy.newInstance(adapterClass.newInstance(), shutdownHandler);
42 addAppListmethod.invoke(application, listener);
43 }
44
45 static class AppListenerProxy implements InvocationHandler {
46
47 private ShutdownHandler shutdownHandler;
48 private Object object;
49
50 public static Object newInstance(Object obj, ShutdownHandler shutdownHandler) {
51 return Proxy.newProxyInstance(
52 obj.getClass().getClassLoader(),
53 obj.getClass().getInterfaces(),
54 new AppListenerProxy(obj, shutdownHandler)
55 );
56 }
57
58 private AppListenerProxy(Object obj, ShutdownHandler shutdownHandler) {
59 this.object = obj;
60 this.shutdownHandler = shutdownHandler;
61 }
62
63 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
64 Object result = null;
65 try {
66 if ("handleQuit".equals(m.getName())) {
67 if (shutdownHandler != null) {
68 shutdownHandler.shutdown();
69 }
70 } else {
71 result = m.invoke(object, args);
72 }
73 } catch (Exception e) {
74
75 }
76 return result;
77 }
78
79 }
80
81 }