|
| 1 | +package edu.stanford.nlp.trees.tregex.gui; |
| 2 | + |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.event.WindowAdapter; |
| 5 | +import java.awt.event.WindowListener; |
| 6 | +import java.lang.reflect.InvocationHandler; |
| 7 | +import java.lang.reflect.InvocationTargetException; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.lang.reflect.Proxy; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * Class to do macOS-specific things for TregexGUI. This was initially written to use the (pre-2014, OS X) |
| 14 | + * {@code com.apple.eawt.*} library (AppleJavaExtensions.jar), but now uses the Java 9+ methods in |
| 15 | + * the {@code java.awt.Desktop} class. However, rather than calling them directly, we do things via |
| 16 | + * reflection, so that the CoreNLP code still compiles and runs under Java 8 (except for this macOS-specific |
| 17 | + * code for {@code TregexGUI}. |
| 18 | + * |
| 19 | + * @author rafferty |
| 20 | + * @author Christopher Manning |
| 21 | + */ |
| 22 | +public class OSXAdapter { |
| 23 | + |
| 24 | + private static OSXAdapter adapter; |
| 25 | + private static Desktop desktop; |
| 26 | + private static TregexGUI mainApp; |
| 27 | + |
| 28 | + private OSXAdapter (TregexGUI inApp) { |
| 29 | + mainApp = inApp; |
| 30 | + } |
| 31 | + |
| 32 | + // implemented handler methods. These are basically hooks into existing |
| 33 | + // functionality from the main app, as if it came over from another platform. |
| 34 | + public static void handleAbout() { |
| 35 | + if (mainApp != null) { |
| 36 | + mainApp.about(); |
| 37 | + } else { |
| 38 | + throw new IllegalStateException("handleAbout: TregexGUI instance detached from listener"); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static void handlePreferences() { |
| 43 | + if (mainApp != null) { |
| 44 | + mainApp.doPreferences(); |
| 45 | + } else { |
| 46 | + throw new IllegalStateException("handlePreferences: TregexGUI instance detached from listener"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void handleQuit() { |
| 51 | + if (mainApp != null) { |
| 52 | + /* |
| 53 | + / You MUST setHandled(false) if you want to delay or cancel the quit. |
| 54 | + / This is important for cross-platform development -- have a universal quit |
| 55 | + / routine that chooses whether or not to quit, so the functionality is identical |
| 56 | + / on all platforms. This example simply cancels the AppleEvent-based quit and |
| 57 | + / defers to that universal method. |
| 58 | + */ |
| 59 | + TregexGUI.doQuit(); |
| 60 | + } else { |
| 61 | + throw new IllegalStateException("handleQuit: TregexGUI instance detached from listener"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** The main entry-point for this class. This is the only method |
| 67 | + * that needs to be called at runtime, and it can easily be done using reflection. |
| 68 | + */ |
| 69 | + public static void registerMacOSXApplication(TregexGUI inApp) { |
| 70 | + System.err.println("The registerMacOSXApplication method was called!!!"); |
| 71 | + try { |
| 72 | + desktop = Desktop.getDesktop(); |
| 73 | + |
| 74 | + Class<?> aboutEventInterface = Class.forName("java.awt.desktop.AboutEvent"); |
| 75 | + Object aboutEvent = aboutEventInterface.getDeclaredConstructor().newInstance(); |
| 76 | + Class<?> aboutHandler = Class.forName("java.awt.desktop.AboutHandler"); |
| 77 | + Object aboutHandlerInstance = Proxy.newProxyInstance(aboutHandler.getClassLoader(), new Class<?>[]{aboutHandler}, new InvocationHandler() { |
| 78 | + @Override |
| 79 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 80 | + |
| 81 | + //Handle the invocations |
| 82 | + if (method.getName().equals("handleAbout")) { |
| 83 | + OSXAdapter.handleAbout(); |
| 84 | + return null; |
| 85 | + } |
| 86 | + else return null; |
| 87 | + } |
| 88 | + }); |
| 89 | + Method setAboutMethod = Desktop.class.getMethod("setAboutHandler", aboutHandler); |
| 90 | + setAboutMethod.invoke(aboutHandlerInstance, aboutHandlerInstance); |
| 91 | + } catch (ClassNotFoundException cnfe) { |
| 92 | + // ignore |
| 93 | + } catch (InvocationTargetException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } catch (InstantiationException e) { |
| 96 | + e.printStackTrace(); |
| 97 | + } catch (IllegalAccessException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } catch (NoSuchMethodException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + /* |
| 103 | + WindowListener windowListener = new WindowAdapter() { }; |
| 104 | + Object oo = new Object(); |
| 105 | + windowListener.windowActivated(oo); |
| 106 | + |
| 107 | + Object x = new Object(); |
| 108 | + desktop.setPreferencesHandler(x); |
| 109 | + |
| 110 | + // desktop.setAboutHandler(e -> handleAbout()); |
| 111 | + Class<?> aboutEvent = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(), |
| 112 | + new Class[] { Foo.class }, |
| 113 | + handler); |
| 114 | + |
| 115 | + Class<?>[] defArgs = { Class.forName("java.awt.desktop.AboutHandler")}; |
| 116 | + Method registerMethod = osxAdapter.getDeclaredMethod("registerMacOSXApplication", defArgs); |
| 117 | + if (registerMethod != null) { |
| 118 | + Object[] args = { this }; |
| 119 | + registerMethod.invoke(osxAdapter, args); |
| 120 | + } |
| 121 | + |
| 122 | + desktop.setPreferencesHandler(e -> handlePreferences()); |
| 123 | + desktop.setQuitHandler((e,r) -> handleQuit()); |
| 124 | + */ |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments