1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.value; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 18 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable; 19 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker; 20 21 25 abstract public class AbstractObservableValue extends AbstractObservable 26 implements IObservableValue { 27 28 private Collection valueChangeListeners = null; 29 30 public void addValueChangeListener(IValueChangeListener listener) { 31 if (valueChangeListeners == null) { 32 boolean hadListeners = hasListeners(); 33 valueChangeListeners = new ArrayList (); 34 valueChangeListeners.add(listener); 35 if (!hadListeners) { 36 firstListenerAdded(); 37 } 38 } else if (valueChangeListeners.size() > 16) { 39 HashSet listenerList = new HashSet (); 40 listenerList.addAll(valueChangeListeners); 41 valueChangeListeners = listenerList; 42 valueChangeListeners.add(listener); 43 } else { 44 valueChangeListeners.add(listener); 45 } 46 } 47 48 public void removeValueChangeListener(IValueChangeListener listener) { 49 if (valueChangeListeners == null) { 50 return; 51 } 52 valueChangeListeners.remove(listener); 53 if (valueChangeListeners.size() == 0) { 54 valueChangeListeners = null; 55 } 56 if (!hasListeners()) { 57 lastListenerRemoved(); 58 } 59 } 60 61 public void setValue(Object value) { 62 throw new UnsupportedOperationException (); 63 } 64 65 protected void fireValueChange(ValueDiff diff) { 66 super.fireChange(); 68 if (valueChangeListeners != null) { 69 IValueChangeListener[] listeners = (IValueChangeListener[]) valueChangeListeners 70 .toArray(new IValueChangeListener[valueChangeListeners 71 .size()]); 72 for (int i = 0; i < listeners.length; i++) { 73 listeners[i].handleValueChange(this, diff); 74 } 75 } 76 } 77 78 public final Object getValue() { 79 ObservableTracker.getterCalled(this); 80 return doGetValue(); 81 } 82 83 abstract protected Object doGetValue(); 84 85 public boolean isStale() { 86 return false; 87 } 88 89 92 protected boolean hasListeners() { 93 return super.hasListeners() || valueChangeListeners != null; 94 } 95 96 protected void fireChange() { 97 throw new RuntimeException ( 98 "fireChange should not be called, use fireValueChange() instead"); } 100 101 106 public void dispose() { 107 valueChangeListeners = null; 108 super.dispose(); 109 } 110 } 111 | Popular Tags |