0N/A/*
2362N/A * Copyright (c) 2003, 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/A
0N/Apackage sun.awt;
0N/A
0N/Aimport java.lang.reflect.Array;
0N/Aimport java.util.EventListener;
0N/A
0N/A
0N/A/**
0N/A * A class that assists in managing {@link java.util.EventListener}s of
0N/A * the specified type. Its instance holds an array of listeners of the same
0N/A * type and allows to perform the typical operations on the listeners.
0N/A * This class is thread-safe.
0N/A *
0N/A * @author Alexander Gerasimov
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic class EventListenerAggregate {
0N/A
0N/A private EventListener[] listenerList;
0N/A
0N/A /**
0N/A * Constructs an <code>EventListenerAggregate</code> object.
0N/A *
0N/A * @param listenerClass the type of the listeners to be managed by this object
0N/A *
0N/A * @throws NullPointerException if <code>listenerClass</code> is
0N/A * <code>null</code>
0N/A * @throws ClassCastException if <code>listenerClass</code> is not
0N/A * assignable to <code>java.util.EventListener</code>
0N/A */
0N/A public EventListenerAggregate(Class listenerClass) {
0N/A if (listenerClass == null) {
0N/A throw new NullPointerException("listener class is null");
0N/A }
0N/A
0N/A if (!EventListener.class.isAssignableFrom(listenerClass)) {
0N/A throw new ClassCastException("listener class " + listenerClass +
0N/A " is not assignable to EventListener");
0N/A }
0N/A
0N/A listenerList = (EventListener[])Array.newInstance(listenerClass, 0);
0N/A }
0N/A
0N/A private Class getListenerClass() {
0N/A return listenerList.getClass().getComponentType();
0N/A }
0N/A
0N/A /**
0N/A * Adds the listener to this aggregate.
0N/A *
0N/A * @param listener the listener to be added
0N/A *
0N/A * @throws ClassCastException if <code>listener</code> is not
0N/A * an instatce of <code>listenerClass</code> specified
0N/A * in the constructor
0N/A */
0N/A public synchronized void add(EventListener listener) {
0N/A Class listenerClass = getListenerClass();
0N/A
0N/A if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
0N/A throw new ClassCastException("listener " + listener + " is not " +
0N/A "an instance of listener class " + listenerClass);
0N/A }
0N/A
0N/A EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
0N/A System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
0N/A tmp[listenerList.length] = listener;
0N/A listenerList = tmp;
0N/A }
0N/A
0N/A /**
0N/A * Removes a listener that is equal to the given one from this aggregate.
0N/A * <code>equals()</code> method is used to compare listeners.
0N/A *
0N/A * @param listener the listener to be removed
0N/A *
0N/A * @return <code>true</code> if this aggregate contained the specified
0N/A * <code>listener</code>; <code>false</code> otherwise
0N/A *
0N/A * @throws ClassCastException if <code>listener</code> is not
0N/A * an instatce of <code>listenerClass</code> specified
0N/A * in the constructor
0N/A */
0N/A public synchronized boolean remove(EventListener listener) {
0N/A Class listenerClass = getListenerClass();
0N/A
0N/A if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
0N/A throw new ClassCastException("listener " + listener + " is not " +
0N/A "an instance of listener class " + listenerClass);
0N/A }
0N/A
0N/A for (int i = 0; i < listenerList.length; i++) {
0N/A if (listenerList[i].equals(listener)) {
0N/A EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
0N/A listenerList.length - 1);
0N/A System.arraycopy(listenerList, 0, tmp, 0, i);
0N/A System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
0N/A listenerList = tmp;
0N/A
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the listeners contained in this aggregate.
0N/A * The array is the data structure in which listeners are stored internally.
0N/A * The runtime type of the returned array is "array of <code>listenerClass</code>"
0N/A * (<code>listenerClass</code> has been specified as a parameter to
0N/A * the constructor of this class).
0N/A *
0N/A * @return all the listeners contained in this aggregate (an empty
0N/A * array if there are no listeners)
0N/A */
0N/A public synchronized EventListener[] getListenersInternal() {
0N/A return listenerList;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the listeners contained in this aggregate.
0N/A * The array is a copy of the data structure in which listeners are stored
0N/A * internally.
0N/A * The runtime type of the returned array is "array of <code>listenerClass</code>"
0N/A * (<code>listenerClass</code> has been specified as a parameter to
0N/A * the constructor of this class).
0N/A *
0N/A * @return a copy of all the listeners contained in this aggregate (an empty
0N/A * array if there are no listeners)
0N/A */
0N/A public synchronized EventListener[] getListenersCopy() {
0N/A return (listenerList.length == 0) ? listenerList : (EventListener[])listenerList.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of lisetners in this aggregate.
0N/A *
0N/A * @return the number of lisetners in this aggregate
0N/A */
0N/A public synchronized int size() {
0N/A return listenerList.length;
0N/A }
0N/A
0N/A /**
0N/A * Returns <code>true</code> if this aggregate contains no listeners,
0N/A * <code>false</code> otherwise.
0N/A *
0N/A * @return <code>true</code> if this aggregate contains no listeners,
0N/A * <code>false</code> otherwise
0N/A */
0N/A public synchronized boolean isEmpty() {
0N/A return listenerList.length == 0;
0N/A }
0N/A}