0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.event.*;
0N/A
0N/Aimport sun.awt.AppContext;
0N/A
0N/Aabstract class ModalEventFilter implements EventFilter {
0N/A
0N/A protected Dialog modalDialog;
0N/A protected boolean disabled;
0N/A
0N/A protected ModalEventFilter(Dialog modalDialog) {
0N/A this.modalDialog = modalDialog;
0N/A disabled = false;
0N/A }
0N/A
0N/A Dialog getModalDialog() {
0N/A return modalDialog;
0N/A }
0N/A
0N/A public FilterAction acceptEvent(AWTEvent event) {
0N/A if (disabled || !modalDialog.isVisible()) {
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A int eventID = event.getID();
0N/A if ((eventID >= MouseEvent.MOUSE_FIRST &&
0N/A eventID <= MouseEvent.MOUSE_LAST) ||
0N/A (eventID >= ActionEvent.ACTION_FIRST &&
0N/A eventID <= ActionEvent.ACTION_LAST) ||
0N/A eventID == WindowEvent.WINDOW_CLOSING)
0N/A {
0N/A Object o = event.getSource();
0N/A if (o instanceof sun.awt.ModalExclude) {
0N/A // Exclude this object from modality and
0N/A // continue to pump it's events.
0N/A } else if (o instanceof Component) {
0N/A Component c = (Component)o;
0N/A while ((c != null) && !(c instanceof Window)) {
0N/A c = c.getParent_NoClientCode();
0N/A }
0N/A if (c != null) {
0N/A return acceptWindow((Window)c);
0N/A }
0N/A }
0N/A }
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A
0N/A protected abstract FilterAction acceptWindow(Window w);
0N/A
0N/A // When a modal dialog is hidden its modal filter may not be deleted from
0N/A // EventDispatchThread event filters immediately, so we need to mark the filter
0N/A // as disabled to prevent it from working. Simple checking for visibility of
0N/A // the modalDialog is not enough, as it can be hidden and then shown again
0N/A // with a new event pump and a new filter
0N/A void disable() {
0N/A disabled = true;
0N/A }
0N/A
0N/A int compareTo(ModalEventFilter another) {
0N/A Dialog anotherDialog = another.getModalDialog();
0N/A // check if modalDialog is from anotherDialog's hierarchy
0N/A // or vice versa
0N/A Component c = modalDialog;
0N/A while (c != null) {
0N/A if (c == anotherDialog) {
0N/A return 1;
0N/A }
0N/A c = c.getParent_NoClientCode();
0N/A }
0N/A c = anotherDialog;
0N/A while (c != null) {
0N/A if (c == modalDialog) {
0N/A return -1;
0N/A }
0N/A c = c.getParent_NoClientCode();
0N/A }
0N/A // check if one dialog blocks (directly or indirectly) another
0N/A Dialog blocker = modalDialog.getModalBlocker();
0N/A while (blocker != null) {
0N/A if (blocker == anotherDialog) {
0N/A return -1;
0N/A }
0N/A blocker = blocker.getModalBlocker();
0N/A }
0N/A blocker = anotherDialog.getModalBlocker();
0N/A while (blocker != null) {
0N/A if (blocker == modalDialog) {
0N/A return 1;
0N/A }
0N/A blocker = blocker.getModalBlocker();
0N/A }
0N/A // compare modality types
0N/A return modalDialog.getModalityType().compareTo(anotherDialog.getModalityType());
0N/A }
0N/A
0N/A static ModalEventFilter createFilterForDialog(Dialog modalDialog) {
0N/A switch (modalDialog.getModalityType()) {
0N/A case DOCUMENT_MODAL: return new DocumentModalEventFilter(modalDialog);
0N/A case APPLICATION_MODAL: return new ApplicationModalEventFilter(modalDialog);
0N/A case TOOLKIT_MODAL: return new ToolkitModalEventFilter(modalDialog);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private static class ToolkitModalEventFilter extends ModalEventFilter {
0N/A
0N/A private AppContext appContext;
0N/A
0N/A ToolkitModalEventFilter(Dialog modalDialog) {
0N/A super(modalDialog);
0N/A appContext = modalDialog.appContext;
0N/A }
0N/A
0N/A protected FilterAction acceptWindow(Window w) {
0N/A if (w.isModalExcluded(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE)) {
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A if (w.appContext != appContext) {
0N/A return FilterAction.REJECT;
0N/A }
0N/A while (w != null) {
0N/A if (w == modalDialog) {
0N/A return FilterAction.ACCEPT_IMMEDIATELY;
0N/A }
0N/A w = w.getOwner();
0N/A }
0N/A return FilterAction.REJECT;
0N/A }
0N/A }
0N/A
0N/A private static class ApplicationModalEventFilter extends ModalEventFilter {
0N/A
0N/A private AppContext appContext;
0N/A
0N/A ApplicationModalEventFilter(Dialog modalDialog) {
0N/A super(modalDialog);
0N/A appContext = modalDialog.appContext;
0N/A }
0N/A
0N/A protected FilterAction acceptWindow(Window w) {
0N/A if (w.isModalExcluded(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)) {
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A if (w.appContext == appContext) {
0N/A while (w != null) {
0N/A if (w == modalDialog) {
0N/A return FilterAction.ACCEPT_IMMEDIATELY;
0N/A }
0N/A w = w.getOwner();
0N/A }
0N/A return FilterAction.REJECT;
0N/A }
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A }
0N/A
0N/A private static class DocumentModalEventFilter extends ModalEventFilter {
0N/A
0N/A private Window documentRoot;
0N/A
0N/A DocumentModalEventFilter(Dialog modalDialog) {
0N/A super(modalDialog);
0N/A documentRoot = modalDialog.getDocumentRoot();
0N/A }
0N/A
0N/A protected FilterAction acceptWindow(Window w) {
0N/A // application- and toolkit-excluded windows are blocked by
0N/A // document-modal dialogs from their child hierarchy
0N/A if (w.isModalExcluded(Dialog.ModalExclusionType.APPLICATION_EXCLUDE)) {
0N/A Window w1 = modalDialog.getOwner();
0N/A while (w1 != null) {
0N/A if (w1 == w) {
0N/A return FilterAction.REJECT;
0N/A }
0N/A w1 = w1.getOwner();
0N/A }
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A while (w != null) {
0N/A if (w == modalDialog) {
0N/A return FilterAction.ACCEPT_IMMEDIATELY;
0N/A }
0N/A if (w == documentRoot) {
0N/A return FilterAction.REJECT;
0N/A }
0N/A w = w.getOwner();
0N/A }
0N/A return FilterAction.ACCEPT;
0N/A }
0N/A }
0N/A}