src/java.desktop/share/classes/java/awt/Desktop.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 

  28 import java.io.File;

  29 import java.io.IOException;
  30 import java.net.URISyntaxException;
  31 import java.net.URI;

  32 import java.net.URL;
  33 import java.net.MalformedURLException;
  34 import java.awt.AWTPermission;
  35 import java.awt.GraphicsEnvironment;
  36 import java.awt.HeadlessException;
  37 import java.awt.peer.DesktopPeer;
  38 import sun.awt.SunToolkit;
  39 import sun.awt.HeadlessToolkit;
  40 import java.io.FilePermission;
  41 import sun.security.util.SecurityConstants;
  42 
  43 /**
  44  * The {@code Desktop} class allows a Java application to launch
  45  * associated applications registered on the native desktop to handle
  46  * a {@link java.net.URI} or a file.
  47  *
  48  * <p> Supported operations include:
  49  * <ul>
  50  *   <li>launching the user-default browser to show a specified
  51  *       URI;</li>
  52  *   <li>launching the user-default mail client with an optional
  53  *       {@code mailto} URI;</li>
  54  *   <li>launching a registered application to open, edit or print a
  55  *       specified file.</li>
  56  * </ul>
  57  *
  58  * <p> This class provides methods corresponding to these
  59  * operations. The methods look for the associated application
  60  * registered on the current platform, and launch it to handle a URI


 105         PRINT,
 106         /**
 107          * Represents a "mail" action.
 108          * @see Desktop#mail()
 109          * @see Desktop#mail(java.net.URI)
 110          */
 111         MAIL,
 112         /**
 113          * Represents a "browse" action.
 114          * @see Desktop#browse(java.net.URI)
 115          */
 116         BROWSE
 117     };
 118 
 119     private DesktopPeer peer;
 120 
 121     /**
 122      * Suppresses default constructor for noninstantiability.
 123      */
 124     private Desktop() {
 125         peer = Toolkit.getDefaultToolkit().createDesktopPeer(this);




 126     }
 127 
 128     /**
 129      * Returns the <code>Desktop</code> instance of the current
 130      * browser context.  On some platforms the Desktop API may not be
 131      * supported; use the {@link #isDesktopSupported} method to
 132      * determine if the current desktop is supported.
 133      * @return the Desktop instance of the current browser context
 134      * @throws HeadlessException if {@link
 135      * GraphicsEnvironment#isHeadless()} returns {@code true}
 136      * @throws UnsupportedOperationException if this class is not
 137      * supported on the current platform
 138      * @see #isDesktopSupported()
 139      * @see java.awt.GraphicsEnvironment#isHeadless
 140      */
 141     public static synchronized Desktop getDesktop(){
 142         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 143         if (!Desktop.isDesktopSupported()) {
 144             throw new UnsupportedOperationException("Desktop API is not " +
 145                                                     "supported on the current platform");


   1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.peer.DesktopPeer;
  29 import java.io.File;
  30 import java.io.FilePermission;
  31 import java.io.IOException;
  32 import java.net.MalformedURLException;
  33 import java.net.URI;
  34 import java.net.URISyntaxException;
  35 import java.net.URL;
  36 




  37 import sun.awt.SunToolkit;


  38 import sun.security.util.SecurityConstants;
  39 
  40 /**
  41  * The {@code Desktop} class allows a Java application to launch
  42  * associated applications registered on the native desktop to handle
  43  * a {@link java.net.URI} or a file.
  44  *
  45  * <p> Supported operations include:
  46  * <ul>
  47  *   <li>launching the user-default browser to show a specified
  48  *       URI;</li>
  49  *   <li>launching the user-default mail client with an optional
  50  *       {@code mailto} URI;</li>
  51  *   <li>launching a registered application to open, edit or print a
  52  *       specified file.</li>
  53  * </ul>
  54  *
  55  * <p> This class provides methods corresponding to these
  56  * operations. The methods look for the associated application
  57  * registered on the current platform, and launch it to handle a URI


 102         PRINT,
 103         /**
 104          * Represents a "mail" action.
 105          * @see Desktop#mail()
 106          * @see Desktop#mail(java.net.URI)
 107          */
 108         MAIL,
 109         /**
 110          * Represents a "browse" action.
 111          * @see Desktop#browse(java.net.URI)
 112          */
 113         BROWSE
 114     };
 115 
 116     private DesktopPeer peer;
 117 
 118     /**
 119      * Suppresses default constructor for noninstantiability.
 120      */
 121     private Desktop() {
 122         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 123         // same cast as in isDesktopSupported()
 124         if (defaultToolkit instanceof SunToolkit) {
 125             peer = ((SunToolkit) defaultToolkit).createDesktopPeer(this);
 126         }
 127     }
 128 
 129     /**
 130      * Returns the <code>Desktop</code> instance of the current
 131      * browser context.  On some platforms the Desktop API may not be
 132      * supported; use the {@link #isDesktopSupported} method to
 133      * determine if the current desktop is supported.
 134      * @return the Desktop instance of the current browser context
 135      * @throws HeadlessException if {@link
 136      * GraphicsEnvironment#isHeadless()} returns {@code true}
 137      * @throws UnsupportedOperationException if this class is not
 138      * supported on the current platform
 139      * @see #isDesktopSupported()
 140      * @see java.awt.GraphicsEnvironment#isHeadless
 141      */
 142     public static synchronized Desktop getDesktop(){
 143         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 144         if (!Desktop.isDesktopSupported()) {
 145             throw new UnsupportedOperationException("Desktop API is not " +
 146                                                     "supported on the current platform");