1 11 package org.eclipse.core.databinding; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.databinding.observable.IChangeListener; 19 import org.eclipse.core.databinding.observable.IObservableCollection; 20 import org.eclipse.core.databinding.observable.IStaleListener; 21 import org.eclipse.core.databinding.observable.Realm; 22 import org.eclipse.core.databinding.observable.value.ComputedValue; 23 import org.eclipse.core.databinding.observable.value.IObservableValue; 24 import org.eclipse.core.databinding.observable.value.IValueChangeListener; 25 import org.eclipse.core.databinding.util.Policy; 26 import org.eclipse.core.internal.databinding.BindingMessages; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.MultiStatus; 29 import org.eclipse.core.runtime.Status; 30 31 40 public final class AggregateValidationStatus implements IObservableValue { 41 42 private IObservableValue implementation; 43 44 52 public static final int MERGED = 1; 53 54 61 public static final int MAX_SEVERITY = 2; 62 63 70 public AggregateValidationStatus(final IObservableCollection bindings, 71 int strategy) { 72 if (strategy == MERGED) { 73 implementation = new ComputedValue(IStatus.class) { 74 protected Object calculate() { 75 return getStatusMerged(bindings); 76 } 77 }; 78 } else { 79 implementation = new ComputedValue(IStatus.class) { 80 protected Object calculate() { 81 return getStatusMaxSeverity(bindings); 82 } 83 }; 84 } 85 } 86 87 91 public void addChangeListener(IChangeListener listener) { 92 implementation.addChangeListener(listener); 93 } 94 95 99 public void addStaleListener(IStaleListener listener) { 100 implementation.addStaleListener(listener); 101 } 102 103 107 public void addValueChangeListener(IValueChangeListener listener) { 108 implementation.addValueChangeListener(listener); 109 } 110 111 public void dispose() { 112 implementation.dispose(); 113 } 114 115 public Realm getRealm() { 116 return implementation.getRealm(); 117 } 118 119 public Object getValue() { 120 return implementation.getValue(); 121 } 122 123 public Object getValueType() { 124 return implementation.getValueType(); 125 } 126 127 public boolean isStale() { 128 return implementation.isStale(); 129 } 130 131 public void removeChangeListener(IChangeListener listener) { 132 implementation.removeChangeListener(listener); 133 } 134 135 public void removeStaleListener(IStaleListener listener) { 136 implementation.removeStaleListener(listener); 137 } 138 139 public void removeValueChangeListener(IValueChangeListener listener) { 140 implementation.removeValueChangeListener(listener); 141 } 142 143 public void setValue(Object value) { 144 implementation.setValue(value); 145 } 146 147 157 public static IStatus getStatusMerged(Collection bindings) { 158 List statuses = new ArrayList (); 159 for (Iterator it = bindings.iterator(); it.hasNext();) { 160 Binding binding = (Binding) it.next(); 161 IStatus status = (IStatus) binding 162 .getValidationStatus().getValue(); 163 if (!status.isOK()) { 164 statuses.add(status); 165 } 166 } 167 if (statuses.size() == 1) { 168 return (IStatus) statuses.get(0); 169 } 170 if (!statuses.isEmpty()) { 171 MultiStatus result = new MultiStatus( 172 Policy.JFACE_DATABINDING, 173 0, 174 BindingMessages 175 .getString(BindingMessages.MULTIPLE_PROBLEMS), 176 null); 177 for (Iterator it = statuses.iterator(); it.hasNext();) { 178 IStatus status = (IStatus) it.next(); 179 result.merge(status); 180 } 181 return result; 182 } 183 return Status.OK_STATUS; 184 } 185 186 196 public static IStatus getStatusMaxSeverity(Collection bindings) { 197 int maxSeverity = IStatus.OK; 198 IStatus maxStatus = Status.OK_STATUS; 199 for (Iterator it = bindings.iterator(); it.hasNext();) { 200 Binding binding = (Binding) it.next(); 201 IStatus status = (IStatus) binding 202 .getValidationStatus().getValue(); 203 if (status.getSeverity() > maxSeverity) { 204 maxSeverity = status.getSeverity(); 205 maxStatus = status; 206 } 207 } 208 return maxStatus; 209 } 210 211 } | Popular Tags |