1631N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1631N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1631N/A *
1631N/A * This code is free software; you can redistribute it and/or modify it
1631N/A * under the terms of the GNU General Public License version 2 only, as
1631N/A * published by the Free Software Foundation.
1631N/A *
1631N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1631N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1631N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1631N/A * version 2 for more details (a copy is included in the LICENSE file that
1631N/A * accompanied this code).
1631N/A *
1631N/A * You should have received a copy of the GNU General Public License version
1631N/A * 2 along with this work; if not, write to the Free Software Foundation,
1631N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1631N/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.
1631N/A */
1631N/A
1631N/A/* @test
1631N/A * @bug 6872503
1631N/A * @summary Checks that JLayer correctly works with its AWTEventListener
1631N/A * @author Alexander Potochkin
1631N/A */
1631N/A
1631N/Aimport javax.swing.*;
1631N/Aimport java.awt.*;
1631N/Aimport java.awt.event.AWTEventListener;
1631N/Aimport java.awt.event.AWTEventListenerProxy;
1631N/A
1631N/Apublic class bug6872503 {
1631N/A
1631N/A static JLayer<Component> l1;
1631N/A static JLayer<Component> l2;
1631N/A
1631N/A private static void createGui() {
1631N/A Toolkit toolkit = Toolkit.getDefaultToolkit();
1631N/A int length = toolkit.getAWTEventListeners().length;
1631N/A
1631N/A l1 = new JLayer<Component>();
1631N/A l1.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
1631N/A
1631N/A l2 = new JLayer<Component>();
1631N/A l2.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
1631N/A
1631N/A if (isLayerEventControllerAdded()) {
1631N/A throw new RuntimeException("Unexpected AWTEventListener was added");
1631N/A }
1631N/A
1631N/A JFrame frame = new JFrame();
1631N/A frame.setLayout(new FlowLayout());
1631N/A frame.add(l1);
1631N/A frame.add(l2);
1631N/A
1631N/A if (isLayerEventControllerAdded()) {
1631N/A throw new RuntimeException("Unexpected AWTEventListener was added");
1631N/A }
1631N/A
1631N/A frame.pack();
1631N/A
1631N/A if (!isLayerEventControllerAdded()) {
1631N/A throw new RuntimeException("AWTEventListener was not added");
1631N/A }
1631N/A
1631N/A if (!layerEventControllerMaskEquals(l1.getLayerEventMask() | l2.getLayerEventMask())) {
1631N/A throw new RuntimeException("Wrong mask for AWTEventListener");
1631N/A }
1631N/A
1631N/A frame.dispose();
1631N/A
1631N/A if (isLayerEventControllerAdded()) {
1631N/A throw new RuntimeException("Unexpected AWTEventListener was added");
1631N/A }
1631N/A }
1631N/A
1631N/A static boolean isLayerEventControllerAdded() {
1631N/A Toolkit toolkit = Toolkit.getDefaultToolkit();
1631N/A AWTEventListener layerEventController = null;
1631N/A for (AWTEventListener listener : toolkit.getAWTEventListeners()) {
1631N/A if (listener instanceof AWTEventListenerProxy) {
1631N/A listener = ((AWTEventListenerProxy)listener).getListener();
1631N/A
1631N/A }
1631N/A if ("LayerEventController".equals(listener.getClass().getSimpleName())) {
1631N/A if (layerEventController != null) {
1631N/A throw new RuntimeException("Duplicated LayerEventController");
1631N/A }
1631N/A layerEventController = listener;
1631N/A }
1631N/A }
1631N/A boolean ret = layerEventController != null;
1631N/A if (ret) {
1631N/A System.out.println("LayerEventController found");
1631N/A } else {
1631N/A System.out.println("No LayerEventController");
1631N/A }
1631N/A return ret;
1631N/A }
1631N/A
1631N/A static boolean layerEventControllerMaskEquals(long mask) {
1631N/A Toolkit toolkit = Toolkit.getDefaultToolkit();
1631N/A AWTEventListener layerEventController = null;
1631N/A for (AWTEventListener listener : toolkit.getAWTEventListeners(mask)) {
1631N/A if (listener instanceof AWTEventListenerProxy) {
1631N/A listener = ((AWTEventListenerProxy)listener).getListener();
1631N/A
1631N/A }
1631N/A if ("LayerEventController".equals(listener.getClass().getSimpleName())) {
1631N/A if (layerEventController != null) {
1631N/A throw new RuntimeException("Duplicated LayerEventController");
1631N/A }
1631N/A layerEventController = listener;
1631N/A }
1631N/A }
1631N/A boolean ret = layerEventController != null;
1631N/A if (ret) {
1631N/A System.out.println("LayerEventController with the correct mask found");
1631N/A } else {
1631N/A System.out.println("No LayerEventController with the correct mask");
1631N/A }
1631N/A return ret;
1631N/A }
1631N/A
1631N/A public static void main(String[] args) throws Exception {
1631N/A SwingUtilities.invokeAndWait(new Runnable() {
1631N/A public void run() {
1631N/A bug6872503.createGui();
1631N/A }
1631N/A });
1631N/A }
1631N/A}