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