01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.swt.ole.win32;
11:
12: /**
13: * The OleEventTable class implements a simple
14: * look up mechanism that maps an event type
15: * to a listener. Multiple listeners for the
16: * same event type are supported.
17: *
18: */
19:
20: class OleEventTable {
21: int[] types;
22: OleListener[] handlers;
23:
24: void hook(int eventType, OleListener handler) {
25: if (types == null)
26: types = new int[4];
27: if (handlers == null)
28: handlers = new OleListener[4];
29: for (int i = 0; i < types.length; i++) {
30: if (types[i] == 0) {
31: types[i] = eventType;
32: handlers[i] = handler;
33: return;
34: }
35: }
36: int size = types.length;
37: int[] newTypes = new int[size + 4];
38: OleListener[] newHandlers = new OleListener[size + 4];
39: System.arraycopy(types, 0, newTypes, 0, size);
40: System.arraycopy(handlers, 0, newHandlers, 0, size);
41: types = newTypes;
42: handlers = newHandlers;
43: types[size] = eventType;
44: handlers[size] = handler;
45: }
46:
47: boolean hooks(int eventType) {
48: if (handlers == null)
49: return false;
50: for (int i = 0; i < types.length; i++) {
51: if (types[i] == eventType)
52: return true;
53: }
54: return false;
55: }
56:
57: void sendEvent(OleEvent event) {
58: if (handlers == null)
59: return;
60: for (int i = 0; i < types.length; i++) {
61: if (types[i] == event.type) {
62: OleListener listener = handlers[i];
63: if (listener != null)
64: listener.handleEvent(event);
65: }
66: }
67: }
68:
69: void unhook(int eventType, OleListener handler) {
70: if (handlers == null)
71: return;
72: for (int i = 0; i < types.length; i++) {
73: if ((types[i] == eventType) && (handlers[i] == handler)) {
74: types[i] = 0;
75: handlers[i] = null;
76: return;
77: }
78: }
79: }
80:
81: boolean hasEntries() {
82: for (int i = 0; i < types.length; i++) {
83: if (types[i] != 0)
84: return true;
85: }
86: return false;
87: }
88: }
|