| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2008 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 dwt.widgets.EventTable; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.widgets.Listener; |
|---|
| 16 |
import dwt.widgets.Event; |
|---|
| 17 |
import dwt.widgets.Listener; |
|---|
| 18 |
import dwt.widgets.TypedListener; |
|---|
| 19 |
import dwt.internal.DWTEventListener; |
|---|
| 20 |
import dwt.dwthelper.System; |
|---|
| 21 |
import dwt.DWT; |
|---|
| 22 |
import dwt.internal.DWTEventListener; |
|---|
| 23 |
|
|---|
| 24 |
/** |
|---|
| 25 |
* Instances of this class implement a simple |
|---|
| 26 |
* look up mechanism that maps an event type |
|---|
| 27 |
* to a listener. Multiple listeners for the |
|---|
| 28 |
* same event type are supported. |
|---|
| 29 |
*/ |
|---|
| 30 |
|
|---|
| 31 |
class EventTable { |
|---|
| 32 |
int [] types; |
|---|
| 33 |
Listener [] listeners; |
|---|
| 34 |
int level; |
|---|
| 35 |
static final int GROW_SIZE = 4; |
|---|
| 36 |
|
|---|
| 37 |
public Listener [] getListeners (int eventType) { |
|---|
| 38 |
if (types is null) return new Listener [0]; |
|---|
| 39 |
int count = 0; |
|---|
| 40 |
for (int i=0; i<types.length; i++) { |
|---|
| 41 |
if (types [i] is eventType) count++; |
|---|
| 42 |
} |
|---|
| 43 |
if (count is 0) return new Listener [0]; |
|---|
| 44 |
Listener [] result = new Listener [count]; |
|---|
| 45 |
count = 0; |
|---|
| 46 |
for (int i=0; i<types.length; i++) { |
|---|
| 47 |
if (types [i] is eventType) { |
|---|
| 48 |
result [count++] = listeners [i]; |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
return result; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
public void hook (int eventType, Listener listener) { |
|---|
| 55 |
if (types is null) types = new int [GROW_SIZE]; |
|---|
| 56 |
if (listeners is null) listeners = new Listener [GROW_SIZE]; |
|---|
| 57 |
int length = types.length, index = length - 1; |
|---|
| 58 |
while (index >= 0) { |
|---|
| 59 |
if (types [index] !is 0) break; |
|---|
| 60 |
--index; |
|---|
| 61 |
} |
|---|
| 62 |
index++; |
|---|
| 63 |
if (index is length) { |
|---|
| 64 |
int [] newTypes = new int [length + GROW_SIZE]; |
|---|
| 65 |
System.arraycopy (types, 0, newTypes, 0, length); |
|---|
| 66 |
types = newTypes; |
|---|
| 67 |
Listener [] newListeners = new Listener [length + GROW_SIZE]; |
|---|
| 68 |
SimpleType!(Listener).arraycopy (listeners, 0, newListeners, 0, length); |
|---|
| 69 |
listeners = newListeners; |
|---|
| 70 |
} |
|---|
| 71 |
types [index] = eventType; |
|---|
| 72 |
listeners [index] = listener; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public bool hooks (int eventType) { |
|---|
| 76 |
if (types is null) return false; |
|---|
| 77 |
for (int i=0; i<types.length; i++) { |
|---|
| 78 |
if (types [i] is eventType) return true; |
|---|
| 79 |
} |
|---|
| 80 |
return false; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
public void sendEvent (Event event) { |
|---|
| 84 |
if (types is null) return; |
|---|
| 85 |
level += level >= 0 ? 1 : -1; |
|---|
| 86 |
try { |
|---|
| 87 |
for (int i=0; i<types.length; i++) { |
|---|
| 88 |
if (event.type is DWT.None) return; |
|---|
| 89 |
if (types [i] is event.type) { |
|---|
| 90 |
Listener listener = listeners [i]; |
|---|
| 91 |
if (listener !is null) listener.handleEvent (event); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
} finally { |
|---|
| 95 |
bool compact = level < 0; |
|---|
| 96 |
level -= level >= 0 ? 1 : -1; |
|---|
| 97 |
if (compact && level is 0) { |
|---|
| 98 |
int index = 0; |
|---|
| 99 |
for (int i=0; i<types.length; i++) { |
|---|
| 100 |
if (types [i] !is 0) { |
|---|
| 101 |
types [index] = types [i]; |
|---|
| 102 |
listeners [index] = listeners [i]; |
|---|
| 103 |
index++; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
for (int i=index; i<types.length; i++) { |
|---|
| 107 |
types [i] = 0; |
|---|
| 108 |
listeners [i] = null; |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
public int size () { |
|---|
| 115 |
if (types is null) return 0; |
|---|
| 116 |
int count = 0; |
|---|
| 117 |
for (int i=0; i<types.length; i++) { |
|---|
| 118 |
if (types [i] !is 0) count++; |
|---|
| 119 |
} |
|---|
| 120 |
return count; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
void remove (int index) { |
|---|
| 124 |
if (level is 0) { |
|---|
| 125 |
int end = types.length - 1; |
|---|
| 126 |
System.arraycopy (types, index + 1, types, index, end - index); |
|---|
| 127 |
SimpleType!(Listener).arraycopy (listeners, index + 1, listeners, index, end - index); |
|---|
| 128 |
index = end; |
|---|
| 129 |
} else { |
|---|
| 130 |
if (level > 0) level = -level; |
|---|
| 131 |
} |
|---|
| 132 |
types [index] = 0; |
|---|
| 133 |
listeners [index] = null; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
public void unhook (int eventType, Listener listener) { |
|---|
| 137 |
if (types is null) return; |
|---|
| 138 |
for (int i=0; i<types.length; i++) { |
|---|
| 139 |
if (types [i] is eventType && listeners [i] is listener) { |
|---|
| 140 |
remove (i); |
|---|
| 141 |
return; |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
public void unhook (int eventType, DWTEventListener listener) { |
|---|
| 147 |
if (types is null) return; |
|---|
| 148 |
for (int i=0; i<types.length; i++) { |
|---|
| 149 |
if (types [i] is eventType) { |
|---|
| 150 |
if ( auto typedListener = cast(TypedListener) listeners [i] ) { |
|---|
| 151 |
if (typedListener.getEventListener () is listener) { |
|---|
| 152 |
remove (i); |
|---|
| 153 |
return; |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
} |
|---|