KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > Desktop


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.desktop;
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.jdesktop.jdic.desktop.internal.BrowserService;
27 import org.jdesktop.jdic.desktop.internal.LaunchFailedException;
28 import org.jdesktop.jdic.desktop.internal.LaunchService;
29 import org.jdesktop.jdic.desktop.internal.MailerService;
30 import org.jdesktop.jdic.desktop.internal.ServiceManager;
31
32
33 /**
34  * The <code>Desktop</code> class provides several methods to interact with
35  * the system applications and facilities. It cannot be instantiated.
36  * <p>
37  * The methods include invoking system programs associated with a particular
38  * file type, invoking the system default browser to show a given URL, invoking
39  * the system default mailer with or without a constructed <code>Message</code>
40  * object, and making use of the printer to print a given file.
41  *
42  * @see Message
43  * @see DesktopException
44  */

45 public class Desktop {
46  
47     /**
48      * Suppresses default constructor for noninstantiability.
49      */

50     private Desktop() {}
51   
52     /**
53      * Checks if the given file is a valid file.
54      *
55      * @throws DesktopException if the given file is not a valid file, or
56      * is not readable.
57      */

58     private static void checkFileValid(File JavaDoc file) throws DesktopException {
59         if (file == null || !file.exists()) {
60             throw new DesktopException("The given file doesn't exist.");
61         }
62
63         if (!file.canRead()) {
64             throw new DesktopException("The given file couldn't be read.");
65         }
66     }
67
68     /**
69      * Launches the associated application to open a file
70      * <p>
71      * If the specified file is a directory, the file manager of the current
72      * platform is launched to open it.
73      * @param file the given file.
74      * @throws DesktopException If the specified file has no associated
75      * application, or the associated application fails to be launched.
76      */

77     public static void open(File JavaDoc file) throws DesktopException {
78         if (file == null) {
79             throw new DesktopException("The given file is null.");
80         }
81         
82         // Get the launch service.
83
LaunchService launchService = (LaunchService) ServiceManager.
84             getService(ServiceManager.LAUNCH_SERVICE);
85         
86         File JavaDoc resolvedFile = launchService.resolveLinkFile(file);
87
88         // Check if the given file is valid.
89
checkFileValid(resolvedFile);
90
91         try {
92             launchService.open(resolvedFile);
93         } catch (LaunchFailedException e) {
94             throw new DesktopException(e.getMessage());
95         }
96     
97     }
98   
99     /**
100      * Tests whether the given file could be printed.
101      *
102      * @param file the given file.
103      * @return <code>true</code> if the given file is valid and is printable;
104      * <code>false</code> otherwise.
105      */

106     public static boolean isPrintable(File JavaDoc file) {
107         if (file == null) {
108             return false;
109         }
110
111         // Get the launch service.
112
LaunchService launchService = (LaunchService) ServiceManager.
113             getService(ServiceManager.LAUNCH_SERVICE);
114                 
115         File JavaDoc resolvedFile = launchService.resolveLinkFile(file);
116         try {
117             checkFileValid(resolvedFile);
118         } catch (DesktopException e) {
119             return false;
120         }
121    
122         return launchService.isPrintable(resolvedFile);
123     }
124   
125     /**
126      * Prints the given file.
127      *
128      * @param file the given file.
129      * @throws DesktopException if the given file is not valid, or the given
130      * file is not printable, or the given file fails to be printed.
131      */

132     public static void print(File JavaDoc file) throws DesktopException {
133         if (file == null) {
134             throw new DesktopException("The given file is null.");
135         }
136
137         // Get the launch service.
138
LaunchService launchService = (LaunchService) ServiceManager.
139             getService(ServiceManager.LAUNCH_SERVICE);
140         
141         File JavaDoc resolvedFile = launchService.resolveLinkFile(file);
142                 
143         // Check if valid file.
144
checkFileValid(resolvedFile);
145     
146         // Check if printable.
147
if (!isPrintable(resolvedFile)) {
148             throw new DesktopException("The given file is not printable.");
149         }
150
151     
152         // Print the file.
153
try {
154             launchService.print(resolvedFile);
155         } catch (LaunchFailedException e) {
156             throw new DesktopException(e.getMessage());
157         }
158     
159     }
160   
161     /**
162      * Tests whether the given file is editable.
163      *
164      * @param file the given file.
165      * @return <code>true</code> if the given file is valid and has an
166      * associated editor; <code>false</code> otherwise.
167      */

168     public static boolean isEditable(File JavaDoc file) {
169         if (file == null) {
170             return false;
171         }
172
173         // Get the launch service.
174
LaunchService launchService = (LaunchService) ServiceManager.
175             getService(ServiceManager.LAUNCH_SERVICE);
176                 
177         File JavaDoc resolvedFile = launchService.resolveLinkFile(file);
178                 
179         try {
180             checkFileValid(resolvedFile);
181         } catch (DesktopException e) {
182             return false;
183         }
184    
185         return launchService.isEditable(resolvedFile);
186     }
187   
188     /**
189      * Launches the associated editor to edit the given file.
190      *
191      * @param file the given file.
192      * @throws DesktopException if the given file is not valid, or the given
193      * file is not editable, or the given editor fails to be launched.
194      */

195     public static void edit(File JavaDoc file) throws DesktopException {
196         if (file == null) {
197             throw new DesktopException("The given file is null.");
198         }
199
200         // Get the launch service.
201
LaunchService launchService = (LaunchService) ServiceManager.
202             getService(ServiceManager.LAUNCH_SERVICE);
203
204         File JavaDoc resolvedFile = launchService.resolveLinkFile(file);
205         
206         // Check if valid file.
207
checkFileValid(resolvedFile);
208     
209         // Check if editable.
210
if (!isEditable(resolvedFile)) {
211             throw new DesktopException("The given file is not editable.");
212         }
213     
214         // Edit the file.
215
try {
216             launchService.edit(resolvedFile);
217         } catch (LaunchFailedException e) {
218             throw new DesktopException(e.getMessage());
219         }
220     
221     }
222   
223     /**
224      * Launches the system default browser to show the given URL.
225      *
226      * @param url the given URL.
227      * @throws DesktopException if the given url is null, or the system default
228      * browser is not found, or the system browser fails to be launched.
229      */

230     public static void browse(URL JavaDoc url) throws DesktopException {
231         if (url == null) {
232             throw new DesktopException("The given URL is null.");
233         }
234         
235         // Get the browser service.
236
BrowserService browserService = (BrowserService) ServiceManager.
237             getService(ServiceManager.BROWSER_SERVICE);
238
239         try {
240             browserService.show(url);
241         } catch (LaunchFailedException e) {
242             throw new DesktopException(e.getMessage());
243         }
244     }
245
246     /**
247      * ======================================================================
248      * This method is not public for this release, and may be public later.
249      * ======================================================================
250      * Opens the given URL in the target window of the system default browser.
251      * <p>
252      * If the target windows doesn't exist, a new browser window will be
253      * created, and named as the given target name. Otherwise, the given URL
254      * will be displayed in the browser window with the given target name.
255      * <p>
256      * If the target name is "_blank", the URL will always be displayed in a
257      * new, unnamed browser window.
258      *
259      * @param url the given URL.
260      * @param target the given name of a target browser window.
261      * @throws DesktopException if the given url is null, or the system default
262      * browser is not found, or it's found but fails to be launched.
263      */

264     private static void browse(URL JavaDoc url, String JavaDoc target) throws DesktopException {
265         if (url == null) {
266             throw new DesktopException("The given URL is null.");
267         }
268         if (target == null) {
269             throw new DesktopException("The given target is null.");
270         }
271
272         // Get the browser service.
273
BrowserService browserService = (BrowserService) ServiceManager.
274             getService(ServiceManager.BROWSER_SERVICE);
275
276         // Show the url in the browser window named as target.
277
try {
278             browserService.show(url, target);
279         } catch (LaunchFailedException e) {
280             throw new DesktopException(e.getMessage());
281         }
282     }
283
284   
285     /**
286      * Launches the message compose window of the default mailer.
287      *
288      * @throws DesktopException if the system default mailer was not found,
289      * or it fails to be launched.
290      */

291     public static void mail() throws DesktopException {
292         // Get the mailer service.
293
MailerService mailerService = (MailerService) ServiceManager.
294             getService(ServiceManager.MAILER_SERVICE);
295     
296         // Launch the mailer.
297
try {
298             mailerService.open();
299         } catch (LaunchFailedException e) {
300             throw new DesktopException(e.getMessage());
301         }
302     }
303
304     /**
305      * Launches the message compose window of the default mailer, and fills
306      * in the message fields with the field values of the given <code>Message
307      * </code> object.
308      *
309      * @param msg the constructed <code>Message</code> object.
310      * @throws DesktopException if the given message object is null, or the
311      * system default mailer was not found, or it fails to be launched.
312      */

313     public static void mail(Message msg) throws DesktopException {
314         if (msg == null) {
315             throw new DesktopException("The given message is null.");
316         }
317
318         // Get the mailerserservice
319
MailerService mailerService = (MailerService) ServiceManager.
320             getService(ServiceManager.MAILER_SERVICE);
321    
322         // Launch the mailer.
323
try {
324             mailerService.open(msg);
325         } catch (LaunchFailedException e) {
326             throw new DesktopException(e.getMessage());
327         }
328     }
329 }
Popular Tags