1 11 package org.eclipse.core.internal.databinding.observable.masterdetail; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.databinding.observable.Diffs; 17 import org.eclipse.core.databinding.observable.IObserving; 18 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory; 19 import org.eclipse.core.databinding.observable.set.IObservableSet; 20 import org.eclipse.core.databinding.observable.set.ISetChangeListener; 21 import org.eclipse.core.databinding.observable.set.ObservableSet; 22 import org.eclipse.core.databinding.observable.set.SetChangeEvent; 23 import org.eclipse.core.databinding.observable.value.IObservableValue; 24 import org.eclipse.core.databinding.observable.value.IValueChangeListener; 25 import org.eclipse.core.databinding.observable.value.ValueChangeEvent; 26 import org.eclipse.core.runtime.Assert; 27 28 32 public class DetailObservableSet extends ObservableSet implements IObserving { 33 34 private boolean updating = false; 35 36 private ISetChangeListener innerChangeListener = new ISetChangeListener() { 37 public void handleSetChange(SetChangeEvent event) { 38 if (!updating) { 39 fireSetChange(event.diff); 40 } 41 } 42 }; 43 44 private Object currentOuterValue; 45 46 private IObservableSet innerObservableSet; 47 48 private IObservableValue outerObservableValue; 49 50 private IObservableFactory factory; 51 52 57 public DetailObservableSet(IObservableFactory factory, 58 IObservableValue outerObservableValue, Object detailType) { 59 super(outerObservableValue.getRealm(), new HashSet (), detailType); 60 this.factory = factory; 61 this.outerObservableValue = outerObservableValue; 62 updateInnerObservableValue(outerObservableValue); 63 64 outerObservableValue.addValueChangeListener(outerChangeListener); 65 } 66 67 IValueChangeListener outerChangeListener = new IValueChangeListener() { 68 public void handleValueChange(ValueChangeEvent event) { 69 Set oldSet = new HashSet (wrappedSet); 70 updateInnerObservableValue(outerObservableValue); 71 fireSetChange(Diffs.computeSetDiff(oldSet, wrappedSet)); 72 } 73 }; 74 75 private void updateInnerObservableValue( 76 IObservableValue outerObservableValue) { 77 currentOuterValue = outerObservableValue.getValue(); 78 if (innerObservableSet != null) { 79 innerObservableSet.removeSetChangeListener(innerChangeListener); 80 innerObservableSet.dispose(); 81 } 82 if (currentOuterValue == null) { 83 innerObservableSet = null; 84 wrappedSet = new HashSet (); 85 } else { 86 this.innerObservableSet = (IObservableSet) factory 87 .createObservable(currentOuterValue); 88 wrappedSet = innerObservableSet; 89 90 if (elementType != null) { 91 Object innerValueType = innerObservableSet.getElementType(); 92 93 Assert.isTrue(elementType.equals(innerValueType), 94 "Cannot change value type in a nested observable set"); } 96 97 innerObservableSet.addSetChangeListener(innerChangeListener); 98 } 99 } 100 101 public void dispose() { 102 super.dispose(); 103 104 if (outerObservableValue != null) { 105 outerObservableValue.removeValueChangeListener(outerChangeListener); 106 outerObservableValue.dispose(); 107 } 108 if (innerObservableSet != null) { 109 innerObservableSet.removeSetChangeListener(innerChangeListener); 110 innerObservableSet.dispose(); 111 } 112 currentOuterValue = null; 113 factory = null; 114 innerObservableSet = null; 115 innerChangeListener = null; 116 } 117 118 public Object getObserved() { 119 if (innerObservableSet instanceof IObserving) { 120 return ((IObserving)innerObservableSet).getObserved(); 121 } 122 return null; 123 } 124 125 } 126 | Popular Tags |