< prev index next >

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

Print this page




   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
  58  * or file. If there is no associated application or the associated
  59  * application fails to be launched, an exception is thrown.
  60  *



  61  * <p> An application is registered to a URI or file type; for
  62  * example, the {@code "sxi"} file extension is typically registered
  63  * to StarOffice.  The mechanism of registering, accessing, and
  64  * launching the associated application is platform-dependent.
  65  *
  66  * <p> Each operation is an action type represented by the {@link
  67  * Desktop.Action} class.
  68  *
  69  * <p> Note: when some action is invoked and the associated
  70  * application is executed, it will be executed on the same system as
  71  * the one on which the Java application was launched.
  72  *


  73  * @since 1.6
  74  * @author Armin Chen
  75  * @author George Zhang
  76  */
  77 public class Desktop {
  78 
  79     /**
  80      * Represents an action type.  Each platform supports a different
  81      * set of actions.  You may use the {@link Desktop#isSupported}
  82      * method to determine if the given action is supported by the
  83      * current platform.
  84      * @see java.awt.Desktop#isSupported(java.awt.Desktop.Action)
  85      * @since 1.6
  86      */
  87     public static enum Action {
  88         /**
  89          * Represents an "open" action.
  90          * @see Desktop#open(java.io.File)
  91          */
  92         OPEN,
  93         /**
  94          * Represents an "edit" action.
  95          * @see Desktop#edit(java.io.File)
  96          */
  97         EDIT,
  98         /**
  99          * Represents a "print" action.
 100          * @see Desktop#print(java.io.File)
 101          */
 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");
 147         }
 148 
 149         sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
 150         Desktop desktop = (Desktop)context.get(Desktop.class);
 151 
 152         if (desktop == null) {
 153             desktop = new Desktop();
 154             context.put(Desktop.class, desktop);


 226             throw new UnsupportedOperationException("The " + actionType.name()
 227                                                     + " action is not supported on the current platform!");
 228         }
 229     }
 230 
 231 
 232     /**
 233      *  Calls to the security manager's <code>checkPermission</code> method with
 234      *  an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 235      *  permission.
 236      */
 237     private void checkAWTPermission(){
 238         SecurityManager sm = System.getSecurityManager();
 239         if (sm != null) {
 240             sm.checkPermission(new AWTPermission(
 241                                    "showWindowWithoutWarningBanner"));
 242         }
 243     }
 244 
 245     /**













 246      * Launches the associated application to open the file.
 247      *
 248      * <p> If the specified file is a directory, the file manager of
 249      * the current platform is launched to open it.
 250      *
 251      * @param file the file to be opened with the associated application
 252      * @throws NullPointerException if {@code file} is {@code null}
 253      * @throws IllegalArgumentException if the specified file doesn't
 254      * exist
 255      * @throws UnsupportedOperationException if the current platform
 256      * does not support the {@link Desktop.Action#OPEN} action
 257      * @throws IOException if the specified file has no associated
 258      * application or the associated application fails to be launched
 259      * @throws SecurityException if a security manager exists and its
 260      * {@link java.lang.SecurityManager#checkRead(java.lang.String)}
 261      * method denies read access to the file, or it denies the
 262      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 263      * permission, or the calling thread is not allowed to create a
 264      * subprocess
 265      * @see java.awt.AWTPermission


 464      */
 465     public  void mail(URI mailtoURI) throws IOException {
 466         checkAWTPermission();
 467         checkExec();
 468         checkActionSupport(Action.MAIL);
 469         if (mailtoURI == null) throw new NullPointerException();
 470 
 471         if (!"mailto".equalsIgnoreCase(mailtoURI.getScheme())) {
 472             throw new IllegalArgumentException("URI scheme is not \"mailto\"");
 473         }
 474 
 475         peer.mail(mailtoURI);
 476     }
 477 
 478     private void checkExec() throws SecurityException {
 479         SecurityManager sm = System.getSecurityManager();
 480         if (sm != null) {
 481             sm.checkPermission(new FilePermission("<<ALL FILES>>",
 482                                                   SecurityConstants.FILE_EXECUTE_ACTION));
 483         }











































































































































































































































































































































































































































 484     }
 485 }


   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.desktop.*;
  29 import java.awt.peer.DesktopPeer;
  30 import java.io.File;
  31 import java.io.FilePermission;
  32 import java.io.IOException;
  33 import java.net.MalformedURLException;
  34 import java.net.URI;
  35 import java.net.URISyntaxException;
  36 import java.net.URL;
  37 
  38 import sun.awt.SunToolkit;
  39 import javax.swing.JMenuBar;
  40 import sun.security.util.SecurityConstants;
  41 
  42 /**
  43  * The {@code Desktop} class allows interact with various desktop capabilities.


  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
  58  * or file. If there is no associated application or the associated
  59  * application fails to be launched, an exception is thrown.
  60  *
  61  * Please see {@link Desktop.Action} for the full list of supported operations
  62  * and capabilities.
  63  *
  64  * <p> An application is registered to a URI or file type; for
  65  * example, the {@code "sxi"} file extension is typically registered
  66  * to StarOffice.  The mechanism of registering, accessing, and
  67  * launching the associated application is platform-dependent.
  68  *
  69  * <p> Each operation is an action type represented by the {@link
  70  * Desktop.Action} class.
  71  *
  72  * <p> Note: when some action is invoked and the associated
  73  * application is executed, it will be executed on the same system as
  74  * the one on which the Java application was launched.
  75  *
  76  * @see Action
  77  *
  78  * @since 1.6
  79  * @author Armin Chen
  80  * @author George Zhang
  81  */
  82 public class Desktop {
  83 
  84     /**
  85      * Represents an action type.  Each platform supports a different
  86      * set of actions.  You may use the {@link Desktop#isSupported}
  87      * method to determine if the given action is supported by the
  88      * current platform.
  89      * @see java.awt.Desktop#isSupported(java.awt.Desktop.Action)
  90      * @since 1.6
  91      */
  92     public static enum Action {
  93         /**
  94          * Represents an "open" action.
  95          * @see Desktop#open(java.io.File)
  96          */
  97         OPEN,
  98         /**
  99          * Represents an "edit" action.
 100          * @see Desktop#edit(java.io.File)
 101          */
 102         EDIT,
 103         /**
 104          * Represents a "print" action.
 105          * @see Desktop#print(java.io.File)
 106          */
 107         PRINT,
 108         /**
 109          * Represents a "mail" action.
 110          * @see Desktop#mail()
 111          * @see Desktop#mail(java.net.URI)
 112          */
 113         MAIL,
 114 
 115         /**
 116          * Represents a "browse" action.
 117          * @see Desktop#browse(java.net.URI)
 118          */
 119         BROWSE,
 120 
 121         /**
 122          * Represents an AppForegroundListener
 123          * @see java.awt.desktop.AppForegroundListener
 124          * @since 1.9
 125          */
 126         APP_EVENT_FOREGROUND,
 127 
 128         /**
 129          * Represents an AppHiddenListener
 130          * @see java.awt.desktop.AppHiddenListener
 131          * @since 1.9
 132          */
 133         APP_EVENT_HIDDEN,
 134 
 135         /**
 136          * Represents an AppReopenedListener
 137          * @see java.awt.desktop.AppReopenedListener
 138          * @since 1.9
 139          */
 140         APP_EVENT_REOPENED,
 141 
 142         /**
 143          * Represents a ScreenSleepListener
 144          * @see java.awt.desktop.ScreenSleepListener
 145          * @since 1.9
 146          */
 147         APP_EVENT_SCREEN_SLEEP,
 148 
 149         /**
 150          * Represents a SystemSleepListener
 151          * @see java.awt.desktop.SystemSleepListener
 152          * @since 1.9
 153          */
 154         APP_EVENT_SYSTEM_SLEEP,
 155 
 156         /**
 157          * Represents a UserSessionListener
 158          * @see java.awt.desktop.UserSessionListener
 159          * @since 1.9
 160          */
 161         APP_EVENT_USER_SESSION,
 162 
 163         /**
 164          * Represents an AboutHandler
 165          * @see #setAboutHandler(java.awt.desktop.AboutHandler)
 166          * @since 1.9
 167          */
 168         APP_ABOUT,
 169 
 170         /**
 171          * Represents a PreferencesHandler
 172          * @see #setPreferencesHandler(java.awt.desktop.PreferencesHandler)
 173          * @since 1.9
 174          */
 175         APP_PREFERENCES,
 176 
 177         /**
 178          * Represents an OpenFilesHandler
 179          * @see #setOpenFileHandler(java.awt.desktop.OpenFilesHandler)
 180          * @since 1.9
 181          */
 182         APP_OPEN_FILE,
 183 
 184         /**
 185          * Represents a PrintFilesHandler
 186          * @see #setPrintFileHandler(java.awt.desktop.PrintFilesHandler)
 187          * @since 1.9
 188          */
 189         APP_PRINT_FILE,
 190 
 191         /**
 192          * Represents an OpenURIHandler
 193          * @see #setOpenURIHandler(java.awt.desktop.OpenURIHandler)
 194          * @since 1.9
 195          */
 196         APP_OPEN_URI,
 197 
 198         /**
 199          * Represents a QuitHandler
 200          * @see #setQuitHandler(java.awt.desktop.QuitHandler)
 201          * @since 1.9
 202          */
 203         APP_QUIT_HANDLER,
 204 
 205         /**
 206          * Represents a QuitStrategy
 207          * @see #setQuitStrategy(java.awt.desktop.QuitStrategy)
 208          * @since 1.9
 209          */
 210         APP_QUIT_STRATEGY,
 211 
 212         /**
 213          * Represents a SuddenTermination
 214          * @see #enableSuddenTermination()
 215          * @since 1.9
 216          */
 217         APP_SUDDEN_TERMINATION,
 218 
 219         /**
 220          * Represents a requestForeground
 221          * @see #requestForeground(boolean)
 222          * @since 1.9
 223          */
 224         APP_REQUEST_FOREGROUND,
 225 
 226         /**
 227          * Represents a HelpViewer
 228          * @see #openHelpViewer()
 229          * @since 1.9
 230          */
 231         APP_HELP_VIEWER,
 232 
 233         /**
 234          * Represents a menu bar
 235          * @see #setDefaultMenuBar(javax.swing.JMenuBar)
 236          * @since 1.9
 237          */
 238         APP_MENU_BAR,
 239 
 240         /**
 241          * Represents a platform-specific full screen
 242          * @see #addWindowFullScreenListener(java.awt.Window, java.awt.desktop.FullScreenListener)
 243          * @see #removeWindowFullScreenListener(java.awt.Window, java.awt.desktop.FullScreenListener)
 244          * @see #setWindowCanFullScreen(java.awt.Window, boolean)
 245          * @see #requestToggleFullScreen(java.awt.Window)
 246          * @since 1.9
 247          */
 248         FULLSCREEN,
 249 
 250         /**
 251          * Represents a browse file directory
 252          * @see #browseFileDirectory(java.io.File)
 253          * @since 1.9
 254          */
 255         BROWSE_FILE_DIR,
 256 
 257         /**
 258          * Represents a move to trash
 259          * @see #moveToTrash(java.io.File)
 260          * @since 1.9
 261          */
 262         MOVE_TO_TRASH
 263     };
 264 
 265     private DesktopPeer peer;
 266 
 267     /**
 268      * Suppresses default constructor for noninstantiability.
 269      */
 270     private Desktop() {
 271         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 272         // same cast as in isDesktopSupported()
 273         if (defaultToolkit instanceof SunToolkit) {
 274             peer = ((SunToolkit) defaultToolkit).createDesktopPeer(this);
 275         }
 276     }
 277 
 278     /**
 279      * Returns the <code>Desktop</code> instance of the current
 280      * desktop context.  On some platforms the Desktop API may not be
 281      * supported; use the {@link #isDesktopSupported} method to
 282      * determine if the current desktop is supported.
 283      * @return the Desktop instance
 284      * @throws HeadlessException if {@link
 285      * GraphicsEnvironment#isHeadless()} returns {@code true}
 286      * @throws UnsupportedOperationException if this class is not
 287      * supported on the current platform
 288      * @see #isDesktopSupported()
 289      * @see java.awt.GraphicsEnvironment#isHeadless
 290      */
 291     public static synchronized Desktop getDesktop(){
 292         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 293         if (!Desktop.isDesktopSupported()) {
 294             throw new UnsupportedOperationException("Desktop API is not " +
 295                                                     "supported on the current platform");
 296         }
 297 
 298         sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
 299         Desktop desktop = (Desktop)context.get(Desktop.class);
 300 
 301         if (desktop == null) {
 302             desktop = new Desktop();
 303             context.put(Desktop.class, desktop);


 375             throw new UnsupportedOperationException("The " + actionType.name()
 376                     + " action is not supported on the current platform!");
 377         }
 378     }
 379 
 380 
 381     /**
 382      *  Calls to the security manager's <code>checkPermission</code> method with
 383      *  an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 384      *  permission.
 385      */
 386     private void checkAWTPermission(){
 387         SecurityManager sm = System.getSecurityManager();
 388         if (sm != null) {
 389             sm.checkPermission(new AWTPermission(
 390                     "showWindowWithoutWarningBanner"));
 391         }
 392     }
 393 
 394     /**
 395      *  Calls to the security manager's <code>checkPermission</code> method with
 396      *  an <code>AWTPermission("fullScreenExclusive")</code>
 397      *  permission.
 398      */
 399     private void checkFullscreenPermission(){
 400         SecurityManager sm = System.getSecurityManager();
 401         if (sm != null) {
 402             sm.checkPermission(new AWTPermission(
 403                     "fullScreenExclusive"));
 404         }
 405     }
 406 
 407     /**
 408      * Launches the associated application to open the file.
 409      *
 410      * <p> If the specified file is a directory, the file manager of
 411      * the current platform is launched to open it.
 412      *
 413      * @param file the file to be opened with the associated application
 414      * @throws NullPointerException if {@code file} is {@code null}
 415      * @throws IllegalArgumentException if the specified file doesn't
 416      * exist
 417      * @throws UnsupportedOperationException if the current platform
 418      * does not support the {@link Desktop.Action#OPEN} action
 419      * @throws IOException if the specified file has no associated
 420      * application or the associated application fails to be launched
 421      * @throws SecurityException if a security manager exists and its
 422      * {@link java.lang.SecurityManager#checkRead(java.lang.String)}
 423      * method denies read access to the file, or it denies the
 424      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 425      * permission, or the calling thread is not allowed to create a
 426      * subprocess
 427      * @see java.awt.AWTPermission


 626      */
 627     public  void mail(URI mailtoURI) throws IOException {
 628         checkAWTPermission();
 629         checkExec();
 630         checkActionSupport(Action.MAIL);
 631         if (mailtoURI == null) throw new NullPointerException();
 632 
 633         if (!"mailto".equalsIgnoreCase(mailtoURI.getScheme())) {
 634             throw new IllegalArgumentException("URI scheme is not \"mailto\"");
 635         }
 636 
 637         peer.mail(mailtoURI);
 638     }
 639 
 640     private void checkExec() throws SecurityException {
 641         SecurityManager sm = System.getSecurityManager();
 642         if (sm != null) {
 643             sm.checkPermission(new FilePermission("<<ALL FILES>>",
 644                     SecurityConstants.FILE_EXECUTE_ACTION));
 645         }
 646     }
 647 
 648     private void checkRead() throws SecurityException {
 649         SecurityManager sm = System.getSecurityManager();
 650         if (sm != null) {
 651             sm.checkPermission(new FilePermission("<<ALL FILES>>",
 652                     SecurityConstants.FILE_READ_ACTION));
 653         }
 654     }
 655 
 656     private void checkWrite() throws SecurityException {
 657         SecurityManager sm = System.getSecurityManager();
 658         if (sm != null) {
 659             sm.checkPermission(new FilePermission("<<ALL FILES>>",
 660                     SecurityConstants.FILE_WRITE_ACTION));
 661         }
 662     }
 663 
 664     /**
 665      * Adds sub-types of {@link SystemEventListener} to listen for notifications
 666      * from the native system.
 667      *
 668      * Has no effect if SystemEventListener's sub-type is unsupported on the current
 669      * platform.
 670      *
 671      * @param listener listener
 672      * @see AppForegroundListener
 673      * @see AppHiddenListener
 674      * @see AppReopenedListener
 675      * @see ScreenSleepListener
 676      * @see SystemSleepListener
 677      * @see UserSessionListener
 678      * @since 1.9
 679      */
 680     public void addAppEventListener(final SystemEventListener listener) {
 681         peer.addAppEventListener(listener);
 682     }
 683 
 684     /**
 685      * Removes sub-types of {@link SystemEventListener} to listen for notifications
 686      * from the native system.
 687      *
 688      * Has no effect if SystemEventListener's sub-type is unsupported on  the current
 689      * platform.
 690      *
 691      * @param listener listener
 692      * @see AppForegroundListener
 693      * @see AppHiddenListener
 694      * @see AppReopenedListener
 695      * @see ScreenSleepListener
 696      * @see SystemSleepListener
 697      * @see UserSessionListener
 698      * @since 1.9
 699      */
 700     public void removeAppEventListener(final SystemEventListener listener) {
 701         peer.removeAppEventListener(listener);
 702     }
 703 
 704     /**
 705      * Installs a handler to show a custom About window for your application.
 706      * <p>
 707      * Setting the {@link AboutHandler} to <code>null</code> reverts it to the
 708      * default behavior.
 709      *
 710      * @param aboutHandler the handler to respond to the
 711      * @throws UnsupportedOperationException if the current platform
 712      * does not support the {@link Desktop.Action#APP_ABOUT} action
 713      * {@link AboutHandler#handleAbout} )} message
 714      * @since 1.9
 715      */
 716     public void setAboutHandler(final AboutHandler aboutHandler) {
 717         checkActionSupport(Action.APP_ABOUT);
 718         peer.setAboutHandler(aboutHandler);
 719     }
 720 
 721     /**
 722      * Installs a handler to show a custom Preferences window for your
 723      * application.
 724      * <p>
 725      * Setting the {@link PreferencesHandler} to <code>null</code> reverts it to
 726      * the default behavior
 727      *
 728      * @param preferencesHandler the handler to respond to the
 729      * @throws UnsupportedOperationException if the current platform
 730      * does not support the {@link Desktop.Action#APP_PREFERENCES} action
 731      * {@link PreferencesHandler#handlePreferences(PreferencesEvent)}
 732      * @since 1.9
 733      */
 734     public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
 735         checkActionSupport(Action.APP_PREFERENCES);
 736         peer.setPreferencesHandler(preferencesHandler);
 737     }
 738 
 739     /**
 740      * Installs the handler which is notified when the application is asked to
 741      * open a list of files.
 742      *
 743      * @implNote Please note that for Mac OS, notifications
 744      * are only sent if the Java app is a bundled application,
 745      * with a {@code CFBundleDocumentTypes} array present in its
 746      * Info.plist. See the
 747      * <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">
 748      * Info.plist Key Reference</a> for more information about adding a
 749      * {@code CFBundleDocumentTypes} key to your app's Info.plist.
 750      *
 751      * @param openFileHandler handler
 752      * @throws UnsupportedOperationException if the current platform
 753      * does not support the {@link Desktop.Action#APP_OPEN_FILE} action
 754      * @since 1.9
 755      */
 756     public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
 757         checkActionSupport(Action.APP_OPEN_FILE);
 758         peer.setOpenFileHandler(openFileHandler);
 759     }
 760 
 761     /**
 762      * Installs the handler which is notified when the application is asked to
 763      * print a list of files.
 764      *
 765      * @implNote Please note that for Mac OS, notifications
 766      * are only sent if the Java app is a bundled application,
 767      * with a {@code CFBundleDocumentTypes} array present in its
 768      * Info.plist. See the
 769      * <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">
 770      * Info.plist Key Reference</a> for more information about adding a
 771      * {@code CFBundleDocumentTypes} key to your app's Info.plist.
 772      *
 773      * @param printFileHandler handler
 774      * @throws UnsupportedOperationException if the current platform
 775      * does not support the {@link Desktop.Action#APP_PRINT_FILE} action
 776      * @since 1.9
 777      */
 778     public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
 779         checkActionSupport(Action.APP_PRINT_FILE);
 780         peer.setPrintFileHandler(printFileHandler);
 781     }
 782 
 783     /**
 784      * Installs the handler which is notified when the application is asked to
 785      * open a URL.
 786      *
 787      * Setting the handler to <code>null</code> causes all
 788      * {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} requests to be
 789      * enqueued until another handler is set.
 790      *
 791      * @implNote Please note that for Mac OS, notifications
 792      * are only sent if the Java app is a bundled application,
 793      * with a {@code CFBundleDocumentTypes} array present in its
 794      * Info.plist. See the
 795      * <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">
 796      * Info.plist Key Reference</a> for more information about adding a
 797      * {@code CFBundleDocumentTypes} key to your app's Info.plist.
 798      *
 799      * @param openURIHandler handler
 800      * @throws UnsupportedOperationException if the current platform
 801      * does not support the {@link Desktop.Action#APP_OPEN_URI} action
 802      * @since 1.9
 803      */
 804     public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
 805         checkActionSupport(Action.APP_OPEN_URI);
 806         peer.setOpenURIHandler(openURIHandler);
 807     }
 808 
 809     /**
 810      * Installs the handler which determines if the application should quit. The
 811      * handler is passed a one-shot {@link QuitResponse} which can cancel or
 812      * proceed with the quit. Setting the handler to <code>null</code> causes
 813      * all quit requests to directly perform the default {@link QuitStrategy}.
 814      *
 815      * @param quitHandler the handler that is called when the application is
 816      * asked to quit
 817      * @throws UnsupportedOperationException if the current platform
 818      * does not support the {@link Desktop.Action#APP_QUIT_HANDLER} action
 819      * @since 1.9
 820      */
 821     public void setQuitHandler(final QuitHandler quitHandler) {
 822         checkActionSupport(Action.APP_QUIT_HANDLER);
 823         peer.setQuitHandler(quitHandler);
 824     }
 825 
 826     /**
 827      * Sets the default strategy used to quit this application. The default is
 828      * calling SYSTEM_EXIT_0.
 829      *
 830      * @param strategy the way this application should be shutdown
 831      * @throws UnsupportedOperationException if the current platform
 832      * does not support the {@link Desktop.Action#APP_QUIT_STRATEGY} action
 833      * @see QuitStrategy
 834      * @since 1.9
 835      */
 836     public void setQuitStrategy(final QuitStrategy strategy) {
 837         checkActionSupport(Action.APP_QUIT_STRATEGY);
 838         peer.setQuitStrategy(strategy);
 839     }
 840 
 841     /**
 842      * Enables this application to be suddenly terminated.
 843      *
 844      * Call this method to indicate your application's state is saved, and
 845      * requires no notification to be terminated. Letting your application
 846      * remain terminatable improves the user experience by avoiding re-paging in
 847      * your application when it's asked to quit.
 848      *
 849      * <b>Note: enabling sudden termination will allow your application to be
 850      * quit without notifying your QuitHandler, or running any shutdown
 851      * hooks.</b>
 852      * E.g. user-initiated Cmd-Q, logout, restart, or shutdown requests will
 853      * effectively "kill -KILL" your application.
 854      *
 855      * @throws UnsupportedOperationException if the current platform
 856      * does not support the {@link Desktop.Action#APP_SUDDEN_TERMINATION} action
 857      * @see #disableSuddenTermination()
 858      * @since 1.9
 859      */
 860     public void enableSuddenTermination() {
 861         checkActionSupport(Action.APP_SUDDEN_TERMINATION);
 862         peer.enableSuddenTermination();
 863     }
 864 
 865     /**
 866      * Prevents this application from being suddenly terminated.
 867      *
 868      * Call this method to indicate that your application has unsaved state, and
 869      * may not be terminated without notification.
 870      *
 871      * @throws UnsupportedOperationException if the current platform
 872      * does not support the {@link Desktop.Action#APP_SUDDEN_TERMINATION} action
 873      * @see #enableSuddenTermination()
 874      * @since 1.9
 875      */
 876     public void disableSuddenTermination() {
 877         checkActionSupport(Action.APP_SUDDEN_TERMINATION);
 878         peer.disableSuddenTermination();
 879     }
 880 
 881     /**
 882      * Requests this application to move to the foreground.
 883      *
 884      * @param allWindows if all windows of this application should be moved to
 885      * the foreground, or only the foremost one
 886      * @throws SecurityException if a security manager exists and it denies the
 887      * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission.
 888      * @throws UnsupportedOperationException if the current platform
 889      * does not support the {@link Desktop.Action#APP_REQUEST_FOREGROUND} action
 890      * @since 1.9
 891      */
 892     public void requestForeground(final boolean allWindows) {
 893         checkAWTPermission();
 894         checkActionSupport(Action.APP_REQUEST_FOREGROUND);
 895         peer.requestForeground(allWindows);
 896     }
 897 
 898     /**
 899      * Opens the native help viewer application.
 900      * 
 901      * @throws SecurityException if a security manager exists and it denies the
 902      * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission.
 903      * @throws UnsupportedOperationException if the current platform
 904      * does not support the {@link Desktop.Action#APP_HELP_VIEWER} action
 905      * @since 1.9
 906      */
 907     public void openHelpViewer() {
 908         checkAWTPermission();
 909         checkActionSupport(Action.APP_HELP_VIEWER);
 910         peer.openHelpViewer();
 911     }
 912 
 913     /**
 914      * Sets the default menu bar to use when there are no active frames.
 915      *
 916      * @implNote Aqua Look and Feel should be active to support this on Mac OS.
 917      *
 918      * @param menuBar to use when no other frames are active
 919      * @throws SecurityException if a security manager exists and it denies the
 920      * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission.
 921      * @throws UnsupportedOperationException if the current platform
 922      * does not support the {@link Desktop.Action#APP_MENU_BAR} action
 923      * @since 1.9
 924      */
 925     public void setDefaultMenuBar(final JMenuBar menuBar) {
 926         checkAWTPermission();
 927         checkActionSupport(Action.APP_MENU_BAR);
 928         peer.setDefaultMenuBar(menuBar);
 929     }
 930 
 931     /**
 932      * Attaches a {@link FullScreenListener} to the specified top-level
 933      * {@link Window}.
 934      *
 935      * @param window to attach the {@link FullScreenListener} to
 936      * @param listener to be notified when a full screen event occurs
 937      * @throws SecurityException if a security manager exists and it denies the
 938      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 939      * and <code>AWTPermission("fullScreenExclusive")</code>
 940      * permission.
 941      * @throws IllegalArgumentException if window is not a
 942      * {@link javax.swing.JFrame} or a {@link javax.swing.JDialog} 
 943      * @throws UnsupportedOperationException if the current platform
 944      * does not support the {@link Desktop.Action#FULLSCREEN} action
 945      * @since 1.9
 946      */
 947     public void addWindowFullScreenListener(final Window window,
 948                                               final FullScreenListener listener) {
 949         checkAWTPermission();
 950         checkFullscreenPermission();
 951         checkActionSupport(Action.FULLSCREEN);
 952         peer.addWindowFullScreenListener(window, listener);
 953     }
 954 
 955     /**
 956      * Removes a {@link FullScreenListener} from the specified top-level
 957      * {@link Window}.
 958      *
 959      * @param window to remove the {@link FullScreenListener} from
 960      * @param listener to be removed
 961      * @throws SecurityException if a security manager exists and it denies the
 962      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 963      * and <code>AWTPermission("fullScreenExclusive")</code>
 964      * permission.
 965      * @throws IllegalArgumentException if window is not a
 966      * {@link javax.swing.JFrame} or a {@link javax.swing.JDialog} 
 967      * @throws UnsupportedOperationException if the current platform
 968      * does not support the {@link Desktop.Action#FULLSCREEN} action
 969      * @since 1.9
 970      */
 971     public void removeWindowFullScreenListener(final Window window,
 972                                                 final FullScreenListener listener) {
 973         checkAWTPermission();
 974         checkFullscreenPermission();
 975         checkActionSupport(Action.FULLSCREEN);
 976         peer.removeWindowFullScreenListener(window, listener);
 977     }
 978 
 979     /**
 980      * Marks a {@link Window} as able to animate into or out of full screen
 981      * mode.
 982      *
 983      * Only top-level {@link Window}s which are a
 984      * {@link javax.swing.JFrame} or a {@link javax.swing.JDialog}
 985      * are able to be animated into and out of full screen mode. The {@link Window}
 986      * must be marked as full screen-able before the native peer is created with
 987      * {@link Component#addNotify()}.
 988      *
 989      * @param window window
 990      * @param canFullScreen flag
 991      * @throws SecurityException if a security manager exists and it denies the
 992      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
 993      * and <code>AWTPermission("fullScreenExclusive")</code>
 994      * permission.
 995      * @throws IllegalArgumentException if window is not a
 996      * {@link javax.swing.JFrame} or a {@link javax.swing.JDialog} 
 997      * @throws UnsupportedOperationException if the current platform
 998      * does not support the {@link Desktop.Action#FULLSCREEN} action
 999      * @since 1.9
1000      */
1001     public void setWindowCanFullScreen(final Window window,
1002                                         final boolean canFullScreen) {
1003         checkAWTPermission();
1004         checkFullscreenPermission();
1005         checkActionSupport(Action.FULLSCREEN);
1006         peer.setWindowCanFullScreen(window, canFullScreen);
1007     }
1008 
1009     /**
1010      * Requests that a {@link Window} should get into or out of full screen
1011      * mode. Only {@link Window}s marked as full screenable by
1012      * {@link #setWindowCanFullScreen(Window, boolean)} can be toggled.
1013      *
1014      * @param window to animate into or out of full screen mode
1015      * @throws SecurityException if a security manager exists and it denies the
1016      * <code>AWTPermission("showWindowWithoutWarningBanner")</code>
1017      * and <code>AWTPermission("fullScreenExclusive")</code>
1018      * permission.
1019      * @throws UnsupportedOperationException if the current platform
1020      *         does not support the {@link Desktop.Action#FULLSCREEN} action
1021      * @since 1.9
1022      */
1023     public void requestToggleFullScreen(final Window window) {
1024         checkAWTPermission();
1025         checkFullscreenPermission();
1026         checkActionSupport(Action.FULLSCREEN);
1027         peer.requestToggleFullScreen(window);
1028     }
1029 
1030 
1031     /**
1032      * Opens a folder containing the {@code file} and selects it 
1033      * in a default system file manager.
1034      * @param file the file
1035      * @throws SecurityException If a security manager exists and its
1036      *         {@link SecurityManager#checkRead(java.lang.String)} method
1037      *         denies read access to the file
1038      * @throws UnsupportedOperationException if the current platform
1039      *         does not support the {@link Desktop.Action#BROWSE_FILE_DIR} action
1040      * @throws NullPointerException if {@code file} is {@code null}
1041      * @throws IllegalArgumentException if the specified file doesn't
1042      * exist
1043      * @since 1.9
1044      */
1045     public void browseFileDirectory(File file) {
1046         checkRead();
1047         checkActionSupport(Action.BROWSE_FILE_DIR);
1048         checkFileValidation(file);
1049         peer.browseFileDirectory(file);
1050     }
1051 
1052     /**
1053      * Moves the specified file to the trash.
1054      *
1055      * @param file the file
1056      * @return returns true if successfully moved the file to the trash.
1057      * @throws SecurityException If a security manager exists and its
1058      *         {@link SecurityManager#checkWrite(java.lang.String)} method
1059      *         denies write access to the file
1060      * @throws UnsupportedOperationException if the current platform
1061      *         does not support the {@link Desktop.Action#MOVE_TO_TRASH} action
1062      * @throws NullPointerException if {@code file} is {@code null}
1063      * @throws IllegalArgumentException if the specified file doesn't
1064      * exist
1065      * 
1066      * @since 1.9
1067      */
1068     public boolean moveToTrash(final File file) {
1069         checkWrite();
1070         checkActionSupport(Action.MOVE_TO_TRASH);
1071         checkFileValidation(file);
1072         return peer.moveToTrash(file);
1073     }
1074 }
< prev index next >