1 11 package org.eclipse.ui.internal.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 25 public final class PropertyListenerList { 26 private Map listeners; 27 private List globalListeners; 28 private static String [] singlePropertyDelta; 29 private static Object mutex = new Object (); 30 31 public PropertyListenerList() { 32 } 33 34 public void firePropertyChange(String prefId) { 35 String [] delta; 36 37 synchronized(mutex) { 41 if (singlePropertyDelta != null) { 42 delta = singlePropertyDelta; 43 singlePropertyDelta = null; 44 } else { 45 delta = new String [] {prefId}; 46 } 47 } 48 49 delta[0] = prefId; 50 51 firePropertyChange(delta); 52 53 if (singlePropertyDelta == null) { 56 synchronized(mutex) { 57 singlePropertyDelta = delta; 58 } 59 } 60 } 61 62 public void firePropertyChange(String [] propertyIds) { 63 if (globalListeners != null) { 64 for (Iterator 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 Collection result = Collections.EMPTY_SET; 77 HashSet union = null; 78 79 for (int i = 0; i < propertyIds.length; i++) { 80 String property = propertyIds[i]; 81 82 List existingListeners = (List )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 (); 90 union.addAll(result); 91 result = union; 92 } 93 94 union.addAll(existingListeners); 95 } 96 } 97 } 98 99 for (Iterator 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 (); 110 } 111 112 globalListeners.add(newListener); 113 newListener.listenerAttached(); 114 } 115 116 123 private void addInternal(String propertyId, IPropertyMapListener newListener) { 124 if (listeners == null) { 125 listeners = new HashMap (); 126 } 127 128 List listenerList = (List )listeners.get(propertyId); 129 130 if (listenerList == null) { 131 listenerList = new ArrayList (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 [] propertyIds, IPropertyMapListener newListener) { 141 for (int i = 0; i < propertyIds.length; i++) { 142 String id = propertyIds[i]; 143 144 addInternal(id, newListener); 145 } 146 newListener.listenerAttached(); 147 } 148 149 public void remove(String propertyId, IPropertyMapListener toRemove) { 150 if (listeners == null) { 151 return; 152 } 153 List listenerList = (List )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 iter = listeners.keySet().iterator(); iter.hasNext();) { 183 String key = (String ) 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 |