< prev index next >

src/java.desktop/macosx/classes/com/apple/eawt/Application.java

Print this page


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

















  33 import java.beans.Beans;
  34 
  35 import javax.swing.JMenuBar;
  36 
  37 import sun.awt.AWTAccessor;
  38 import sun.lwawt.LWWindowPeer;
  39 import sun.lwawt.macosx.CPlatformWindow;
  40 
  41 /**
  42  * The {@code Application} class allows you to integrate your Java application with the native Mac OS X environment.
  43  * You can provide your Mac OS X users a greatly enhanced experience by implementing a few basic handlers for standard system events.
  44  *
  45  * For example:
  46  * <ul>
  47  * <li>Open an about dialog when a user chooses About from the application menu.</li>
  48  * <li>Open a preferences window when the users chooses Preferences from the application menu.</li>
  49  * <li>Create a new document when the user clicks on your Dock icon, and no windows are open.</li>
  50  * <li>Open a document that the user double-clicked on in the Finder.</li>
  51  * <li>Open a custom URL scheme when a user clicks on link in a web browser.</li>
  52  * <li>Reconnect to network services after the system has awoke from sleep.</li>


  91     final _AppEventHandler eventHandler = _AppEventHandler.getInstance();
  92     final _AppMenuBarHandler menuBarHandler = _AppMenuBarHandler.getInstance();
  93     final _AppDockIconHandler iconHandler = new _AppDockIconHandler();
  94 
  95     /**
  96      * Creates an Application instance. Should only be used in JavaBean environments.
  97      * @deprecated use {@link #getApplication()}
  98      *
  99      * @since 1.4
 100      */
 101     @Deprecated
 102     public Application() {
 103         checkSecurity();
 104     }
 105 
 106     /**
 107      * Adds sub-types of {@link SystemEventListener} to listen for notifications from the native Mac OS X system.
 108      *
 109      * @see AppForegroundListener
 110      * @see AppHiddenListener
 111      * @see AppReOpenedListener
 112      * @see AppScreenSleepListener
 113      * @see AppSystemSleepListener
 114      * @see AppUserSessionListener
 115      *
 116      * @param listener
 117      * @since Java for Mac OS X 10.6 Update 3
 118      * @since Java for Mac OS X 10.5 Update 8
 119      */
 120     public void addAppEventListener(final SystemEventListener listener) {
 121         eventHandler.addListener(listener);
 122     }
 123 
 124     /**
 125      * Removes sub-types of {@link SystemEventListener} from listening for notifications from the native Mac OS X system.
 126      *
 127      * @see AppForegroundListener
 128      * @see AppHiddenListener
 129      * @see AppReOpenedListener
 130      * @see AppScreenSleepListener
 131      * @see AppSystemSleepListener
 132      * @see AppUserSessionListener
 133      *
 134      * @param listener
 135      * @since Java for Mac OS X 10.6 Update 3
 136      * @since Java for Mac OS X 10.5 Update 8
 137      */
 138     public void removeAppEventListener(final SystemEventListener listener) {
 139         eventHandler.removeListener(listener);
 140     }
 141 
 142     /**
 143      * Installs a handler to show a custom About window for your application.
 144      *
 145      * Setting the {@link AboutHandler} to {@code null} reverts it to the default Cocoa About window.
 146      *
 147      * @param aboutHandler the handler to respond to the {@link AboutHandler#handleAbout()} message
 148      * @since Java for Mac OS X 10.6 Update 3
 149      * @since Java for Mac OS X 10.5 Update 8
 150      */
 151     public void setAboutHandler(final AboutHandler aboutHandler) {
 152         eventHandler.aboutDispatcher.setHandler(aboutHandler);
 153     }
 154 
 155     /**
 156      * Installs a handler to create the Preferences menu item in your application's app menu.
 157      *
 158      * Setting the {@link PreferencesHandler} to {@code null} will remove the Preferences item from the app menu.
 159      *
 160      * @param preferencesHandler
 161      * @since Java for Mac OS X 10.6 Update 3
 162      * @since Java for Mac OS X 10.5 Update 8
 163      */
 164     public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
 165         eventHandler.preferencesDispatcher.setHandler(preferencesHandler);
 166     }
 167 
 168     /**
 169      * Installs the handler which is notified when the application is asked to open a list of files.
 170      * The {@link OpenFilesHandler#openFiles(AppEvent.OpenFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.
 171      * 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.
 172      *
 173      * @param openFileHandler
 174      * @since Java for Mac OS X 10.6 Update 3
 175      * @since Java for Mac OS X 10.5 Update 8
 176      */
 177     public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
 178         eventHandler.openFilesDispatcher.setHandler(openFileHandler);
 179     }
 180 
 181     /**
 182      * Installs the handler which is notified when the application is asked to print a list of files.
 183      * The {@link PrintFilesHandler#printFiles(AppEvent.PrintFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.
 184      * 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.
 185      *
 186      * @param printFileHandler
 187      * @since Java for Mac OS X 10.6 Update 3
 188      * @since Java for Mac OS X 10.5 Update 8
 189      */
 190     public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
 191         eventHandler.printFilesDispatcher.setHandler(printFileHandler);
 192     }
 193 
 194     /**
 195      * Installs the handler which is notified when the application is asked to open a URL.
 196      * The {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleURLTypes} array present in it's Info.plist.
 197      * 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.
 198      *
 199      * Setting the handler to {@code null} causes all {@link OpenURIHandler#openURI(AppEvent.OpenURIEvent)} requests to be enqueued until another handler is set.
 200      *
 201      * @param openURIHandler
 202      * @since Java for Mac OS X 10.6 Update 3
 203      * @since Java for Mac OS X 10.5 Update 8
 204      */
 205     public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
 206         eventHandler.openURIDispatcher.setHandler(openURIHandler);
 207     }
 208 
 209     /**
 210      * Installs the handler which determines if the application should quit.
 211      * The handler is passed a one-shot {@link QuitResponse} which can cancel or proceed with the quit.
 212      * Setting the handler to {@code null} causes all quit requests to directly perform the default {@link QuitStrategy}.
 213      *
 214      * @param quitHandler the handler that is called when the application is asked to quit
 215      * @since Java for Mac OS X 10.6 Update 3
 216      * @since Java for Mac OS X 10.5 Update 8
 217      */
 218     public void setQuitHandler(final QuitHandler quitHandler) {
 219         eventHandler.quitDispatcher.setHandler(quitHandler);


   1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.eawt;
  27 
  28 import java.awt.Image;
  29 import java.awt.PopupMenu;
  30 import java.awt.Toolkit;
  31 import java.awt.Window;
  32 import java.awt.desktop.AboutHandler;
  33 import java.awt.desktop.AppForegroundListener;
  34 import java.awt.desktop.AppHiddenListener;
  35 import java.awt.desktop.AppReopenedListener;
  36 import java.awt.desktop.OpenFilesEvent;
  37 import java.awt.desktop.OpenFilesHandler;
  38 import java.awt.desktop.OpenURIEvent;
  39 import java.awt.desktop.OpenURIHandler;
  40 import java.awt.desktop.PreferencesHandler;
  41 import java.awt.desktop.PrintFilesEvent;
  42 import java.awt.desktop.PrintFilesHandler;
  43 import java.awt.desktop.QuitHandler;
  44 import java.awt.desktop.QuitResponse;
  45 import java.awt.desktop.QuitStrategy;
  46 import java.awt.desktop.ScreenSleepListener;
  47 import java.awt.desktop.SystemEventListener;
  48 import java.awt.desktop.SystemSleepListener;
  49 import java.awt.desktop.UserSessionListener;
  50 import java.beans.Beans;
  51 
  52 import javax.swing.JMenuBar;
  53 
  54 import sun.awt.AWTAccessor;
  55 import sun.lwawt.LWWindowPeer;
  56 import sun.lwawt.macosx.CPlatformWindow;
  57 
  58 /**
  59  * The {@code Application} class allows you to integrate your Java application with the native Mac OS X environment.
  60  * You can provide your Mac OS X users a greatly enhanced experience by implementing a few basic handlers for standard system events.
  61  *
  62  * For example:
  63  * <ul>
  64  * <li>Open an about dialog when a user chooses About from the application menu.</li>
  65  * <li>Open a preferences window when the users chooses Preferences from the application menu.</li>
  66  * <li>Create a new document when the user clicks on your Dock icon, and no windows are open.</li>
  67  * <li>Open a document that the user double-clicked on in the Finder.</li>
  68  * <li>Open a custom URL scheme when a user clicks on link in a web browser.</li>
  69  * <li>Reconnect to network services after the system has awoke from sleep.</li>


 108     final _AppEventHandler eventHandler = _AppEventHandler.getInstance();
 109     final _AppMenuBarHandler menuBarHandler = _AppMenuBarHandler.getInstance();
 110     final _AppDockIconHandler iconHandler = new _AppDockIconHandler();
 111 
 112     /**
 113      * Creates an Application instance. Should only be used in JavaBean environments.
 114      * @deprecated use {@link #getApplication()}
 115      *
 116      * @since 1.4
 117      */
 118     @Deprecated
 119     public Application() {
 120         checkSecurity();
 121     }
 122 
 123     /**
 124      * Adds sub-types of {@link SystemEventListener} to listen for notifications from the native Mac OS X system.
 125      *
 126      * @see AppForegroundListener
 127      * @see AppHiddenListener
 128      * @see AppReopenedListener
 129      * @see ScreenSleepListener
 130      * @see SystemSleepListener
 131      * @see UserSessionListener
 132      *
 133      * @param listener
 134      * @since Java for Mac OS X 10.6 Update 3
 135      * @since Java for Mac OS X 10.5 Update 8
 136      */
 137     public void addAppEventListener(final SystemEventListener listener) {
 138         eventHandler.addListener(listener);
 139     }
 140 
 141     /**
 142      * Removes sub-types of {@link SystemEventListener} from listening for notifications from the native Mac OS X system.
 143      *
 144      * @see AppForegroundListener
 145      * @see AppHiddenListener
 146      * @see AppReopenedListener
 147      * @see ScreenSleepListener
 148      * @see SystemSleepListener
 149      * @see UserSessionListener
 150      *
 151      * @param listener
 152      * @since Java for Mac OS X 10.6 Update 3
 153      * @since Java for Mac OS X 10.5 Update 8
 154      */
 155     public void removeAppEventListener(final SystemEventListener listener) {
 156         eventHandler.removeListener(listener);
 157     }
 158 
 159     /**
 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} message
 165      * @since Java for Mac OS X 10.6 Update 3
 166      * @since Java for Mac OS X 10.5 Update 8
 167      */
 168     public void setAboutHandler(final AboutHandler aboutHandler) {
 169         eventHandler.aboutDispatcher.setHandler(aboutHandler);
 170     }
 171 
 172     /**
 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 preferencesHandler
 178      * @since Java for Mac OS X 10.6 Update 3
 179      * @since Java for Mac OS X 10.5 Update 8
 180      */
 181     public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
 182         eventHandler.preferencesDispatcher.setHandler(preferencesHandler);
 183     }
 184 
 185     /**
 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 openFileHandler
 191      * @since Java for Mac OS X 10.6 Update 3
 192      * @since Java for Mac OS X 10.5 Update 8
 193      */
 194     public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
 195         eventHandler.openFilesDispatcher.setHandler(openFileHandler);
 196     }
 197 
 198     /**
 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 printFileHandler
 204      * @since Java for Mac OS X 10.6 Update 3
 205      * @since Java for Mac OS X 10.5 Update 8
 206      */
 207     public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
 208         eventHandler.printFilesDispatcher.setHandler(printFileHandler);
 209     }
 210 
 211     /**
 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 openURIHandler
 219      * @since Java for Mac OS X 10.6 Update 3
 220      * @since Java for Mac OS X 10.5 Update 8
 221      */
 222     public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
 223         eventHandler.openURIDispatcher.setHandler(openURIHandler);
 224     }
 225 
 226     /**
 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 quit
 232      * @since Java for Mac OS X 10.6 Update 3
 233      * @since Java for Mac OS X 10.5 Update 8
 234      */
 235     public void setQuitHandler(final QuitHandler quitHandler) {
 236         eventHandler.quitDispatcher.setHandler(quitHandler);


< prev index next >