|
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} |
|
13 import java.lang.reflect.{InvocationHandler, Method, Proxy} |
|
14 |
|
15 |
|
16 object OSX_Adapter |
|
17 { |
|
18 def set_quit_handler(target: AnyRef, quit_handler: Method) |
|
19 { |
|
20 set_handler(new OSX_Adapter("handle_quit", target, quit_handler)) |
|
21 } |
|
22 |
|
23 var application: Any = null |
|
24 |
|
25 def set_handler(adapter: OSX_Adapter) |
|
26 { |
|
27 try { |
|
28 val application_class: Class[_] = Class.forName("com.apple.eawt.Application") |
|
29 if (application == null) |
|
30 application = application_class.getConstructor().newInstance() |
|
31 |
|
32 val application_listener_class: Class[_] = |
|
33 Class.forName("com.apple.eawt.ApplicationListener") |
|
34 val add_listener_method = |
|
35 application_class.getDeclaredMethod("addApplicationListener", application_listener_class) |
|
36 |
|
37 val osx_adapter_proxy = |
|
38 Proxy.newProxyInstance(classOf[OSX_Adapter].getClassLoader, |
|
39 Array(application_listener_class), adapter) |
|
40 add_listener_method.invoke(application, osx_adapter_proxy) |
|
41 } |
|
42 catch { |
|
43 case exn: ClassNotFoundException => |
|
44 java.lang.System.err.println( |
|
45 "com.apple.eawt.Application unavailable -- cannot install native OS X handler") |
|
46 } |
|
47 } |
|
48 } |
|
49 |
|
50 class OSX_Adapter(proxy_signature: String, target_object: AnyRef, target_method: Method) |
|
51 extends InvocationHandler |
|
52 { |
|
53 override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = |
|
54 { |
|
55 if (proxy_signature == method.getName && args.length == 1) { |
|
56 val result = target_method.invoke(target_object) |
|
57 val handled = result == null || result.toString == "true" |
|
58 |
|
59 val event = args(0) |
|
60 if (event != null) { |
|
61 val set_handled_method = |
|
62 event.getClass.getDeclaredMethod("setHandled", classOf[java.lang.Boolean]) |
|
63 set_handled_method.invoke(event, java.lang.Boolean.valueOf(handled)) |
|
64 } |
|
65 } |
|
66 null |
|
67 } |
|
68 } |
|
69 |