1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.util;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.awt.Desktop;
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.URI;
31
32
33
34
35
36
37
38 public class DesktopUtil {
39
40
41 private static final Log log = LogFactory.getLog(DesktopUtil.class);
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static void browse(URI uri) {
56 boolean useXdg = true;
57 try {
58 if (Desktop.isDesktopSupported()) {
59 Desktop desktop = Desktop.getDesktop();
60 if (desktop.isSupported(Desktop.Action.BROWSE)) {
61 useXdg = false;
62 desktop.browse(uri);
63 }
64 }
65 } catch (IOException ioe) {
66
67 }
68
69 if (useXdg) {
70 if (log.isDebugEnabled()) {
71 log.debug("Desktop API not supported, launching xdg-open");
72 }
73 ProcessBuilder pb = new ProcessBuilder("xdg-open", uri.toString());
74 try {
75 pb.start();
76 } catch (IOException e) {
77 if (log.isDebugEnabled()) {
78 log.debug("Could not launch browser, there is maybe no " +
79 "default browser configured on the system", e);
80 }
81 }
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static void open(File file) {
98 boolean useXdg = true;
99 try {
100 if (Desktop.isDesktopSupported()) {
101 Desktop desktop = Desktop.getDesktop();
102 if (desktop.isSupported(Desktop.Action.OPEN)) {
103 useXdg = false;
104 desktop.open(file);
105 }
106 }
107 } catch (IOException ioe) {
108
109 }
110
111 if (useXdg) {
112 if (log.isDebugEnabled()) {
113 log.debug("Desktop API not supported, launching xdg-open");
114 }
115
116 ProcessBuilder pb = new ProcessBuilder("xdg-open", file.toURI().toString());
117 try {
118 pb.start();
119 } catch (IOException e) {
120 if (log.isDebugEnabled()) {
121 log.debug("Could not open file, there is maybe no " +
122 "default viewer configured on the system", e);
123 }
124 }
125
126 }
127
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public static void mail(URI uri) {
143 boolean useXdg = true;
144 try {
145 if (Desktop.isDesktopSupported()) {
146 Desktop desktop = Desktop.getDesktop();
147 if (desktop.isSupported(Desktop.Action.MAIL)) {
148 useXdg = false;
149 desktop.mail(uri);
150 }
151 }
152 } catch (IOException ioe) {
153
154 }
155
156 if (useXdg) {
157 if (log.isDebugEnabled()) {
158 log.debug("Desktop API not supported, launching xdg-open");
159 }
160
161 ProcessBuilder pb = new ProcessBuilder("xdg-email", uri.toString());
162 try {
163 pb.start();
164 } catch (IOException e) {
165 if (log.isDebugEnabled()) {
166 log.debug("Could not open file, there is maybe no " +
167 "default viewer configured on the system", e);
168 }
169 }
170
171 }
172
173 }
174 }