< prev index next >

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

Print this page




   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.Point;
  30 import java.awt.PopupMenu;
  31 import java.awt.Toolkit;
  32 import java.awt.Window;

  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</code> 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>


  87         return sApplication;
  88     }
  89 
  90     // some singletons, since they get called back into from native
  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 AppEventListener} to listen for notifications from the native Mac OS X system.
 108      *
 109      * @see AppForegroundListener
 110      * @see AppHiddenListener
 111      * @see AppReOpenedListener
 112      * @see ScreenSleepListener
 113      * @see SystemSleepListener
 114      * @see UserSessionListener
 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 AppEventListener listener) {
 121         eventHandler.addListener(listener);
 122     }
 123 
 124     /**
 125      * Removes sub-types of {@link AppEventListener} from listening for notifications from the native Mac OS X system.
 126      *
 127      * @see AppForegroundListener
 128      * @see AppHiddenListener
 129      * @see AppReOpenedListener
 130      * @see ScreenSleepListener
 131      * @see SystemSleepListener
 132      * @see UserSessionListener
 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 AppEventListener 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</code> 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</code> will remove the Preferences item from the app menu.


 351      * @since Java for Mac OS X 10.5 - 1.5
 352      * @since Java for Mac OS X 10.5 Update 1 - 1.6
 353      */
 354     public Image getDockIconImage() {
 355         return iconHandler.getDockIconImage();
 356     }
 357 
 358     /**
 359      * Affixes a small system provided badge to this application's Dock icon. Usually a number.
 360      *
 361      * @param badge textual label to affix to the Dock icon
 362      *
 363      * @since Java for Mac OS X 10.5 - 1.5
 364      * @since Java for Mac OS X 10.5 Update 1 - 1.6
 365      */
 366     public void setDockIconBadge(final String badge) {
 367         iconHandler.setDockIconBadge(badge);
 368     }
 369 
 370     /**











 371      * Sets the default menu bar to use when there are no active frames.
 372      * Only used when the system property "apple.laf.useScreenMenuBar" is "true", and
 373      * the Aqua Look and Feel is active.
 374      *
 375      * @param menuBar to use when no other frames are active
 376      *
 377      * @since Java for Mac OS X 10.6 Update 1
 378      * @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
 379      */
 380     public void setDefaultMenuBar(final JMenuBar menuBar) {
 381         menuBarHandler.setDefaultMenuBar(menuBar);
 382     }
 383 
 384     /**
 385      * Requests that a {@link Window} should animate into or out of full screen mode.
 386      * Only {@link Window}s marked as full screenable by {@link FullScreenUtilities#setWindowCanFullScreen(Window, boolean)} can be toggled.
 387      *
 388      * @param window to animate into or out of full screen mode
 389      *
 390      * @since Java for Mac OS X 10.7 Update 1
 391      */
 392     public void requestToggleFullScreen(final Window window) {
 393         final Object peer = AWTAccessor.getComponentAccessor().getPeer(window);
 394         if (!(peer instanceof LWWindowPeer)) return;
 395         Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();
 396         if (!(platformWindow instanceof CPlatformWindow)) return;
 397         ((CPlatformWindow)platformWindow).toggleFullScreen();
 398     }
 399 
 400 
 401     // -- DEPRECATED API --
 402 
 403     /**
 404      * Adds the specified ApplicationListener as a receiver of callbacks from this class.
 405      * This method throws a RuntimeException if the newer About, Preferences, Quit, etc handlers are installed.
 406      *
 407      * @param listener an implementation of ApplicationListener that handles ApplicationEvents
 408      *
 409      * @deprecated register individual handlers for each task (About, Preferences, Open, Print, Quit, etc)
 410      * @since 1.4
 411      */
 412     @SuppressWarnings("deprecation")
 413     @Deprecated
 414     public void addApplicationListener(final ApplicationListener listener) {
 415         eventHandler.legacyHandler.addLegacyAppListener(listener);
 416     }
 417 
 418     /**
 419      * Removes the specified ApplicationListener from being a receiver of callbacks from this class.
 420      * This method throws a RuntimeException if the newer About, Preferences, Quit, etc handlers are installed.
 421      *
 422      * @param listener an implementation of ApplicationListener that had previously been registered to handle ApplicationEvents
 423      *
 424      * @deprecated unregister individual handlers for each task (About, Preferences, Open, Print, Quit, etc)
 425      * @since 1.4
 426      */
 427     @SuppressWarnings("deprecation")
 428     @Deprecated
 429     public void removeApplicationListener(final ApplicationListener listener) {
 430         eventHandler.legacyHandler.removeLegacyAppListener(listener);
 431     }
 432 
 433     /**
 434      * Enables the Preferences item in the application menu. The ApplicationListener receives a callback for
 435      * selection of the Preferences item in the application menu only if this is set to <code>true</code>.
 436      *
 437      * If a Preferences item isn't present, this method adds and enables it.
 438      *
 439      * @param enable specifies whether the Preferences item in the application menu should be enabled (<code>true</code>) or not (<code>false</code>)
 440      *
 441      * @deprecated no replacement
 442      * @since 1.4
 443      */
 444     @Deprecated
 445     public void setEnabledPreferencesMenu(final boolean enable) {
 446         menuBarHandler.setPreferencesMenuItemVisible(true);
 447         menuBarHandler.setPreferencesMenuItemEnabled(enable);
 448     }
 449 
 450     /**
 451      * Enables the About item in the application menu. The ApplicationListener receives a callback for
 452      * selection of the About item in the application menu only if this is set to <code>true</code>. Because AWT supplies
 453      * a standard About window when an application may not, by default this is set to <code>true</code>.
 454      *
 455      * If the About item isn't present, this method adds and enables it.
 456      *
 457      * @param enable specifies whether the About item in the application menu should be enabled (<code>true</code>) or not (<code>false</code>)
 458      *
 459      * @deprecated no replacement
 460      * @since 1.4
 461      */
 462     @Deprecated
 463     public void setEnabledAboutMenu(final boolean enable) {
 464         menuBarHandler.setAboutMenuItemEnabled(enable);
 465     }
 466 
 467     /**
 468      * Determines if the Preferences item of the application menu is enabled.
 469      *
 470      * @deprecated no replacement
 471      * @since 1.4
 472      */
 473     @Deprecated
 474     public boolean getEnabledPreferencesMenu() {
 475         return menuBarHandler.isPreferencesMenuItemEnabled();
 476     }
 477 
 478     /**
 479      * Determines if the About item of the application menu is enabled.
 480      *
 481      * @deprecated no replacement
 482      * @since 1.4
 483      */
 484     @Deprecated
 485     public boolean getEnabledAboutMenu() {
 486         return menuBarHandler.isAboutMenuItemEnabled();
 487     }
 488 
 489     /**
 490      * Determines if the About item of the application menu is present.
 491      *
 492      * @deprecated no replacement
 493      * @since 1.4
 494      */
 495     @Deprecated
 496     public boolean isAboutMenuItemPresent() {
 497         return menuBarHandler.isAboutMenuItemVisible();
 498     }
 499 
 500     /**
 501      * Adds the About item to the application menu if the item is not already present.
 502      *
 503      * @deprecated use {@link #setAboutHandler(AboutHandler)} with a non-null {@link AboutHandler} parameter
 504      * @since 1.4
 505      */
 506     @Deprecated
 507     public void addAboutMenuItem() {
 508         menuBarHandler.setAboutMenuItemVisible(true);
 509     }
 510 
 511     /**
 512      * Removes the About item from the application menu if  the item is present.
 513      *
 514      * @deprecated use {@link #setAboutHandler(AboutHandler)} with a null parameter
 515      * @since 1.4
 516      */
 517     @Deprecated
 518     public void removeAboutMenuItem() {
 519         menuBarHandler.setAboutMenuItemVisible(false);
 520     }
 521 
 522     /**
 523      * Determines if the About Preferences of the application menu is present. By default there is no Preferences menu item.
 524      *
 525      * @deprecated no replacement
 526      * @since 1.4
 527      */
 528     @Deprecated
 529     public boolean isPreferencesMenuItemPresent() {
 530         return menuBarHandler.isPreferencesMenuItemVisible();
 531     }
 532 
 533     /**
 534      * Adds the Preferences item to the application menu if the item is not already present.
 535      *
 536      * @deprecated use {@link #setPreferencesHandler(PreferencesHandler)} with a non-null {@link PreferencesHandler} parameter
 537      * @since 1.4
 538      */
 539     @Deprecated
 540     public void addPreferencesMenuItem() {
 541         menuBarHandler.setPreferencesMenuItemVisible(true);
 542     }
 543 
 544     /**
 545      * Removes the Preferences item from the application menu if that item is present.
 546      *
 547      * @deprecated use {@link #setPreferencesHandler(PreferencesHandler)} with a null parameter
 548      * @since 1.4
 549      */
 550     @Deprecated
 551     public void removePreferencesMenuItem() {
 552         menuBarHandler.setPreferencesMenuItemVisible(false);
 553     }
 554 
 555     /**
 556      * @deprecated Use <code>java.awt.MouseInfo.getPointerInfo().getLocation()</code>.
 557      *
 558      * @since 1.4
 559      */
 560     @Deprecated
 561     public static Point getMouseLocationOnScreen() {
 562         return java.awt.MouseInfo.getPointerInfo().getLocation();
 563     }
 564 }


   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</code> 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>


  87         return sApplication;
  88     }
  89 
  90     // some singletons, since they get called back into from native
  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</code> 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</code> will remove the Preferences item from the app menu.


 351      * @since Java for Mac OS X 10.5 - 1.5
 352      * @since Java for Mac OS X 10.5 Update 1 - 1.6
 353      */
 354     public Image getDockIconImage() {
 355         return iconHandler.getDockIconImage();
 356     }
 357 
 358     /**
 359      * Affixes a small system provided badge to this application's Dock icon. Usually a number.
 360      *
 361      * @param badge textual label to affix to the Dock icon
 362      *
 363      * @since Java for Mac OS X 10.5 - 1.5
 364      * @since Java for Mac OS X 10.5 Update 1 - 1.6
 365      */
 366     public void setDockIconBadge(final String badge) {
 367         iconHandler.setDockIconBadge(badge);
 368     }
 369     
 370     /**
 371      * Displays a progress bar to this application's Dock icon.
 372      * Acceptable values are from 0 to 100, any other disables progress indication.
 373      *
 374      * @param value progress value
 375      * @since 1.9
 376      */
 377     public void setDockIconProgress(final int value) {
 378         iconHandler.setDockIconProgress(value);
 379     }
 380 
 381     /**
 382      * Sets the default menu bar to use when there are no active frames.
 383      * Only used when the system property "apple.laf.useScreenMenuBar" is "true", and
 384      * the Aqua Look and Feel is active.
 385      *
 386      * @param menuBar to use when no other frames are active
 387      *
 388      * @since Java for Mac OS X 10.6 Update 1
 389      * @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
 390      */
 391     public void setDefaultMenuBar(final JMenuBar menuBar) {
 392         menuBarHandler.setDefaultMenuBar(menuBar);
 393     }
 394 
 395     /**
 396      * Requests that a {@link Window} should animate into or out of full screen mode.
 397      * Only {@link Window}s marked as full screenable by {@link FullScreenUtilities#setWindowCanFullScreen(Window, boolean)} can be toggled.
 398      *
 399      * @param window to animate into or out of full screen mode
 400      *
 401      * @since Java for Mac OS X 10.7 Update 1
 402      */
 403     public void requestToggleFullScreen(final Window window) {
 404         final Object peer = AWTAccessor.getComponentAccessor().getPeer(window);
 405         if (!(peer instanceof LWWindowPeer)) return;
 406         Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();
 407         if (!(platformWindow instanceof CPlatformWindow)) return;
 408         ((CPlatformWindow)platformWindow).toggleFullScreen();
 409     }
 410 




































































































































































 411 }
< prev index next >