78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Matt Carter - bug 182822
|
|
11 * Boris Bokowski - bug 218269
|
|
12 * Matthew Hall - bug 218269
|
|
13 *******************************************************************************/
|
|
14 module org.eclipse.core.databinding.AggregateValidationStatus;
|
81
|
15 import org.eclipse.core.databinding.DataBindingContext;
|
|
16 import org.eclipse.core.databinding.ValidationStatusProvider;
|
78
|
17
|
|
18 import java.lang.all;
|
|
19
|
|
20 import java.util.ArrayList;
|
|
21 import java.util.Collection;
|
|
22 import java.util.Iterator;
|
|
23 import java.util.List;
|
|
24
|
|
25 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
26 import org.eclipse.core.databinding.observable.IObservableCollection;
|
|
27 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
28 import org.eclipse.core.databinding.observable.Realm;
|
|
29 import org.eclipse.core.databinding.observable.value.ComputedValue;
|
|
30 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
31 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
32 import org.eclipse.core.databinding.util.Policy;
|
|
33 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
34 import org.eclipse.core.runtime.IStatus;
|
|
35 import org.eclipse.core.runtime.MultiStatus;
|
|
36 import org.eclipse.core.runtime.Status;
|
|
37
|
|
38 /**
|
|
39 * This class can be used to aggregate status values from a data binding context
|
|
40 * into a single status value. Instances of this class can be used as an
|
|
41 * observable value with a value type of {@link IStatus}, or the static methods
|
|
42 * can be called directly if an aggregated status result is only needed once.
|
|
43 *
|
|
44 * @since 1.0
|
|
45 *
|
|
46 */
|
|
47 public final class AggregateValidationStatus : IObservableValue {
|
|
48
|
|
49 private IObservableValue implementation;
|
|
50
|
|
51 /**
|
|
52 * Constant denoting an aggregation strategy that merges multiple non-OK
|
|
53 * status objects in a {@link MultiStatus}. Returns an OK status result if
|
|
54 * all statuses from the given validation status providers are the an OK
|
|
55 * status. Returns a single status if there is only one non-OK status.
|
|
56 *
|
|
57 * @see #getStatusMergedcast(Collection)
|
|
58 */
|
|
59 public static final int MERGED = 1;
|
|
60
|
|
61 /**
|
|
62 * Constant denoting an aggregation strategy that always returns the most
|
|
63 * severe status from the given validation status providers. If there is
|
|
64 * more than one status at the same severity level, it picks the first one
|
|
65 * it encounters.
|
|
66 *
|
|
67 * @see #getStatusMaxSeveritycast(Collection)
|
|
68 */
|
|
69 public static final int MAX_SEVERITY = 2;
|
|
70
|
|
71 /**
|
|
72 * Creates a new aggregate validation status observable for the given data
|
|
73 * binding context.
|
|
74 *
|
|
75 * @param dbc
|
|
76 * a data binding context
|
|
77 * @param strategy
|
|
78 * a strategy constant, one of {@link #MERGED} or
|
|
79 * {@link #MAX_SEVERITY}.
|
|
80 * @since 1.1
|
|
81 */
|
|
82 public this(DataBindingContext dbc, int strategy) {
|
|
83 this(dbc.getValidationRealm(), dbc.getValidationStatusProviders(),
|
|
84 strategy);
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * @param validationStatusProviders
|
|
89 * an observable collection containing elements of type
|
|
90 * {@link ValidationStatusProvider}
|
|
91 * @param strategy
|
|
92 * a strategy constant, one of {@link #MERGED} or
|
|
93 * {@link #MAX_SEVERITY}.
|
|
94 * @see DataBindingContext#getValidationStatusProviders()
|
|
95 */
|
|
96 public this(
|
|
97 IObservableCollection validationStatusProviders, strategy) {
|
|
98 this(Realm.getDefault(), validationStatusProviders, strategy);
|
|
99 }
|
|
100
|
|
101 /**
|
|
102 * @param realm
|
|
103 * Realm
|
|
104 * @param validationStatusProviders
|
|
105 * an observable collection containing elements of type
|
|
106 * {@link ValidationStatusProvider}
|
|
107 * @param strategy
|
|
108 * a strategy constant, one of {@link #MERGED} or
|
|
109 * {@link #MAX_SEVERITY}.
|
|
110 * @see DataBindingContext#getValidationStatusProviders()
|
|
111 * @since 1.1
|
|
112 */
|
|
113 public this(Realm realm,
|
|
114 IObservableCollection validationStatusProviders, int strategy) {
|
|
115 if (strategy is MERGED) {
|
|
116 implementation = new class(realm, IStatus.classinfo, validationStatusProviders) ComputedValue {
|
|
117 IObservableCollection validationStatusProviders_;
|
|
118 this(Realm r, ClassInfo c, IObservableCollection v){
|
|
119 super(r, c);
|
|
120 validationStatusProviders_=v;
|
|
121 }
|
|
122 protected Object calculate() {
|
|
123 return getStatusMerged(validationStatusProviders_);
|
|
124 }
|
|
125 };
|
|
126 } else {
|
|
127 implementation = new class(realm, IStatus.classinfo, validationStatusProviders) ComputedValue {
|
|
128 IObservableCollection validationStatusProviders_;
|
|
129 this(Realm r, ClassInfo c, IObservableCollection v){
|
|
130 super(r, c);
|
|
131 validationStatusProviders_=v;
|
|
132 }
|
|
133 protected Object calculate() {
|
|
134 return getStatusMaxSeverity(validationStatusProviders_);
|
|
135 }
|
|
136 };
|
|
137 }
|
|
138 }
|
|
139
|
|
140 /**
|
|
141 * @param listener
|
|
142 * @see org.eclipse.core.databinding.observable.IObservable#addChangeListener(org.eclipse.core.databinding.observable.IChangeListener)
|
|
143 */
|
|
144 public void addChangeListener(IChangeListener listener) {
|
|
145 implementation.addChangeListener(listener);
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * @param listener
|
|
150 * @see org.eclipse.core.databinding.observable.IObservable#addStaleListener(org.eclipse.core.databinding.observable.IStaleListener)
|
|
151 */
|
|
152 public void addStaleListener(IStaleListener listener) {
|
|
153 implementation.addStaleListener(listener);
|
|
154 }
|
|
155
|
|
156 /**
|
|
157 * @param listener
|
|
158 * @see org.eclipse.core.databinding.observable.value.IObservableValue#addValueChangeListener(org.eclipse.core.databinding.observable.value.IValueChangeListener)
|
|
159 */
|
|
160 public void addValueChangeListener(IValueChangeListener listener) {
|
|
161 implementation.addValueChangeListener(listener);
|
|
162 }
|
|
163
|
|
164 public void dispose() {
|
|
165 implementation.dispose();
|
|
166 }
|
|
167
|
|
168 public Realm getRealm() {
|
|
169 return implementation.getRealm();
|
|
170 }
|
|
171
|
|
172 public Object getValue() {
|
|
173 return implementation.getValue();
|
|
174 }
|
|
175
|
|
176 public Object getValueType() {
|
|
177 return implementation.getValueType();
|
|
178 }
|
|
179
|
|
180 public bool isStale() {
|
|
181 return implementation.isStale();
|
|
182 }
|
|
183
|
|
184 public void removeChangeListener(IChangeListener listener) {
|
|
185 implementation.removeChangeListener(listener);
|
|
186 }
|
|
187
|
|
188 public void removeStaleListener(IStaleListener listener) {
|
|
189 implementation.removeStaleListener(listener);
|
|
190 }
|
|
191
|
|
192 public void removeValueChangeListener(IValueChangeListener listener) {
|
|
193 implementation.removeValueChangeListener(listener);
|
|
194 }
|
|
195
|
|
196 public void setValue(Object value) {
|
|
197 implementation.setValue(value);
|
|
198 }
|
|
199
|
|
200 /**
|
|
201 * Returns a status object that merges multiple non-OK status objects in a
|
|
202 * {@link MultiStatus}. Returns an OK status result if all statuses from
|
|
203 * the given validation status providers are the an OK status. Returns a
|
|
204 * single status if there is only one non-OK status.
|
|
205 *
|
|
206 * @param validationStatusProviders
|
|
207 * a collection of validation status providers
|
|
208 * @return a merged status
|
|
209 */
|
|
210 public static IStatus getStatusMerged(Collection validationStatusProviders) {
|
|
211 List statuses = new ArrayList();
|
|
212 for (Iterator it = validationStatusProviders.iterator(); it.hasNext();) {
|
|
213 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
214 .next();
|
|
215 IStatus status = cast(IStatus) validationStatusProvider
|
|
216 .getValidationStatus().getValue();
|
|
217 if (!status.isOK()) {
|
|
218 statuses.add(status);
|
|
219 }
|
|
220 }
|
|
221 if (statuses.size() is 1) {
|
|
222 return cast(IStatus) statuses.get(0);
|
|
223 }
|
|
224 if (!statuses.isEmpty()) {
|
|
225 MultiStatus result = new MultiStatus(Policy.JFACE_DATABINDING, 0,
|
|
226 BindingMessages
|
|
227 .getStringcast(BindingMessages.MULTIPLE_PROBLEMS), null);
|
|
228 for (Iterator it = statuses.iterator(); it.hasNext();) {
|
|
229 IStatus status = cast(IStatus) it.next();
|
|
230 result.merge(status);
|
|
231 }
|
|
232 return result;
|
|
233 }
|
|
234 return Status.OK_STATUS;
|
|
235 }
|
|
236
|
|
237 /**
|
|
238 * Returns a status that always returns the most severe status from the
|
|
239 * given validation status providers. If there is more than one status at
|
|
240 * the same severity level, it picks the first one it encounters.
|
|
241 *
|
|
242 * @param validationStatusProviders
|
|
243 * a collection of validation status providers
|
|
244 * @return a single status reflecting the most severe status from the given
|
|
245 * validation status providers
|
|
246 */
|
|
247 public static IStatus getStatusMaxSeverity(
|
|
248 Collection validationStatusProviders) {
|
|
249 int maxSeverity = IStatus.OK;
|
|
250 IStatus maxStatus = Status.OK_STATUS;
|
|
251 for (Iterator it = validationStatusProviders.iterator(); it.hasNext();) {
|
|
252 ValidationStatusProvider validationStatusProvider = cast(ValidationStatusProvider) it
|
|
253 .next();
|
|
254 IStatus status = cast(IStatus) validationStatusProvider
|
|
255 .getValidationStatus().getValue();
|
|
256 if (status.getSeverity() > maxSeverity) {
|
|
257 maxSeverity = status.getSeverity();
|
|
258 maxStatus = status;
|
|
259 }
|
|
260 }
|
|
261 return maxStatus;
|
|
262 }
|
|
263
|
|
264 }
|