comparison org.eclipse.draw2d/src/org/eclipse/draw2d/EventListenerList.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.EventListenerList;
14
15 import java.lang.all;
16
17
18 /**
19 * This class is intended for internal use only.
20 * TODO: If this is for internal use only, we should move it to the internal package.
21 */
22 public final class EventListenerList {
23
24 private /+volatile+/ Object array[];
25
26 /**
27 * Adds a listener of type <i>c</i> to the list.
28 * @param c the class
29 * @param listener the listener
30 */
31 public synchronized void addListener(ClassInfo c, Object listener) {
32 if (listener is null || c is null)
33 throw new IllegalArgumentException("");
34
35 int oldSize = (array is null) ? 0 : array.length;
36 Object[] newArray = new Object[oldSize + 2];
37 if (oldSize !is 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 of type
46 * <i>c</i>.
47 * @param c the type
48 * @return whether this list contains a listener of type <i>c</i>
49 */
50 public synchronized bool containsListener(ClassInfo c) {
51 if (array is null)
52 return false;
53 for (int i = 0; i < array.length; i += 2)
54 if (array[i] is c)
55 return true;
56 return false;
57 }
58
59 static class TypeIterator : Iterator {
60 private final Object[] items;
61 private final ClassInfo type;
62 private int index;
63 this(Object items[], ClassInfo type) {
64 this.items = items;
65 this.type = type;
66 }
67 public Object next() {
68 Object result = items[index + 1];
69 index += 2;
70 return result;
71 }
72
73 public bool hasNext() {
74 if (items is null)
75 return false;
76 while (index < items.length && items[index] !is type)
77 index += 2;
78 return index < items.length;
79 }
80
81 public void remove() {
82 throw new UnsupportedOperationException("Iterator removal not supported"); //$NON-NLS-1$
83 }
84 }
85
86 /**
87 * Returns an Iterator of all the listeners of type <i>c</i>.
88 * @param listenerType the type
89 * @return an Iterator of all the listeners of type <i>c</i>
90 */
91 public synchronized Iterator getListeners(ClassInfo listenerType) {
92 return new TypeIterator(array, listenerType);
93 }
94
95 /**
96 * Removes the first <i>listener</i> of the specified type by identity.
97 * @param c the type
98 * @param listener the listener
99 */
100 public synchronized void removeListener(ClassInfo c, Object listener) {
101 if (array is null || array.length is 0)
102 return;
103 if (listener is null || c is null)
104 throw new IllegalArgumentException("");
105
106 int index = 0;
107 while (index < array.length) {
108 if (array[index] is c && array[index + 1] is listener)
109 break;
110 index += 2;
111 }
112 if (index is array.length)
113 return; //listener was not found
114
115 Object newArray[] = new Object[array.length - 2];
116 System.arraycopy(array, 0, newArray, 0, index);
117 System.arraycopy(array, index + 2, newArray, index, array.length - index - 2);
118 array = newArray;
119 }
120
121 }