]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/EventTable.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / EventTable.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.*;
19
20 /**
21  * Instances of this class implement a simple
22  * look up mechanism that maps an event type
23  * to a listener.  Multiple listeners for the
24  * same event type are supported.
25  */
26
27 class EventTable {
28         int [] types;
29         Listener [] listeners;
30         int level;
31         static final int GROW_SIZE = 4;
32
33 public Listener [] getListeners (int eventType) {
34         if (types == null) return new Listener [0];
35         int count = 0;
36         for (int i=0; i<types.length; i++) {
37                 if (types [i] == eventType) count++;
38         }
39         if (count == 0) return new Listener [0];
40         Listener [] result = new Listener [count];
41         count = 0;
42         for (int i=0; i<types.length; i++) {
43                 if (types [i] == eventType) {
44                         result [count++] = listeners [i];
45                 }
46         }
47         return result;
48 }
49
50 public void hook (int eventType, Listener listener) {
51         if (types == null) types = new int [GROW_SIZE];
52         if (listeners == null) listeners = new Listener [GROW_SIZE];
53         int length = types.length, index = length - 1;
54         while (index >= 0) {
55                 if (types [index] != 0) break;
56                 --index;
57         }
58         index++;
59         if (index == length) {
60                 int [] newTypes = new int [length + GROW_SIZE];
61                 System.arraycopy (types, 0, newTypes, 0, length);
62                 types = newTypes;
63                 Listener [] newListeners = new Listener [length + GROW_SIZE];
64                 System.arraycopy (listeners, 0, newListeners, 0, length);
65                 listeners = newListeners;
66         }
67         types [index] = eventType;
68         listeners [index] = listener;
69 }
70
71 public boolean hooks (int eventType) {
72         if (types == null) return false;
73         for (int i=0; i<types.length; i++) {
74                 if (types [i] == eventType) return true;
75         }
76         return false;
77 }
78
79 public void sendEvent (Event event) {
80         if (types == null) return;
81         level += level >= 0 ? 1 : -1;
82         try {
83                 for (int i=0; i<types.length; i++) {
84                         if (event.type == SWT.None) return;
85                         if (types [i] == event.type) {
86                                 Listener listener = listeners [i];
87                                 if (listener != null) {
88                                         try {
89                                                 listener.handleEvent (event);
90                                         } catch (RuntimeException runtimeException) {
91                                                 Display display = Display.getCurrent ();
92
93                                                 if (display == null) {
94                                                         throw runtimeException;
95                                                 }
96
97                                                 display.getRuntimeExceptionHandler ().accept (runtimeException);
98                                         } catch (Error error) {
99                                                 Display display = Display.getCurrent ();
100
101                                                 if (display == null) {
102                                                         throw error;
103                                                 }
104
105                                                 display.getErrorHandler ().accept (error);
106                                         }
107                                 }
108                         }
109                 }
110         } finally {
111                 boolean compact = level < 0;
112                 level -= level >= 0 ? 1 : -1;
113                 if (compact && level == 0) {
114                         int index = 0;
115                         for (int i=0; i<types.length; i++) {
116                                 if (types [i] != 0) {
117                                         types [index] = types [i];
118                                         listeners [index] = listeners [i];
119                                         index++;
120                                 }
121                         }
122                         for (int i=index; i<types.length; i++) {
123                                 types [i] = 0;
124                                 listeners [i] = null;
125                         }
126                 }
127         }
128 }
129
130 public int size () {
131         if (types == null) return 0;
132         int count = 0;
133         for (int i=0; i<types.length; i++) {
134                 if (types [i] != 0) count++;
135         }
136         return count;
137 }
138
139 void remove (int index) {
140         if (level == 0) {
141                 int end = types.length - 1;
142                 System.arraycopy (types, index + 1, types, index, end - index);
143                 System.arraycopy (listeners, index + 1, listeners, index, end - index);
144                 index = end;
145         } else {
146                 if (level > 0) level = -level;
147         }
148         types [index] = 0;
149         listeners [index] = null;
150 }
151
152 public void unhook (int eventType, Listener listener) {
153         if (types == null) return;
154         for (int i=0; i<types.length; i++) {
155                 if (types [i] == eventType && listeners [i] == listener) {
156                         remove (i);
157                         return;
158                 }
159         }
160 }
161
162 public void unhook (int eventType, SWTEventListener listener) {
163         if (types == null) return;
164         for (int i=0; i<types.length; i++) {
165                 if (types [i] == eventType) {
166                         if (listeners [i] instanceof TypedListener) {
167                                 TypedListener typedListener = (TypedListener) listeners [i];
168                                 if (typedListener.getEventListener () == listener) {
169                                         remove (i);
170                                         return;
171                                 }
172                         }
173                 }
174         }
175 }
176
177 }