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 org.seamless.swing.Controller;
19  
20  import javax.swing.JFrame;
21  import java.lang.reflect.InvocationHandler;
22  import java.lang.reflect.Method;
23  import java.lang.reflect.Proxy;
24  
25  /**
26   * @author Christian Bauer
27   */
28  public class PlatformApple {
29  
30      public static void setup(final Controller<JFrame> appController, String appName) throws Exception {
31  
32          System.setProperty("apple.laf.useScreenMenuBar", "true");
33          System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName);
34          System.setProperty("apple.awt.showGrowBox", "true");
35  
36          // Use reflection to avoid compile-time dependency
37          Class appClass = Class.forName("com.apple.eawt.Application");
38          Object application = appClass.newInstance();
39          Class listenerClass = Class.forName("com.apple.eawt.ApplicationListener");
40          Method addAppListmethod = appClass.getDeclaredMethod("addApplicationListener", listenerClass);
41  
42          // creating and adding a custom adapter/listener to the Application
43          Class adapterClass = Class.forName("com.apple.eawt.ApplicationAdapter");
44          Object listener = AppListenerProxy.newInstance(adapterClass.newInstance(), appController);
45          addAppListmethod.invoke(application, listener);
46      }
47  
48      static class AppListenerProxy implements InvocationHandler {
49  
50          private Controller<JFrame> appController;
51          private Object object;
52  
53          public static Object newInstance(Object obj, Controller<JFrame> appController) {
54              return Proxy.newProxyInstance(
55                      obj.getClass().getClassLoader(),
56                      obj.getClass().getInterfaces(),
57                      new AppListenerProxy(obj, appController)
58              );
59          }
60  
61          private AppListenerProxy(Object obj, Controller<JFrame> appController) {
62              this.object = obj;
63              this.appController = appController;
64          }
65  
66          public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
67              Object result = null;
68              try {
69                  if ("handleQuit".equals(m.getName())) {
70                      if (appController != null) {
71                          appController.dispose();
72                          appController.getView().dispose();
73                      }
74                  } else {
75                      result = m.invoke(object, args);
76                  }
77              } catch (Exception e) {
78                  // Ignore
79              }
80              return result;
81          }
82  
83      }
84  
85  }