-
-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathMacHandler.java
More file actions
149 lines (135 loc) · 4.33 KB
/
MacHandler.java
File metadata and controls
149 lines (135 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.app;
import java.awt.Desktop;
import java.awt.desktop.AboutEvent;
import java.awt.desktop.AboutHandler;
import java.awt.desktop.OpenFilesEvent;
import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.OpenURIEvent;
import java.awt.desktop.OpenURIHandler;
import java.awt.desktop.PreferencesEvent;
import java.awt.desktop.PreferencesHandler;
import java.awt.desktop.QuitEvent;
import java.awt.desktop.QuitHandler;
import java.awt.desktop.QuitResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MacHandler {
Desktop application;
FileOutputStream output;
Object app;
String openMeLater;
MacHandler() {
log("starting mac application");
this.application = Desktop.getDesktop();
application.setOpenFileHandler(new MyOpenFilesHandler());
application.setOpenURIHandler(new MyOpenURIHandler());
application.setPreferencesHandler(new MyPreferencesHandler());
application.setAboutHandler(new MyAboutHandler());
application.setQuitHandler(new MyQuitHandler());
log("in directory:");
log(System.getProperty("user.dir"));
log(new File(".").getAbsolutePath());
}
void log(String s) {
try {
if (output == null) {
this.output = new FileOutputStream("/tmp/nlogoapp.log");
}
output.write((s + "\n").getBytes());
output.flush();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
class MyOpenURIHandler implements OpenURIHandler {
@Override
public void openURI(OpenURIEvent event) {
log("received OpenURI event");
log(event.getURI().toString());
}
}
class MyOpenFilesHandler implements OpenFilesHandler {
@Override
public void openFiles(OpenFilesEvent event) {
log("received OpenFiles event");
log(event.getFiles().get(0).getAbsolutePath());
doOpen(event.getFiles().get(0).getAbsolutePath());
}
}
class MyPreferencesHandler implements PreferencesHandler {
@Override
public void handlePreferences(PreferencesEvent event) {
try {
Class<?> appClass = app.getClass();
appClass.getDeclaredMethod("handleShowPreferences").invoke(app);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
log("failed to invoke handleShowPreferences");
}
}
}
class MyAboutHandler implements AboutHandler {
@Override
public void handleAbout(AboutEvent event) {
try {
Class<?> appClass = app.getClass();
appClass.getDeclaredMethod("handleShowAbout").invoke(app);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
log("failed to invoke handleShowAbout");
}
}
}
class MyQuitHandler implements QuitHandler {
@Override
public void handleQuitRequestWith(QuitEvent event, QuitResponse response) {
try {
Class<?> appClass = app.getClass();
Method handleQuit = appClass.getDeclaredMethod("handleQuit");
handleQuit.invoke(app);
} catch (NoSuchMethodException e) {
response.performQuit();
} catch (InvocationTargetException e) {
if (e.getCause().getClass().getName().contains("UserCancelException")) {
log("cancelled quit");
response.cancelQuit();
} else {
log("invocation of handleQuit failed");
e.printStackTrace();
log(e.getMessage());
response.performQuit();
}
} catch (Exception e) {
response.performQuit();
}
}
}
public void init() { }
public void ready(Object app) {
this.app = app;
if (openMeLater != null) {
doOpen(openMeLater);
}
}
void doOpen(String path) {
if (app == null) {
openMeLater = path;
} else {
try {
Class<?> appClass = app.getClass();
appClass.getDeclaredMethod("handleOpenPath", String.class).invoke(app, path);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
log("failed to invoke handleOpenPath");
}
}
}
}