author | wenzelm |
Thu, 31 Jan 2013 22:21:05 +0100 | |
changeset 51071 | b7e7557e80b5 |
child 53487 | fc87164e3577 |
permissions | -rw-r--r-- |
51071
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Tools/jEdit/src/osx_adapter.scala |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
3 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
4 |
Some native OS X support. |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
6 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.jedit |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
8 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
9 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
10 |
import isabelle._ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
11 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
12 |
import java.lang.{Class, ClassNotFoundException} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
13 |
import java.lang.reflect.{InvocationHandler, Method, Proxy} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
14 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
15 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
16 |
object OSX_Adapter |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
17 |
{ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
18 |
def set_quit_handler(target: AnyRef, quit_handler: Method) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
19 |
{ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
20 |
set_handler(new OSX_Adapter("handle_quit", target, quit_handler)) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
21 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
22 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
23 |
var application: Any = null |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
24 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
25 |
def set_handler(adapter: OSX_Adapter) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
26 |
{ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
27 |
try { |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
28 |
val application_class: Class[_] = Class.forName("com.apple.eawt.Application") |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
29 |
if (application == null) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
30 |
application = application_class.getConstructor().newInstance() |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
31 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
32 |
val application_listener_class: Class[_] = |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
33 |
Class.forName("com.apple.eawt.ApplicationListener") |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
34 |
val add_listener_method = |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
35 |
application_class.getDeclaredMethod("addApplicationListener", application_listener_class) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
36 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
37 |
val osx_adapter_proxy = |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
38 |
Proxy.newProxyInstance(classOf[OSX_Adapter].getClassLoader, |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
39 |
Array(application_listener_class), adapter) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
40 |
add_listener_method.invoke(application, osx_adapter_proxy) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
41 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
42 |
catch { |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
43 |
case exn: ClassNotFoundException => |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
44 |
java.lang.System.err.println( |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
45 |
"com.apple.eawt.Application unavailable -- cannot install native OS X handler") |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
46 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
47 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
48 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
49 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
50 |
class OSX_Adapter(proxy_signature: String, target_object: AnyRef, target_method: Method) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
51 |
extends InvocationHandler |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
52 |
{ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
53 |
override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
54 |
{ |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
55 |
if (proxy_signature == method.getName && args.length == 1) { |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
56 |
val result = target_method.invoke(target_object) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
57 |
val handled = result == null || result.toString == "true" |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
58 |
|
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
59 |
val event = args(0) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
60 |
if (event != null) { |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
61 |
val set_handled_method = |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
62 |
event.getClass.getDeclaredMethod("setHandled", classOf[java.lang.Boolean]) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
63 |
set_handled_method.invoke(event, java.lang.Boolean.valueOf(handled)) |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
64 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
65 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
66 |
null |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
67 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
68 |
} |
b7e7557e80b5
some native Mac OS X support, notably quit_handler (important for dialog on unsaved changes);
wenzelm
parents:
diff
changeset
|
69 |