1 20 21 package org.jdesktop.jdic.desktop; 22 23 import java.io.File ; 24 import java.net.URL ; 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 45 public class Desktop { 46 47 50 private Desktop() {} 51 52 58 private static void checkFileValid(File 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 77 public static void open(File file) throws DesktopException { 78 if (file == null) { 79 throw new DesktopException("The given file is null."); 80 } 81 82 LaunchService launchService = (LaunchService) ServiceManager. 84 getService(ServiceManager.LAUNCH_SERVICE); 85 86 File resolvedFile = launchService.resolveLinkFile(file); 87 88 checkFileValid(resolvedFile); 90 91 try { 92 launchService.open(resolvedFile); 93 } catch (LaunchFailedException e) { 94 throw new DesktopException(e.getMessage()); 95 } 96 97 } 98 99 106 public static boolean isPrintable(File file) { 107 if (file == null) { 108 return false; 109 } 110 111 LaunchService launchService = (LaunchService) ServiceManager. 113 getService(ServiceManager.LAUNCH_SERVICE); 114 115 File 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 132 public static void print(File file) throws DesktopException { 133 if (file == null) { 134 throw new DesktopException("The given file is null."); 135 } 136 137 LaunchService launchService = (LaunchService) ServiceManager. 139 getService(ServiceManager.LAUNCH_SERVICE); 140 141 File resolvedFile = launchService.resolveLinkFile(file); 142 143 checkFileValid(resolvedFile); 145 146 if (!isPrintable(resolvedFile)) { 148 throw new DesktopException("The given file is not printable."); 149 } 150 151 152 try { 154 launchService.print(resolvedFile); 155 } catch (LaunchFailedException e) { 156 throw new DesktopException(e.getMessage()); 157 } 158 159 } 160 161 168 public static boolean isEditable(File file) { 169 if (file == null) { 170 return false; 171 } 172 173 LaunchService launchService = (LaunchService) ServiceManager. 175 getService(ServiceManager.LAUNCH_SERVICE); 176 177 File 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 195 public static void edit(File file) throws DesktopException { 196 if (file == null) { 197 throw new DesktopException("The given file is null."); 198 } 199 200 LaunchService launchService = (LaunchService) ServiceManager. 202 getService(ServiceManager.LAUNCH_SERVICE); 203 204 File resolvedFile = launchService.resolveLinkFile(file); 205 206 checkFileValid(resolvedFile); 208 209 if (!isEditable(resolvedFile)) { 211 throw new DesktopException("The given file is not editable."); 212 } 213 214 try { 216 launchService.edit(resolvedFile); 217 } catch (LaunchFailedException e) { 218 throw new DesktopException(e.getMessage()); 219 } 220 221 } 222 223 230 public static void browse(URL url) throws DesktopException { 231 if (url == null) { 232 throw new DesktopException("The given URL is null."); 233 } 234 235 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 264 private static void browse(URL url, String 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 BrowserService browserService = (BrowserService) ServiceManager. 274 getService(ServiceManager.BROWSER_SERVICE); 275 276 try { 278 browserService.show(url, target); 279 } catch (LaunchFailedException e) { 280 throw new DesktopException(e.getMessage()); 281 } 282 } 283 284 285 291 public static void mail() throws DesktopException { 292 MailerService mailerService = (MailerService) ServiceManager. 294 getService(ServiceManager.MAILER_SERVICE); 295 296 try { 298 mailerService.open(); 299 } catch (LaunchFailedException e) { 300 throw new DesktopException(e.getMessage()); 301 } 302 } 303 304 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 MailerService mailerService = (MailerService) ServiceManager. 320 getService(ServiceManager.MAILER_SERVICE); 321 322 try { 324 mailerService.open(msg); 325 } catch (LaunchFailedException e) { 326 throw new DesktopException(e.getMessage()); 327 } 328 } 329 } | Popular Tags |