1 7 8 package java.awt; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.net.URISyntaxException ; 13 import java.net.URI ; 14 import java.net.URL ; 15 import java.net.MalformedURLException ; 16 import java.awt.AWTPermission ; 17 import java.awt.GraphicsEnvironment ; 18 import java.awt.HeadlessException ; 19 import java.awt.peer.DesktopPeer; 20 import sun.awt.SunToolkit; 21 import sun.awt.HeadlessToolkit; 22 import java.io.FilePermission ; 23 import sun.security.util.SecurityConstants; 24 25 62 public class Desktop { 63 64 72 public static enum Action { 73 77 OPEN, 78 82 EDIT, 83 87 PRINT, 88 93 MAIL, 94 98 BROWSE 99 }; 100 101 private DesktopPeer peer; 102 103 106 private Desktop() { 107 peer = Toolkit.getDefaultToolkit().createDesktopPeer(this); 108 } 109 110 123 public static synchronized Desktop getDesktop(){ 124 if (GraphicsEnvironment.isHeadless()) throw new HeadlessException (); 125 if (!Desktop.isDesktopSupported()) { 126 throw new UnsupportedOperationException ("Desktop API is not " + 127 "supported on the current platform"); 128 } 129 130 sun.awt.AppContext context = sun.awt.AppContext.getAppContext(); 131 Desktop desktop = (Desktop )context.get(Desktop .class); 132 133 if (desktop == null) { 134 desktop = new Desktop (); 135 context.put(Desktop .class, desktop); 136 } 137 138 return desktop; 139 } 140 141 150 public static boolean isDesktopSupported(){ 151 Toolkit defaultToolkit = Toolkit.getDefaultToolkit(); 152 if (defaultToolkit instanceof SunToolkit) { 153 return ((SunToolkit)defaultToolkit).isDesktopSupported(); 154 } 155 return false; 156 } 157 158 174 public boolean isSupported(Action action) { 175 return peer.isSupported(action); 176 } 177 178 187 private static void checkFileValidation(File file){ 188 if (file == null) throw new NullPointerException ("File must not be null"); 189 190 if (!file.exists()) { 191 throw new IllegalArgumentException ("The file: " 192 + file.getPath() + " doesn't exist."); 193 } 194 195 file.canRead(); 196 } 197 198 205 private void checkActionSupport(Action actionType){ 206 if (!isSupported(actionType)) { 207 throw new UnsupportedOperationException ("The " + actionType.name() 208 + " action is not supported on the current platform!"); 209 } 210 } 211 212 213 218 private void checkAWTPermission(){ 219 SecurityManager sm = System.getSecurityManager(); 220 if (sm != null) { 221 sm.checkPermission(new AWTPermission ( 222 "showWindowWithoutWarningBanner")); 223 } 224 } 225 226 248 public void open(File file) throws IOException { 249 checkAWTPermission(); 250 checkExec(); 251 checkActionSupport(Action.OPEN); 252 checkFileValidation(file); 253 254 peer.open(file); 255 } 256 257 279 public void edit(File file) throws IOException { 280 checkAWTPermission(); 281 checkExec(); 282 checkActionSupport(Action.EDIT); 283 file.canWrite(); 284 checkFileValidation(file); 285 286 peer.edit(file); 287 } 288 289 309 public void print(File file) throws IOException { 310 checkExec(); 311 SecurityManager sm = System.getSecurityManager(); 312 if (sm != null) { 313 sm.checkPrintJobAccess(); 314 } 315 checkActionSupport(Action.PRINT); 316 checkFileValidation(file); 317 318 peer.print(file); 319 } 320 321 355 public void browse(URI uri) throws IOException { 356 SecurityException securityException = null; 357 try { 358 checkAWTPermission(); 359 checkExec(); 360 } catch (SecurityException e) { 361 securityException = e; 362 } 363 checkActionSupport(Action.BROWSE); 364 if (uri == null) { 365 throw new NullPointerException (); 366 } 367 if (securityException == null) { 368 peer.browse(uri); 369 return; 370 } 371 372 URL url = null; 376 try { 377 url = uri.toURL(); 378 } catch (MalformedURLException e) { 379 throw new IllegalArgumentException ("Unable to convert URI to URL", e); 380 } 381 sun.awt.DesktopBrowse db = sun.awt.DesktopBrowse.getInstance(); 382 if (db == null) { 383 throw securityException; 385 } 386 db.browse(url); 387 } 388 389 404 public void mail() throws IOException { 405 checkAWTPermission(); 406 checkExec(); 407 checkActionSupport(Action.MAIL); 408 URI mailtoURI = null; 409 try{ 410 mailtoURI = new URI ("mailto:?"); 411 peer.mail(mailtoURI); 412 } catch (URISyntaxException e){ 413 } 415 } 416 417 446 public void mail(URI mailtoURI) throws IOException { 447 checkAWTPermission(); 448 checkExec(); 449 checkActionSupport(Action.MAIL); 450 if (mailtoURI == null) throw new NullPointerException (); 451 452 if (!"mailto".equalsIgnoreCase(mailtoURI.getScheme())) { 453 throw new IllegalArgumentException ("URI scheme is not \"mailto\""); 454 } 455 456 peer.mail(mailtoURI); 457 } 458 459 private void checkExec() throws SecurityException { 460 SecurityManager sm = System.getSecurityManager(); 461 if (sm != null) { 462 sm.checkPermission(new FilePermission ("<<ALL FILES>>", 463 SecurityConstants.FILE_EXECUTE_ACTION)); 464 } 465 } 466 } 467 | Popular Tags |