1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc.event; |
|
27 |
|
28 import java.beans.*; |
|
29 import java.util.*; |
|
30 |
|
31 public class PropertyChangeListeners |
|
32 extends EventListeners<PropertyChangeListener> |
|
33 implements EventDispatcher<PropertyChangeEvent, PropertyChangeListener>, |
|
34 PropertyChangeListener { |
|
35 |
|
36 // |
|
37 // Instance data |
|
38 // |
|
39 |
|
40 // Map of PropertyChangeListeners interested only in a single property |
|
41 private Map<PropertyChangeListener, List<String>> map = |
|
42 new HashMap<PropertyChangeListener, List<String>>(); |
|
43 |
|
44 // |
|
45 // EventDispatcher methods |
|
46 // |
|
47 |
|
48 @Override |
|
49 public void dispatch(PropertyChangeListener listener, |
|
50 PropertyChangeEvent event) { |
|
51 |
|
52 List<String> propNames = map.get(listener); |
|
53 |
|
54 // Dispatch if listener is not property-specific, or is specific to the |
|
55 // changed property |
|
56 if (propNames == null || propNames.contains(event.getPropertyName())) { |
|
57 listener.propertyChange(event); |
|
58 } |
|
59 } |
|
60 |
|
61 // |
|
62 // PropertyChangeListener methods |
|
63 // |
|
64 |
|
65 @Override |
|
66 public void propertyChange(PropertyChangeEvent event) { |
|
67 dispatch((EventDispatcher<PropertyChangeEvent, PropertyChangeListener>) |
|
68 this, event); |
|
69 } |
|
70 |
|
71 // |
|
72 // EventListeners methods |
|
73 // |
|
74 |
|
75 @Override |
|
76 public boolean remove(PropertyChangeListener listener) { |
|
77 boolean removed = super.remove(listener); |
|
78 if (removed) { |
|
79 map.remove(listener); |
|
80 } |
|
81 return removed; |
|
82 } |
|
83 |
|
84 // |
|
85 // PropertyChangeListeners methods |
|
86 // |
|
87 |
|
88 public void add(String propName, PropertyChangeListener listener) { |
|
89 add(listener); |
|
90 |
|
91 List<String> propNames = map.get(listener); |
|
92 if (propNames == null) { |
|
93 propNames = new LinkedList<String>(); |
|
94 map.put(listener, propNames); |
|
95 } |
|
96 |
|
97 int index = Collections.binarySearch(propNames, propName); |
|
98 if (index < 0) { |
|
99 index = -1 - index; |
|
100 propNames.add(index, propName); |
|
101 } |
|
102 } |
|
103 |
|
104 public boolean remove(String propName, PropertyChangeListener listener) { |
|
105 List<String> propNames = map.get(listener); |
|
106 if (propNames != null) { |
|
107 propNames.remove(propName); |
|
108 if (propNames.isEmpty()) { |
|
109 map.remove(listener); |
|
110 } else { |
|
111 return false; |
|
112 } |
|
113 } |
|
114 return super.remove(listener); |
|
115 } |
|
116 } |
|