1 /* Title: Tools/jEdit/src/osx_adapter.scala |
|
2 Author: Makarius |
|
3 |
|
4 Some native OS X support. |
|
5 */ |
|
6 |
|
7 package isabelle.jedit |
|
8 |
|
9 |
|
10 import isabelle._ |
|
11 |
|
12 import java.lang.{Class, ClassNotFoundException, NoSuchMethodException} |
|
13 import java.lang.reflect.{InvocationHandler, Method, Proxy} |
|
14 |
|
15 |
|
16 object OSX_Adapter |
|
17 { |
|
18 private lazy val application_class: Class[_] = Class.forName("com.apple.eawt.Application") |
|
19 private lazy val application = application_class.getConstructor().newInstance() |
|
20 |
|
21 def init |
|
22 { |
|
23 if (PIDE.options.bool("jedit_macos_application")) { |
|
24 try { |
|
25 set_handler("handleQuit") |
|
26 set_handler("handleAbout") |
|
27 |
|
28 if (PIDE.options.bool("jedit_macos_preferences")) { |
|
29 application_class.getDeclaredMethod("setEnabledPreferencesMenu", classOf[Boolean]). |
|
30 invoke(application, java.lang.Boolean.valueOf(true)) |
|
31 set_handler("handlePreferences") |
|
32 } |
|
33 } |
|
34 catch { |
|
35 case exn: ClassNotFoundException => |
|
36 java.lang.System.err.println( |
|
37 "com.apple.eawt.Application unavailable -- cannot install native OS X handler") |
|
38 } |
|
39 } |
|
40 } |
|
41 |
|
42 private def set_handler(name: String) |
|
43 { |
|
44 val handler = PIDE.plugin.getClass.getDeclaredMethod(name) |
|
45 val adapter = new OSX_Adapter(name, PIDE.plugin, handler) |
|
46 try { |
|
47 val application_listener_class: Class[_] = |
|
48 Class.forName("com.apple.eawt.ApplicationListener") |
|
49 val add_listener_method = |
|
50 application_class.getDeclaredMethod("addApplicationListener", application_listener_class) |
|
51 |
|
52 val osx_adapter_proxy = |
|
53 Proxy.newProxyInstance(classOf[OSX_Adapter].getClassLoader, |
|
54 Array(application_listener_class), adapter) |
|
55 add_listener_method.invoke(application, osx_adapter_proxy) |
|
56 } |
|
57 catch { |
|
58 case exn: ClassNotFoundException => |
|
59 java.lang.System.err.println( |
|
60 "com.apple.eawt.Application unavailable -- cannot install native OS X handler") |
|
61 } |
|
62 } |
|
63 } |
|
64 |
|
65 class OSX_Adapter(proxy_signature: String, target_object: AnyRef, target_method: Method) |
|
66 extends InvocationHandler |
|
67 { |
|
68 override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = |
|
69 { |
|
70 if (proxy_signature == method.getName && args.length == 1) { |
|
71 val result = target_method.invoke(target_object) |
|
72 val handled = result == null || result.toString == "true" |
|
73 |
|
74 val event = args(0) |
|
75 if (event != null) { |
|
76 try { |
|
77 val set_handled_method = |
|
78 event.getClass.getDeclaredMethod("setHandled", classOf[java.lang.Boolean]) |
|
79 set_handled_method.invoke(event, java.lang.Boolean.valueOf(handled)) |
|
80 } |
|
81 catch { case _: NoSuchMethodException => } |
|
82 } |
|
83 } |
|
84 null |
|
85 } |
|
86 } |
|
87 |
|