KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > EventTable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.swt.widgets;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.internal.SWTEventListener;
16
17 /**
18  * Instances of this class implement a simple
19  * look up mechanism that maps an event type
20  * to a listener. Multiple listeners for the
21  * same event type are supported.
22  */

23
24 class EventTable {
25     int [] types;
26     Listener [] listeners;
27     int level;
28     
29 public void hook (int eventType, Listener listener) {
30     if (types == null) types = new int [4];
31     if (listeners == null) listeners = new Listener [4];
32     int length = types.length, index = length - 1;
33     while (index >= 0) {
34         if (types [index] != 0) break;
35         --index;
36     }
37     index++;
38     if (index == length) {
39         int [] newTypes = new int [length + 4];
40         System.arraycopy (types, 0, newTypes, 0, length);
41         types = newTypes;
42         Listener [] newListeners = new Listener [length + 4];
43         System.arraycopy (listeners, 0, newListeners, 0, length);
44         listeners = newListeners;
45     }
46     types [index] = eventType;
47     listeners [index] = listener;
48 }
49
50 public boolean hooks (int eventType) {
51     if (types == null) return false;
52     for (int i=0; i<types.length; i++) {
53         if (types [i] == eventType) return true;
54     }
55     return false;
56 }
57
58 public void sendEvent (Event event) {
59     if (types == null) return;
60     level += level >= 0 ? 1 : -1;
61     try {
62         for (int i=0; i<types.length; i++) {
63             if (event.type == SWT.None) return;
64             if (types [i] == event.type) {
65                 Listener listener = listeners [i];
66                 if (listener != null) listener.handleEvent (event);
67             }
68         }
69     } finally {
70         boolean compact = level < 0;
71         level -= level >= 0 ? 1 : -1;
72         if (compact && level == 0) {
73             int index = 0;
74             for (int i=0; i<types.length; i++) {
75                 if (types [i] != 0) {
76                     types [index] = types [i];
77                     listeners [index] = listeners [i];
78                     index++;
79                 }
80             }
81             for (int i=index; i<types.length; i++) {
82                 types [i] = 0;
83                 listeners [i] = null;
84             }
85         }
86     }
87 }
88
89 public int size () {
90     if (types == null) return 0;
91     int count = 0;
92     for (int i=0; i<types.length; i++) {
93         if (types [i] != 0) count++;
94     }
95     return count;
96 }
97
98 void remove (int index) {
99     if (level == 0) {
100         int end = types.length - 1;
101         System.arraycopy (types, index + 1, types, index, end - index);
102         System.arraycopy (listeners, index + 1, listeners, index, end - index);
103         index = end;
104     } else {
105         if (level > 0) level = -level;
106     }
107     types [index] = 0;
108     listeners [index] = null;
109 }
110
111 public void unhook (int eventType, Listener listener) {
112     if (types == null) return;
113     for (int i=0; i<types.length; i++) {
114         if (types [i] == eventType && listeners [i] == listener) {
115             remove (i);
116             return;
117         }
118     }
119 }
120
121 public void unhook (int eventType, SWTEventListener listener) {
122     if (types == null) return;
123     for (int i=0; i<types.length; i++) {
124         if (types [i] == eventType) {
125             if (listeners [i] instanceof TypedListener) {
126                 TypedListener typedListener = (TypedListener) listeners [i];
127                 if (typedListener.getEventListener () == listener) {
128                     remove (i);
129                     return;
130                 }
131             }
132         }
133     }
134 }
135
136 }
137
Popular Tags