001/*
002 * $Id: EventListenerMap.java 3189 2009-01-20 17:46:04Z kschaefe $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.event;
022
023import java.util.ArrayList;
024import java.util.EventListener;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Intended to be a replacement for {@link javax.swing.event.EventListenerList}.
031 * 
032 * @author Joshua Outwater
033 * @author Karl Schaefer
034 * @see javax.swing.event.EventListenerList
035 */
036public class EventListenerMap {
037    private final Map<Class<? extends EventListener>, List<? extends EventListener>> listenerList =
038            new HashMap<Class<? extends EventListener>, List<? extends EventListener>>();
039
040    /**
041     * Returns a list containing all of the listeners managed by this {@code EventListenerMap}.
042     * 
043     * @return all managed listeners
044     */
045    public List<EventListener> getListeners() {
046        List<EventListener> listeners = new ArrayList<EventListener>();
047        
048        for (List<? extends EventListener> list : listenerList.values()) {
049            listeners.addAll(list);
050        }
051
052        return listeners;
053    }
054
055    /**
056     * Return a list of all the listeners of the given type.
057     * 
058     * @return all of the listeners of the specified type.
059     */
060    @SuppressWarnings("unchecked")
061    public <T extends EventListener> List<T> getListeners(Class<T> clazz) {
062        List<T> list = (List<T>) listenerList.get(clazz);
063        if (list == null) {
064            list = new ArrayList<T>();
065        }
066        return list;
067    }
068
069    /**
070     * Returns the total number of listeners of the supplied type 
071     * for this listener list.
072     */
073    public int getListenerCount() {
074        int count = 0;
075        
076        for (List<? extends EventListener> list : listenerList.values()) {
077            count += list.size();
078        }
079        
080        return count;
081    }
082
083    /**
084     * Returns the total number of listeners for this listener type.
085     */
086    @SuppressWarnings("unchecked")
087    public <T extends EventListener> int getListenerCount(Class<T> clazz) {
088        List<T> list = (List<T>) listenerList.get(clazz);
089        if (list != null) {
090            return list.size();
091        }
092        return 0;
093    }
094
095    /**
096     * Adds the listener as a listener of the specified type.
097     * 
098     * @param <T>
099     *            the type of the listener to be added
100     * @param clazz
101     *            the class type to add
102     * @param l
103     *            the listener to be added
104     */
105    @SuppressWarnings("unchecked")
106    public synchronized <T extends EventListener> void add(Class<T> clazz, T listener) {
107        if (listener == null) {
108            return;
109        }
110
111        List<T> list = (List<T>) listenerList.get(clazz);
112        if (list == null) {
113            list = new ArrayList<T>();
114            listenerList.put(clazz, list);
115        }
116        list.add(listener);
117    }
118
119    /**
120     * Removes the listener as a listener of the specified type.
121     * 
122     * @param <T>
123     *            the type of the listener to remove
124     * @param clazz
125     *            the class type to remove
126     * @param l
127     *            the listener to remove
128     */
129    @SuppressWarnings("unchecked")
130    public synchronized <T extends EventListener> void remove(Class<T> clazz, T listener) {
131        if (listener == null) {
132            return;
133        }
134
135        List<T> list = (List<T>) listenerList.get(clazz);
136        if (list != null) {
137            list.remove(listener);
138        }
139    }
140}