1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.util.*; 24 25 import org.openide.src.*; 26 27 37 class EventQueue extends Object { 38 42 Collection eventQueue; 43 47 Map sourceMap; 48 49 53 Map changedElements; 54 55 58 Set newElements; 59 60 63 Set removedElements; 64 65 68 EventQueue parent; 69 70 private boolean firingEvents; 71 72 74 public EventQueue(EventQueue parent) { 75 this.parent = parent; 76 } 77 78 83 public void elementChanged(Element el, Element oldVersion) { 84 if (containsChanges(el)) 85 return; 86 if (changedElements == null) 87 changedElements = new HashMap(17); 88 changedElements.put(el, oldVersion); 89 } 90 91 96 public void elementChanged(ElementImpl impl) { 97 Element el = impl.getElement(); 98 if (containsChanges(el)) 99 return; 100 if (changedElements == null) 101 changedElements = new HashMap(17); 102 changedElements.put(el, impl.cloneSelf()); 103 } 104 105 108 public void elementCreated(Element el) { 109 if (newElements == null) 110 newElements = new HashSet(17); 111 newElements.add(el); 112 } 113 114 117 public void elementRemoved(Element el) { 118 if (newElements != null) 119 newElements.remove(el); 120 if (removedElements == null) 121 removedElements = new HashSet(17); 122 removedElements.add(el); 123 } 124 125 129 public boolean containsChanges(Element el) { 130 if (newElements != null && 131 newElements.contains(el)) 132 return true; 133 if (changedElements != null && 134 changedElements.containsKey(el)) 135 return true; 136 return false; 140 } 141 142 146 public synchronized void addPropertyChange(ElementImpl impl, PropertyChangeEvent evt) { 147 Object source = impl; 148 Collection queue = getQueue(source); 149 queue.add(evt); 150 if (eventQueue == null) 151 eventQueue = new LinkedList(); 152 eventQueue.add(impl); 153 eventQueue.add(evt); 154 } 155 156 159 public void fireEvents() { 160 synchronized (this) { 161 if (firingEvents) 162 return; 163 firingEvents = true; 164 } 165 for (Collection col = pollEventQueue(); col != null && !col.isEmpty(); col = pollEventQueue()) { 166 for (Iterator it = col.iterator(); it.hasNext(); ) { 167 ElementImpl impl = (ElementImpl)it.next(); 168 PropertyChangeEvent evt = (PropertyChangeEvent )it.next(); 169 impl.firePropertyChangeEvent(evt); 170 } 171 } 172 synchronized (this) { 173 firingEvents = false; 174 } 175 } 176 177 private void fireElementEvents(ElementImpl impl, Collection eventQueue) { 178 for (Iterator it = eventQueue.iterator(); it.hasNext(); ) { 179 PropertyChangeEvent evt = (PropertyChangeEvent )it.next(); 180 impl.firePropertyChangeEvent(evt); 181 } 182 } 183 184 private synchronized Map pollEvents() { 185 Map m = sourceMap; 186 sourceMap = null; 187 return m; 188 } 189 190 private synchronized Collection pollEventQueue() { 191 Collection c = eventQueue; 192 eventQueue = null; 193 sourceMap = null; 194 return c; 195 } 196 197 private synchronized Collection getQueue(Object source) { 198 Collection c; 199 200 if (sourceMap == null) { 201 sourceMap = new HashMap(17); 202 c = null; 203 } else { 204 c = (Collection)sourceMap.get(source); 205 } 206 207 if (c != null) 208 return c; 209 c = new LinkedList(); 210 sourceMap.put(source, c); 211 return c; 212 } 213 214 219 public void fixupChanges() { 220 if (changedElements == null) 221 return; 222 for (Iterator it = changedElements.entrySet().iterator(); it.hasNext(); ) { 223 Map.Entry en = (Map.Entry)it.next(); 224 Element source = (Element)en.getKey(); 225 Element oldSnapshot = (Element)en.getValue(); 226 227 ElementImpl impl = (ElementImpl)source.getCookie(ElementImpl.class); 228 Element newSnapshot = impl.cloneSelf(); 230 231 en.setValue(new Object [] { oldSnapshot, newSnapshot }); 232 } 233 } 234 235 private void mergeChild(EventQueue other) { 236 if (other.removedElements != null) { 237 if (newElements != null) { 240 Collection copy = new HashSet(other.removedElements); 241 other.removedElements.removeAll(newElements); 242 newElements.removeAll(copy); 243 } 244 if (removedElements == null) 245 removedElements = other.removedElements; 246 else 247 removedElements.addAll(other.removedElements); 248 } 249 if (other.newElements != null) { 250 if (newElements == null) 251 newElements = other.newElements; 252 else 253 newElements.addAll(other.newElements); 254 } 255 if (other.changedElements != null) { 256 if (newElements != null) { 257 other.changedElements.keySet().removeAll(newElements); 258 } 259 if (changedElements != null) 260 other.changedElements.putAll(changedElements); 261 changedElements = other.changedElements; 262 } 263 if (other.sourceMap != null) { 264 mergePropertyEventMaps(other.sourceMap); 265 } 266 if (other.eventQueue != null) { 267 if (eventQueue == null) 268 eventQueue = other.eventQueue; 269 else 270 eventQueue.addAll(other.eventQueue); 271 } 272 } 273 274 280 private synchronized void mergePropertyEventMaps(Map otherMap) { 281 if (sourceMap == null) { 282 sourceMap = otherMap; 283 return; 284 } 285 for (Iterator otherIterator = otherMap.entrySet().iterator(); 286 otherIterator.hasNext(); ) { 287 Map.Entry otherEntry = (Map.Entry)otherIterator.next(); 288 Object otherKey = otherEntry.getKey(); 289 Collection myQueue = (Collection)sourceMap.get(otherKey); 290 291 if (myQueue == null) 292 sourceMap.put(otherKey, otherEntry.getValue()); 293 else 294 myQueue.addAll((Collection)otherEntry.getValue()); 295 } 296 } 297 298 302 public void mergeToParent() { 303 if (parent == null) 304 return; 305 parent.mergeChild(this); 306 } 307 308 public final Map getChangedElements() { 309 return this.changedElements; 310 } 311 312 public final Set getCreatedElements() { 313 return this.newElements; 314 } 315 316 public final Set getRemovedElements() { 317 return this.removedElements; 318 } 319 320 public final EventQueue getParent() { 321 return this.parent; 322 } 323 324 public void clearSummary() { 325 newElements = removedElements = null; 326 changedElements = null; 327 } 328 329 public boolean isEmpty() { 330 if (newElements != null && !newElements.isEmpty()) 331 return false; 332 if (changedElements != null && !changedElements.isEmpty()) 333 return false; 334 return removedElements == null || removedElements.isEmpty(); 335 } 336 } 337 | Popular Tags |