1 12 13 package org.eclipse.core.databinding.observable.map; 14 15 import java.util.AbstractMap ; 16 17 import org.eclipse.core.databinding.observable.ChangeEvent; 18 import org.eclipse.core.databinding.observable.ChangeSupport; 19 import org.eclipse.core.databinding.observable.IChangeListener; 20 import org.eclipse.core.databinding.observable.IStaleListener; 21 import org.eclipse.core.databinding.observable.Realm; 22 import org.eclipse.core.databinding.observable.StaleEvent; 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.AssertionFailedException; 25 26 35 public abstract class AbstractObservableMap extends AbstractMap implements 36 IObservableMap { 37 38 private ChangeSupport changeSupport; 39 40 private boolean stale; 41 42 44 public AbstractObservableMap() { 45 this(Realm.getDefault()); 46 } 47 48 51 protected void lastListenerRemoved() { 52 } 53 54 57 protected void firstListenerAdded() { 58 } 59 60 63 public AbstractObservableMap(Realm realm) { 64 Assert.isNotNull(realm); 65 changeSupport = new ChangeSupport(realm){ 66 protected void firstListenerAdded() { 67 AbstractObservableMap.this.firstListenerAdded(); 68 } 69 protected void lastListenerRemoved() { 70 AbstractObservableMap.this.lastListenerRemoved(); 71 } 72 }; 73 } 74 75 public synchronized void addMapChangeListener(IMapChangeListener listener) { 76 changeSupport.addListener(MapChangeEvent.TYPE, listener); 77 } 78 79 public synchronized void removeMapChangeListener(IMapChangeListener listener) { 80 changeSupport.removeListener(MapChangeEvent.TYPE, listener); 81 } 82 83 public synchronized void addChangeListener(IChangeListener listener) { 84 changeSupport.addChangeListener(listener); 85 } 86 87 public synchronized void addStaleListener(IStaleListener listener) { 88 changeSupport.addStaleListener(listener); 89 } 90 91 public synchronized void dispose() { 92 changeSupport.dispose(); 93 changeSupport = null; 94 } 95 96 public Realm getRealm() { 97 return changeSupport.getRealm(); 98 } 99 100 public boolean isStale() { 101 checkRealm(); 102 return stale; 103 } 104 105 public synchronized void removeChangeListener(IChangeListener listener) { 106 changeSupport.removeChangeListener(listener); 107 } 108 109 public synchronized void removeStaleListener(IStaleListener listener) { 110 changeSupport.removeStaleListener(listener); 111 } 112 113 118 public void setStale(boolean stale) { 119 checkRealm(); 120 this.stale = stale; 121 if (stale) { 122 fireStale(); 123 } 124 } 125 126 129 protected void fireStale() { 130 checkRealm(); 131 changeSupport.fireEvent(new StaleEvent(this)); 132 } 133 134 137 protected void fireChange() { 138 checkRealm(); 139 changeSupport.fireEvent(new ChangeEvent(this)); 140 } 141 142 147 protected void fireMapChange(MapDiff diff) { 148 checkRealm(); 149 changeSupport.fireEvent(new MapChangeEvent(this, diff)); 150 } 151 152 159 protected void checkRealm() { 160 Assert.isTrue(getRealm().isCurrent()); 161 } 162 } 163 | Popular Tags |