1 11 12 package org.eclipse.jface.internal.databinding.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.internal.databinding.provisional.Binding; 19 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 20 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener; 21 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 22 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff; 23 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry; 24 import org.eclipse.jface.internal.databinding.provisional.observable.list.ObservableList; 25 import org.eclipse.jface.internal.databinding.provisional.observable.list.WritableList; 26 import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue; 27 import org.eclipse.jface.internal.databinding.provisional.validation.ValidationError; 28 29 33 public class ValidationErrorList extends ObservableList { 34 35 private boolean isDirty = true; 36 37 private final WritableList bindings; 38 39 private final boolean usePartialErrors; 40 41 private List dependencies = new ArrayList (); 42 43 private IChangeListener markDirtyChangeListener = new IChangeListener() { 44 public void handleChange(IObservable source) { 45 markDirty(); 46 } 47 }; 48 49 53 public ValidationErrorList(WritableList bindings, 54 boolean usePartialErrors) { 55 super(new ArrayList (), ValidationError.class); 56 this.bindings = bindings; 57 this.usePartialErrors = usePartialErrors; 58 bindings.addChangeListener(markDirtyChangeListener); 59 } 60 61 protected void getterCalled() { 62 recompute(); 63 super.getterCalled(); 64 } 65 66 private void markDirty() { 67 removeElementChangeListener(); 69 final List oldList = wrappedList; 70 ListDiff listDiff = new ListDiff() { 72 ListDiffEntry[] cachedDifferences = null; 73 public ListDiffEntry[] getDifferences() { 74 if(cachedDifferences==null) { 75 recompute(); 76 cachedDifferences = Diffs.computeListDiff(oldList, wrappedList).getDifferences(); 77 } 78 return cachedDifferences; 79 } 80 }; 81 wrappedList = new ArrayList (); 82 isDirty = true; 83 fireListChange(listDiff); 84 } 85 86 private void recompute() { 87 if (isDirty) { 88 List newContents = new ArrayList (); 89 for (Iterator it = bindings.iterator(); it.hasNext();) { 90 Binding binding = (Binding) it.next(); 91 IObservableValue validationError = usePartialErrors ? binding 92 .getPartialValidationError() : binding 93 .getValidationError(); 94 dependencies.add(validationError); 95 validationError.addChangeListener(markDirtyChangeListener); 96 Object validationErrorValue = validationError.getValue(); 97 if (validationErrorValue != null) { 98 newContents.add(validationErrorValue); 99 } 100 } 101 wrappedList.addAll(newContents); 102 isDirty = false; 103 } 104 } 105 106 111 public void dispose() { 112 bindings.removeChangeListener(markDirtyChangeListener); 113 removeElementChangeListener(); 114 super.dispose(); 115 } 116 117 private void removeElementChangeListener() { 118 for (Iterator it = dependencies.iterator(); it.hasNext();) { 119 IObservableValue observableValue = (IObservableValue) it.next(); 120 observableValue.removeChangeListener(markDirtyChangeListener); 121 } 122 } 123 124 public Object getElementType() { 125 return ValidationError.class; 126 } 127 128 } 129 | Popular Tags |