KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > PropertyListenerList


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ui.internal.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * @since 3.1
24  */

25 public final class PropertyListenerList {
26     private Map JavaDoc listeners;
27     private List JavaDoc globalListeners;
28     private static String JavaDoc[] singlePropertyDelta;
29     private static Object JavaDoc mutex = new Object JavaDoc();
30     
31     public PropertyListenerList() {
32     }
33     
34     public void firePropertyChange(String JavaDoc prefId) {
35         String JavaDoc[] delta;
36         
37         // Optimization: as long as we're not being called recursively,
38
// we can reuse the same delta object to avoid repeated memory
39
// allocation.
40
synchronized(mutex) {
41             if (singlePropertyDelta != null) {
42                 delta = singlePropertyDelta;
43                 singlePropertyDelta = null;
44             } else {
45                 delta = new String JavaDoc[] {prefId};
46             }
47         }
48         
49         delta[0] = prefId;
50         
51         firePropertyChange(delta);
52         
53         // Optimization: allow this same delta object to be reused at a later
54
// time
55
if (singlePropertyDelta == null) {
56             synchronized(mutex) {
57                 singlePropertyDelta = delta;
58             }
59         }
60     }
61     
62     public void firePropertyChange(String JavaDoc[] propertyIds) {
63         if (globalListeners != null) {
64             for (Iterator JavaDoc iter = globalListeners.iterator(); iter.hasNext();) {
65                 IPropertyMapListener next = (IPropertyMapListener) iter.next();
66                 
67                 next.propertyChanged(propertyIds);
68             }
69         }
70         
71         if (listeners != null) {
72             
73             // To avoid temporary memory allocation, we try to simply move the
74
// result pointer around if possible. We only allocate a HashSet
75
// to compute which listeners we care about
76
Collection JavaDoc result = Collections.EMPTY_SET;
77             HashSet JavaDoc union = null;
78             
79             for (int i = 0; i < propertyIds.length; i++) {
80                 String JavaDoc property = propertyIds[i];
81                 
82                 List JavaDoc existingListeners = (List JavaDoc)listeners.get(property);
83                 
84                 if (existingListeners != null) {
85                     if (result == Collections.EMPTY_SET) {
86                         result = existingListeners;
87                     } else {
88                         if (union == null) {
89                             union = new HashSet JavaDoc();
90                             union.addAll(result);
91                             result = union;
92                         }
93                         
94                         union.addAll(existingListeners);
95                     }
96                 }
97             }
98             
99             for (Iterator JavaDoc iter = result.iterator(); iter.hasNext();) {
100                 IPropertyMapListener next = (IPropertyMapListener) iter.next();
101                 
102                 next.propertyChanged(propertyIds);
103             }
104         }
105     }
106
107     public void add(IPropertyMapListener newListener) {
108         if (globalListeners == null) {
109             globalListeners = new ArrayList JavaDoc();
110         }
111         
112         globalListeners.add(newListener);
113         newListener.listenerAttached();
114     }
115     
116     /**
117      * Adds a listener which will be notified when the given property changes
118      *
119      * @param propertyId
120      * @param newListener
121      * @since 3.1
122      */

123     private void addInternal(String JavaDoc propertyId, IPropertyMapListener newListener) {
124         if (listeners == null) {
125             listeners = new HashMap JavaDoc();
126         }
127         
128         List JavaDoc listenerList = (List JavaDoc)listeners.get(propertyId);
129         
130         if (listenerList == null) {
131             listenerList = new ArrayList JavaDoc(1);
132             listeners.put(propertyId, listenerList);
133         }
134         
135         if (!listenerList.contains(newListener)) {
136             listenerList.add(newListener);
137         }
138     }
139     
140     public void add(String JavaDoc[] propertyIds, IPropertyMapListener newListener) {
141         for (int i = 0; i < propertyIds.length; i++) {
142             String JavaDoc id = propertyIds[i];
143             
144             addInternal(id, newListener);
145         }
146         newListener.listenerAttached();
147     }
148     
149     public void remove(String JavaDoc propertyId, IPropertyMapListener toRemove) {
150         if (listeners == null) {
151             return;
152         }
153         List JavaDoc listenerList = (List JavaDoc)listeners.get(propertyId);
154         
155         if (listenerList != null) {
156             listenerList.remove(toRemove);
157             
158             if (listenerList.isEmpty()) {
159                 listeners.remove(propertyId);
160                 
161                 if (listeners.isEmpty()) {
162                     listeners = null;
163                 }
164             }
165         }
166     }
167     
168     public void removeAll() {
169         globalListeners = null;
170         listeners = null;
171     }
172     
173     public void remove(IPropertyMapListener toRemove) {
174         if (globalListeners != null) {
175             globalListeners.remove(toRemove);
176             if (globalListeners.isEmpty()) {
177                 globalListeners = null;
178             }
179         }
180         
181         if (listeners != null) {
182             for (Iterator JavaDoc iter = listeners.keySet().iterator(); iter.hasNext();) {
183                 String JavaDoc key = (String JavaDoc) iter.next();
184                 
185                 remove(key, toRemove);
186             }
187         }
188     }
189     
190     public boolean isEmpty() {
191         return globalListeners == null && listeners == null;
192     }
193 }
194
Popular Tags