< prev index next >

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

Print this page

        

@@ -20,23 +20,24 @@
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-
 package java.awt;
 
+import java.awt.desktop.*;
 import java.awt.peer.DesktopPeer;
 import java.io.File;
 import java.io.FilePermission;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 
 import sun.awt.SunToolkit;
+import javax.swing.JMenuBar;
 import sun.security.util.SecurityConstants;
 
 /**
  * The {@code Desktop} class allows a Java application to launch
  * associated applications registered on the native desktop to handle

@@ -68,10 +69,22 @@
  *
  * <p> Note: when some action is invoked and the associated
  * application is executed, it will be executed on the same system as
  * the one on which the Java application was launched.
  *
+ * <p>
+ * Please note that for Mac OS notifications for 
+ * {@link OpenFilesHandler#openFiles(AppEvent.OpenFilesEvent)},
+ * {@link PrintFilesHandler#printFiles(AppEvent.PrintFilesEvent)} )} and
+ * {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)}
+ * are only sent if the Java app is a bundled application,
+ * with a <code>CFBundleDocumentTypes</code> array present in it's
+ * Info.plist. See the
+ * <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">Info.plist
+ * Key Reference</a> for more information about adding a
+ * <code>CFBundleDocumentTypes</code> key to your app's Info.plist.
+ *
  * @since 1.6
  * @author Armin Chen
  * @author George Zhang
  */
 public class Desktop {

@@ -108,11 +121,128 @@
         MAIL,
         /**
          * Represents a "browse" action.
          * @see Desktop#browse(java.net.URI)
          */
-        BROWSE
+        BROWSE,
+        
+        /**
+         * Represents a AppForegroundListener
+         * @see java.awt.desktop.AppForegroundListener
+         */
+        APP_EVENT_FOREGROUND,
+
+        /**
+         * Represents a AppHiddenListener
+         * @see java.awt.desktop.AppHiddenListener
+         */
+        APP_EVENT_HIDDEN,
+
+        /**
+         * Represents a AppReopenedListener
+         * @see java.awt.desktop.AppReopenedListener
+         */
+        APP_EVENT_REOPENED,
+
+        /**
+         * Represents a ScreenSleepListener
+         * @see java.awt.desktop.ScreenSleepListener
+         */
+        APP_EVENT_SCREEN_SLEEP,
+
+        /**
+         * Represents a SystemSleepListener
+         * @see java.awt.desktop.SystemSleepListener
+         */
+        APP_EVENT_SYSTEM_SLEEP,
+
+        /**
+         * Represents a UserSessionListener
+         * @see java.awt.desktop.UserSessionListener
+         */
+        APP_EVENT_USER_SESSION,
+
+        /**
+         * Represents a AboutHandler
+         * @see #setAboutHandler(java.awt.desktop.AboutHandler) 
+         */
+        APP_ABOUT,
+
+        /**
+         * Represents a PreferencesHandler
+         * @see #setPreferencesHandler(java.awt.desktop.PreferencesHandler) 
+         */
+        APP_PREFERENCES,
+
+        /**
+         * Represents a OpenFilesHandler
+         * @see #setOpenFileHandler(java.awt.desktop.OpenFilesHandler) 
+         */
+        APP_OPEN_FILE,
+
+        /**
+         * Represents a PrintFilesHandler
+         * @see #setPrintFileHandler(java.awt.desktop.PrintFilesHandler) 
+         */
+        APP_PRINT_FILE,
+
+        /**
+         * Represents a OpenURIHandler
+         * @see #setOpenURIHandler(java.awt.desktop.OpenURIHandler) 
+         */
+        APP_OPEN_URI,
+
+        /**
+         * Represents a QuitHandler
+         * @see #setQuitHandler(java.awt.desktop.QuitHandler) 
+         */        
+        APP_QUIT_HANDLER,
+
+        /**
+         * Represents a QuitStrategy
+         * @see #setQuitStrategy(java.awt.desktop.QuitStrategy) 
+         */
+        APP_QUIT_STRATEGY,
+
+        /**
+         * Represents a SuddenTermination
+         * @see #enableSuddenTermination() 
+         */
+        APP_SUDDEN_TERMINATION,
+
+        /**
+         * Represents a requestForeground
+         * @see #requestForeground(boolean) 
+         */
+        APP_REQUEST_FOREGROUND,
+
+        /**
+         * Represents a HelpViewer
+         * @see #openHelpViewer() 
+         */
+        APP_HELP_VIEWER,
+
+        /**
+         * Represents a menu bar
+         * @see #setDefaultMenuBar(javax.swing.JMenuBar) 
+         */
+        APP_MENU_BAR,
+
+        /**
+         * Represents a platform specific full screen
+         * @see #addWindowFullScreenListener(java.awt.Window, java.awt.desktop.FullScreenListener) 
+         * @see #removeWindowFullScreenListener(java.awt.Window, java.awt.desktop.FullScreenListener) 
+         * @see #setWindowCanFullScreen(java.awt.Window, boolean) 
+         * @see #requestToggleFullScreen(java.awt.Window) 
+         */
+        FULLSCREEN,
+
+        /**
+         * Represents a FileManager
+         * @see #getFileManager() 
+         */
+        FILEMANAGER
     };
 
     private DesktopPeer peer;
 
     /**

@@ -480,6 +610,272 @@
         if (sm != null) {
             sm.checkPermission(new FilePermission("<<ALL FILES>>",
                                                   SecurityConstants.FILE_EXECUTE_ACTION));
         }
     }
+
+    /**
+     * Adds sub-types of {@link SystemEventListener} to listen for notifications
+     * from the native system.
+     * 
+     * Has no effect if SystemEventListener's sub-type is unsupported on current
+     * platform.
+     *
+     * @param listener listener
+     * @see AppForegroundListener
+     * @see AppHiddenListener
+     * @see AppReopenedListener
+     * @see ScreenSleepListener
+     * @see SystemSleepListener
+     * @see UserSessionListener
+     */
+    public void addAppEventListener(final SystemEventListener listener) {
+        peer.addAppEventListener(listener);
+    }
+
+    /**
+     * Removes sub-types of {@link SystemEventListener} to listen for notifications
+     * from the native system.
+     *
+     * Has no effect if SystemEventListener's sub-type is unsupported on current
+     * platform.
+     * 
+     * @param listener listener
+     * @see AppForegroundListener
+     * @see AppHiddenListener
+     * @see AppReOpenedListener
+     * @see AppScreenSleepListener
+     * @see AppSystemSleepListener
+     * @see AppUserSessionListener
+     */
+    public void removeAppEventListener(final SystemEventListener listener) {
+        peer.removeAppEventListener(listener);
+    }
+
+    /**
+     * Installs a handler to show a custom About window for your application.
+     * <p>
+     * Setting the {@link AboutHandler} to <code>null</code> reverts it to the
+     * default behavior.
+     *
+     * @param aboutHandler the handler to respond to the
+     * {@link AboutHandler#handleAbout} )} message
+     */
+    public void setAboutHandler(final AboutHandler aboutHandler) {
+        checkActionSupport(Action.APP_ABOUT);
+        peer.setAboutHandler(aboutHandler);
+    }
+
+    /**
+     * Installs a handler to show a custom Preferences window for your
+     * application.
+     * <p>
+     * Setting the {@link PreferencesHandler} to <code>null</code> reverts it to
+     * the default behavior
+     *
+     * @param preferencesHandler the handler to respond to the
+     * {@link PreferencesHandler#handlePreferences(PreferencesEvent)}
+     */
+    public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
+        checkActionSupport(Action.APP_PREFERENCES);
+        peer.setPreferencesHandler(preferencesHandler);
+    }
+
+    /**
+     * Installs the handler which is notified when the application is asked to
+     * open a list of files.
+     *
+     * @param openFileHandler handler
+     */
+    public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
+        checkActionSupport(Action.APP_OPEN_FILE);
+        peer.setOpenFileHandler(openFileHandler);
+    }
+
+    /**
+     * Installs the handler which is notified when the application is asked to
+     * print a list of files.
+     *
+     * @param printFileHandler handler
+     */
+    public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
+        checkActionSupport(Action.APP_PRINT_FILE);
+        peer.setPrintFileHandler(printFileHandler);
+    }
+
+    /**
+     * Installs the handler which is notified when the application is asked to
+     * open a URL.
+     * 
+     * Setting the handler to <code>null</code> causes all
+     * {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} requests to be
+     * enqueued until another handler is set.
+     *
+     * @param openURIHandler handler
+     */
+    public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
+        checkActionSupport(Action.APP_OPEN_URI);
+        peer.setOpenURIHandler(openURIHandler);
+    }
+
+    /**
+     * Installs the handler which determines if the application should quit. The
+     * handler is passed a one-shot {@link QuitResponse} which can cancel or
+     * proceed with the quit. Setting the handler to <code>null</code> causes
+     * all quit requests to directly perform the default {@link QuitStrategy}.
+     *
+     * @param quitHandler the handler that is called when the application is
+     * asked to quit
+     */
+    public void setQuitHandler(final QuitHandler quitHandler) {
+        checkActionSupport(Action.APP_QUIT_HANDLER);
+        peer.setQuitHandler(quitHandler);
+    }
+
+    /**
+     * Sets the default strategy used to quit this application. The default is
+     * calling SYSTEM_EXIT_0.
+     *
+     * @param strategy the way this application should be shutdown
+     */
+    public void setQuitStrategy(final QuitStrategy strategy) {
+        checkActionSupport(Action.APP_QUIT_STRATEGY);
+        peer.setQuitStrategy(strategy);
+    }
+
+    /**
+     * Enables this application to be suddenly terminated.
+     *
+     * Call this method to indicate your application's state is saved, and
+     * requires no notification to be terminated. Letting your application
+     * remain terminatable improves the user experience by avoiding re-paging in
+     * your application when it's asked to quit.
+     *
+     * <b>Note: enabling sudden termination will allow your application to be
+     * quit without notifying your QuitHandler, or running any shutdown
+     * hooks.</b>
+     * E.g. user initiated Cmd-Q, logout, restart, or shutdown requests will
+     * effectively "kill -KILL" your application.
+     *
+     * @see #disableSuddenTermination()
+     */
+    public void enableSuddenTermination() {
+        checkActionSupport(Action.APP_SUDDEN_TERMINATION);
+        peer.enableSuddenTermination();
+    }
+
+    /**
+     * Prevents this application from being suddenly terminated.
+     *
+     * Call this method to indicate that your application has unsaved state, and
+     * may not be terminated without notification.
+     *
+     * @see #enableSuddenTermination()
+     */
+    public void disableSuddenTermination() {
+        checkActionSupport(Action.APP_SUDDEN_TERMINATION);
+        peer.disableSuddenTermination();
+    }
+
+    /**
+     * Requests this application to move to the foreground.
+     *
+     * @param allWindows if all windows of this application should be moved to
+     * the foreground, or only the foremost one
+     */
+    public void requestForeground(final boolean allWindows) {
+        checkActionSupport(Action.APP_REQUEST_FOREGROUND);
+        peer.requestForeground(allWindows);
+    }
+
+    /**
+     * Opens the native help viewer application.
+     */
+    public void openHelpViewer() {
+        checkActionSupport(Action.APP_HELP_VIEWER);
+        peer.openHelpViewer();
+    }
+
+    /**
+     * Sets the default menu bar to use when there are no active frames.
+     * 
+     * Note, Aqua Look and Feel should be active to support this on Mac OS.
+     *
+     * @param menuBar to use when no other frames are active
+     */
+    public void setDefaultMenuBar(final JMenuBar menuBar) {
+        checkActionSupport(Action.APP_MENU_BAR);
+        peer.setDefaultMenuBar(menuBar);
+    }
+
+    /**
+     * Attaches a {@link FullScreenListener} to the specified top-level
+     * {@link Window}.
+     *
+     * @param window to attach the {@link FullScreenListener} to
+     * @param listener to be notified when a full screen event occurs
+     * @throws IllegalArgumentException if window is not a
+     * {@link RootPaneContainer}
+     */
+    public void addWindowFullScreenListener(final Window window,
+            final FullScreenListener listener) {
+        checkActionSupport(Action.FULLSCREEN);
+        peer.addWindowFullScreenListener(window, listener);
+    }
+
+    /**
+     * Removes a {@link FullScreenListener} from the specified top-level
+     * {@link Window}.
+     *
+     * @param window to remove the {@link FullScreenListener} from
+     * @param listener to be removed
+     * @throws IllegalArgumentException if window is not a
+     * {@link RootPaneContainer}
+     */
+    public void removeWindowFullScreenListener(final Window window,
+            final FullScreenListener listener) {
+        checkActionSupport(Action.FULLSCREEN);
+        peer.removeWindowFullScreenListener(window, listener);
+    }
+    
+    /**
+     * Marks a {@link Window} as able to animate into or out of full screen
+     * mode.
+     *
+     * Only top-level {@link Window}s which are {@link RootPaneContainer}s are
+     * able to be animated into and out of full screen mode. The {@link Window}
+     * must be marked as full screen-able before the native peer is created with
+     * {@link Component#addNotify()}.
+     *
+     * @param window window
+     * @param canFullScreen flag
+     * @throws IllegalArgumentException if window is not a
+     * {@link RootPaneContainer}
+     */
+    public void setWindowCanFullScreen(final Window window, final boolean canFullScreen) {
+        checkActionSupport(Action.FULLSCREEN);
+        peer.setWindowCanFullScreen(window, canFullScreen);
+    }
+
+    /**
+     * Requests that a {@link Window} should get into or out of full screen
+     * mode. Only {@link Window}s marked as full screenable by
+     * {@link #setWindowCanFullScreen(Window, boolean)} can be toggled.
+     *
+     * @param window to animate into or out of full screen mode
+     */
+    public void requestToggleFullScreen(final Window window) {
+        checkActionSupport(Action.FULLSCREEN);
+        peer.requestToggleFullScreen(window);
+    }
+
+    /**
+     * Returns the mac specific FileManager.
+     * @return FileManager
+     * @see FileManager
+     */
+    public FileManager getFileManager() {
+        checkActionSupport(Action.FILEMANAGER);
+        return peer.getFileManager();
+    }
+
 }
< prev index next >