View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * @author Christian Bauer
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          // Use reflection to avoid compile-time dependency
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          // creating and adding a custom adapter/listener to the Application
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                  // Ignore
75              }
76              return result;
77          }
78  
79      }
80  
81  }