KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > AggregateValidationStatus


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  *******************************************************************************/

11 package org.eclipse.core.databinding;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
32  * This class can be used to aggregate status values from a data binding context
33  * into a single status value. Instances of this class can be used as an
34  * observable value with a value type of {@link IStatus}, or the static methods
35  * can be called directly if an aggregated status result is only needed once.
36  *
37  * @since 1.0
38  *
39  */

40 public final class AggregateValidationStatus implements IObservableValue {
41
42     private IObservableValue implementation;
43
44     /**
45      * Constant denoting an aggregation strategy that merges multiple non-OK
46      * status objects in a {@link MultiStatus}. Returns an OK status result if
47      * all statuses from the given bindings are the an OK status. Returns a
48      * single status if there is only one non-OK status.
49      *
50      * @see #getStatusMerged(Collection)
51      */

52     public static final int MERGED = 1;
53
54     /**
55      * Constant denoting an aggregation strategy that always returns the most
56      * severe status from the given bindings. If there is more than one status
57      * at the same severity level, it picks the first one it encounters.
58      *
59      * @see #getStatusMaxSeverity(Collection)
60      */

61     public static final int MAX_SEVERITY = 2;
62
63     /**
64      * @param bindings
65      * an observable collection containing elements of type IStatus
66      * @param strategy
67      * a strategy constant, one of {@link #MERGED} or
68      * {@link #MAX_SEVERITY}.
69      */

70     public AggregateValidationStatus(final IObservableCollection bindings,
71             int strategy) {
72         if (strategy == MERGED) {
73             implementation = new ComputedValue(IStatus.class) {
74                 protected Object JavaDoc calculate() {
75                     return getStatusMerged(bindings);
76                 }
77             };
78         } else {
79             implementation = new ComputedValue(IStatus.class) {
80                 protected Object JavaDoc calculate() {
81                     return getStatusMaxSeverity(bindings);
82                 }
83             };
84         }
85     }
86
87     /**
88      * @param listener
89      * @see org.eclipse.core.databinding.observable.IObservable#addChangeListener(org.eclipse.core.databinding.observable.IChangeListener)
90      */

91     public void addChangeListener(IChangeListener listener) {
92         implementation.addChangeListener(listener);
93     }
94
95     /**
96      * @param listener
97      * @see org.eclipse.core.databinding.observable.IObservable#addStaleListener(org.eclipse.core.databinding.observable.IStaleListener)
98      */

99     public void addStaleListener(IStaleListener listener) {
100         implementation.addStaleListener(listener);
101     }
102
103     /**
104      * @param listener
105      * @see org.eclipse.core.databinding.observable.value.IObservableValue#addValueChangeListener(org.eclipse.core.databinding.observable.value.IValueChangeListener)
106      */

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 JavaDoc getValue() {
120         return implementation.getValue();
121     }
122
123     public Object JavaDoc 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 JavaDoc value) {
144         implementation.setValue(value);
145     }
146
147     /**
148      * Returns a status object that merges multiple non-OK status objects in a
149      * {@link MultiStatus}. Returns an OK status result if all statuses from
150      * the given bindings are the an OK status. Returns a single status if there
151      * is only one non-OK status.
152      *
153      * @param bindings
154      * a collection of bindings
155      * @return a merged status
156      */

157     public static IStatus getStatusMerged(Collection JavaDoc bindings) {
158         List JavaDoc statuses = new ArrayList JavaDoc();
159         for (Iterator JavaDoc 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 JavaDoc 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     /**
187      * Returns a status that always returns the most severe status from the
188      * given bindings. If there is more than one status at the same severity
189      * level, it picks the first one it encounters.
190      *
191      * @param bindings
192      * a collection of bindings
193      * @return a single status reflecting the most severe status from the given
194      * bindings
195      */

196     public static IStatus getStatusMaxSeverity(Collection JavaDoc bindings) {
197         int maxSeverity = IStatus.OK;
198         IStatus maxStatus = Status.OK_STATUS;
199         for (Iterator JavaDoc 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