|
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 |
} |