-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPropertyChangeSupport.java
More file actions
500 lines (438 loc) · 17.6 KB
/
Copy pathPropertyChangeSupport.java
File metadata and controls
500 lines (438 loc) · 17.6 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.beans;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
/**
* This utility class
*
*/
public class PropertyChangeSupport implements Serializable {
private static final long serialVersionUID = 6401253773779951803l;
private transient Object sourceBean;
private transient List<PropertyChangeListener> allPropertiesChangeListeners =
new ArrayList<PropertyChangeListener>();
private transient Map<String, List<PropertyChangeListener>>
selectedPropertiesChangeListeners =
new HashMap<String, List<PropertyChangeListener>>();
// fields for serialization compatibility
private Hashtable<String, List<PropertyChangeListener>> children;
private Object source;
private int propertyChangeSupportSerializedDataVersion = 1;
/**
* Creates a new instance that uses the source bean as source for any event.
*
* @param sourceBean
* the bean used as source for all events.
*/
public PropertyChangeSupport(Object sourceBean) {
if (sourceBean == null) {
throw new NullPointerException();
}
this.sourceBean = sourceBean;
}
/**
* Fires a {@link PropertyChangeEvent} with the given name, old value and
* new value. As source the bean used to initialize this instance is used.
* If the old value and the new value are not null and equal the event will
* not be fired.
*
* @param propertyName
* the name of the property
* @param oldValue
* the old value of the property
* @param newValue
* the new value of the property
*/
public void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
oldValue, newValue);
doFirePropertyChange(event);
}
/**
* Fires an {@link IndexedPropertyChangeEvent} with the given name, old
* value, new value and index. As source the bean used to initialize this
* instance is used. If the old value and the new value are not null and
* equal the event will not be fired.
*
* @param propertyName
* the name of the property
* @param index
* the index
* @param oldValue
* the old value of the property
* @param newValue
* the new value of the property
*/
public void fireIndexedPropertyChange(String propertyName, int index,
Object oldValue, Object newValue) {
// nulls and equals check done in doFire...
doFirePropertyChange(new IndexedPropertyChangeEvent(sourceBean,
propertyName, oldValue, newValue, index));
}
/**
* Removes the listener from the specific property. This only happens if it
* was registered to this property. Nothing happens if it was not
* registered with this property or if the property name or the listener is
* null.
*
* @param propertyName
* the property name the listener is listening to
* @param listener
* the listener to remove
*/
public synchronized void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
if ((propertyName != null) && (listener != null)) {
List<PropertyChangeListener> listeners =
selectedPropertiesChangeListeners.get(propertyName);
if (listeners != null) {
listeners.remove(listener);
}
}
}
/**
* Adds a listener to a specific property. Nothing happens if the property
* name or the listener is null.
*
* @param propertyName
* the name of the property
* @param listener
* the listener to register for the property with the given name
*/
public synchronized void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
if ((listener != null) && (propertyName != null)) {
List<PropertyChangeListener> listeners =
selectedPropertiesChangeListeners.get(propertyName);
if (listeners == null) {
listeners = new ArrayList<PropertyChangeListener>();
selectedPropertiesChangeListeners.put(propertyName, listeners);
}
// RI compatibility
if (listener instanceof PropertyChangeListenerProxy) {
PropertyChangeListenerProxy proxy =
(PropertyChangeListenerProxy) listener;
listeners.add(new PropertyChangeListenerProxy(
proxy.getPropertyName(),
(PropertyChangeListener) proxy.getListener()));
} else {
listeners.add(listener);
}
}
}
/**
* Returns an array of listeners that registered to the property with the
* given name. If the property name is null an empty array is returned.
*
* @param propertyName
* the name of the property whose listeners should be returned
* @return the array of listeners to the property with the given name.
*/
public synchronized PropertyChangeListener[] getPropertyChangeListeners(
String propertyName) {
List<PropertyChangeListener> listeners = null;
if (propertyName != null) {
listeners = selectedPropertiesChangeListeners.get(propertyName);
}
return (listeners == null) ? new PropertyChangeListener[] {}
: listeners.toArray(
new PropertyChangeListener[listeners.size()]);
}
/**
* Fires a property change of a boolean property with the given name. If the
* old value and the new value are not null and equal the event will not be
* fired.
*
* @param propertyName
* the property name
* @param oldValue
* the old value
* @param newValue
* the new value
*/
public void firePropertyChange(String propertyName, boolean oldValue,
boolean newValue) {
PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
oldValue, newValue);
doFirePropertyChange(event);
}
/**
* Fires a property change of a boolean property with the given name. If the
* old value and the new value are not null and equal the event will not be
* fired.
*
* @param propertyName
* the property name
* @param index
* the index of the changed property
* @param oldValue
* the old value
* @param newValue
* the new value
*/
public void fireIndexedPropertyChange(String propertyName, int index,
boolean oldValue, boolean newValue) {
if (oldValue != newValue) {
fireIndexedPropertyChange(propertyName, index, Boolean
.valueOf(oldValue), Boolean.valueOf(newValue));
}
}
/**
* Fires a property change of an integer property with the given name. If
* the old value and the new value are not null and equal the event will not
* be fired.
*
* @param propertyName
* the property name
* @param oldValue
* the old value
* @param newValue
* the new value
*/
public void firePropertyChange(String propertyName, int oldValue,
int newValue) {
PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
oldValue, newValue);
doFirePropertyChange(event);
}
/**
* Fires a property change of an integer property with the given name. If
* the old value and the new value are not null and equal the event will not
* be fired.
*
* @param propertyName
* the property name
* @param index
* the index of the changed property
* @param oldValue
* the old value
* @param newValue
* the new value
*/
public void fireIndexedPropertyChange(String propertyName, int index,
int oldValue, int newValue) {
if (oldValue != newValue) {
fireIndexedPropertyChange(propertyName, index,
Integer.valueOf(oldValue), Integer.valueOf(newValue));
}
}
/**
* Returns true if there are listeners registered to the property with the
* given name.
*
* @param propertyName
* the name of the property
* @return true if there are listeners registered to that property, false
* otherwise.
*/
public synchronized boolean hasListeners(String propertyName) {
boolean result = allPropertiesChangeListeners.size() > 0;
if (!result && (propertyName != null)) {
List<PropertyChangeListener> listeners =
selectedPropertiesChangeListeners.get(propertyName);
if (listeners != null) {
result = listeners.size() > 0;
}
}
return result;
}
/**
* removes a property change listener that was registered to all properties.
*
* @param listener
* the listener to remove
*/
public synchronized void removePropertyChangeListener(
PropertyChangeListener listener) {
if (listener != null) {
if (listener instanceof PropertyChangeListenerProxy) {
String name = ((PropertyChangeListenerProxy) listener)
.getPropertyName();
PropertyChangeListener lst = (PropertyChangeListener)
((PropertyChangeListenerProxy) listener).getListener();
removePropertyChangeListener(name, lst);
} else {
allPropertiesChangeListeners.remove(listener);
}
}
}
/**
* Registers a listener with all properties.
*
* @param listener
* the listener to register
*/
public synchronized void addPropertyChangeListener(
PropertyChangeListener listener) {
if (listener != null) {
if (listener instanceof PropertyChangeListenerProxy) {
String name = ((PropertyChangeListenerProxy) listener)
.getPropertyName();
PropertyChangeListener lst = (PropertyChangeListener)
((PropertyChangeListenerProxy) listener).getListener();
addPropertyChangeListener(name, lst);
} else {
allPropertiesChangeListeners.add(listener);
}
}
}
/**
* Returns an array with the listeners that registered to all properties.
*
* @return the array of listeners
*/
public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
ArrayList<PropertyChangeListener> result =
new ArrayList<PropertyChangeListener>(
allPropertiesChangeListeners);
for (String propertyName : selectedPropertiesChangeListeners.keySet()) {
List<PropertyChangeListener> selectedListeners =
selectedPropertiesChangeListeners.get(propertyName);
if (selectedListeners != null) {
for (PropertyChangeListener listener : selectedListeners) {
result.add(new PropertyChangeListenerProxy(propertyName,
listener));
}
}
}
return result.toArray(new PropertyChangeListener[result.size()]);
}
private void writeObject(ObjectOutputStream oos) throws IOException {
List<PropertyChangeListener> allSerializedPropertiesChangeListeners =
new ArrayList<PropertyChangeListener>();
for (PropertyChangeListener pcl : allPropertiesChangeListeners) {
if (pcl instanceof Serializable) {
allSerializedPropertiesChangeListeners.add(pcl);
}
}
Map<String, List<PropertyChangeListener>>
selectedSerializedPropertiesChangeListeners =
new HashMap<String, List<PropertyChangeListener>>();
for (String propertyName : selectedPropertiesChangeListeners.keySet()) {
List<PropertyChangeListener> keyValues =
selectedPropertiesChangeListeners.get(propertyName);
if (keyValues != null) {
List<PropertyChangeListener> serializedPropertiesChangeListeners
= new ArrayList<PropertyChangeListener>();
for (PropertyChangeListener pcl : keyValues) {
if (pcl instanceof Serializable) {
serializedPropertiesChangeListeners.add(pcl);
}
}
if (!serializedPropertiesChangeListeners.isEmpty()) {
selectedSerializedPropertiesChangeListeners.put(
propertyName, serializedPropertiesChangeListeners);
}
}
}
children = new Hashtable<String, List<PropertyChangeListener>>(
selectedSerializedPropertiesChangeListeners);
children.put("", allSerializedPropertiesChangeListeners);
oos.writeObject(children);
Object source = null;
if (sourceBean instanceof Serializable) {
source = sourceBean;
}
oos.writeObject(source);
oos.writeInt(propertyChangeSupportSerializedDataVersion);
}
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
children = (Hashtable<String, List<PropertyChangeListener>>) ois
.readObject();
selectedPropertiesChangeListeners = new HashMap<String, List<PropertyChangeListener>>(
children);
allPropertiesChangeListeners = selectedPropertiesChangeListeners
.remove("");
if (allPropertiesChangeListeners == null) {
allPropertiesChangeListeners = new ArrayList<PropertyChangeListener>();
}
sourceBean = ois.readObject();
propertyChangeSupportSerializedDataVersion = ois.readInt();
}
/**
* Fires a property change event to all listeners of that property.
*
* @param event
* the event to fire
*/
public void firePropertyChange(PropertyChangeEvent event) {
doFirePropertyChange(event);
}
private PropertyChangeEvent createPropertyChangeEvent(String propertyName,
Object oldValue, Object newValue) {
return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
newValue);
}
private PropertyChangeEvent createPropertyChangeEvent(String propertyName,
boolean oldValue, boolean newValue) {
return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
newValue);
}
private PropertyChangeEvent createPropertyChangeEvent(String propertyName,
int oldValue, int newValue) {
return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
newValue);
}
private void doFirePropertyChange(PropertyChangeEvent event) {
String propertyName = event.getPropertyName();
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if ((newValue != null) && (oldValue != null)
&& newValue.equals(oldValue)) {
return;
}
/*
* Copy the listeners collections so they can be modified while we fire
* events.
*/
// Listeners to all property change events
PropertyChangeListener[] listensToAll;
// Listens to a given property change
PropertyChangeListener[] listensToOne = null;
synchronized (this) {
listensToAll = allPropertiesChangeListeners
.toArray(new PropertyChangeListener[allPropertiesChangeListeners
.size()]);
List<PropertyChangeListener> listeners = selectedPropertiesChangeListeners
.get(propertyName);
if (listeners != null) {
listensToOne = listeners
.toArray(new PropertyChangeListener[listeners.size()]);
}
}
// Fire the listeners
for (PropertyChangeListener listener : listensToAll) {
listener.propertyChange(event);
}
if (listensToOne != null) {
for (PropertyChangeListener listener : listensToOne) {
listener.propertyChange(event);
}
}
}
}