1 11 package org.eclipse.ui.internal.preferences; 12 13 import java.util.ArrayList ; 14 15 18 public abstract class PropertyMapAdapter implements IDynamicPropertyMap { 19 20 private PropertyListenerList listeners; 21 private int ignoreCount = 0; 22 private ArrayList queuedEvents = new ArrayList (); 23 24 27 public final void addListener(IPropertyMapListener listener) { 28 if (listeners == null) { 29 listeners = new PropertyListenerList(); 30 attachListener(); 31 } 32 listeners.add(listener); 33 } 34 35 38 public final void removeListener(IPropertyMapListener listener) { 39 if (listeners != null) { 40 listeners.remove(listener); 41 if (listeners.isEmpty()) { 42 detachListener(); 43 listeners = null; 44 } 45 } 46 } 47 48 51 public final boolean isCommonProperty(String propertyId) { 52 return true; 53 } 54 55 60 public void dispose() { 61 if (listeners != null) { 62 detachListener(); 63 listeners = null; 64 } 65 } 66 67 protected final void firePropertyChange(String prefId) { 68 if (ignoreCount > 0) { 69 queuedEvents.add(prefId); 70 return; 71 } 72 73 if (listeners != null) { 74 listeners.firePropertyChange(prefId); 75 } 76 } 77 78 public final void addListener(String [] eventsOfInterest, IPropertyMapListener listener) { 79 if (listeners == null) { 80 listeners = new PropertyListenerList(); 81 attachListener(); 82 } 83 listeners.add(eventsOfInterest, listener); 84 } 85 86 protected final void firePropertyChange(String [] prefIds) { 87 if (ignoreCount > 0) { 88 for (int i = 0; i < prefIds.length; i++) { 89 queuedEvents.add(prefIds[i]); 90 } 91 return; 92 } 93 94 if (listeners != null) { 95 listeners.firePropertyChange(prefIds); 96 } 97 } 98 99 public final void startTransaction() { 100 ignoreCount++; 101 } 102 103 public final void endTransaction() { 104 ignoreCount--; 105 if (ignoreCount == 0 && !queuedEvents.isEmpty()) { 106 if (listeners != null) { 107 listeners.firePropertyChange((String []) queuedEvents.toArray(new String [queuedEvents.size()])); 108 } 109 queuedEvents.clear(); 110 } 111 } 112 113 public boolean equals(Object toCompare) { 114 return toCompare instanceof IPropertyMap && PropertyUtil.isEqual(this, (IPropertyMap)toCompare); 115 } 116 117 protected abstract void attachListener(); 118 protected abstract void detachListener(); 119 } 120 | Popular Tags |