View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.ui.tray;
21  
22  import java.awt.*;
23  import java.lang.reflect.InvocationHandler;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.lang.reflect.Proxy;
27  
28  public class OSXHandler implements InvocationHandler {
29      private final OSXTrayInterface davGatewayTray;
30  
31      public OSXHandler(OSXTrayInterface davGatewayTray) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
32          this.davGatewayTray = davGatewayTray;
33          addEventHandlers();
34      }
35  
36      public static final boolean IS_JAVA9 = Double.parseDouble(System.getProperty("java.specification.version")) >= 1.9;
37  
38      public void addEventHandlers() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
39              InvocationTargetException {
40  
41          Class<?> applicationClass;
42          Class<?> aboutHandlerClass;
43          Class<?> preferencesHandlerClass;
44  
45          Object application;
46          if (IS_JAVA9) {
47              applicationClass = Class.forName("java.awt.Desktop");
48              application = Desktop.getDesktop();
49              aboutHandlerClass = Class.forName("java.awt.desktop.AboutHandler");
50              preferencesHandlerClass = Class.forName("java.awt.desktop.PreferencesHandler");
51          } else {
52              applicationClass = Class.forName("com.apple.eawt.Application");
53              application = applicationClass.getMethod("getApplication").invoke(null);
54              aboutHandlerClass = Class.forName("com.apple.eawt.AboutHandler");
55              preferencesHandlerClass = Class.forName("com.apple.eawt.PreferencesHandler");
56          }
57  
58          Object proxy = Proxy.newProxyInstance(OSXHandler.class.getClassLoader(), new Class<?>[]{
59                  aboutHandlerClass, preferencesHandlerClass}, this);
60  
61          applicationClass.getDeclaredMethod("setAboutHandler", aboutHandlerClass).invoke(application, proxy);
62          applicationClass.getDeclaredMethod("setPreferencesHandler", preferencesHandlerClass).invoke(application, proxy);
63      }
64  
65      @Override
66      public Object invoke(Object proxy, Method method, Object[] args) {
67          if ("handleAbout".equals(method.getName())) {
68              davGatewayTray.about();
69          } else if ("handlePreferences".equals(method.getName())) {
70              davGatewayTray.preferences();
71          }
72          return null;
73      }
74  
75  }