Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/eawt/Application.java
38831 views
/*1* Copyright (c) 2011, 2012, 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.*;28import java.awt.peer.*;29import java.beans.Beans;3031import javax.swing.JMenuBar;3233import sun.lwawt.*;34import sun.lwawt.macosx.*;3536/**37* The <code>Application</code> class allows you to integrate your Java application with the native Mac OS X environment.38* You can provide your Mac OS X users a greatly enhanced experience by implementing a few basic handlers for standard system events.39*40* For example:41* <ul>42* <li>Open an about dialog when a user chooses About from the application menu.</li>43* <li>Open a preferences window when the users chooses Preferences from the application menu.</li>44* <li>Create a new document when the user clicks on your Dock icon, and no windows are open.</li>45* <li>Open a document that the user double-clicked on in the Finder.</li>46* <li>Open a custom URL scheme when a user clicks on link in a web browser.</li>47* <li>Reconnect to network services after the system has awoke from sleep.</li>48* <li>Cleanly shutdown your application when the user chooses Quit from the application menu, Dock icon, or types Command-Q.</li>49* <li>Cancel shutdown/logout if the user has unsaved changes in your application.</li>50* </ul>51*52* @since 1.453*/54public class Application {55private static native void nativeInitializeApplicationDelegate();5657static Application sApplication = null;5859static {60java.security.AccessController.doPrivileged(61new java.security.PrivilegedAction<Void>() {62public Void run() {63System.loadLibrary("awt");64return null;65}66});6768checkSecurity();69if (!Beans.isDesignTime()) {70nativeInitializeApplicationDelegate();71}7273sApplication = new Application();74}7576private static void checkSecurity() {77final SecurityManager security = System.getSecurityManager();78if (security == null) return;79security.checkPermission(new RuntimePermission("canProcessApplicationEvents"));80}8182/**83* @return the singleton representing this Mac OS X Application84*85* @since 1.486*/87public static Application getApplication() {88checkSecurity();89return sApplication;90}9192// some singletons, since they get called back into from native93final _AppEventHandler eventHandler = _AppEventHandler.getInstance();94final _AppMenuBarHandler menuBarHandler = _AppMenuBarHandler.getInstance();95final _AppDockIconHandler iconHandler = new _AppDockIconHandler();9697/**98* Creates an Application instance. Should only be used in JavaBean environments.99* @deprecated use {@link #getApplication()}100*101* @since 1.4102*/103@Deprecated104public Application() {105checkSecurity();106}107108/**109* Adds sub-types of {@link AppEventListener} to listen for notifications from the native Mac OS X system.110*111* @see AppForegroundListener112* @see AppHiddenListener113* @see AppReOpenedListener114* @see ScreenSleepListener115* @see SystemSleepListener116* @see UserSessionListener117*118* @param listener119* @since Java for Mac OS X 10.6 Update 3120* @since Java for Mac OS X 10.5 Update 8121*/122public void addAppEventListener(final AppEventListener listener) {123eventHandler.addListener(listener);124}125126/**127* Removes sub-types of {@link AppEventListener} from listening for notifications from the native Mac OS X system.128*129* @see AppForegroundListener130* @see AppHiddenListener131* @see AppReOpenedListener132* @see ScreenSleepListener133* @see SystemSleepListener134* @see UserSessionListener135*136* @param listener137* @since Java for Mac OS X 10.6 Update 3138* @since Java for Mac OS X 10.5 Update 8139*/140public void removeAppEventListener(final AppEventListener listener) {141eventHandler.removeListener(listener);142}143144/**145* Installs a handler to show a custom About window for your application.146*147* Setting the {@link AboutHandler} to <code>null</code> reverts it to the default Cocoa About window.148*149* @param aboutHandler the handler to respond to the {@link AboutHandler#handleAbout()} message150* @since Java for Mac OS X 10.6 Update 3151* @since Java for Mac OS X 10.5 Update 8152*/153public void setAboutHandler(final AboutHandler aboutHandler) {154eventHandler.aboutDispatcher.setHandler(aboutHandler);155}156157/**158* Installs a handler to create the Preferences menu item in your application's app menu.159*160* Setting the {@link PreferencesHandler} to <code>null</code> will remove the Preferences item from the app menu.161*162* @param preferencesHandler163* @since Java for Mac OS X 10.6 Update 3164* @since Java for Mac OS X 10.5 Update 8165*/166public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {167eventHandler.preferencesDispatcher.setHandler(preferencesHandler);168}169170/**171* Installs the handler which is notified when the application is asked to open a list of files.172* The {@link OpenFilesHandler#openFiles(AppEvent.OpenFilesEvent)} notifications are only sent if the Java app is a bundled application, with a <code>CFBundleDocumentTypes</code> array present in it's Info.plist.173* 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.174*175* @param openFileHandler176* @since Java for Mac OS X 10.6 Update 3177* @since Java for Mac OS X 10.5 Update 8178*/179public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {180eventHandler.openFilesDispatcher.setHandler(openFileHandler);181}182183/**184* Installs the handler which is notified when the application is asked to print a list of files.185* The {@link PrintFilesHandler#printFiles(AppEvent.PrintFilesEvent)} notifications are only sent if the Java app is a bundled application, with a <code>CFBundleDocumentTypes</code> array present in it's Info.plist.186* 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.187*188* @param printFileHandler189* @since Java for Mac OS X 10.6 Update 3190* @since Java for Mac OS X 10.5 Update 8191*/192public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {193eventHandler.printFilesDispatcher.setHandler(printFileHandler);194}195196/**197* Installs the handler which is notified when the application is asked to open a URL.198* The {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} notifications are only sent if the Java app is a bundled application, with a <code>CFBundleURLTypes</code> array present in it's Info.plist.199* 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</code> key to your app's Info.plist.200*201* Setting the handler to <code>null</code> causes all {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} requests to be enqueued until another handler is set.202*203* @param openURIHandler204* @since Java for Mac OS X 10.6 Update 3205* @since Java for Mac OS X 10.5 Update 8206*/207public void setOpenURIHandler(final OpenURIHandler openURIHandler) {208eventHandler.openURIDispatcher.setHandler(openURIHandler);209}210211/**212* Installs the handler which determines if the application should quit.213* The handler is passed a one-shot {@link QuitResponse} which can cancel or proceed with the quit.214* Setting the handler to <code>null</code> causes all quit requests to directly perform the default {@link QuitStrategy}.215*216* @param quitHandler the handler that is called when the application is asked to quit217* @since Java for Mac OS X 10.6 Update 3218* @since Java for Mac OS X 10.5 Update 8219*/220public void setQuitHandler(final QuitHandler quitHandler) {221eventHandler.quitDispatcher.setHandler(quitHandler);222}223224/**225* Sets the default strategy used to quit this application. The default is calling SYSTEM_EXIT_0.226*227* The quit strategy can also be set with the "apple.eawt.quitStrategy" system property.228*229* @param strategy the way this application should be shutdown230* @since Java for Mac OS X 10.6 Update 3231* @since Java for Mac OS X 10.5 Update 8232*/233public void setQuitStrategy(final QuitStrategy strategy) {234eventHandler.setDefaultQuitStrategy(strategy);235}236237/**238* Enables this application to be suddenly terminated.239*240* Call this method to indicate your application's state is saved, and requires no notification to be terminated.241* Letting your application remain terminatable improves the user experience by avoiding re-paging in your application when it's asked to quit.242*243* <b>Note: enabling sudden termination will allow your application to be quit without notifying your QuitHandler, or running any shutdown hooks.</b>244* User initiated Cmd-Q, logout, restart, or shutdown requests will effectively "kill -KILL" your application.245*246* This call has no effect on Mac OS X versions prior to 10.6.247*248* @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.249* @see Application#disableSuddenTermination()250*251* @since Java for Mac OS X 10.6 Update 3252* @since Java for Mac OS X 10.5 Update 8253*/254public void enableSuddenTermination() {255_AppMiscHandlers.enableSuddenTermination();256}257258/**259* Prevents this application from being suddenly terminated.260*261* Call this method to indicate that your application has unsaved state, and may not be terminated without notification.262*263* This call has no effect on Mac OS X versions prior to 10.6.264*265* @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.266* @see Application#enableSuddenTermination()267*268* @since Java for Mac OS X 10.6 Update 3269* @since Java for Mac OS X 10.5 Update 8270*/271public void disableSuddenTermination() {272_AppMiscHandlers.disableSuddenTermination();273}274275/**276* Requests this application to move to the foreground.277*278* @param allWindows if all windows of this application should be moved to the foreground, or only the foremost one279*280* @since Java for Mac OS X 10.6 Update 1281* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5282*/283public void requestForeground(final boolean allWindows) {284_AppMiscHandlers.requestActivation(allWindows);285}286287/**288* Requests user attention to this application (usually through bouncing the Dock icon). Critical289* requests will continue to bounce the Dock icon until the app is activated. An already active290* application requesting attention does nothing.291*292* @param critical if this is an important request293*294* @since Java for Mac OS X 10.6 Update 1295* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5296*/297public void requestUserAttention(final boolean critical) {298_AppMiscHandlers.requestUserAttention(critical);299}300301/**302* Opens the native help viewer application if a Help Book has been added to the303* application bundler and registered in the Info.plist with CFBundleHelpBookFolder.304*305* See http://developer.apple.com/qa/qa2001/qa1022.html for more information.306*307* @since Java for Mac OS X 10.5 - 1.5308* @since Java for Mac OS X 10.5 Update 1 - 1.6309*/310public void openHelpViewer() {311_AppMiscHandlers.openHelpViewer();312}313314/**315* Attaches the contents of the provided PopupMenu to the application's Dock icon.316*317* @param menu the PopupMenu to attach to this application's Dock icon318*319* @since Java for Mac OS X 10.5 - 1.5320* @since Java for Mac OS X 10.5 Update 1 - 1.6321*/322public void setDockMenu(final PopupMenu menu) {323iconHandler.setDockMenu(menu);324}325326/**327* @return the PopupMenu used to add items to this application's Dock icon328*329* @since Java for Mac OS X 10.5 - 1.5330* @since Java for Mac OS X 10.5 Update 1 - 1.6331*/332public PopupMenu getDockMenu() {333return iconHandler.getDockMenu();334}335336/**337* Changes this application's Dock icon to the provided image.338*339* @param image340*341* @since Java for Mac OS X 10.5 - 1.5342* @since Java for Mac OS X 10.5 Update 1 - 1.6343*/344public void setDockIconImage(final Image image) {345iconHandler.setDockIconImage(image);346}347348/**349* Obtains an image of this application's Dock icon.350*351* @return an image of this application's Dock icon352*353* @since Java for Mac OS X 10.5 - 1.5354* @since Java for Mac OS X 10.5 Update 1 - 1.6355*/356public Image getDockIconImage() {357return iconHandler.getDockIconImage();358}359360/**361* Affixes a small system provided badge to this application's Dock icon. Usually a number.362*363* @param badge textual label to affix to the Dock icon364*365* @since Java for Mac OS X 10.5 - 1.5366* @since Java for Mac OS X 10.5 Update 1 - 1.6367*/368public void setDockIconBadge(final String badge) {369iconHandler.setDockIconBadge(badge);370}371372/**373* Sets the default menu bar to use when there are no active frames.374* Only used when the system property "apple.laf.useScreenMenuBar" is "true", and375* the Aqua Look and Feel is active.376*377* @param menuBar to use when no other frames are active378*379* @since Java for Mac OS X 10.6 Update 1380* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5381*/382public void setDefaultMenuBar(final JMenuBar menuBar) {383menuBarHandler.setDefaultMenuBar(menuBar);384}385386/**387* Requests that a {@link Window} should animate into or out of full screen mode.388* Only {@link Window}s marked as full screenable by {@link FullScreenUtilities#setWindowCanFullScreen(Window, boolean)} can be toggled.389*390* @param window to animate into or out of full screen mode391*392* @since Java for Mac OS X 10.7 Update 1393*/394@SuppressWarnings("deprecation")395public void requestToggleFullScreen(final Window window) {396final ComponentPeer peer = window.getPeer();397398if (!(peer instanceof LWWindowPeer)) return;399Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();400if (!(platformWindow instanceof CPlatformWindow)) return;401((CPlatformWindow)platformWindow).toggleFullScreen();402}403404405// -- DEPRECATED API --406407/**408* Adds the specified ApplicationListener as a receiver of callbacks from this class.409* This method throws a RuntimeException if the newer About, Preferences, Quit, etc handlers are installed.410*411* @param listener an implementation of ApplicationListener that handles ApplicationEvents412*413* @deprecated register individual handlers for each task (About, Preferences, Open, Print, Quit, etc)414* @since 1.4415*/416@SuppressWarnings("deprecation")417@Deprecated418public void addApplicationListener(final ApplicationListener listener) {419eventHandler.legacyHandler.addLegacyAppListener(listener);420}421422/**423* Removes the specified ApplicationListener from being a receiver of callbacks from this class.424* This method throws a RuntimeException if the newer About, Preferences, Quit, etc handlers are installed.425*426* @param listener an implementation of ApplicationListener that had previously been registered to handle ApplicationEvents427*428* @deprecated unregister individual handlers for each task (About, Preferences, Open, Print, Quit, etc)429* @since 1.4430*/431@SuppressWarnings("deprecation")432@Deprecated433public void removeApplicationListener(final ApplicationListener listener) {434eventHandler.legacyHandler.removeLegacyAppListener(listener);435}436437/**438* Enables the Preferences item in the application menu. The ApplicationListener receives a callback for439* selection of the Preferences item in the application menu only if this is set to <code>true</code>.440*441* If a Preferences item isn't present, this method adds and enables it.442*443* @param enable specifies whether the Preferences item in the application menu should be enabled (<code>true</code>) or not (<code>false</code>)444*445* @deprecated no replacement446* @since 1.4447*/448@Deprecated449public void setEnabledPreferencesMenu(final boolean enable) {450menuBarHandler.setPreferencesMenuItemVisible(true);451menuBarHandler.setPreferencesMenuItemEnabled(enable);452}453454/**455* Enables the About item in the application menu. The ApplicationListener receives a callback for456* selection of the About item in the application menu only if this is set to <code>true</code>. Because AWT supplies457* a standard About window when an application may not, by default this is set to <code>true</code>.458*459* If the About item isn't present, this method adds and enables it.460*461* @param enable specifies whether the About item in the application menu should be enabled (<code>true</code>) or not (<code>false</code>)462*463* @deprecated no replacement464* @since 1.4465*/466@Deprecated467public void setEnabledAboutMenu(final boolean enable) {468menuBarHandler.setAboutMenuItemEnabled(enable);469}470471/**472* Determines if the Preferences item of the application menu is enabled.473*474* @deprecated no replacement475* @since 1.4476*/477@Deprecated478public boolean getEnabledPreferencesMenu() {479return menuBarHandler.isPreferencesMenuItemEnabled();480}481482/**483* Determines if the About item of the application menu is enabled.484*485* @deprecated no replacement486* @since 1.4487*/488@Deprecated489public boolean getEnabledAboutMenu() {490return menuBarHandler.isAboutMenuItemEnabled();491}492493/**494* Determines if the About item of the application menu is present.495*496* @deprecated no replacement497* @since 1.4498*/499@Deprecated500public boolean isAboutMenuItemPresent() {501return menuBarHandler.isAboutMenuItemVisible();502}503504/**505* Adds the About item to the application menu if the item is not already present.506*507* @deprecated use {@link #setAboutHandler(AboutHandler)} with a non-null {@link AboutHandler} parameter508* @since 1.4509*/510@Deprecated511public void addAboutMenuItem() {512menuBarHandler.setAboutMenuItemVisible(true);513}514515/**516* Removes the About item from the application menu if the item is present.517*518* @deprecated use {@link #setAboutHandler(AboutHandler)} with a null parameter519* @since 1.4520*/521@Deprecated522public void removeAboutMenuItem() {523menuBarHandler.setAboutMenuItemVisible(false);524}525526/**527* Determines if the About Preferences of the application menu is present. By default there is no Preferences menu item.528*529* @deprecated no replacement530* @since 1.4531*/532@Deprecated533public boolean isPreferencesMenuItemPresent() {534return menuBarHandler.isPreferencesMenuItemVisible();535}536537/**538* Adds the Preferences item to the application menu if the item is not already present.539*540* @deprecated use {@link #setPreferencesHandler(PreferencesHandler)} with a non-null {@link PreferencesHandler} parameter541* @since 1.4542*/543@Deprecated544public void addPreferencesMenuItem() {545menuBarHandler.setPreferencesMenuItemVisible(true);546}547548/**549* Removes the Preferences item from the application menu if that item is present.550*551* @deprecated use {@link #setPreferencesHandler(PreferencesHandler)} with a null parameter552* @since 1.4553*/554@Deprecated555public void removePreferencesMenuItem() {556menuBarHandler.setPreferencesMenuItemVisible(false);557}558559/**560* @deprecated Use <code>java.awt.MouseInfo.getPointerInfo().getLocation()</code>.561*562* @since 1.4563*/564@Deprecated565public static Point getMouseLocationOnScreen() {566return java.awt.MouseInfo.getPointerInfo().getLocation();567}568}569570571