1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44: import ;
45: import ;
46:
47: import ;
48:
49: import ;
50:
51:
57: public class ClasspathDesktopPeer
58: implements DesktopPeer
59: {
60:
61: protected static final String _DEFAULT_BROWSER = "firefox";
62:
63:
64: protected static final String _BROWSE = "html";
65:
66:
67: protected static final String _MAIL = "mail";
68:
69:
70: protected static final String _EDIT = "edit";
71:
72:
73: protected static final String _PRINT = "print";
74:
75:
76: protected static final String _OPEN = "open";
77:
78:
79: protected static final KDEDesktopPeer kde = new KDEDesktopPeer();
80:
81:
82: protected static final GnomeDesktopPeer gnome = new GnomeDesktopPeer();
83:
84:
85: protected static final ClasspathDesktopPeer classpath =
86: new ClasspathDesktopPeer();
87:
88:
92: protected Preferences prefs =
93: Preferences.userNodeForPackage(ClasspathDesktopPeer.class).node("Desktop");
94:
95:
98: protected ClasspathDesktopPeer()
99: {
100:
101: }
102:
103: public boolean isSupported(Action action)
104: {
105: String check = null;
106:
107: switch(action)
108: {
109: case BROWSE:
110: check = _BROWSE;
111: break;
112:
113: case MAIL:
114: check = _MAIL;
115: break;
116:
117: case EDIT:
118: check = _EDIT;
119: break;
120:
121: case PRINT:
122: check = _PRINT;
123: break;
124:
125: case OPEN: default:
126: check = _OPEN;
127: break;
128: }
129:
130: return this.supportCommand(check);
131: }
132:
133: public void browse(URI url) throws IOException
134: {
135: checkPermissions();
136:
137: String browser = getCommand(_BROWSE);
138:
139: if (browser == null)
140: throw new UnsupportedOperationException();
141:
142: browser = browser + " " + url.toString();
143:
144: Runtime.getRuntime().exec(browser);
145: }
146:
147: public void edit(File file) throws IOException
148: {
149: checkPermissions(file, false);
150:
151: String edit = getCommand(_EDIT);
152:
153: if (edit == null)
154: throw new UnsupportedOperationException();
155:
156: edit = edit + " " + file.getAbsolutePath();
157: Runtime.getRuntime().exec(edit);
158: }
159:
160: public void mail(URI mailtoURL) throws IOException
161: {
162: checkPermissions();
163:
164: String scheme = mailtoURL.getScheme();
165: if (scheme == null || !scheme.equalsIgnoreCase("mailto"))
166: throw new IllegalArgumentException("URI Scheme not of type mailto");
167:
168: String mail = getCommand(_MAIL);
169:
170: if (mail == null)
171: throw new UnsupportedOperationException();
172:
173: mail = mail + " " + mailtoURL.toString();
174:
175: Runtime.getRuntime().exec(mail);
176: }
177:
178: public void mail() throws IOException
179: {
180: checkPermissions();
181:
182: String mail = getCommand(_MAIL);
183:
184: if (mail == null)
185: throw new UnsupportedOperationException();
186:
187: Runtime.getRuntime().exec(mail);
188: }
189:
190: public void open(File file) throws IOException
191: {
192: checkPermissions(file, true);
193:
194: String open = getCommand(_OPEN);
195:
196: if (open == null)
197: throw new UnsupportedOperationException();
198:
199: open = open + " " + file.getAbsolutePath();
200: Runtime.getRuntime().exec(open);
201: }
202:
203: public void print(File file) throws IOException
204: {
205: checkPrintPermissions(file);
206:
207: String print = getCommand(_PRINT);
208:
209: if (print == null)
210: throw new UnsupportedOperationException();
211:
212: print = print + " " + file.getAbsolutePath();
213: Runtime.getRuntime().exec(print);
214: }
215:
216: protected String getCommand(String action)
217: {
218:
219: String command =
220: System.getProperty("gnu.java.awt.peer.Desktop." + action + ".command");
221:
222:
223: if (command == null)
224: {
225: command = prefs.node(action).get("command", null);
226: }
227:
228: return command;
229: }
230:
231:
234: protected void checkPermissions()
235: {
236: SecurityManager sm = System.getSecurityManager();
237: if (sm != null) {
238: sm.checkPermission(new AWTPermission("showWindowWithoutWarningBanner"));
239: }
240: }
241:
242:
246: protected void checkPermissions(File file, boolean readOnly)
247: {
248: checkPermissions();
249:
250: SecurityManager sm = System.getSecurityManager();
251: if (sm != null) {
252: sm.checkRead(file.toString());
253: if (!readOnly) sm.checkWrite(file.toString());
254: }
255: }
256:
257:
261: protected void checkPrintPermissions(File file)
262: {
263: checkPermissions(file, true);
264:
265: SecurityManager sm = System.getSecurityManager();
266: if (sm != null) {
267: sm.checkPrintJobAccess();
268: }
269: }
270:
271:
275: protected boolean supportCommand(String check)
276: {
277: return ((this.getCommand(check) != null) ? true : false);
278: }
279:
280:
283: public static DesktopPeer getDesktop()
284: {
285:
286: String desktopSession = System.getenv("GNOME_DESKTOP_SESSION_ID");
287: if (desktopSession == null)
288: {
289: desktopSession = System.getenv("KDE_FULL_SESSION");
290: if (desktopSession != null)
291: return kde;
292: }
293: else
294: {
295: return gnome;
296: }
297:
298:
299: return classpath;
300: }
301: }