Path: blob/master/src/java.desktop/macosx/classes/com/apple/eawt/Application.java
41153 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.eawt;2627import java.awt.Image;28import java.awt.PopupMenu;29import java.awt.Toolkit;30import java.awt.Window;31import java.awt.desktop.AboutHandler;32import java.awt.desktop.AppForegroundListener;33import java.awt.desktop.AppHiddenListener;34import java.awt.desktop.AppReopenedListener;35import java.awt.desktop.OpenFilesEvent;36import java.awt.desktop.OpenFilesHandler;37import java.awt.desktop.OpenURIEvent;38import java.awt.desktop.OpenURIHandler;39import java.awt.desktop.PreferencesHandler;40import java.awt.desktop.PrintFilesEvent;41import java.awt.desktop.PrintFilesHandler;42import java.awt.desktop.QuitHandler;43import java.awt.desktop.QuitResponse;44import java.awt.desktop.QuitStrategy;45import java.awt.desktop.ScreenSleepListener;46import java.awt.desktop.SystemEventListener;47import java.awt.desktop.SystemSleepListener;48import java.awt.desktop.UserSessionListener;49import java.beans.Beans;5051import javax.swing.JMenuBar;5253import sun.awt.AWTAccessor;54import sun.lwawt.LWWindowPeer;55import sun.lwawt.macosx.CPlatformWindow;5657/**58* The {@code Application} class allows you to integrate your Java application with the native Mac OS X environment.59* You can provide your Mac OS X users a greatly enhanced experience by implementing a few basic handlers for standard system events.60*61* For example:62* <ul>63* <li>Open an about dialog when a user chooses About from the application menu.</li>64* <li>Open a preferences window when the users chooses Preferences from the application menu.</li>65* <li>Create a new document when the user clicks on your Dock icon, and no windows are open.</li>66* <li>Open a document that the user double-clicked on in the Finder.</li>67* <li>Open a custom URL scheme when a user clicks on link in a web browser.</li>68* <li>Reconnect to network services after the system has awoke from sleep.</li>69* <li>Cleanly shutdown your application when the user chooses Quit from the application menu, Dock icon, or types Command-Q.</li>70* <li>Cancel shutdown/logout if the user has unsaved changes in your application.</li>71* </ul>72*73* @since 1.474*/75public class Application {76private static native void nativeInitializeApplicationDelegate();7778static Application sApplication = null;7980static {81checkSecurity();82Toolkit.getDefaultToolkit(); // Start AppKit83if (!Beans.isDesignTime()) {84nativeInitializeApplicationDelegate();85}8687sApplication = new Application();88}8990private static void checkSecurity() {91@SuppressWarnings("removal")92final SecurityManager security = System.getSecurityManager();93if (security == null) return;94security.checkPermission(new RuntimePermission("canProcessApplicationEvents"));95}9697/**98* @return the singleton representing this Mac OS X Application99*100* @since 1.4101*/102public static Application getApplication() {103checkSecurity();104return sApplication;105}106107// some singletons, since they get called back into from native108final _AppEventHandler eventHandler = _AppEventHandler.getInstance();109final _AppMenuBarHandler menuBarHandler = _AppMenuBarHandler.getInstance();110final _AppDockIconHandler iconHandler = new _AppDockIconHandler();111112/**113* Creates an Application instance. Should only be used in JavaBean environments.114* @deprecated use {@link #getApplication()}115*116* @since 1.4117*/118@Deprecated119public Application() {120checkSecurity();121}122123/**124* Adds sub-types of {@link SystemEventListener} to listen for notifications from the native Mac OS X system.125*126* @see AppForegroundListener127* @see AppHiddenListener128* @see AppReopenedListener129* @see ScreenSleepListener130* @see SystemSleepListener131* @see UserSessionListener132*133* @param listener134* @since Java for Mac OS X 10.6 Update 3135* @since Java for Mac OS X 10.5 Update 8136*/137public void addAppEventListener(final SystemEventListener listener) {138eventHandler.addListener(listener);139}140141/**142* Removes sub-types of {@link SystemEventListener} from listening for notifications from the native Mac OS X system.143*144* @see AppForegroundListener145* @see AppHiddenListener146* @see AppReopenedListener147* @see ScreenSleepListener148* @see SystemSleepListener149* @see UserSessionListener150*151* @param listener152* @since Java for Mac OS X 10.6 Update 3153* @since Java for Mac OS X 10.5 Update 8154*/155public void removeAppEventListener(final SystemEventListener listener) {156eventHandler.removeListener(listener);157}158159/**160* Installs a handler to show a custom About window for your application.161*162* Setting the {@link AboutHandler} to {@code null} reverts it to the default Cocoa About window.163*164* @param aboutHandler the handler to respond to the {@link AboutHandler#handleAbout} message165* @since Java for Mac OS X 10.6 Update 3166* @since Java for Mac OS X 10.5 Update 8167*/168public void setAboutHandler(final AboutHandler aboutHandler) {169eventHandler.aboutDispatcher.setHandler(aboutHandler);170}171172/**173* Installs a handler to create the Preferences menu item in your application's app menu.174*175* Setting the {@link PreferencesHandler} to {@code null} will remove the Preferences item from the app menu.176*177* @param preferencesHandler178* @since Java for Mac OS X 10.6 Update 3179* @since Java for Mac OS X 10.5 Update 8180*/181public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {182eventHandler.preferencesDispatcher.setHandler(preferencesHandler);183}184185/**186* Installs the handler which is notified when the application is asked to open a list of files.187* The {@link OpenFilesHandler#openFiles(OpenFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.188* 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} key to your app's Info.plist.189*190* @param openFileHandler191* @since Java for Mac OS X 10.6 Update 3192* @since Java for Mac OS X 10.5 Update 8193*/194public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {195eventHandler.openFilesDispatcher.setHandler(openFileHandler);196}197198/**199* Installs the handler which is notified when the application is asked to print a list of files.200* The {@link PrintFilesHandler#printFiles(PrintFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.201* 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} key to your app's Info.plist.202*203* @param printFileHandler204* @since Java for Mac OS X 10.6 Update 3205* @since Java for Mac OS X 10.5 Update 8206*/207public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {208eventHandler.printFilesDispatcher.setHandler(printFileHandler);209}210211/**212* Installs the handler which is notified when the application is asked to open a URL.213* The {@link OpenURIHandler#openURI(OpenURIEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleURLTypes} array present in it's Info.plist.214* 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 CFBundleURLTypes} key to your app's Info.plist.215*216* Setting the handler to {@code null} causes all {@link OpenURIHandler#openURI(OpenURIEvent)} requests to be enqueued until another handler is set.217*218* @param openURIHandler219* @since Java for Mac OS X 10.6 Update 3220* @since Java for Mac OS X 10.5 Update 8221*/222public void setOpenURIHandler(final OpenURIHandler openURIHandler) {223eventHandler.openURIDispatcher.setHandler(openURIHandler);224}225226/**227* Installs the handler which determines if the application should quit.228* The handler is passed a one-shot {@link QuitResponse} which can cancel or proceed with the quit.229* Setting the handler to {@code null} causes all quit requests to directly perform the default {@link QuitStrategy}.230*231* @param quitHandler the handler that is called when the application is asked to quit232* @since Java for Mac OS X 10.6 Update 3233* @since Java for Mac OS X 10.5 Update 8234*/235public void setQuitHandler(final QuitHandler quitHandler) {236eventHandler.quitDispatcher.setHandler(quitHandler);237}238239/**240* Sets the default strategy used to quit this application. The default is calling SYSTEM_EXIT_0.241*242* The quit strategy can also be set with the "apple.eawt.quitStrategy" system property.243*244* @param strategy the way this application should be shutdown245* @since Java for Mac OS X 10.6 Update 3246* @since Java for Mac OS X 10.5 Update 8247*/248public void setQuitStrategy(final QuitStrategy strategy) {249eventHandler.setDefaultQuitStrategy(strategy);250}251252/**253* Enables this application to be suddenly terminated.254*255* Call this method to indicate your application's state is saved, and requires no notification to be terminated.256* Letting your application remain terminatable improves the user experience by avoiding re-paging in your application when it's asked to quit.257*258* <b>Note: enabling sudden termination will allow your application to be quit without notifying your QuitHandler, or running any shutdown hooks.</b>259* User initiated Cmd-Q, logout, restart, or shutdown requests will effectively "kill -KILL" your application.260*261* This call has no effect on Mac OS X versions prior to 10.6.262*263* @see <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSProcessInfo_Class">NSProcessInfo class references</a> for more information about Sudden Termination.264* @see Application#disableSuddenTermination()265*266* @since Java for Mac OS X 10.6 Update 3267* @since Java for Mac OS X 10.5 Update 8268*/269public void enableSuddenTermination() {270_AppMiscHandlers.enableSuddenTermination();271}272273/**274* Prevents this application from being suddenly terminated.275*276* Call this method to indicate that your application has unsaved state, and may not be terminated without notification.277*278* This call has no effect on Mac OS X versions prior to 10.6.279*280* @see <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSProcessInfo_Class">NSProcessInfo class references</a> for more information about Sudden Termination.281* @see Application#enableSuddenTermination()282*283* @since Java for Mac OS X 10.6 Update 3284* @since Java for Mac OS X 10.5 Update 8285*/286public void disableSuddenTermination() {287_AppMiscHandlers.disableSuddenTermination();288}289290/**291* Requests this application to move to the foreground.292*293* @param allWindows if all windows of this application should be moved to the foreground, or only the foremost one294*295* @since Java for Mac OS X 10.6 Update 1296* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5297*/298public void requestForeground(final boolean allWindows) {299_AppMiscHandlers.requestActivation(allWindows);300}301302/**303* Requests user attention to this application (usually through bouncing the Dock icon). Critical304* requests will continue to bounce the Dock icon until the app is activated. An already active305* application requesting attention does nothing.306*307* @param critical if this is an important request308*309* @since Java for Mac OS X 10.6 Update 1310* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5311*/312public void requestUserAttention(final boolean critical) {313_AppMiscHandlers.requestUserAttention(critical);314}315316/**317* Opens the native help viewer application if a Help Book has been added to the318* application bundler and registered in the Info.plist with CFBundleHelpBookFolder.319*320* See http://developer.apple.com/qa/qa2001/qa1022.html for more information.321*322* @since Java for Mac OS X 10.5 - 1.5323* @since Java for Mac OS X 10.5 Update 1 - 1.6324*/325public void openHelpViewer() {326_AppMiscHandlers.openHelpViewer();327}328329/**330* Attaches the contents of the provided PopupMenu to the application's Dock icon.331*332* @param menu the PopupMenu to attach to this application's Dock icon333*334* @since Java for Mac OS X 10.5 - 1.5335* @since Java for Mac OS X 10.5 Update 1 - 1.6336*/337public void setDockMenu(final PopupMenu menu) {338iconHandler.setDockMenu(menu);339}340341/**342* @return the PopupMenu used to add items to this application's Dock icon343*344* @since Java for Mac OS X 10.5 - 1.5345* @since Java for Mac OS X 10.5 Update 1 - 1.6346*/347public PopupMenu getDockMenu() {348return iconHandler.getDockMenu();349}350351/**352* Changes this application's Dock icon to the provided image.353*354* @param image355*356* @since Java for Mac OS X 10.5 - 1.5357* @since Java for Mac OS X 10.5 Update 1 - 1.6358*/359public void setDockIconImage(final Image image) {360iconHandler.setDockIconImage(image);361}362363/**364* Obtains an image of this application's Dock icon.365*366* @return an image of this application's Dock icon367*368* @since Java for Mac OS X 10.5 - 1.5369* @since Java for Mac OS X 10.5 Update 1 - 1.6370*/371public Image getDockIconImage() {372return iconHandler.getDockIconImage();373}374375/**376* Affixes a small system provided badge to this application's Dock icon. Usually a number.377*378* @param badge textual label to affix to the Dock icon379*380* @since Java for Mac OS X 10.5 - 1.5381* @since Java for Mac OS X 10.5 Update 1 - 1.6382*/383public void setDockIconBadge(final String badge) {384iconHandler.setDockIconBadge(badge);385}386387/**388* Displays a progress bar to this application's Dock icon.389* Acceptable values are from 0 to 100, any other disables progress indication.390*391* @param value progress value392*/393public void setDockIconProgress(final int value) {394iconHandler.setDockIconProgress(value);395}396397/**398* Sets the default menu bar to use when there are no active frames.399* Only used when the system property "apple.laf.useScreenMenuBar" is "true", and400* the Aqua Look and Feel is active.401*402* @param menuBar to use when no other frames are active403*404* @since Java for Mac OS X 10.6 Update 1405* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5406*/407public void setDefaultMenuBar(final JMenuBar menuBar) {408menuBarHandler.setDefaultMenuBar(menuBar);409}410411/**412* Requests that a {@link Window} should animate into or out of full screen mode.413* Only {@link Window}s marked as full screenable by {@link FullScreenUtilities#setWindowCanFullScreen(Window, boolean)} can be toggled.414*415* @param window to animate into or out of full screen mode416*417* @since Java for Mac OS X 10.7 Update 1418*/419public void requestToggleFullScreen(final Window window) {420final Object peer = AWTAccessor.getComponentAccessor().getPeer(window);421if (!(peer instanceof LWWindowPeer)) return;422Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();423if (!(platformWindow instanceof CPlatformWindow)) return;424((CPlatformWindow)platformWindow).toggleFullScreen();425}426427}428429430