comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/AggregateValidationStatus.d @ 78:0a55d2d5a946

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