Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/eawt/_AppEventLegacyHandler.java
38831 views
/*1* Copyright (c) 2011, 2013, 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.Toolkit;28import java.io.File;29import java.util.*;3031import com.apple.eawt.AppEvent.*;3233@SuppressWarnings("deprecation")34class _AppEventLegacyHandler implements AboutHandler, PreferencesHandler, _OpenAppHandler, AppReOpenedListener, OpenFilesHandler, PrintFilesHandler, QuitHandler {35final _AppEventHandler parent;36final Vector<ApplicationListener> legacyAppListeners = new Vector<ApplicationListener>();37boolean blockLegacyAPI;38boolean initializedParentDispatchers;3940_AppEventLegacyHandler(final _AppEventHandler parent) {41this.parent = parent;42}4344void blockLegacyAPI() {45blockLegacyAPI = true;46}4748void checkIfLegacyAPIBlocked() {49if (!blockLegacyAPI) return;50throw new IllegalStateException("Cannot add com.apple.eawt.ApplicationListener after installing an app event handler");51}5253void addLegacyAppListener(final ApplicationListener listener) {54checkIfLegacyAPIBlocked();5556if (!initializedParentDispatchers) {57final _AppMenuBarHandler menuBarHandler = Application.getApplication().menuBarHandler;58final boolean prefsMenuAlreadyExplicitlySet = menuBarHandler.prefsMenuItemExplicitlySet;5960parent.aboutDispatcher.setHandler(this);61parent.preferencesDispatcher.setHandler(this);62if (!prefsMenuAlreadyExplicitlySet) {63menuBarHandler.setPreferencesMenuItemVisible(false); // default behavior is not to have a preferences item64}65parent.openAppDispatcher.setHandler(this);66parent.reOpenAppDispatcher.addListener(this);67parent.openFilesDispatcher.setHandler(this);68parent.printFilesDispatcher.setHandler(this);69parent.quitDispatcher.setHandler(this);7071initializedParentDispatchers = true;72}7374synchronized (legacyAppListeners) {75legacyAppListeners.addElement(listener);76}77}7879public void removeLegacyAppListener(final ApplicationListener listener) {80checkIfLegacyAPIBlocked();8182synchronized (legacyAppListeners) {83legacyAppListeners.removeElement(listener);84}85}8687@Override88public void handleAbout(final AboutEvent e) {89final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit());90sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {91public void dispatchEvent(final ApplicationListener listener) {92listener.handleAbout(ae);93}94});9596if (ae.isHandled()) return;97parent.openCocoaAboutWindow();98}99100@Override101public void handlePreferences(final PreferencesEvent e) {102final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit());103sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {104public void dispatchEvent(final ApplicationListener listener) {105listener.handlePreferences(ae);106}107});108}109110@Override111public void handleOpenApp() {112final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit());113sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {114public void dispatchEvent(final ApplicationListener listener) {115listener.handleOpenApplication(ae);116}117});118}119120@Override121public void appReOpened(final AppReOpenedEvent e) {122final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit());123sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {124public void dispatchEvent(final ApplicationListener listener) {125listener.handleReOpenApplication(ae);126}127});128}129130@Override131public void openFiles(final OpenFilesEvent e) {132final List<File> files = e.getFiles();133for (final File file : files) { // legacy ApplicationListeners only understood one file at a time134final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit(), file.getAbsolutePath());135sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {136public void dispatchEvent(final ApplicationListener listener) {137listener.handleOpenFile(ae);138}139});140}141}142143@Override144public void printFiles(PrintFilesEvent e) {145final List<File> files = e.getFiles();146for (final File file : files) { // legacy ApplicationListeners only understood one file at a time147final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit(), file.getAbsolutePath());148sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {149public void dispatchEvent(final ApplicationListener listener) {150listener.handlePrintFile(ae);151}152});153}154}155156@Override157public void handleQuitRequestWith(final QuitEvent e, final QuitResponse response) {158final ApplicationEvent ae = new ApplicationEvent(Toolkit.getDefaultToolkit());159sendEventToEachListenerUntilHandled(ae, new EventDispatcher() {160public void dispatchEvent(final ApplicationListener listener) {161listener.handleQuit(ae);162}163});164165if (ae.isHandled()) {166parent.performQuit();167} else {168parent.cancelQuit();169}170}171172interface EventDispatcher {173void dispatchEvent(final ApplicationListener listener);174}175176// helper that cycles through the loop and aborts if the event is handled, or there are no listeners177void sendEventToEachListenerUntilHandled(final ApplicationEvent event, final EventDispatcher dispatcher) {178synchronized (legacyAppListeners) {179if (legacyAppListeners.size() == 0) return;180181final Enumeration<ApplicationListener> e = legacyAppListeners.elements();182while (e.hasMoreElements() && !event.isHandled()) {183dispatcher.dispatchEvent(e.nextElement());184}185}186}187}188189190