Skip to main content
summaryrefslogtreecommitdiffstats
blob: f858fc54c857cac9c06a22397c496f76fb367b79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.eclipse.swt.widgets;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved
 */

import org.eclipse.swt.internal.EventListenerCompatability;

/**
 * Instances of this class implement a simple
 * look up mechanism that maps an event type
 * to a listener.  Muliple listeners for the
 * same event type are supported.
 */

class EventTable {
	int [] types;
	Listener [] handlers;
	
public void hook (int eventType, Listener handler) {
	if (types == null) types = new int [4];
	if (handlers == null) handlers = new Listener [4];
	for (int i=0; i<types.length; i++) {
		if (types [i] == 0) {
			types [i] = eventType;
			handlers [i] = handler;
			return;
		}
	}
	int size = types.length;
	int [] newTypes = new int [size + 4];
	Listener [] newHandlers = new Listener [size + 4];
	System.arraycopy (types, 0, newTypes, 0, size);
	System.arraycopy (handlers, 0, newHandlers, 0, size);
	types = newTypes;  handlers = newHandlers;
	types [size] = eventType;  handlers [size] = handler;
}

public boolean hooks (int eventType) {
	if (handlers == null) return false;
	for (int i=0; i<types.length; i++) {
		if (types [i] == eventType) return true;
	}
	return false;
}

public void sendEvent (Event event) {
	if (handlers == null) return;
	for (int i=0; i<types.length; i++) {
		if (types [i] == event.type) {
			Listener listener = handlers [i];
			if (listener != null) listener.handleEvent (event);
		}
	}
}

public void unhook (int eventType, Listener handler) {
	if (handlers == null) return;
	for (int i=0; i<types.length; i++) {
		if ((types [i] == eventType) && (handlers [i] == handler)) {
			types [i] = 0;
			handlers [i] = null;
			return;
		}
	}
}

public void unhook (int eventType, EventListenerCompatability handler) {
	if (handlers == null) return;
	for (int i=0; i<types.length; i++) {
		if (types [i] == eventType) {
			if (handlers [i] instanceof TypedListener) {
				TypedListener listener = (TypedListener) handlers [i];
				if (listener.getEventListener () == handler) {
					types [i] = 0;
					handlers [i] = null;
					return;
				}
			}
		}
	}
}

}

Back to the top