1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.util; 23 24 import java.util.EventListener; 25 import java.lang.reflect.Array; 26 27 28 /** 29 * A convenient storage place for Listeners. The listeners are stored in the 30 * same manner as in javax.swing.event.EventListenerList. 31 * <STRONG>Note:</STRONG> This class is not thread safe. 32 */ 33 34 public class EventListenerList{ 35 36 37 /** 38 * The array where we keep the listeners and their types. 39 */ 40 41 private Object [] listenerList = new Object[0]; 42 43 44 45 /** 46 * Adds the given listener as a listener of the given type. 47 */ 48 add(Class listenerType, EventListener listener)49 public void add(Class listenerType, EventListener listener){ 50 if (listener==null) 51 return; 52 if (!listenerType.isInstance(listener)) 53 throw new IllegalArgumentException("The listener is not an instance of the listener class."); 54 55 Object [] tempArr = new Object[listenerList.length+2]; 56 System.arraycopy(listenerList,0,tempArr,0,listenerList.length); 57 58 tempArr[tempArr.length-2] = listenerType; 59 tempArr[tempArr.length-1] = listener; 60 61 listenerList = tempArr; 62 } 63 64 65 66 67 /** 68 * Removes the given listener as a listener of the specified type. 69 */ 70 remove(Class listenerType, EventListener listener)71 public void remove(Class listenerType, EventListener listener){ 72 if (listener == null) 73 return; 74 if (!listenerType.isInstance(listener)) 75 throw new IllegalArgumentException("The listener is not an instance of the listener class."); 76 77 for (int i=0;i<listenerList.length;i+=2){ 78 if ((listenerList[i]==listenerType)&&(listenerList[i+1].equals(listener))){ 79 Object [] tempArr = new Object[listenerList.length-2]; 80 System.arraycopy(listenerList,0,tempArr,0,i); 81 System.arraycopy(listenerList,i+2,tempArr,i,listenerList.length-i-2); 82 listenerList = tempArr; 83 return; 84 } 85 } 86 } 87 88 89 90 /** 91 * Returns the total number of listeners for this listener list. 92 */ 93 getListenerCount()94 public int getListenerCount(){ 95 return listenerList.length; 96 } 97 98 99 100 /** 101 * Returns the amount of listeners of the specified type in this listener 102 * list. 103 */ 104 getListenerCount(Class listenerType)105 public int getListenerCount(Class listenerType){ 106 int count = 0; 107 for (int i=0;i<listenerList.length;i+=2){ 108 if (listenerList[i]==listenerType) 109 count++; 110 } 111 return count; 112 } 113 114 115 116 /** 117 * Returns an array containing all the listeners in this listener list. The 118 * array contains listeners and their types in the following format: the 119 * element at index 2*N is the type of the listener at index 2*N+1 120 * <B>WARNING!!!</B> Absolutely NO modification of the data contained in this array 121 * should be made -- if any such manipulation is necessary, it should be done on a 122 * copy of the array returned rather than the array itself. 123 */ 124 getListenerList()125 public Object [] getListenerList(){ 126 return listenerList; 127 } 128 129 130 131 /** 132 * Returns an array of listeners registered as the listeners of the given 133 * type. Note that the returned array is an array of the actual given type 134 * so it can be safely casted to that type once instead of casting each of the 135 * listener. 136 */ 137 getListeners(Class listenerType)138 public EventListener [] getListeners(Class listenerType){ 139 EventListener [] listeners = (EventListener [])Array.newInstance(listenerType,getListenerCount(listenerType)); 140 int count = 0; 141 for (int i=0;i<listenerList.length;i+=2){ 142 if (listenerType==listenerList[i]) 143 listeners[count++] = (EventListener)listenerList[i+1]; 144 } 145 return listeners; 146 } 147 148 149 } 150