forked from facchinm/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThinkDifferent.java
More file actions
125 lines (110 loc) · 3.67 KB
/
Copy pathThinkDifferent.java
File metadata and controls
125 lines (110 loc) · 3.67 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2007 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.macosx;
import com.apple.eawt.*;
import processing.app.Base;
import processing.app.Editor;
import java.io.File;
import java.util.List;
/**
* Deal with issues related to thinking different. This handles the basic
* Mac OS X menu commands (and apple events) for open, about, prefs, etc.
* <p>
* Based on OSXAdapter.java from Apple DTS.
* </p>
* As of 0140, this code need not be built on platforms other than OS X,
* because of the new platform structure which isolates through reflection.
*/
public class ThinkDifferent {
private static final int MAX_WAIT_FOR_BASE = 30000;
static public void init() {
Application application = Application.getApplication();
application.setAboutHandler(new AboutHandler() {
@Override
public void handleAbout(AppEvent.AboutEvent aboutEvent) {
new Thread(() -> {
if (waitForBase()) {
Base.INSTANCE.handleAbout();
}
}).start();
}
});
application.setPreferencesHandler(new PreferencesHandler() {
@Override
public void handlePreferences(AppEvent.PreferencesEvent preferencesEvent) {
new Thread(() -> {
if (waitForBase()) {
Base.INSTANCE.handlePrefs();
}
}).start();
}
});
application.setOpenFileHandler(new OpenFilesHandler() {
@Override
public void openFiles(final AppEvent.OpenFilesEvent openFilesEvent) {
new Thread(() -> {
if (waitForBase()) {
for (File file : openFilesEvent.getFiles()) {
System.out.println(file);
try {
Base.INSTANCE.handleOpen(file);
List<Editor> editors = Base.INSTANCE.getEditors();
if (editors.size() == 2 && editors.get(0).getSketch().isUntitled()) {
Base.INSTANCE.handleClose(editors.get(0));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
});
application.setQuitHandler(new QuitHandler() {
@Override
public void handleQuitRequestWith(AppEvent.QuitEvent quitEvent, QuitResponse quitResponse) {
new Thread(() -> {
if (waitForBase()) {
if (Base.INSTANCE.handleQuit()) {
quitResponse.performQuit();
} else {
quitResponse.cancelQuit();
}
}
}).start();
}
});
}
private static boolean waitForBase() {
int slept = 0;
while (Base.INSTANCE == null) {
if (slept >= MAX_WAIT_FOR_BASE) {
return false;
}
sleep(100);
slept += 100;
}
return true;
}
private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
//ignore
}
}
}