-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathPlatformUtils.java
More file actions
219 lines (191 loc) · 7.91 KB
/
PlatformUtils.java
File metadata and controls
219 lines (191 loc) · 7.91 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package nodebox.client;
import com.sun.jna.*;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class PlatformUtils {
public static int WIN = 1;
public static int MAC = 2;
public static int OTHER = 3;
public static int current_platform = -1;
public static int platformSpecificModifier;
public static final String SEP = System.getProperty("file.separator");
private static File userDataDirectory = null;
private static Map<String, Object> JNA_OPTIONS = new HashMap<String, Object>();
static {
try {
platformSpecificModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
} catch (HeadlessException e) {
platformSpecificModifier = Event.CTRL_MASK;
}
if (Platform.isWindows()) {
current_platform = WIN;
JNA_OPTIONS.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
JNA_OPTIONS.put(Library.OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
} else if (Platform.isMac()) {
current_platform = MAC;
} else {
current_platform = OTHER;
}
}
public static boolean onMac() {
return current_platform == MAC;
}
public static boolean onWindows() {
return current_platform == WIN;
}
public static boolean onOther() {
return current_platform == OTHER;
}
//// Application directories ////
public static File getHomeDirectory() {
return new File(System.getProperty("user.home"));
}
/**
* Get the directory that contains the user's NodeBox library directory.
* <p/>
* <p/>
* <ul>
* <li>Mac: <code>/Users/username/Library/NodeBox</code></li>
* <li>Windows: <code>/Documents And Settings/username/Local Settings/Application Data/NodeBox</code></li>
* <li>Other: <code>~/nodebox</code></li>
* </ul>
*
* @return the user's library directory.
*/
public static File getUserDataDirectory() throws RuntimeException {
if (userDataDirectory != null)
return userDataDirectory;
if (onMac()) {
userDataDirectory = new File(getHomeDirectory(), "Library/" + Application.NAME);
} else if (onWindows()) {
String localAppData;
HWND hwndOwner = null;
int nFolder = Shell32.CSIDL_LOCAL_APPDATA;
HANDLE hToken = null;
int dwFlags = Shell32.SHGFP_TYPE_CURRENT;
char[] pszPath = new char[Shell32.MAX_PATH];
int hResult = Shell32.INSTANCE.SHGetFolderPath(hwndOwner, nFolder, hToken, dwFlags, pszPath);
if (Shell32.S_OK == hResult) {
String path = new String(pszPath);
int len = path.indexOf('\0');
localAppData = path.substring(0, len);
} else {
// If the native call fails, use the home directory.
localAppData = getHomeDirectory().getPath();
}
userDataDirectory = new File(localAppData, Application.NAME);
} else {
userDataDirectory = new File(getHomeDirectory(), Application.NAME.toLowerCase(Locale.US));
}
return userDataDirectory;
}
/**
* Get the directory that contains NodeBox scripts the user has installed.
* <p/>
* <ul>
* <li>Mac: <code>/Users/username/Library/NodeBox/Scripts</code></li>
* <li>Windows: <code>/Users/username/Application Data/NodeBox/Scripts</code></li>
* <li>Other: <code>~/nodebox/scripts</code></li>
* </ul>
*
* @return the user's NodeBox scripts directory.
*/
public static File getUserScriptsDirectory() {
if (onMac() || onWindows())
return new File(getUserDataDirectory(), "Scripts");
else
return new File(getUserDataDirectory(), "scripts");
}
/**
* Get the directory that contains Python libraries the user has installed.
* <p/>
* This directory is added to the PYTHONPATH; anything below it can be used in scripts.
* <p/>
* <ul>
* <li>Mac: <code>/Users/username/Library/NodeBox/Python</code></li>
* <li>Windows: <code>/Users/username/Application Data/NodeBox/Python</code></li>
* <li>Other: <code>~/nodebox/python</code></li>
* </ul>
*
* @return the user's Python directory.
*/
public static File getUserPythonDirectory() {
if (onMac() || onWindows())
return new File(getUserDataDirectory(), "Python");
else
return new File(getUserDataDirectory(), "python");
}
/**
* Get the directory that contains the application's builtin NodeBox scripts.
*
* @return the application's NodeBox scripts directory.
*/
public static File getApplicationScriptsDirectory() {
return new File("libraries");
}
//// Keystrokes ////
public static KeyStroke getKeyStroke(int key) {
return KeyStroke.getKeyStroke(key, platformSpecificModifier);
}
public static KeyStroke getKeyStroke(int key, int modifier) {
return KeyStroke.getKeyStroke(key, platformSpecificModifier | modifier);
}
public static void openURL(String url) {
try {
if (onMac()) {
Class fileManagerClass = Class.forName("com.apple.eio.FileManager");
Method openURLMethod = fileManagerClass.getDeclaredMethod("openURL", String.class);
openURLMethod.invoke(null, url);
} else if (onWindows()) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
} else {
String[] browsers = {"firefox", "konqueror", "mozilla", "opera"};
String availableBrowser = null;
for (String browser : browsers) {
if (Runtime.getRuntime().exec(new String[]{"which", browser}).waitFor() == 0) {
availableBrowser = browser;
break;
}
}
if (availableBrowser != null) {
Runtime.getRuntime().exec(new String[]{availableBrowser, url});
} else {
JOptionPane.showMessageDialog(null, "Could not find a web browser. Go to " + url + " directly.");
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not open browser window. Go to " + url + " directly. \n" + ex.getLocalizedMessage());
}
}
private static class HANDLE extends PointerType implements NativeMapped {
}
private static class HWND extends HANDLE {
}
private static interface Shell32 extends Library {
public static final int MAX_PATH = 260;
public static final int CSIDL_LOCAL_APPDATA = 0x001c;
public static final int SHGFP_TYPE_CURRENT = 0;
public static final int S_OK = 0;
static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32", Shell32.class, JNA_OPTIONS);
/**
* See http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
* <p/>
*
* @param hwndOwner [in] Reserved.
* @param nFolder [in] A CSIDL value that identifies the folder whose path is to be retrieved.
* @param hToken [in] An access token that can be used to represent a particular user. Always set this to null.
* @param dwFlags [in] Flags that specify the path to be returned.
* @param pszPath [out] A pointer to a null-terminated string of length MAX_PATH which will receive the path.
* @return S_OK if successful, or an error value otherwise.
*/
public int SHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken,
int dwFlags, char[] pszPath);
}
}