/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.jstatcom.component.CompSettings;

/**
 * A support class for Symbol objects that manages adding,
 * removing and notification of the different SymbolListener.
 * The events fired in dispatch are fired within the AWT event
 * dispatching thread.
 * 

* This class is not thread-save but clients must handle synchronization. * * @author Markus Kraetzig */ public final class SymbolEventSupport { private static final Logger log = Logger .getLogger(SymbolEventSupport.class); private Map> typeMap = null; /** * SymbolEventSupport constructor. */ public SymbolEventSupport() { super(); } /** * Adds evtListener to the list of listeners for events of * type evtType. * * @param evtListener * the SymbolListener that gets notified whenever * an event of evtType occurs * @param evtType * the event type * @throws IllegalArgumentException * if (evtListener == null || evtType == null) */ public void addListener(SymbolListener evtListener, SymbolEventTypes evtType) { if (evtListener == null || evtType == null) throw new IllegalArgumentException("Argument was null."); if (typeMap == null) typeMap = new HashMap>(); List toPut = null; if (!typeMap.containsKey(evtType)) { // First listener for that type. toPut = new ArrayList(); typeMap.put(evtType, toPut); } else toPut = typeMap.get(evtType); if (!toPut.contains(evtListener)) toPut.add(evtListener); } /** * Dispatches evt by calling valueChanged of * all registered listeners to events of type evt.getType. * The valueChanged methods are called within the event * dispatching thread. * * @param evt * the event to be dispatched * @throws IllegalArgumentException * if (evt == null) */ public void dispatchEvent(final SymbolEvent evt) { if (evt == null) throw new IllegalArgumentException("Argument was null."); // No listeners at all. if (typeMap == null || typeMap.size() == 0) return; // No listeners for that type. if (!typeMap.containsKey(evt.getType())) return; final List currentList = typeMap.get(evt.getType()); if (currentList.size() == 0) return; final Runnable runner = new Runnable() { public void run() { for (int i = 0; i < currentList.size(); i++) try { ((SymbolListener) currentList.get(i)).valueChanged(evt); } catch (Throwable ex) { log.error("Error in value changed of " + currentList.get(i), ex); } } }; CompSettings.dispatchEvent(runner); } /** * Gets a list with all registered listeners for evtType. * * @param evtType * the event type * @return a copy of the respective list or an initialized list instance * with size == 0 if no listeners are registered for * evtType */ public List getListenersForType(SymbolEventTypes evtType) { if (!typeMap.containsKey(evtType)) return new ArrayList(); List list = typeMap.get(evtType); return new ArrayList(list); } /** * Removes evtListener from all listener lists. Nothing * happens if evtListener is currently not a listener. * * @param evtListener * the SymbolListener to be removed * @throws IllegalArgumentException * if (evtListener == null) */ public void removeListener(SymbolListener evtListener) { if (evtListener == null) throw new IllegalArgumentException("Argument was null."); // No listeners at all. if (typeMap == null || typeMap.size() == 0) return; Collection lists = typeMap.values(); for (Iterator it = lists.iterator(); it.hasNext();) { List currentList = (ArrayList) it.next(); currentList.remove(evtListener); } } /** * Gets a string representation of this. * * @return informative string representation */ public String toString() { StringBuffer str = new StringBuffer(super.toString() + "\n"); if (typeMap == null || typeMap.size() == 0) str.append("no listeners registered\n"); else { Collection lists = typeMap.values(); Collection keys = typeMap.keySet(); for (Iterator it = lists.iterator(), kit = keys.iterator(); it .hasNext();) str.append(kit.next() + " " + it.next() + "\n"); } return str.toString(); } }

Ads help maintain this website.