|
1 |
package org.gvsig.desktopopen;
|
|
2 |
|
|
3 |
import java.awt.Desktop;
|
|
4 |
import java.io.File;
|
|
5 |
import java.io.IOException;
|
|
6 |
import java.net.URI;
|
|
7 |
import java.util.ArrayList;
|
|
8 |
import java.util.List;
|
|
9 |
import org.slf4j.Logger;
|
|
10 |
import org.slf4j.LoggerFactory;
|
|
11 |
|
|
12 |
/*
|
|
13 |
* Based on portions of code of MightyPork, http://www.ondrovo.com/
|
|
14 |
* extracted from :
|
|
15 |
* http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
|
|
16 |
*/
|
|
17 |
public class DefaultDesktopOpen implements DesktopOpen {
|
|
18 |
|
|
19 |
private static Logger logger = LoggerFactory.getLogger(DefaultDesktopOpen.class);
|
|
20 |
|
|
21 |
public enum EnumOS {
|
|
22 |
|
|
23 |
linux, macos, solaris, unknown, windows;
|
|
24 |
|
|
25 |
public boolean isLinux() {
|
|
26 |
|
|
27 |
return this == linux || this == solaris;
|
|
28 |
}
|
|
29 |
|
|
30 |
public boolean isMac() {
|
|
31 |
|
|
32 |
return this == macos;
|
|
33 |
}
|
|
34 |
|
|
35 |
public boolean isWindows() {
|
|
36 |
|
|
37 |
return this == windows;
|
|
38 |
}
|
|
39 |
}
|
|
40 |
|
|
41 |
@Override
|
|
42 |
public boolean browse(URI uri) {
|
|
43 |
|
|
44 |
if ( browseDESKTOP(uri) ) {
|
|
45 |
return true;
|
|
46 |
}
|
|
47 |
|
|
48 |
return openSystemSpecific(uri.toString());
|
|
49 |
}
|
|
50 |
|
|
51 |
@Override
|
|
52 |
public boolean open(File file) {
|
|
53 |
|
|
54 |
if ( openDESKTOP(file) ) {
|
|
55 |
return true;
|
|
56 |
}
|
|
57 |
|
|
58 |
return openSystemSpecific(file.getPath());
|
|
59 |
}
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public boolean edit(File file) {
|
|
63 |
|
|
64 |
if ( editDESKTOP(file) ) {
|
|
65 |
return true;
|
|
66 |
}
|
|
67 |
|
|
68 |
return openSystemSpecific(file.getPath());
|
|
69 |
}
|
|
70 |
|
|
71 |
private boolean openSystemSpecific(String what) {
|
|
72 |
|
|
73 |
EnumOS os = getOs();
|
|
74 |
|
|
75 |
if (os.isLinux()) {
|
|
76 |
// if (runCommand("gnome-open", "%s", what)) {
|
|
77 |
// return true;
|
|
78 |
// }
|
|
79 |
// if (runCommand("kde-open", "%s", what)) {
|
|
80 |
// return true;
|
|
81 |
// }
|
|
82 |
if (runCommand("xdg-open", "%s", what)) {
|
|
83 |
return true;
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
if ( os.isMac() ) {
|
|
88 |
if ( runCommand("open", "%s", what) ) {
|
|
89 |
return true;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
if ( os.isWindows() ) {
|
|
94 |
if ( runCommand("explorer", "%s", what) ) {
|
|
95 |
return true;
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
return false;
|
|
100 |
}
|
|
101 |
|
|
102 |
private boolean browseDESKTOP(URI uri) {
|
|
103 |
|
|
104 |
logger.info("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
|
|
105 |
try {
|
|
106 |
if ( !Desktop.isDesktopSupported() ) {
|
|
107 |
logger.warn("Platform is not supported.");
|
|
108 |
return false;
|
|
109 |
}
|
|
110 |
|
|
111 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.BROWSE) ) {
|
|
112 |
logger.warn("BROWSE is not supported.");
|
|
113 |
return false;
|
|
114 |
}
|
|
115 |
|
|
116 |
Desktop.getDesktop().browse(uri);
|
|
117 |
|
|
118 |
return true;
|
|
119 |
} catch (Throwable t) {
|
|
120 |
logger.warn("Error using desktop browse.", t);
|
|
121 |
return false;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
private boolean openDESKTOP(File file) {
|
|
126 |
|
|
127 |
logger.info("Trying to use Desktop.getDesktop().open() with " + file.toString());
|
|
128 |
try {
|
|
129 |
if ( !Desktop.isDesktopSupported() ) {
|
|
130 |
logger.warn("Platform is not supported.");
|
|
131 |
return false;
|
|
132 |
}
|
|
133 |
|
|
134 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.OPEN) ) {
|
|
135 |
logger.warn("OPEN is not supported.");
|
|
136 |
return false;
|
|
137 |
}
|
|
138 |
|
|
139 |
Desktop.getDesktop().open(file);
|
|
140 |
|
|
141 |
return true;
|
|
142 |
} catch (Throwable t) {
|
|
143 |
logger.warn("Error using desktop open.", t);
|
|
144 |
return false;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
private boolean editDESKTOP(File file) {
|
|
149 |
|
|
150 |
logger.info("Trying to use Desktop.getDesktop().edit() with " + file);
|
|
151 |
try {
|
|
152 |
if ( !Desktop.isDesktopSupported() ) {
|
|
153 |
logger.warn("Platform is not supported.");
|
|
154 |
return false;
|
|
155 |
}
|
|
156 |
|
|
157 |
if ( !Desktop.getDesktop().isSupported(Desktop.Action.EDIT) ) {
|
|
158 |
logger.warn("EDIT is not supported.");
|
|
159 |
return false;
|
|
160 |
}
|
|
161 |
|
|
162 |
Desktop.getDesktop().edit(file);
|
|
163 |
|
|
164 |
return true;
|
|
165 |
} catch (Throwable t) {
|
|
166 |
logger.warn("Error using desktop edit.", t);
|
|
167 |
return false;
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
private boolean runCommand(String command, String args, String file) {
|
|
172 |
|
|
173 |
logger.info("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file);
|
|
174 |
|
|
175 |
String[] parts = prepareCommand(command, args, file);
|
|
176 |
|
|
177 |
try {
|
|
178 |
Process p = Runtime.getRuntime().exec(parts);
|
|
179 |
if ( p == null ) {
|
|
180 |
return false;
|
|
181 |
}
|
|
182 |
|
|
183 |
try {
|
|
184 |
int retval = p.exitValue();
|
|
185 |
if ( retval == 0 ) {
|
|
186 |
logger.warn("Process ended immediately.");
|
|
187 |
return false;
|
|
188 |
} else {
|
|
189 |
logger.warn("Process crashed.");
|
|
190 |
return false;
|
|
191 |
}
|
|
192 |
} catch (IllegalThreadStateException itse) {
|
|
193 |
logger.warn("Process is running.");
|
|
194 |
return true;
|
|
195 |
}
|
|
196 |
} catch (IOException e) {
|
|
197 |
logger.warn("Error running command.", e);
|
|
198 |
return false;
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
private String[] prepareCommand(String command, String args, String file) {
|
|
203 |
|
|
204 |
List<String> parts = new ArrayList<>();
|
|
205 |
parts.add(command);
|
|
206 |
|
|
207 |
if ( args != null ) {
|
|
208 |
for ( String s : args.split(" ") ) {
|
|
209 |
s = String.format(s, file); // put in the filename thing
|
|
210 |
|
|
211 |
parts.add(s.trim());
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
return parts.toArray(new String[parts.size()]);
|
|
216 |
}
|
|
217 |
|
|
218 |
public EnumOS getOs() {
|
|
219 |
|
|
220 |
String s = System.getProperty("os.name").toLowerCase();
|
|
221 |
|
|
222 |
if ( s.contains("win") ) {
|
|
223 |
return EnumOS.windows;
|
|
224 |
}
|
|
225 |
|
|
226 |
if ( s.contains("mac") ) {
|
|
227 |
return EnumOS.macos;
|
|
228 |
}
|
|
229 |
|
|
230 |
if ( s.contains("solaris") ) {
|
|
231 |
return EnumOS.solaris;
|
|
232 |
}
|
|
233 |
|
|
234 |
if ( s.contains("sunos") ) {
|
|
235 |
return EnumOS.solaris;
|
|
236 |
}
|
|
237 |
|
|
238 |
if ( s.contains("linux") ) {
|
|
239 |
return EnumOS.linux;
|
|
240 |
}
|
|
241 |
|
|
242 |
if ( s.contains("unix") ) {
|
|
243 |
return EnumOS.linux;
|
|
244 |
} else {
|
|
245 |
return EnumOS.unknown;
|
|
246 |
}
|
|
247 |
}
|
|
248 |
}
|