Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 147489 | Differences between
and this patch

Collapse All | Expand All

(-)org/eclipse/core/databinding/validation/OKValidator.java (+50 lines)
Line 0 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.validation;
12
13
import org.eclipse.core.databinding.validation.IValidator;
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
17
/**
18
 * A validator that always returns Status.OK_STATUS.
19
 * <p>
20
 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
21
 * part of a work in progress. There is no guarantee that this API will remain
22
 * unchanged during the 3.3 release cycle. Please do not use this API without
23
 * consulting with the Platform/UI team.
24
 * </p>
25
 * 
26
 * @since 3.3
27
 */
28
public class OKValidator implements IValidator {
29
	
30
	/**
31
	 * Singleton field.
32
	 */
33
	public static final OKValidator OK_VALIDATOR = new OKValidator();
34
	
35
	/**
36
	 * Singleton pattern -- use OKValidator.OK_VALIDATOR.
37
	 */
38
	private OKValidator() {}
39
40
	/**
41
	 * Determines if the given value is valid.
42
	 * 
43
	 * @param value
44
	 *            the value to validate
45
	 * @return always Status.OK_STATUS
46
	 */
47
	public IStatus validate(Object value) {
48
		return Status.OK_STATUS;
49
	}
50
}
(-)org/eclipse/core/databinding/validation/AndValidator.java (+180 lines)
Line 0 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.validation;
12
13
import java.util.ArrayList;
14
15
import org.eclipse.core.databinding.BindSpec;
16
import org.eclipse.core.databinding.util.Policy;
17
import org.eclipse.core.databinding.validation.IValidator;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.MultiStatus;
20
21
/**
22
 * A validator that combines the results of multiple nested validators with AND
23
 * semantics.
24
 * <p>
25
 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
26
 * part of a work in progress. There is no guarantee that this API will remain
27
 * unchanged during the 3.3 release cycle. Please do not use this API without
28
 * consulting with the Platform/UI team.
29
 * </p>
30
 * 
31
 * @since 3.3
32
 */
33
public class AndValidator implements IValidator {
34
	
35
	/**
36
	 * Combine the existing target validator (if one exists) from a {@link BindSpec}
37
	 * with a new validator by wrapping it in an AndValidator.
38
	 * @param bindSpec
39
	 * @param validator the new validator to be combined with bindSpec.getTargetValidator()
40
	 * @return the new AndValidator, which has also been set as the target validator of bindSpec
41
	 */
42
	public static AndValidator target(BindSpec bindSpec, IValidator validator) {
43
		AndValidator and = wrap(validator, bindSpec.getTargetValidator());
44
		bindSpec.setTargetValidator(and);
45
		return and;
46
	}
47
	
48
	/**
49
	 * Combine the existing partial target validator (if one exists) from a {@link BindSpec}
50
	 * with a new validator by wrapping it in an AndValidator.
51
	 * @param bindSpec
52
	 * @param validator the new validator to be combined with bindSpec.getPartialTargetValidator()
53
	 * @return the new AndValidator, which has also been set as the partial target validator of bindSpec
54
	 */
55
	public static AndValidator partialTarget(BindSpec bindSpec, IValidator validator) {
56
		AndValidator and = wrap(validator, bindSpec.getPartialTargetValidator());
57
		bindSpec.setPartialTargetValidator(and);
58
		return and;
59
	}
60
	
61
	/**
62
	 * Combine the existing domain validator (if one exists) from a {@link BindSpec}
63
	 * with a new validator by wrapping it in an AndValidator.
64
	 * @param bindSpec
65
	 * @param validator the new validator to be combined with bindSpec.getDomainValidator()
66
	 * @return the new AndValidator, which has also been set as the domain validator of bindSpec
67
	 */
68
	public static AndValidator domain(BindSpec bindSpec, IValidator validator) {
69
		AndValidator and = wrap(validator, bindSpec.getDomainValidator());
70
		bindSpec.setDomainValidator(and);
71
		return and;
72
	}
73
74
	/**
75
	 * Wraps an existing validator, checking for null.
76
	 * @param validator
77
	 * @param old
78
	 * @return a new AndValidator nesting one or two IValidators
79
	 */
80
	private static AndValidator wrap(IValidator validator, IValidator old) {
81
		if (validator == null)
82
			throw new IllegalArgumentException("validators must be non-null");
83
		
84
		if (old == null) {
85
			return new AndValidator().addValidator(validator);
86
		}
87
		else {
88
			return new AndValidator(old, validator);
89
		}
90
	}
91
	
92
	/**
93
	 * Nested validators.
94
	 */
95
	private ArrayList validators = new ArrayList(4);
96
	
97
	/**
98
	 * Zero-arg bean constructor -- use addValidator() to configure.
99
	 */
100
	public AndValidator() {}
101
	
102
	/**
103
	 * Main constructor -- combines two existing validators.
104
	 */
105
	public AndValidator(IValidator first, IValidator second) {
106
		if (first == null || second == null)
107
			throw new IllegalArgumentException("validators must be non-null");
108
		
109
		validators.add(first);
110
		validators.add(second);
111
	}
112
	
113
	/**
114
	 * Add a new validator to the set of nested validators.
115
	 * @param validator the new validator
116
	 * @return this, to support method chaining
117
	 */
118
	public AndValidator addValidator(IValidator validator) {
119
		if (validator == null)
120
			throw new IllegalArgumentException("validators must be non-null");
121
		
122
		validators.add(validator);
123
		return this;
124
	}
125
	
126
	/**
127
	 * Remove a validator from the set of nested validators.
128
	 * @param validator the validator to remove
129
	 * @return this, to support method chaining
130
	 */
131
	public AndValidator removeValidator(IValidator validator) {
132
		validators.remove(validator);
133
		return this;
134
	}
135
	
136
	/**
137
	 * Get all validators currently nested here.
138
	 * @return the validators
139
	 */
140
	public IValidator[] getValidators() {
141
		return (IValidator[]) validators.toArray(new IValidator[validators.size()]);
142
	}
143
144
	/**
145
	 * Determines if the given value is valid.
146
	 * 
147
	 * @param value
148
	 *            the value to validate
149
	 * @return a status object indicating whether the validation succeeded
150
	 *         {@link IStatus#isOK()} or not. Never null.  IStatus.isMultiStatus() == true
151
	 */
152
	public IStatus validate(Object value) {
153
		// Compute all validations
154
		int size = validators.size();
155
		IStatus[] stati = new IStatus[size];
156
		
157
		int worst = IStatus.OK;
158
		String msg = "";
159
		
160
		for (int i = 0; i < size; i++) {
161
			IValidator v = (IValidator) validators.get(i);
162
			IStatus s = v.validate(value);
163
			stati[i] = s;
164
			
165
			// Hang onto the last, worst message
166
			if (s.getSeverity() >= worst) {
167
				worst = s.getSeverity();
168
				msg = s.getMessage();
169
			}
170
		}
171
		
172
		// init the MultiStatus with the worst message
173
		MultiStatus status = new MultiStatus(Policy.JFACE_DATABINDING, IStatus.OK, msg, null);
174
		for (int i = 0; i < size; i++) {
175
			status.merge(stati[i]);
176
		}
177
		
178
		return status;
179
	}
180
}
(-)org/eclipse/core/databinding/validation/AndValidatorTest.java (+62 lines)
Line 0 Link Here
1
package org.eclipse.core.databinding.validation;
2
3
import org.eclipse.core.databinding.util.Policy;
4
import org.eclipse.core.databinding.validation.IValidator;
5
import org.eclipse.core.databinding.validation.ValidationStatus;
6
import org.eclipse.core.runtime.IStatus;
7
import org.eclipse.core.runtime.Status;
8
9
import junit.framework.TestCase;
10
11
public class AndValidatorTest extends TestCase {
12
13
	private static final String EMSG = "This is bad!";
14
	private static final IValidator ERROR = new IValidator() {
15
		public IStatus validate(Object value) {
16
			return ValidationStatus.error(EMSG);
17
		};
18
	};
19
	
20
	private static final String WMSG = "Not so bad.";
21
	private static final IValidator WARNING = new IValidator() {
22
		public IStatus validate(Object value) {
23
			return new Status(IStatus.WARNING, Policy.JFACE_DATABINDING, WMSG);
24
		};
25
	};
26
	
27
	public void testErrorFirst() {
28
		AndValidator and = new AndValidator(ERROR, OKValidator.OK_VALIDATOR);
29
		IStatus status = and.validate(new Object());
30
		assertEquals("The message for the worst status should be promoted",
31
				EMSG, status.getMessage());
32
		assertEquals("The worst severity should be promoted",
33
				IStatus.ERROR, status.getSeverity());
34
	}
35
	
36
	public void testErrorLast() {
37
		AndValidator and = new AndValidator(OKValidator.OK_VALIDATOR, ERROR);
38
		IStatus status = and.validate(new Object());
39
		assertEquals("The message for the worst status should be promoted",
40
				EMSG, status.getMessage());
41
		assertEquals("The worst severity should be promoted",
42
				IStatus.ERROR, status.getSeverity());
43
	}
44
45
	public void testWarningLast() {
46
		AndValidator and = new AndValidator(OKValidator.OK_VALIDATOR, WARNING);
47
		IStatus status = and.validate(new Object());
48
		assertEquals("The message for the worst status should be promoted",
49
				WMSG, status.getMessage());
50
		assertEquals("The worst severity should be promoted",
51
				IStatus.WARNING, status.getSeverity());
52
	}
53
	
54
	public void testErrorWarning() {
55
		AndValidator and = new AndValidator(ERROR, WARNING);
56
		IStatus status = and.validate(new Object());
57
		assertEquals("The message for the worst status should be promoted",
58
				EMSG, status.getMessage());
59
		assertEquals("The worst severity should be promoted",
60
				IStatus.ERROR, status.getSeverity());
61
	}
62
}

Return to bug 147489