| 1 | /*- |
|---|
| 2 | * Copyright (c) 2007, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | package org.logicprobe.LogicMail.util; |
|---|
| 33 | |
|---|
| 34 | import net.rim.device.api.util.Arrays; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Utility class to hold EventListeners. |
|---|
| 38 | * Based on the interface of javax.swing.event.EventListenerList |
|---|
| 39 | */ |
|---|
| 40 | public class EventListenerList { |
|---|
| 41 | protected Object[] listenerList = new Object[0]; |
|---|
| 42 | |
|---|
| 43 | /** Creates a new instance of EventListenerList */ |
|---|
| 44 | public EventListenerList() { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public void add(Class t, EventListener l) { |
|---|
| 48 | if(l == null || !t.isInstance(l)) { |
|---|
| 49 | throw new IllegalArgumentException(); |
|---|
| 50 | } |
|---|
| 51 | if(listenerList.length == 0) { |
|---|
| 52 | listenerList = new Object[] { t, l }; |
|---|
| 53 | } |
|---|
| 54 | else { |
|---|
| 55 | Arrays.add(listenerList, t); |
|---|
| 56 | Arrays.add(listenerList, l); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public void remove(Class t, EventListener l) { |
|---|
| 61 | if(l == null || !t.isInstance(l)) { |
|---|
| 62 | throw new IllegalArgumentException(); |
|---|
| 63 | } |
|---|
| 64 | int removeCount = 0; |
|---|
| 65 | for(int i=0; i<listenerList.length; i+=2) { |
|---|
| 66 | if(t == (Class)listenerList[i] && |
|---|
| 67 | l == (EventListener)listenerList[i+1]) { |
|---|
| 68 | removeCount += 2; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | Object[] newListenerList = new Object[listenerList.length - removeCount]; |
|---|
| 72 | int index = 0; |
|---|
| 73 | for(int i=0; i<listenerList.length; i+=2) { |
|---|
| 74 | if(!(t == (Class)listenerList[i] && |
|---|
| 75 | l == (EventListener)listenerList[i+1])) { |
|---|
| 76 | newListenerList[index++] = listenerList[i]; |
|---|
| 77 | newListenerList[index++] = listenerList[i+1]; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | listenerList = newListenerList; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public int getListenerCount() { |
|---|
| 84 | return listenerList.length/2; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public int getListenerCount(Class t) { |
|---|
| 88 | int count = 0; |
|---|
| 89 | for(int i=0; i<listenerList.length; i+=2) { |
|---|
| 90 | if(t == (Class)listenerList[i]) { |
|---|
| 91 | count++; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | return count; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public Object[] getListenerList() { |
|---|
| 98 | return listenerList; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public EventListener[] getListeners(Class t) { |
|---|
| 102 | int count = getListenerCount(t); |
|---|
| 103 | int index = 0; |
|---|
| 104 | EventListener[] result = new EventListener[count]; |
|---|
| 105 | for(int i=0; i<listenerList.length; i+=2) { |
|---|
| 106 | if(t == (Class)listenerList[i]) { |
|---|
| 107 | result[index++] = (EventListener)listenerList[i+1]; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | return result; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | public String toString() { |
|---|
| 114 | StringBuffer buf = new StringBuffer(); |
|---|
| 115 | buf.append("EventListenerList: "); |
|---|
| 116 | for (int i=0 ; i<listenerList.length; i+=2) { |
|---|
| 117 | buf.append(((Class)listenerList[i]).getName()); |
|---|
| 118 | buf.append(" --> "); |
|---|
| 119 | buf.append(listenerList[i+1]); |
|---|
| 120 | } |
|---|
| 121 | return buf.toString(); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|