1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
import java.util.Iterator;
|
14
|
|
15
|
/**
|
16
|
* This class is intended for internal use only. TODO: If this is for internal
|
17
|
* use only, we should move it to the internal package.
|
18
|
*/
|
19
|
public final class EventListenerList {
|
20
|
|
21
|
private volatile Object array[];
|
22
|
|
23
|
/**
|
24
|
* Adds a listener of type <i>c</i> to the list.
|
25
|
*
|
26
|
* @param c
|
27
|
* the class
|
28
|
* @param listener
|
29
|
* the listener
|
30
|
*/
|
31
|
public synchronized void addListener(Class c, Object listener) {
|
32
|
if (listener == null || c == null)
|
33
|
throw new IllegalArgumentException();
|
34
|
|
35
|
int oldSize = (array == null) ? 0 : array.length;
|
36
|
Object[] newArray = new Object[oldSize + 2];
|
37
|
if (oldSize != 0)
|
38
|
System.arraycopy(array, 0, newArray, 0, oldSize);
|
39
|
newArray[oldSize++] = c;
|
40
|
newArray[oldSize] = listener;
|
41
|
array = newArray;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Returns <code>true</code> if this list of listeners contains a listener
|
46
|
* of type <i>c</i>.
|
47
|
*
|
48
|
* @param c
|
49
|
* the type
|
50
|
* @return whether this list contains a listener of type <i>c</i>
|
51
|
*/
|
52
|
public synchronized boolean containsListener(Class c) {
|
53
|
if (array == null)
|
54
|
return false;
|
55
|
for (int i = 0; i < array.length; i += 2)
|
56
|
if (array[i] == c)
|
57
|
return true;
|
58
|
return false;
|
59
|
}
|
60
|
|
61
|
static class TypeIterator implements Iterator {
|
62
|
private final Object[] items;
|
63
|
private final Class type;
|
64
|
private int index;
|
65
|
|
66
|
TypeIterator(Object items[], Class type) {
|
67
|
this.items = items;
|
68
|
this.type = type;
|
69
|
}
|
70
|
|
71
|
public Object next() {
|
72
|
Object result = items[index + 1];
|
73
|
index += 2;
|
74
|
return result;
|
75
|
}
|
76
|
|
77
|
public boolean hasNext() {
|
78
|
if (items == null)
|
79
|
return false;
|
80
|
while (index < items.length && items[index] != type)
|
81
|
index += 2;
|
82
|
return index < items.length;
|
83
|
}
|
84
|
|
85
|
public void remove() {
|
86
|
throw new UnsupportedOperationException(
|
87
|
"Iterator removal not supported"); //$NON-NLS-1$
|
88
|
}
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Returns an Iterator of all the listeners of type <i>c</i>.
|
93
|
*
|
94
|
* @param listenerType
|
95
|
* the type
|
96
|
* @return an Iterator of all the listeners of type <i>c</i>
|
97
|
*/
|
98
|
public synchronized Iterator getListeners(final Class listenerType) {
|
99
|
return new TypeIterator(array, listenerType);
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Removes the first <i>listener</i> of the specified type by identity.
|
104
|
*
|
105
|
* @param c
|
106
|
* the type
|
107
|
* @param listener
|
108
|
* the listener
|
109
|
*/
|
110
|
public synchronized void removeListener(Class c, Object listener) {
|
111
|
if (array == null || array.length == 0)
|
112
|
return;
|
113
|
if (listener == null || c == null)
|
114
|
throw new IllegalArgumentException();
|
115
|
|
116
|
int index = 0;
|
117
|
while (index < array.length) {
|
118
|
if (array[index] == c && array[index + 1] == listener)
|
119
|
break;
|
120
|
index += 2;
|
121
|
}
|
122
|
if (index == array.length)
|
123
|
return; // listener was not found
|
124
|
|
125
|
Object newArray[] = new Object[array.length - 2];
|
126
|
System.arraycopy(array, 0, newArray, 0, index);
|
127
|
System.arraycopy(array, index + 2, newArray, index, array.length
|
128
|
- index - 2);
|
129
|
array = newArray;
|
130
|
}
|
131
|
|
132
|
}
|