### Eclipse Workspace Patch 1.0
#P org.eclipse.core.databinding
Index: .settings/org.eclipse.jdt.ui.prefs
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/.settings/org.eclipse.jdt.ui.prefs,v
retrieving revision 1.3
diff -u -r1.3 org.eclipse.jdt.ui.prefs
--- .settings/org.eclipse.jdt.ui.prefs 16 Feb 2009 23:03:21 -0000 1.3
+++ .settings/org.eclipse.jdt.ui.prefs 19 Sep 2010 15:12:42 -0000
@@ -1,4 +1,4 @@
-#Tue Feb 10 16:05:48 MST 2009
+#Thu Jan 07 19:10:54 CET 2010
cleanup.add_default_serial_version_id=true
cleanup.add_generated_serial_version_id=false
cleanup.add_missing_annotations=true
@@ -51,7 +51,7 @@
cleanup_profile=org.eclipse.jdt.ui.default.eclipse_clean_up_profile
cleanup_settings_version=2
eclipse.preferences.version=1
-editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
+editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=false
formatter_profile=org.eclipse.jdt.ui.default.eclipse_profile
formatter_settings_version=11
org.eclipse.jdt.ui.exception.name=e
@@ -78,7 +78,7 @@
sp_cleanup.always_use_this_for_non_static_method_access=false
sp_cleanup.convert_to_enhanced_for_loop=false
sp_cleanup.correct_indentation=false
-sp_cleanup.format_source_code=true
+sp_cleanup.format_source_code=false
sp_cleanup.format_source_code_changes_only=false
sp_cleanup.make_local_variable_final=false
sp_cleanup.make_parameters_final=false
Index: META-INF/MANIFEST.MF
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/META-INF/MANIFEST.MF,v
retrieving revision 1.22
diff -u -r1.22 MANIFEST.MF
--- META-INF/MANIFEST.MF 10 Mar 2010 07:20:11 -0000 1.22
+++ META-INF/MANIFEST.MF 19 Sep 2010 15:12:42 -0000
@@ -2,13 +2,15 @@
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.core.databinding
-Bundle-Version: 1.3.100.qualifier
+Bundle-Version: 1.4.0.qualifier
Bundle-ClassPath: .
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.core.databinding,
org.eclipse.core.databinding.conversion;x-internal:=false,
+ org.eclipse.core.databinding.editing,
org.eclipse.core.databinding.validation;x-internal:=false,
+ org.eclipse.core.databinding.validation.constraint,
org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
org.eclipse.core.internal.databinding.conversion;x-friends:="org.eclipse.jface.tests.databinding",
org.eclipse.core.internal.databinding.validation;x-friends:="org.eclipse.jface.tests.databinding"
Index: src/org/eclipse/core/databinding/UpdateStrategy.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/databinding/UpdateStrategy.java,v
retrieving revision 1.17
diff -u -r1.17 UpdateStrategy.java
--- src/org/eclipse/core/databinding/UpdateStrategy.java 25 May 2009 20:52:19 -0000 1.17
+++ src/org/eclipse/core/databinding/UpdateStrategy.java 19 Sep 2010 15:12:42 -0000
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* Matt Carter - bug 180392
* - bug 197679 (Character support completed)
+ * Ovidio Mallo - bug 183055
*******************************************************************************/
package org.eclipse.core.databinding;
@@ -24,6 +25,7 @@
import org.eclipse.core.databinding.util.Policy;
import org.eclipse.core.internal.databinding.ClassLookupSupport;
import org.eclipse.core.internal.databinding.Pair;
+import org.eclipse.core.internal.databinding.conversion.BooleanToStringConverter;
import org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter;
import org.eclipse.core.internal.databinding.conversion.IdentityConverter;
import org.eclipse.core.internal.databinding.conversion.IntegerToStringConverter;
@@ -35,7 +37,6 @@
import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter;
import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter;
import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter;
-import org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter;
import org.eclipse.core.internal.databinding.conversion.StringToByteConverter;
import org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter;
import org.eclipse.core.internal.databinding.conversion.StringToShortConverter;
@@ -307,7 +308,7 @@
new Pair(BOOLEAN_CLASS, "java.lang.Boolean"), new IdentityConverter(Boolean.class, Boolean.class)); //$NON-NLS-1$
converterMap
.put(
- new Pair(BOOLEAN_CLASS, "java.lang.String"), new ObjectToStringConverter(Boolean.class)); //$NON-NLS-1$
+ new Pair(BOOLEAN_CLASS, "java.lang.String"), new BooleanToStringConverter(Boolean.class)); //$NON-NLS-1$
converterMap
.put(
new Pair(BOOLEAN_CLASS, "java.lang.Object"), new IdentityConverter(Boolean.class, Object.class)); //$NON-NLS-1$
Index: src/org/eclipse/core/databinding/editing/BigDecimalEditing.java
===================================================================
RCS file: src/org/eclipse/core/databinding/editing/BigDecimalEditing.java
diff -N src/org/eclipse/core/databinding/editing/BigDecimalEditing.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/editing/BigDecimalEditing.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,197 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.editing;
+
+import java.math.BigDecimal;
+
+import org.eclipse.core.databinding.conversion.NumberToStringConverter;
+import org.eclipse.core.databinding.conversion.StringToNumberConverter;
+import org.eclipse.core.databinding.validation.constraint.BigDecimalConstraints;
+import org.eclipse.core.databinding.validation.constraint.Constraints;
+import org.eclipse.core.databinding.validation.constraint.StringConstraints;
+import org.eclipse.core.internal.databinding.validation.AbstractStringToNumberValidator;
+import org.eclipse.core.internal.databinding.validation.StringToBigDecimalValidator;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * @noextend This class is not intended to be subclassed by clients.
+ * @since 1.4
+ */
+public class BigDecimalEditing extends Editing {
+
+ private final NumberFormat displayFormat;
+
+ /**
+ * Creates a new editing object for BigDecimal
s.
+ *
+ * @param format
+ * The BigDecimal format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigDecimal.
+ *
+ * @noreference This constructor is not intended to be referenced by
+ * clients.
+ */
+ protected BigDecimalEditing(NumberFormat format, String parseErrorMessage) {
+ this.displayFormat = format;
+
+ final StringToNumberConverter targetConverter = StringToNumberConverter
+ .toBigDecimal(format);
+ final NumberToStringConverter modelConverter = NumberToStringConverter
+ .fromBigDecimal(format);
+
+ final AbstractStringToNumberValidator targetValidator = new StringToBigDecimalValidator(
+ targetConverter);
+ if (parseErrorMessage != null) {
+ targetValidator.setParseErrorMessage(parseErrorMessage);
+ }
+
+ setTargetConverter(targetConverter);
+ setModelConverter(modelConverter);
+ targetConstraints().addValidator(targetValidator);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigDecimal}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getNumberInstance()
+ */
+ public static BigDecimalEditing withDefaults() {
+ return withDefaults(null);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigDecimal}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation message.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigDecimal.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getNumberInstance()
+ */
+ public static BigDecimalEditing withDefaults(String parseErrorMessage) {
+ return new BigDecimalEditing(NumberFormat.getNumberInstance(),
+ parseErrorMessage);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigDecimal}s whose validations
+ * and conversions used for editing are based on the given BigDecimal
+ * format. Uses the default validation messages.
+ *
+ * @param format
+ * The BigDecimal format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given BigDecimal format.
+ */
+ public static BigDecimalEditing forFormat(NumberFormat format) {
+ return forFormat(format, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigDecimal}s whose validations
+ * and conversions used for editing are based on the given BigDecimal
+ * format. Uses the specified custom validation message.
+ *
+ * @param format
+ * The BigDecimal format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigDecimal.
+ * @return The new editing object configured by the given BigDecimal format.
+ */
+ public static BigDecimalEditing forFormat(NumberFormat format,
+ String parseErrorMessage) {
+ return new BigDecimalEditing(format, parseErrorMessage);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ *
+ * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link BigDecimalConstraints BigDecimal model constraints} of this + * editing object and is equivalent to {@code (BigDecimalConstraints) + * modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see BigDecimalConstraints + */ + public BigDecimalConstraints modelBigDecimalConstraints() { + return (BigDecimalConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link BigDecimalConstraints BigDecimal before-set model constraints} of + * this editing object and is equivalent to {@code (BigDecimalConstraints) + * beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see BigDecimalConstraints + */ + public BigDecimalConstraints beforeSetModelBigDecimalConstraints() { + return (BigDecimalConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new BigDecimalConstraints().bigDecimalFormat(displayFormat); + } + + protected Constraints createBeforeSetModelConstraints() { + return new BigDecimalConstraints().bigDecimalFormat(displayFormat); + } +} Index: src/org/eclipse/core/databinding/editing/BigIntegerEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/BigIntegerEditing.java diff -N src/org/eclipse/core/databinding/editing/BigIntegerEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/BigIntegerEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import java.math.BigInteger; + +import org.eclipse.core.databinding.conversion.NumberToStringConverter; +import org.eclipse.core.databinding.conversion.StringToNumberConverter; +import org.eclipse.core.databinding.validation.constraint.BigIntegerConstraints; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.validation.AbstractStringToNumberValidator; +import org.eclipse.core.internal.databinding.validation.StringToBigIntegerValidator; + +import com.ibm.icu.text.NumberFormat; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class BigIntegerEditing extends Editing { + + private final NumberFormat displayFormat; + + /** + * Creates a new editing object forBigInteger
s.
+ *
+ * @param format
+ * The BigInteger format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigInteger.
+ *
+ * @noreference This constructor is not intended to be referenced by
+ * clients.
+ */
+ protected BigIntegerEditing(NumberFormat format, String parseErrorMessage) {
+ this.displayFormat = format;
+
+ final StringToNumberConverter targetConverter = StringToNumberConverter
+ .toBigInteger(format);
+ final NumberToStringConverter modelConverter = NumberToStringConverter
+ .fromBigInteger(format);
+
+ final AbstractStringToNumberValidator targetValidator = new StringToBigIntegerValidator(
+ targetConverter);
+ if (parseErrorMessage != null) {
+ targetValidator.setParseErrorMessage(parseErrorMessage);
+ }
+
+ setTargetConverter(targetConverter);
+ setModelConverter(modelConverter);
+ targetConstraints().addValidator(targetValidator);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigInteger}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static BigIntegerEditing withDefaults() {
+ return withDefaults(null);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigInteger}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation message.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigInteger.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static BigIntegerEditing withDefaults(String parseErrorMessage) {
+ return new BigIntegerEditing(NumberFormat.getIntegerInstance(),
+ parseErrorMessage);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigInteger}s whose validations
+ * and conversions used for editing are based on the given BigInteger
+ * format. Uses the default validation messages.
+ *
+ * @param format
+ * The BigInteger format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given BigInteger format.
+ */
+ public static BigIntegerEditing forFormat(NumberFormat format) {
+ return forFormat(format, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link BigInteger}s whose validations
+ * and conversions used for editing are based on the given BigInteger
+ * format. Uses the specified custom validation message.
+ *
+ * @param format
+ * The BigInteger format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a BigInteger.
+ * @return The new editing object configured by the given BigInteger format.
+ */
+ public static BigIntegerEditing forFormat(NumberFormat format,
+ String parseErrorMessage) {
+ return new BigIntegerEditing(format, parseErrorMessage);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link BigIntegerConstraints BigInteger model constraints} of this + * editing object and is equivalent to {@code (BigIntegerConstraints) + * modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see BigIntegerConstraints + */ + public BigIntegerConstraints modelBigIntegerConstraints() { + return (BigIntegerConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link BigIntegerConstraints BigInteger before-set model constraints} of + * this editing object and is equivalent to {@code (BigIntegerConstraints) + * beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see BigIntegerConstraints + */ + public BigIntegerConstraints beforeSetModelBigIntegerConstraints() { + return (BigIntegerConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new BigIntegerConstraints().bigIntegerFormat(displayFormat); + } + + protected Constraints createBeforeSetModelConstraints() { + return new BigIntegerConstraints().bigIntegerFormat(displayFormat); + } +} Index: src/org/eclipse/core/databinding/editing/BooleanEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/BooleanEditing.java diff -N src/org/eclipse/core/databinding/editing/BooleanEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/BooleanEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,204 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.validation.constraint.BooleanConstraints; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.conversion.BooleanToStringConverter; +import org.eclipse.core.internal.databinding.conversion.StringToBooleanConverter; +import org.eclipse.core.internal.databinding.validation.StringToBooleanValidator; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class BooleanEditing extends Editing { + + /** + * Creates a new editing object for booleans. + * + * @param trueValues + * The set of strings representing atrue
value.
+ * @param falseValues
+ * The set of strings representing a false
value.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a boolean.
+ *
+ * @noreference This constructor is not intended to be referenced by
+ * clients.
+ */
+ protected BooleanEditing(String[] trueValues, String[] falseValues,
+ String parseErrorMessage) {
+ StringToBooleanConverter targetConverter = new StringToBooleanConverter();
+ BooleanToStringConverter modelConverter = new BooleanToStringConverter(
+ Boolean.class);
+
+ StringToBooleanValidator targetValidator = new StringToBooleanValidator();
+ if (parseErrorMessage != null) {
+ targetValidator.setParseErrorMessage(parseErrorMessage);
+ }
+
+ if (trueValues != null && falseValues != null) {
+ targetValidator.setSourceStrings(trueValues, falseValues);
+ targetConverter.setSourceStrings(trueValues, falseValues);
+ modelConverter.setTargetStrings(trueValues[0], falseValues[0]);
+ }
+
+ setTargetConverter(targetConverter);
+ setModelConverter(modelConverter);
+ targetConstraints().addValidator(targetValidator);
+ }
+
+ /**
+ * Creates a new editing object which uses the default set of string
+ * representations for boolean values for parsing and displaying.
+ *
+ * @return The new editing object for the default set of string
+ * representations for boolean values.
+ */
+ public static BooleanEditing withDefaults() {
+ return withDefaults(null);
+ }
+
+ /**
+ * Creates a new editing object which uses the default set of string
+ * representations for boolean values for parsing and displaying. Uses the
+ * specified custom validation message.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a boolean.
+ * @return The new editing object for the default set of string
+ * representations for boolean values.
+ */
+ public static BooleanEditing withDefaults(String parseErrorMessage) {
+ return new BooleanEditing(null, null, parseErrorMessage);
+ }
+
+ /**
+ * Creates a new editing object which uses the given set of string
+ * representations for boolean values for parsing and displaying.
+ *
+ * + * For parsing, the given string values will be considered valid + * representations of {@code true} and {@code false}, respectively. For + * displaying, the first element of the respective array will be used for + * representing the corresponding boolean value. + *
+ * + * @param trueValues + * The set of strings representing atrue
value.
+ * @param falseValues
+ * The set of strings representing a false
value.
+ * @return The new editing object for the given set of string
+ * representations for boolean values.
+ */
+ public static BooleanEditing forStringValues(String[] trueValues,
+ String[] falseValues) {
+ return forStringValues(trueValues, falseValues, null);
+ }
+
+ /**
+ * Creates a new editing object which uses the given set of string
+ * representations for boolean values for parsing and displaying. Uses the
+ * specified custom validation message.
+ *
+ * + * For parsing, the given string values will be considered valid + * representations of {@code true} and {@code false}, respectively. For + * displaying, the first element of the respective array will be used for + * representing the corresponding boolean value. + *
+ * + * @param trueValues + * The set of strings representing atrue
value.
+ * @param falseValues
+ * The set of strings representing a false
value.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a boolean.
+ * @return The new editing object for the given set of string
+ * representations for boolean values.
+ */
+ public static BooleanEditing forStringValues(String[] trueValues,
+ String[] falseValues, String parseErrorMessage) {
+ return new BooleanEditing(trueValues, falseValues, parseErrorMessage);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link BooleanConstraints + * bool model constraints} of this editing object and is equivalent to + * {@code (BooleanConstraints) modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see BooleanConstraints + */ + public BooleanConstraints modelBooleanConstraints() { + return (BooleanConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link BooleanConstraints + * bool before-set model constraints} of this editing object and is + * equivalent to {@code (BooleanConstraints) beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see BooleanConstraints + */ + public BooleanConstraints beforeSetModelBooleanConstraints() { + return (BooleanConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new BooleanConstraints(); + } + + protected Constraints createBeforeSetModelConstraints() { + return new BooleanConstraints(); + } +} Index: src/org/eclipse/core/databinding/editing/CharacterEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/CharacterEditing.java diff -N src/org/eclipse/core/databinding/editing/CharacterEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/CharacterEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,145 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.validation.constraint.CharacterConstraints; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter; +import org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter; +import org.eclipse.core.internal.databinding.validation.StringToCharacterValidator; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class CharacterEditing extends Editing { + + /** + * Creates a new editing object for booleans. + * + * @param parseErrorMessage + * The validation message issued in case the input string is not + * a valid character. + * + * @noreference This constructor is not intended to be referenced by + * clients. + */ + protected CharacterEditing(String parseErrorMessage) { + StringToCharacterConverter targetConverter = new StringToCharacterConverter( + false); + CharacterToStringConverter modelConverter = CharacterToStringConverter + .fromCharacter(false); + + StringToCharacterValidator targetValidator = new StringToCharacterValidator( + targetConverter); + if (parseErrorMessage != null) { + targetValidator.setParseErrorMessage(parseErrorMessage); + } + + setTargetConverter(targetConverter); + setModelConverter(modelConverter); + targetConstraints().addValidator(targetValidator); + } + + /** + * Creates a new editing object which uses the default validations and + * conversions for the editing of characters. + * + * @return The new editing object for the default editing of characters. + */ + public static CharacterEditing withDefaults() { + return withDefaults(null); + } + + /** + * Creates a new editing object which uses the default validations and + * conversions for the editing of characters. Uses the specified custom + * validation message. + * + * @param parseErrorMessage + * The validation message issued in case the input string is not + * a valid character. + * @return The new editing object for the default editing of characters. + */ + public static CharacterEditing withDefaults(String parseErrorMessage) { + return new CharacterEditing(parseErrorMessage); + } + + /** + * Returns the target constraints to apply. + * + *+ * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link CharacterConstraints character model constraints} of this editing + * object and is equivalent to {@code (CharacterConstraints) + * modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see CharacterConstraints + */ + public CharacterConstraints modelCharacterConstraints() { + return (CharacterConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the + * {@link CharacterConstraints character before-set model constraints} of + * this editing object and is equivalent to {@code (CharacterConstraints) + * beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see CharacterConstraints + */ + public CharacterConstraints beforeSetModelCharacterConstraints() { + return (CharacterConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new CharacterConstraints(); + } + + protected Constraints createBeforeSetModelConstraints() { + return new CharacterConstraints(); + } +} Index: src/org/eclipse/core/databinding/editing/DateEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/DateEditing.java diff -N src/org/eclipse/core/databinding/editing/DateEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/DateEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,196 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.DateConstraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.conversion.DateToStringConverter; +import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; +import org.eclipse.core.internal.databinding.validation.StringToDateValidator; + +import com.ibm.icu.text.DateFormat; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class DateEditing extends Editing { + + private final DateFormat displayFormat; + + /** + * Creates a new editing object forDate
s.
+ *
+ * @param inputFormats
+ * The date formats supported for reading in dates.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a date.
+ * @param displayFormat
+ * The date format for displaying dates.
+ *
+ * @noreference This constructor is not intended to be referenced by
+ * clients.
+ */
+ protected DateEditing(DateFormat[] inputFormats, String parseErrorMessage,
+ DateFormat displayFormat) {
+ this.displayFormat = displayFormat;
+
+ StringToDateConverter targetConverter = new StringToDateConverter();
+ if (inputFormats != null) {
+ targetConverter.setFormatters(inputFormats);
+ }
+
+ DateToStringConverter modelConverter = new DateToStringConverter();
+ if (displayFormat != null) {
+ modelConverter.setFormatters(new DateFormat[] { displayFormat });
+ }
+
+ StringToDateValidator targetValidator = new StringToDateValidator(
+ targetConverter);
+ if (parseErrorMessage != null) {
+ targetValidator.setParseErrorMessage(parseErrorMessage);
+ }
+
+ setTargetConverter(targetConverter);
+ setModelConverter(modelConverter);
+ targetConstraints().addValidator(targetValidator);
+ }
+
+ /**
+ * Creates a new editing object which defaults the validations and
+ * conversions used for editing. Uses the default validation message.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ */
+ public static DateEditing withDefaults() {
+ return withDefaults(null);
+ }
+
+ /**
+ * Creates a new editing object which defaults the validations and
+ * conversions used for editing. Uses the specified custom validation
+ * message.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a date.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ */
+ public static DateEditing withDefaults(String parseErrorMessage) {
+ return new DateEditing(null, parseErrorMessage, null);
+ }
+
+ /**
+ * Creates a new editing object which supports all the given date input
+ * formats and which uses the specified format for displaying a date. Uses
+ * the default validation message.
+ *
+ * @param inputFormats
+ * The date formats supported for reading in dates.
+ * @param displayFormat
+ * The date format for displaying dates.
+ * @return The new editing object configured by the given date formats.
+ */
+ public static DateEditing forFormats(DateFormat[] inputFormats,
+ DateFormat displayFormat) {
+ return new DateEditing(inputFormats, null, displayFormat);
+ }
+
+ /**
+ * Creates a new editing object which supports all the given date input
+ * formats and which uses the specified format for displaying a date. Uses
+ * the specified custom validation message.
+ *
+ * @param inputFormats
+ * The date formats supported for reading in dates.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a date.
+ * @param displayFormat
+ * The date format for displaying dates.
+ * @return The new editing object configured by the given date formats.
+ */
+ public static DateEditing forFormats(DateFormat[] inputFormats,
+ String parseErrorMessage, DateFormat displayFormat) {
+ return new DateEditing(inputFormats, parseErrorMessage, displayFormat);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link DateConstraints date + * model constraints} of this editing object and is equivalent to + * {@code (DateConstraints) modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see DateConstraints + */ + public DateConstraints modelDateConstraints() { + return (DateConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link DateConstraints date + * before-set model constraints} of this editing object and is equivalent to + * {@code (DateConstraints) beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see DateConstraints + */ + public DateConstraints beforeSetModelDateConstraints() { + return (DateConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new DateConstraints().dateFormat(displayFormat); + } + + protected Constraints createBeforeSetModelConstraints() { + return new DateConstraints().dateFormat(displayFormat); + } +} Index: src/org/eclipse/core/databinding/editing/DecimalEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/DecimalEditing.java diff -N src/org/eclipse/core/databinding/editing/DecimalEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/DecimalEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,301 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.conversion.NumberToStringConverter; +import org.eclipse.core.databinding.conversion.StringToNumberConverter; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.DecimalConstraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.validation.AbstractStringToNumberValidator; +import org.eclipse.core.internal.databinding.validation.StringToDoubleValidator; +import org.eclipse.core.internal.databinding.validation.StringToFloatValidator; + +import com.ibm.icu.text.NumberFormat; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class DecimalEditing extends Editing { + + private final NumberFormat displayFormat; + + /** + * Creates a new editing object for decimal numbers. + * + * @param format + * The number format defining the validations and conversions + * used for editing. + * @param parseErrorMessage + * The validation message issued in case the input string cannot + * be parsed to a double. + * @param outOfRangeMessage + * The validation message issued in case the input string + * represents a double whose value is out of range. + * @param decimalType + * The specific decimal type for which to set up an editing + * instance. + * + * @noreference This constructor is not intended to be referenced by + * clients. + */ + protected DecimalEditing(NumberFormat format, String parseErrorMessage, + String outOfRangeMessage, Class decimalType) { + this.displayFormat = format; + + final StringToNumberConverter targetConverter; + final NumberToStringConverter modelConverter; + final AbstractStringToNumberValidator targetValidator; + if (Double.class.equals(decimalType)) { + targetConverter = StringToNumberConverter.toDouble(format, false); + modelConverter = NumberToStringConverter.fromDouble(format, false); + targetValidator = new StringToDoubleValidator(targetConverter); + } else if (Float.class.equals(decimalType)) { + targetConverter = StringToNumberConverter.toFloat(format, false); + modelConverter = NumberToStringConverter.fromFloat(format, false); + targetValidator = new StringToFloatValidator(targetConverter); + } else { + throw new IllegalArgumentException( + "Unsupported decimal type: " + decimalType); //$NON-NLS-1$ + } + + if (parseErrorMessage != null) { + targetValidator.setParseErrorMessage(parseErrorMessage); + } + if (outOfRangeMessage != null) { + targetValidator.setOutOfRangeMessage(outOfRangeMessage); + } + + setTargetConverter(targetConverter); + setModelConverter(modelConverter); + targetConstraints().addValidator(targetValidator); + } + + /** + * Creates a new editing object for {@link Double}s which defaults the + * validations and conversions used for editing based on the platform's + * locale. Uses the default validation messages. + * + * @return The new editing object using the default validations and + * conversions for editing. + * + * @see NumberFormat#getNumberInstance() + */ + public static DecimalEditing withDoubleDefaults() { + return withDoubleDefaults(null, null); + } + + /** + * Creates a new editing object for {@link Double}s which defaults the + * validations and conversions used for editing based on the platform's + * locale. Uses the specified custom validation messages. + * + * @param parseErrorMessage + * The validation message issued in case the input string cannot + * be parsed to a double. + * @param outOfRangeMessage + * The validation message issued in case the input string + * represents a double whose value is out of range. Can be + * parameterized by the-Double.MAX_VALUE
({0}) and
+ * +Double.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getNumberInstance()
+ */
+ public static DecimalEditing withDoubleDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new DecimalEditing(NumberFormat.getNumberInstance(),
+ parseErrorMessage, outOfRangeMessage, Double.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Double}s whose validations and
+ * conversions used for editing are based on the given number format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The number format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given number format.
+ */
+ public static DecimalEditing forDoubleFormat(NumberFormat format) {
+ return forDoubleFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Double}s whose validations and
+ * conversions used for editing are based on the given number format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The number format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a double.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents a double whose value is out of range. Can be
+ * parameterized by the -Double.MAX_VALUE
({0}) and
+ * +Double.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given number format.
+ */
+ public static DecimalEditing forDoubleFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new DecimalEditing(format, parseErrorMessage, outOfRangeMessage,
+ Double.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Float}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getNumberInstance()
+ */
+ public static DecimalEditing withFloatDefaults() {
+ return withFloatDefaults(null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Float}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation messages.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a double.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents a float whose value is out of range. Can be
+ * parameterized by the -Float.MAX_VALUE
({0}) and
+ * +Float.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getNumberInstance()
+ */
+ public static DecimalEditing withFloatDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new DecimalEditing(NumberFormat.getNumberInstance(),
+ parseErrorMessage, outOfRangeMessage, Float.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Float}s whose validations and
+ * conversions used for editing are based on the given number format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The number format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given number format.
+ */
+ public static DecimalEditing forFloatFormat(NumberFormat format) {
+ return forFloatFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Float}s whose validations and
+ * conversions used for editing are based on the given number format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The number format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to a float.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents a float whose value is out of range. Can be
+ * parameterized by the -Float.MAX_VALUE
({0}) and
+ * +Float.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given number format.
+ */
+ public static DecimalEditing forFloatFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new DecimalEditing(format, parseErrorMessage, outOfRangeMessage,
+ Float.class);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link DecimalConstraints + * decimal model constraints} of this editing object and is equivalent to + * {@code (DecimalConstraints) modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see DecimalConstraints + */ + public DecimalConstraints modelDecimalConstraints() { + return (DecimalConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link DecimalConstraints + * decimal before-set model constraints} of this editing object and is + * equivalent to {@code (DecimalConstraints) beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see DecimalConstraints + */ + public DecimalConstraints beforeSetModelDecimalConstraints() { + return (DecimalConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new DecimalConstraints().decimalFormat(displayFormat); + } + + protected Constraints createBeforeSetModelConstraints() { + return new DecimalConstraints().decimalFormat(displayFormat); + } +} Index: src/org/eclipse/core/databinding/editing/Editing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/Editing.java diff -N src/org/eclipse/core/databinding/editing/Editing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/Editing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,962 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.Binding; +import org.eclipse.core.databinding.DataBindingContext; +import org.eclipse.core.databinding.UpdateListStrategy; +import org.eclipse.core.databinding.UpdateSetStrategy; +import org.eclipse.core.databinding.UpdateValueStrategy; +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.databinding.observable.list.IObservableList; +import org.eclipse.core.databinding.observable.set.IObservableSet; +import org.eclipse.core.databinding.observable.value.IObservableValue; +import org.eclipse.core.databinding.validation.IValidator; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; + +/** + * @since 1.4 + */ +public class Editing { + + private Constraints targetConstraints; + + private Constraints modelConstraints; + + private Constraints beforeSetModelConstraints; + + private IConverter targetConverter; + + private IConverter modelConverter; + + /** + * Default constructor which creates an unconfigured editing instance. + */ + protected Editing() { + // empty editing instance + } + + /** + * Creates an unconfigured editing instance. + * + * @return A new, unconfigured editing instance. + */ + public static Editing create() { + return new Editing(); + } + + /** + * Creates an editing instance with the given target-to-model and + * model-to-target converters. + * + * @param t2mConverter + * The target-to-model converter to set. May benull
+ * .
+ * @param m2tConverter
+ * The model-to-target converter to set. May be null
+ * .
+ * @return A new editing instance with the given target-to-model and
+ * model-to-target converters.
+ */
+ public static Editing withConverters(IConverter t2mConverter,
+ IConverter m2tConverter) {
+ Editing editing = new Editing();
+ editing.setTargetConverter(t2mConverter);
+ editing.setModelConverter(m2tConverter);
+ return editing;
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * @return The target constraints to apply.
+ *
+ * @see #createTargetConstraints()
+ */
+ public final Constraints targetConstraints() {
+ if (targetConstraints == null) {
+ targetConstraints = createTargetConstraints();
+ }
+ return targetConstraints;
+ }
+
+ /**
+ * Returns the model constraints to apply.
+ *
+ * @return The model constraints to apply.
+ *
+ * @see #createModelConstraints()
+ */
+ public final Constraints modelConstraints() {
+ if (modelConstraints == null) {
+ modelConstraints = createModelConstraints();
+ }
+ return modelConstraints;
+ }
+
+ /**
+ * Returns the before-set model constraints to apply.
+ *
+ * @return The before-set model constraints to apply.
+ *
+ * @see #createBeforeSetModelConstraints()
+ */
+ public final Constraints beforeSetModelConstraints() {
+ if (beforeSetModelConstraints == null) {
+ beforeSetModelConstraints = createBeforeSetModelConstraints();
+ }
+ return beforeSetModelConstraints;
+ }
+
+ /**
+ * Creates the target constraints to apply.
+ *
+ * + * The constraints object created by this method will be exposed as the + * {@link #targetConstraints()} of this editing object. By default, this + * method returns a plain {@link Constraints} object. + *
+ * + *+ * This method will be called lazily the first time the target constraints + * are accessed. Subclasses may overwrite this method in order to create a + * custom constraints object which will then be typically exposed in the + * subclass as API in order to allow for a typesafe access. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + */ + protected Constraints createTargetConstraints() { + return new Constraints(); + } + + /** + * Creates the model constraints to apply. + * + *+ * The constraints object created by this method will be exposed as the + * {@link #modelConstraints()} of this editing object. By default, this + * method returns a plain {@link Constraints} object. + *
+ * + *+ * This method will be called lazily the first time the model constraints + * are accessed. Subclasses may overwrite this method in order to create a + * custom constraints object which will then be typically exposed in the + * subclass as API in order to allow for a typesafe access. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + */ + protected Constraints createModelConstraints() { + return new Constraints(); + } + + /** + * Creates the before-set model constraints to apply. + * + *+ * The constraints object created by this method will be exposed as the + * {@link #beforeSetModelConstraints()} of this editing object. By default, + * this method returns a plain {@link Constraints} object. + *
+ * + *+ * This method will be called lazily the first time the before-set model + * constraints are accessed. Subclasses may overwrite this method in order + * to create a custom constraints object which will then be typically + * exposed in the subclass as API in order to allow for a typesafe access. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + */ + protected Constraints createBeforeSetModelConstraints() { + return new Constraints(); + } + + /** + * Sets the target-to-model converter for this editing instance. + * + * @param converter + * The target-to-model converter to set. + */ + protected final void setTargetConverter(IConverter converter) { + this.targetConverter = converter; + } + + /** + * Sets the model-to-target converter for this editing instance. + * + * @param converter + * The model-to-target converter to set. + */ + protected final void setModelConverter(IConverter converter) { + this.modelConverter = converter; + } + + /** + * Creates a new target-to-model {@link UpdateValueStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new target-to-model {@link UpdateValueStrategy} configured by + * the current state of this editing object. + * + * @see UpdateValueStrategy#UpdateValueStrategy() + * @see #adaptT2MValueStrategy(UpdateValueStrategy) + */ + public final UpdateValueStrategy createT2MValueStrategy() { + return adaptT2MValueStrategy(new UpdateValueStrategy()); + } + + /** + * Creates a new target-to-model {@link UpdateValueStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new target-to-model {@link UpdateValueStrategy} configured by + * the current state of this editing object. + * + * @see UpdateValueStrategy#UpdateValueStrategy(int) + * @see #adaptT2MValueStrategy(UpdateValueStrategy) + */ + public final UpdateValueStrategy createT2MValueStrategy(int updatePolicy) { + return adaptT2MValueStrategy(new UpdateValueStrategy(updatePolicy)); + } + + /** + * Creates a new model-to-target {@link UpdateValueStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new model-to-target {@link UpdateValueStrategy} configured by + * the current state of this editing object. + * + * @see UpdateValueStrategy#UpdateValueStrategy() + * @see #adaptM2TValueStrategy(UpdateValueStrategy) + */ + public final UpdateValueStrategy createM2TValueStrategy() { + return adaptM2TValueStrategy(new UpdateValueStrategy()); + } + + /** + * Creates a new model-to-target {@link UpdateValueStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new model-to-target {@link UpdateValueStrategy} configured by + * the current state of this editing object. + * + * @see UpdateValueStrategy#UpdateValueStrategy(int) + * @see #adaptM2TValueStrategy(UpdateValueStrategy) + */ + public final UpdateValueStrategy createM2TValueStrategy(int updatePolicy) { + return adaptM2TValueStrategy(new UpdateValueStrategy(updatePolicy)); + } + + /** + * Creates a new target-to-model {@link UpdateListStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new target-to-model {@link UpdateListStrategy} configured by + * the current state of this editing object. + * + * @see UpdateListStrategy#UpdateListStrategy() + * @see #adaptT2MListStrategy(UpdateListStrategy) + */ + public final UpdateListStrategy createT2MListStrategy() { + return adaptT2MListStrategy(new UpdateListStrategy()); + } + + /** + * Creates a new target-to-model {@link UpdateListStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new target-to-model {@link UpdateListStrategy} configured by + * the current state of this editing object. + * + * @see UpdateListStrategy#UpdateListStrategy(int) + * @see #adaptT2MListStrategy(UpdateListStrategy) + */ + public final UpdateListStrategy createT2MListStrategy(int updatePolicy) { + return adaptT2MListStrategy(new UpdateListStrategy(updatePolicy)); + } + + /** + * Creates a new model-to-target {@link UpdateListStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new model-to-target {@link UpdateListStrategy} configured by + * the current state of this editing object. + * + * @see UpdateListStrategy#UpdateListStrategy() + * @see #adaptM2TListStrategy(UpdateListStrategy) + */ + public final UpdateListStrategy createM2TListStrategy() { + return adaptM2TListStrategy(new UpdateListStrategy()); + } + + /** + * Creates a new model-to-target {@link UpdateListStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new model-to-target {@link UpdateListStrategy} configured by + * the current state of this editing object. + * + * @see UpdateListStrategy#UpdateListStrategy(int) + * @see #adaptM2TListStrategy(UpdateListStrategy) + */ + public final UpdateListStrategy createM2TListStrategy(int updatePolicy) { + return adaptM2TListStrategy(new UpdateListStrategy(updatePolicy)); + } + + /** + * Creates a new target-to-model {@link UpdateSetStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new target-to-model {@link UpdateSetStrategy} configured by the + * current state of this editing object. + * + * @see UpdateListStrategy#UpdateListStrategy() + * @see #adaptT2MSetStrategy(UpdateSetStrategy) + */ + public final UpdateSetStrategy createT2MSetStrategy() { + return adaptT2MSetStrategy(new UpdateSetStrategy()); + } + + /** + * Creates a new target-to-model {@link UpdateListStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new target-to-model {@link UpdateListStrategy} configured by + * the current state of this editing object. + * + * @see UpdateSetStrategy#UpdateSetStrategy(int) + * @see #adaptT2MSetStrategy(UpdateSetStrategy) + */ + public final UpdateSetStrategy createT2MSetStrategy(int updatePolicy) { + return adaptT2MSetStrategy(new UpdateSetStrategy(updatePolicy)); + } + + /** + * Creates a new model-to-target {@link UpdateSetStrategy} with a default + * update policy configured by the current state of this editing object. + * + * @return A new model-to-target {@link UpdateSetStrategy} configured by the + * current state of this editing object. + * + * @see UpdateSetStrategy#UpdateSetStrategy() + * @see #adaptM2TSetStrategy(UpdateSetStrategy) + */ + public final UpdateSetStrategy createM2TSetStrategy() { + return adaptM2TSetStrategy(new UpdateSetStrategy()); + } + + /** + * Creates a new model-to-target {@link UpdateSetStrategy} with the given + * update policy configured by the current state of this editing object. + * + * @param updatePolicy + * The update policy to use. + * @return A new model-to-target {@link UpdateSetStrategy} configured by the + * current state of this editing object. + * + * @see UpdateSetStrategy#UpdateSetStrategy(int) + * @see #adaptM2TSetStrategy(UpdateSetStrategy) + */ + public final UpdateSetStrategy createM2TSetStrategy(int updatePolicy) { + return adaptM2TSetStrategy(new UpdateSetStrategy(updatePolicy)); + } + + /** + * Configures an existing target-to-model {@link UpdateValueStrategy} with + * the current state of this editing object. + * + *+ * The configuration is done as follows: + *
+ * The conversion process will be aborted by returning null
+ * whenever any of the applied validators produces a {@link IStatus
+ * validation status} having {@link IStatus#getSeverity() severity}
+ * IStatus.ERROR
or IStatus.CANCEL
. During the
+ * conversion process, any validation status whose severity is different
+ * from IStatus.OK
will be {@link MultiStatus#merge(IStatus)
+ * aggregated} on the given validationStatus
.
+ *
null
.
+ * @return The converted model value or null
in case the
+ * conversion has been aborted due to a validation error.
+ */
+ public final Object convertToModel(Object targetValue,
+ MultiStatus validationStatus) {
+ if (!applyConstraints(targetConstraints, targetValue, validationStatus)) {
+ return null;
+ }
+
+ Object modelValue = applyConverter(targetConverter, targetValue);
+
+ if (!applyConstraints(modelConstraints, modelValue, validationStatus)) {
+ return null;
+ }
+
+ if (!applyConstraints(beforeSetModelConstraints, modelValue,
+ validationStatus)) {
+ return null;
+ }
+
+ return modelValue;
+ }
+
+ /**
+ * {@link #setModelConverter(IConverter) Converts} a model value to a target
+ * value.
+ *
+ * @param modelValue
+ * The model value to be converted to a target value.
+ * @return The converted target value.
+ */
+ public final Object convertToTarget(Object modelValue) {
+ return applyConverter(modelConverter, modelValue);
+ }
+
+ /**
+ * Creates a binding between a target and model observable value on the
+ * given binding context by creating new update strategies which will be
+ * both configured with the state of this editing object before passing them
+ * to the binding.
+ *
+ * + * The target-to-model and model-to-target update strategies for the binding + * will be created by the methods {@link #createT2MValueStrategy()} and + * {@link #createM2TValueStrategy()}, respectively. + *
+ * + * @param dbc + * The binding context on which to create the value binding. + * @param targetObservableValue + * The target observable value of the binding. + * @param modelObservableValue + * The model observable value of the binding. + * @return The new value binding. + * + * @see #createT2MValueStrategy() + * @see #createM2TValueStrategy() + * @see DataBindingContext#bindValue(IObservableValue, IObservableValue, + * UpdateValueStrategy, UpdateValueStrategy) + */ + public final Binding bindValue(DataBindingContext dbc, + IObservableValue targetObservableValue, + IObservableValue modelObservableValue) { + return dbc.bindValue(targetObservableValue, modelObservableValue, + createT2MValueStrategy(), createM2TValueStrategy()); + } + + /** + * Creates a binding between a target and model observable value on the + * given binding context by creating new update strategies with the provided + * update policies which will be both configured with the state of this + * editing object before passing them to the binding. + * + * @param dbc + * The binding context on which to create the value binding. + * @param targetObservableValue + * The target observable value of the binding. + * @param modelObservableValue + * The model observable value of the binding. + * @param t2mUpdatePolicy + * The update policy for the target-to-model + * {@link UpdateValueStrategy} which is + * {@link #createT2MValueStrategy(int) created} and passed to the + * new binding. + * @param m2tUpdatePolicy + * The update policy for the model-to-target + * {@link UpdateValueStrategy} which is + * {@link #createM2TValueStrategy(int) created} and passed to the + * new binding. + * @return The new value binding. + * + * @see #createT2MValueStrategy(int) + * @see #createM2TValueStrategy(int) + * @see DataBindingContext#bindValue(IObservableValue, IObservableValue, + * UpdateValueStrategy, UpdateValueStrategy) + */ + public final Binding bindValue(DataBindingContext dbc, + IObservableValue targetObservableValue, + IObservableValue modelObservableValue, int t2mUpdatePolicy, + int m2tUpdatePolicy) { + return dbc.bindValue(targetObservableValue, modelObservableValue, + createT2MValueStrategy(t2mUpdatePolicy), + createM2TValueStrategy(m2tUpdatePolicy)); + } + + /** + * Creates a binding between a target and model observable value on the + * given binding context by using the provided update strategies which will + * be both configured with the state of this editing object before passing + * them to the binding. + * + * @param dbc + * The binding context on which to create the value binding. + * @param targetObservableValue + * The target observable value of the binding. + * @param modelObservableValue + * The model observable value of the binding. + * @param t2mUpdateStrategy + * The target-to-model {@link UpdateValueStrategy} of the binding + * to be {@link #adaptT2MValueStrategy(UpdateValueStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @param m2tUpdateStrategy + * The model-to-target {@link UpdateValueStrategy} of the binding + * to be {@link #adaptM2TValueStrategy(UpdateValueStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @return The new value binding. + * + * @see #adaptT2MValueStrategy(UpdateValueStrategy) + * @see #adaptM2TValueStrategy(UpdateValueStrategy) + * @see DataBindingContext#bindValue(IObservableValue, IObservableValue, + * UpdateValueStrategy, UpdateValueStrategy) + */ + public final Binding bindValue(DataBindingContext dbc, + IObservableValue targetObservableValue, + IObservableValue modelObservableValue, + UpdateValueStrategy t2mUpdateStrategy, + UpdateValueStrategy m2tUpdateStrategy) { + return dbc.bindValue(targetObservableValue, modelObservableValue, + adaptT2MValueStrategy(t2mUpdateStrategy), + adaptM2TValueStrategy(m2tUpdateStrategy)); + } + + /** + * Creates a binding between a target and model observable list on the given + * binding context by creating new update strategies which will be both + * configured with the state of this editing object before passing them to + * the binding. + * + *+ * The target-to-model and model-to-target update strategies for the binding + * will be created by the methods {@link #createT2MListStrategy()} and + * {@link #createM2TListStrategy()}, respectively. + *
+ * + * @param dbc + * The binding context on which to create the list binding. + * @param targetObservableList + * The target observable list of the binding. + * @param modelObservableList + * The model observable list of the binding. + * @return The new list binding. + * + * @see #createT2MListStrategy() + * @see #createM2TListStrategy() + * @see DataBindingContext#bindList(IObservableList, IObservableList, + * UpdateListStrategy, UpdateListStrategy) + */ + public final Binding bindList(DataBindingContext dbc, + IObservableList targetObservableList, + IObservableList modelObservableList) { + return dbc.bindList(targetObservableList, modelObservableList, + createT2MListStrategy(), createM2TListStrategy()); + } + + /** + * Creates a binding between a target and model observable list on the given + * binding context by creating new update strategies with the provided + * update policies which will be both configured with the state of this + * editing object before passing them to the binding. + * + * @param dbc + * The binding context on which to create the list binding. + * @param targetObservableList + * The target observable list of the binding. + * @param modelObservableList + * The model observable list of the binding. + * @param t2mUpdatePolicy + * The update policy for the target-to-model + * {@link UpdateListStrategy} which is + * {@link #createT2MListStrategy(int) created} and passed to the + * new binding. + * @param m2tUpdatePolicy + * The update policy for the model-to-target + * {@link UpdateListStrategy} which is + * {@link #createM2TListStrategy(int) created} and passed to the + * new binding. + * @return The new list binding. + * + * @see #createT2MListStrategy(int) + * @see #createM2TListStrategy(int) + * @see DataBindingContext#bindList(IObservableList, IObservableList, + * UpdateListStrategy, UpdateListStrategy) + */ + public final Binding bindList(DataBindingContext dbc, + IObservableList targetObservableList, + IObservableList modelObservableList, int t2mUpdatePolicy, + int m2tUpdatePolicy) { + return dbc.bindList(targetObservableList, modelObservableList, + createT2MListStrategy(t2mUpdatePolicy), + createM2TListStrategy(m2tUpdatePolicy)); + } + + /** + * Creates a binding between a target and model observable list on the given + * binding context by using the provided update strategies which will be + * both configured with the state of this editing object before passing them + * to the binding. + * + * @param dbc + * The binding context on which to create the list binding. + * @param targetObservableList + * The target observable list of the binding. + * @param modelObservableList + * The model observable list of the binding. + * @param t2mUpdateStrategy + * The target-to-model {@link UpdateListStrategy} of the binding + * to be {@link #adaptT2MListStrategy(UpdateListStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @param m2tUpdateStrategy + * The model-to-target {@link UpdateListStrategy} of the binding + * to be {@link #adaptM2TListStrategy(UpdateListStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @return The new list binding. + * + * @see #adaptT2MListStrategy(UpdateListStrategy) + * @see #adaptM2TListStrategy(UpdateListStrategy) + * @see DataBindingContext#bindList(IObservableList, IObservableList, + * UpdateListStrategy, UpdateListStrategy) + */ + public final Binding bindList(DataBindingContext dbc, + IObservableList targetObservableList, + IObservableList modelObservableList, + UpdateListStrategy t2mUpdateStrategy, + UpdateListStrategy m2tUpdateStrategy) { + return dbc.bindList(targetObservableList, modelObservableList, + adaptT2MListStrategy(t2mUpdateStrategy), + adaptM2TListStrategy(m2tUpdateStrategy)); + } + + /** + * Creates a binding between a target and model observable set on the given + * binding context by creating new update strategies which will be both + * configured with the state of this editing object before passing them to + * the binding. + * + *+ * The target-to-model and model-to-target update strategies for the binding + * will be created by the methods {@link #createT2MSetStrategy()} and + * {@link #createM2TSetStrategy()}, respectively. + *
+ * + * @param dbc + * The binding context on which to create the set binding. + * @param targetObservableSet + * The target observable set of the binding. + * @param modelObservableSet + * The model observable set of the binding. + * @return The new set binding. + * + * @see #createT2MSetStrategy() + * @see #createM2TSetStrategy() + * @see DataBindingContext#bindSet(IObservableSet, IObservableSet, + * UpdateSetStrategy, UpdateSetStrategy) + */ + public final Binding bindSet(DataBindingContext dbc, + IObservableSet targetObservableSet, + IObservableSet modelObservableSet) { + return dbc.bindSet(targetObservableSet, modelObservableSet, + createT2MSetStrategy(), createM2TSetStrategy()); + } + + /** + * Creates a binding between a target and model observable set on the given + * binding context by creating new update strategies with the provided + * update policies which will be both configured with the state of this + * editing object before passing them to the binding. + * + * @param dbc + * The binding context on which to create the set binding. + * @param targetObservableSet + * The target observable set of the binding. + * @param modelObservableSet + * The model observable set of the binding. + * @param t2mUpdatePolicy + * The update policy for the target-to-model + * {@link UpdateSetStrategy} which is + * {@link #createT2MSetStrategy(int) created} and passed to the + * new binding. + * @param m2tUpdatePolicy + * The update policy for the model-to-target + * {@link UpdateSetStrategy} which is + * {@link #createM2TSetStrategy(int) created} and passed to the + * new binding. + * @return The new set binding. + * + * @see #createT2MSetStrategy(int) + * @see #createM2TSetStrategy(int) + * @see DataBindingContext#bindSet(IObservableSet, IObservableSet, + * UpdateSetStrategy, UpdateSetStrategy) + */ + public final Binding bindSet(DataBindingContext dbc, + IObservableSet targetObservableSet, + IObservableSet modelObservableSet, int t2mUpdatePolicy, + int m2tUpdatePolicy) { + return dbc.bindSet(targetObservableSet, modelObservableSet, + createT2MSetStrategy(t2mUpdatePolicy), + createM2TSetStrategy(m2tUpdatePolicy)); + } + + /** + * Creates a binding between a target and model observable set on the given + * binding context by using the provided update strategies which will be + * both configured with the state of this editing object before passing them + * to the binding. + * + * @param dbc + * The binding context on which to create the set binding. + * @param targetObservableSet + * The target observable set of the binding. + * @param modelObservableSet + * The model observable set of the binding. + * @param t2mUpdateStrategy + * The target-to-model {@link UpdateSetStrategy} of the binding + * to be {@link #adaptT2MSetStrategy(UpdateSetStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @param m2tUpdateStrategy + * The model-to-target {@link UpdateSetStrategy} of the binding + * to be {@link #adaptM2TSetStrategy(UpdateSetStrategy) + * configured} with the state of this editing object before + * passing it to the binding. + * @return The new set binding. + * + * @see #adaptT2MSetStrategy(UpdateSetStrategy) + * @see #adaptM2TSetStrategy(UpdateSetStrategy) + * @see DataBindingContext#bindSet(IObservableSet, IObservableSet, + * UpdateSetStrategy, UpdateSetStrategy) + */ + public final Binding bindSet(DataBindingContext dbc, + IObservableSet targetObservableSet, + IObservableSet modelObservableSet, + UpdateSetStrategy t2mUpdateStrategy, + UpdateSetStrategy m2tUpdateStrategy) { + return dbc.bindSet(targetObservableSet, modelObservableSet, + adaptT2MSetStrategy(t2mUpdateStrategy), + adaptM2TSetStrategy(m2tUpdateStrategy)); + } + + private static IValidator createValidator(Constraints constraints) { + return constraints != null ? constraints.createValidator() : null; + } + + private static boolean applyConstraints(Constraints constraints, + Object value, MultiStatus aggregateStatus) { + IValidator validator = createValidator(constraints); + if (validator != null) { + IStatus validationStatus = validator.validate(value); + if (aggregateStatus != null && !validationStatus.isOK()) { + aggregateStatus.merge(validationStatus); + } + return isValid(validationStatus); + } + return true; + } + + private static Object applyConverter(IConverter converter, Object value) { + return converter != null ? converter.convert(value) : value; + } + + private static boolean isValid(IStatus status) { + return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); + } +} Index: src/org/eclipse/core/databinding/editing/IntegerEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/IntegerEditing.java diff -N src/org/eclipse/core/databinding/editing/IntegerEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/IntegerEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,463 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.conversion.NumberToStringConverter; +import org.eclipse.core.databinding.conversion.StringToNumberConverter; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.IntegerConstraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.validation.AbstractStringToNumberValidator; +import org.eclipse.core.internal.databinding.validation.StringToByteValidator; +import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator; +import org.eclipse.core.internal.databinding.validation.StringToLongValidator; +import org.eclipse.core.internal.databinding.validation.StringToShortValidator; + +import com.ibm.icu.text.NumberFormat; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class IntegerEditing extends Editing { + + private final NumberFormat displayFormat; + + /** + * Creates a new editing object for integer numbers. + * + * @param format + * The integer format defining the validations and conversions + * used for editing. + * @param parseErrorMessage + * The validation message issued in case the input string cannot + * be parsed to an integer. + * @param outOfRangeMessage + * The validation message issued in case the input string + * represents an integer whose value is out of range. + * @param integerType + * The specific integer type for which to set up an editing + * instance. + * + * @noreference This constructor is not intended to be referenced by + * clients. + */ + protected IntegerEditing(NumberFormat format, String parseErrorMessage, + String outOfRangeMessage, Class integerType) { + this.displayFormat = format; + + final StringToNumberConverter targetConverter; + final NumberToStringConverter modelConverter; + final AbstractStringToNumberValidator targetValidator; + if (Long.class.equals(integerType)) { + targetConverter = StringToNumberConverter.toLong(format, false); + modelConverter = NumberToStringConverter.fromLong(format, false); + targetValidator = new StringToLongValidator(targetConverter); + } else if (Integer.class.equals(integerType)) { + targetConverter = StringToNumberConverter.toInteger(format, false); + modelConverter = NumberToStringConverter.fromInteger(format, false); + targetValidator = new StringToIntegerValidator(targetConverter); + } else if (Short.class.equals(integerType)) { + targetConverter = StringToNumberConverter.toShort(format, false); + modelConverter = NumberToStringConverter.fromShort(format, false); + targetValidator = new StringToShortValidator(targetConverter); + } else if (Byte.class.equals(integerType)) { + targetConverter = StringToNumberConverter.toByte(format, false); + modelConverter = NumberToStringConverter.fromByte(format, false); + targetValidator = new StringToByteValidator(targetConverter); + } else { + throw new IllegalArgumentException( + "Unsupported integer type: " + integerType); //$NON-NLS-1$ + } + + if (parseErrorMessage != null) { + targetValidator.setParseErrorMessage(parseErrorMessage); + } + if (outOfRangeMessage != null) { + targetValidator.setOutOfRangeMessage(outOfRangeMessage); + } + + setTargetConverter(targetConverter); + setModelConverter(modelConverter); + targetConstraints().addValidator(targetValidator); + } + + /** + * Creates a new editing object for {@link Long}s which defaults the + * validations and conversions used for editing based on the platform's + * locale. Uses the default validation messages. + * + * @return The new editing object using the default validations and + * conversions for editing. + * + * @see NumberFormat#getIntegerInstance() + */ + public static IntegerEditing withLongDefaults() { + return withLongDefaults(null, null); + } + + /** + * Creates a new editing object for {@link Long}s which defaults the + * validations and conversions used for editing based on the platform's + * locale. Uses the specified custom validation messages. + * + * @param parseErrorMessage + * The validation message issued in case the input string cannot + * be parsed to an integer. + * @param outOfRangeMessage + * The validation message issued in case the input string + * represents an integer whose value is out of range. Can be + * parameterized by theLong.MIN_VALUE
({0}) and
+ * Long.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withLongDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new IntegerEditing(NumberFormat.getIntegerInstance(),
+ parseErrorMessage, outOfRangeMessage, Long.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Long}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forLongFormat(NumberFormat format) {
+ return forLongFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Long}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Long.MIN_VALUE
({0}) and
+ * Long.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forLongFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new IntegerEditing(format, parseErrorMessage, outOfRangeMessage,
+ Long.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Integer}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withIntegerDefaults() {
+ return withIntegerDefaults(null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Integer}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation messages.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Integer.MIN_VALUE
({0}) and
+ * Integer.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withIntegerDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new IntegerEditing(NumberFormat.getIntegerInstance(),
+ parseErrorMessage, outOfRangeMessage, Integer.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Integer}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forIntegerFormat(NumberFormat format) {
+ return forIntegerFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Integer}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Integer.MIN_VALUE
({0}) and
+ * Integer.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forIntegerFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new IntegerEditing(format, parseErrorMessage, outOfRangeMessage,
+ Integer.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Short}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withShortDefaults() {
+ return withShortDefaults(null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Short}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation messages.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Short.MIN_VALUE
({0}) and
+ * Short.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withShortDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new IntegerEditing(NumberFormat.getIntegerInstance(),
+ parseErrorMessage, outOfRangeMessage, Short.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Short}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forShortFormat(NumberFormat format) {
+ return forShortFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Short}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Short.MIN_VALUE
({0}) and
+ * Short.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forShortFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new IntegerEditing(format, parseErrorMessage, outOfRangeMessage,
+ Short.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Byte}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the default validation messages.
+ *
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withByteDefaults() {
+ return withByteDefaults(null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Byte}s which defaults the
+ * validations and conversions used for editing based on the platform's
+ * locale. Uses the specified custom validation messages.
+ *
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Byte.MIN_VALUE
({0}) and
+ * Byte.MAX_VALUE
({1}) values.
+ * @return The new editing object using the default validations and
+ * conversions for editing.
+ *
+ * @see NumberFormat#getIntegerInstance()
+ */
+ public static IntegerEditing withByteDefaults(String parseErrorMessage,
+ String outOfRangeMessage) {
+ return new IntegerEditing(NumberFormat.getIntegerInstance(),
+ parseErrorMessage, outOfRangeMessage, Byte.class);
+ }
+
+ /**
+ * Creates a new editing object for {@link Byte}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the default validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forByteFormat(NumberFormat format) {
+ return forByteFormat(format, null, null);
+ }
+
+ /**
+ * Creates a new editing object for {@link Byte}s whose validations and
+ * conversions used for editing are based on the given integer format. Uses
+ * the specified custom validation messages.
+ *
+ * @param format
+ * The integer format defining the validations and conversions
+ * used for editing.
+ * @param parseErrorMessage
+ * The validation message issued in case the input string cannot
+ * be parsed to an integer.
+ * @param outOfRangeMessage
+ * The validation message issued in case the input string
+ * represents an integer whose value is out of range. Can be
+ * parameterized by the Byte.MIN_VALUE
({0}) and
+ * Byte.MAX_VALUE
({1}) values.
+ * @return The new editing object configured by the given integer format.
+ */
+ public static IntegerEditing forByteFormat(NumberFormat format,
+ String parseErrorMessage, String outOfRangeMessage) {
+ return new IntegerEditing(format, parseErrorMessage, outOfRangeMessage,
+ Byte.class);
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link IntegerConstraints + * integer model constraints} of this editing object and is equivalent to + * {@code (IntegerConstraints) modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see IntegerConstraints + */ + public IntegerConstraints modelIntegerConstraints() { + return (IntegerConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link IntegerConstraints + * integer before-set model constraints} of this editing object and is + * equivalent to {@code (IntegerConstraints) beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see IntegerConstraints + */ + public IntegerConstraints beforeSetModelIntegerConstraints() { + return (IntegerConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new IntegerConstraints().integerFormat(displayFormat); + } + + protected Constraints createBeforeSetModelConstraints() { + return new IntegerConstraints().integerFormat(displayFormat); + } +} Index: src/org/eclipse/core/databinding/editing/StringEditing.java =================================================================== RCS file: src/org/eclipse/core/databinding/editing/StringEditing.java diff -N src/org/eclipse/core/databinding/editing/StringEditing.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/editing/StringEditing.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,172 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.editing; + +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.databinding.validation.constraint.Constraints; +import org.eclipse.core.databinding.validation.constraint.StringConstraints; +import org.eclipse.core.internal.databinding.conversion.StringStripConverter; +import org.eclipse.core.internal.databinding.conversion.StringTrimConverter; + +/** + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class StringEditing extends Editing { + + /** + * Creates a new editing object for booleans. + * + * @param targetConverter + * The {@link #setTargetConverter(IConverter) target converter} + * to use for editing. + * + * @noreference This constructor is not intended to be referenced by + * clients. + */ + protected StringEditing(IConverter targetConverter) { + setTargetConverter(targetConverter); + } + + /** + * Creates a new editing object for strings which performs no validation or + * conversion. + * + * @return The new editing object which performs no validation or + * conversion. + */ + public static StringEditing withDefaults() { + return new StringEditing(null); + } + + /** + * Creates a new editing object which strips whitespace from both ends of + * the input string. + * + * @return The new editing object which strips whitespace from both ends of + * the input string. + * + * @see Character#isWhitespace(char) + */ + public static StringEditing stripped() { + return new StringEditing(new StringStripConverter(false)); + } + + /** + * Creates a new editing object which strips whitespace from both ends of + * the input string. In case stripping the input string results in an empty + * string, the input string will be converted tonull
.
+ *
+ * @return The new editing object which strips whitespace from both ends of
+ * the input string. Resulting empty strings are converted to
+ * null
.
+ *
+ * @see Character#isWhitespace(char)
+ */
+ public static StringEditing strippedToNull() {
+ return new StringEditing(new StringStripConverter(true));
+ }
+
+ /**
+ * Creates a new editing object which {@link String#trim()}s the input
+ * string.
+ *
+ * @return The new editing object which trims whitespace from both ends of
+ * the input string.
+ *
+ * @see String#trim()
+ */
+ public static StringEditing trimmed() {
+ return new StringEditing(new StringTrimConverter(false));
+ }
+
+ /**
+ * Creates a new editing object which {@link String#trim()}s the input
+ * string. In case trimming the input string results in an empty string, the
+ * input string will be converted to null
.
+ *
+ * @return The new editing object which trims whitespace from both ends of
+ * the input string. Resulting empty strings are converted to
+ * null
.
+ *
+ * @see String#trim()
+ */
+ public static StringEditing trimmedToNull() {
+ return new StringEditing(new StringTrimConverter(true));
+ }
+
+ /**
+ * Returns the target constraints to apply.
+ *
+ * + * This method provides a typesafe access to the {@link StringConstraints + * string target constraints} of this editing object and is equivalent to + * {@code (StringConstraints) targetConstraints()}. + *
+ * + * @return The target constraints to apply. + * + * @see #targetConstraints() + * @see StringConstraints + */ + public StringConstraints targetStringConstraints() { + return (StringConstraints) targetConstraints(); + } + + /** + * Returns the model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link StringConstraints + * string model constraints} of this editing object and is equivalent to + * {@code (StringConstraints) modelConstraints()}. + *
+ * + * @return The model constraints to apply. + * + * @see #modelConstraints() + * @see StringConstraints + */ + public StringConstraints modelStringConstraints() { + return (StringConstraints) modelConstraints(); + } + + /** + * Returns the before-set model constraints to apply. + * + *+ * This method provides a typesafe access to the {@link StringConstraints + * string before-set model constraints} of this editing object and is + * equivalent to {@code (StringConstraints) beforeSetModelConstraints()}. + *
+ * + * @return The before-set model constraints to apply. + * + * @see #beforeSetModelConstraints() + * @see StringConstraints + */ + public StringConstraints beforeSetModelStringConstraints() { + return (StringConstraints) beforeSetModelConstraints(); + } + + protected Constraints createTargetConstraints() { + return new StringConstraints(); + } + + protected Constraints createModelConstraints() { + return new StringConstraints(); + } + + protected Constraints createBeforeSetModelConstraints() { + return new StringConstraints(); + } +} Index: src/org/eclipse/core/databinding/validation/constraint/BigDecimalConstraints.java =================================================================== RCS file: src/org/eclipse/core/databinding/validation/constraint/BigDecimalConstraints.java diff -N src/org/eclipse/core/databinding/validation/constraint/BigDecimalConstraints.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/databinding/validation/constraint/BigDecimalConstraints.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,430 @@ +/******************************************************************************* + * Copyright (c) 2009 Ovidio Mallo and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ovidio Mallo - initial API and implementation (bug 183055) + ******************************************************************************/ + +package org.eclipse.core.databinding.validation.constraint; + +import java.math.BigDecimal; + +import org.eclipse.core.internal.databinding.validation.BigDecimalRangeValidator; +import org.eclipse.core.internal.databinding.validation.DecimalPrecisionValidator; +import org.eclipse.core.internal.databinding.validation.DecimalScaleValidator; +import org.eclipse.core.internal.databinding.validation.NonNullValidator; + +import com.ibm.icu.text.NumberFormat; + +/** + * Provides a set of constraints to apply toBigDecimal
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to BigDecimal numbers. In addition, the validation messages to be issued for + * the individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + * @see BigDecimal + * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class BigDecimalConstraints extends Constraints { + + private NumberFormat bigDecimalFormat = NumberFormat.getNumberInstance(); + + private NumberFormat scaleFormat = NumberFormat.getIntegerInstance(); + + private NumberFormat precisionFormat = NumberFormat.getIntegerInstance(); + + private String requiredMessage = null; + + private String greaterMessage = null; + + private String greaterEqualMessage = null; + + private String lessMessage = null; + + private String lessEqualMessage = null; + + private String rangeMessage = null; + + private String positiveMessage = null; + + private String nonNegativeMessage = null; + + private String maxScaleMessage = null; + + private String maxPrecisionMessage = null; + + /** + * Sets the format to use when formatting a BigDecimal to be included in any + * of the validation messages. + * + * @param format + * The format to use for displaying BigDecimals in validation + * messages. + * @return This constraints instance for method chaining. + */ + public BigDecimalConstraints bigDecimalFormat(NumberFormat format) { + this.bigDecimalFormat = format; + return this; + } + + /** + * Adds a validator ensuring that the input BigDecimal is not + *null
. Uses the current {@link #requiredMessage(String)
+ * validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public BigDecimalConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is greater than the
+ * given number. Uses the current {@link #greaterMessage(String) validation
+ * message} and {@link #bigDecimalFormat(NumberFormat) BigDecimal format}.
+ *
+ * @param number
+ * The number which the input BigDecimal must be greater than.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints greater(BigDecimal number) {
+ addValidator(BigDecimalRangeValidator.greater(number, greaterMessage,
+ bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greater(BigDecimal)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greater(BigDecimal)} constraint. Can be parameterized
+ * with the number ({0}) which the input BigDecimal must be
+ * greater than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greater(BigDecimal)
+ */
+ public BigDecimalConstraints greaterMessage(String message) {
+ this.greaterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is greater than or
+ * equal to the given number. Uses the current
+ * {@link #greaterEqualMessage(String) validation message} and
+ * {@link #bigDecimalFormat(NumberFormat) BigDecimal format}.
+ *
+ * @param number
+ * The number which the input BigDecimal must be greater than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints greaterEqual(BigDecimal number) {
+ addValidator(BigDecimalRangeValidator.greaterEqual(number,
+ greaterEqualMessage, bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #greaterEqual(BigDecimal)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greaterEqual(BigDecimal)} constraint. Can be
+ * parameterized with the number ({0}) which the input BigDecimal
+ * must be greater than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greaterEqual(BigDecimal)
+ */
+ public BigDecimalConstraints greaterEqualMessage(String message) {
+ this.greaterEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is less than the
+ * given number. Uses the current {@link #lessMessage(String) validation
+ * message} and {@link #bigDecimalFormat(NumberFormat) BigDecimal format}.
+ *
+ * @param number
+ * The number which the input BigDecimal must be less than.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints less(BigDecimal number) {
+ addValidator(BigDecimalRangeValidator.less(number, lessMessage,
+ bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #less(BigDecimal)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #less(BigDecimal)} constraint. Can be parameterized
+ * with the number ({0}) which the input BigDecimal must be less
+ * than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #less(BigDecimal)
+ */
+ public BigDecimalConstraints lessMessage(String message) {
+ this.lessMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is less than or equal
+ * to the given number. Uses the current {@link #lessEqualMessage(String)
+ * validation message} and {@link #bigDecimalFormat(NumberFormat) BigDecimal
+ * format}.
+ *
+ * @param number
+ * The number which the input BigDecimal must be less than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints lessEqual(BigDecimal number) {
+ addValidator(BigDecimalRangeValidator.greaterEqual(number,
+ lessEqualMessage, bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #lessEqual(BigDecimal)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #lessEqual(BigDecimal)} constraint. Can be
+ * parameterized with the number ({0}) which the input BigDecimal
+ * must be less than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #lessEqual(BigDecimal)
+ */
+ public BigDecimalConstraints lessEqualMessage(String message) {
+ this.lessEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is within the given
+ * range. Uses the current {@link #rangeMessage(String) validation message}
+ * and {@link #bigDecimalFormat(NumberFormat) BigDecimal format}.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints range(BigDecimal min, BigDecimal max) {
+ addValidator(BigDecimalRangeValidator.range(min, max, rangeMessage,
+ bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #range(BigDecimal, BigDecimal)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #range(BigDecimal, BigDecimal)} constraint. Can be
+ * parameterized with the min ({0}) and max ({1}) values of the
+ * range in which the input BigDecimal must lie.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #range(BigDecimal, BigDecimal)
+ */
+ public BigDecimalConstraints rangeMessage(String message) {
+ this.rangeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is positive. Uses the
+ * current {@link #positiveMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints positive() {
+ addValidator(BigDecimalRangeValidator.positive(positiveMessage,
+ bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #positive()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #positive()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #positive()
+ */
+ public BigDecimalConstraints positiveMessage(String message) {
+ this.positiveMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal is non-negative. Uses
+ * the current {@link #nonNegativeMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints nonNegative() {
+ addValidator(BigDecimalRangeValidator.nonNegative(nonNegativeMessage,
+ bigDecimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #nonNegative()
+ */
+ public BigDecimalConstraints nonNegativeMessage(String message) {
+ this.nonNegativeMessage = message;
+ return this;
+ }
+
+ /**
+ * Sets the format to use when formatting the scale in any of the validation
+ * messages stemming from a scale constraint.
+ *
+ * @param format
+ * The format to use for displaying the scale in validation
+ * messages stemming from a scale constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxScale(int)
+ */
+ public BigDecimalConstraints scaleFormat(NumberFormat format) {
+ this.scaleFormat = format;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigDecimal's scale is not
+ * greater than the given one. Uses the current
+ * {@link #maxScaleMessage(String) validation message} and
+ * {@link #scaleFormat(NumberFormat) scale format}.
+ *
+ * @param scale
+ * The maximum scale to enforce on the input BigDecimal.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints maxScale(int scale) {
+ addValidator(DecimalScaleValidator.max(scale, maxScaleMessage,
+ scaleFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #maxScale(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #maxScale(int)}
+ * constraint. Can be parameterized with the maximum scale ({0})
+ * of the BigDecimal.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxScale(int)
+ */
+ public BigDecimalConstraints maxScaleMessage(String message) {
+ this.maxScaleMessage = message;
+ return this;
+ }
+
+ /**
+ * Sets the format to use when formatting the precision in any of the
+ * validation messages stemming from a precision constraint.
+ *
+ * @param format
+ * The format to use for displaying the precision in validation
+ * messages stemming from a precision constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxPrecision(int)
+ */
+ public BigDecimalConstraints precisionFormat(NumberFormat format) {
+ this.precisionFormat = format;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal's precision is not
+ * greater than the given one. Uses the current
+ * {@link #maxPrecisionMessage(String) validation message} and
+ * {@link #precisionFormat(NumberFormat) precision format}.
+ *
+ * @param precision
+ * The maximum precision to enforce on the input decimal.
+ * @return This constraints instance for method chaining.
+ */
+ public BigDecimalConstraints maxPrecision(int precision) {
+ addValidator(DecimalPrecisionValidator.max(precision,
+ maxPrecisionMessage, precisionFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #maxPrecision(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #maxPrecision(int)} constraint. Can be parameterized
+ * with the maximum precision ({0}) of the BigDecimal.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxPrecision(int)
+ */
+ public BigDecimalConstraints maxPrecisionMessage(String message) {
+ this.maxPrecisionMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/BigIntegerConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/BigIntegerConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/BigIntegerConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/BigIntegerConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,322 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import java.math.BigInteger;
+
+import org.eclipse.core.internal.databinding.validation.BigIntegerRangeValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides a set of constraints to apply to BigInteger
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to BigInteger numbers. In addition, the validation messages to be issued for + * the individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + * @see BigInteger + * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class BigIntegerConstraints extends Constraints { + + private NumberFormat bigIntegerFormat = NumberFormat.getIntegerInstance(); + + private String requiredMessage = null; + + private String greaterMessage = null; + + private String greaterEqualMessage = null; + + private String lessMessage = null; + + private String lessEqualMessage = null; + + private String rangeMessage = null; + + private String positiveMessage = null; + + private String nonNegativeMessage = null; + + /** + * Sets the format to use when formatting a BigInteger to be included in any + * of the validation messages. + * + * @param format + * The format to use for displaying BigIntegers in validation + * messages. + * @return This constraints instance for method chaining. + */ + public BigIntegerConstraints bigIntegerFormat(NumberFormat format) { + this.bigIntegerFormat = format; + return this; + } + + /** + * Adds a validator ensuring that the input BigInteger is not + *null
. Uses the current {@link #requiredMessage(String)
+ * validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public BigIntegerConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is greater than the
+ * given number. Uses the current {@link #greaterMessage(String) validation
+ * message} and {@link #bigIntegerFormat(NumberFormat) BigInteger format}.
+ *
+ * @param number
+ * The number which the input BigInteger must be greater than.
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints greater(BigInteger number) {
+ addValidator(BigIntegerRangeValidator.greater(number, greaterMessage,
+ bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greater(BigInteger)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greater(BigInteger)} constraint. Can be parameterized
+ * with the number ({0}) which the input BigInteger must be
+ * greater than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greater(BigInteger)
+ */
+ public BigIntegerConstraints greaterMessage(String message) {
+ this.greaterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is greater than or
+ * equal to the given number. Uses the current
+ * {@link #greaterEqualMessage(String) validation message} and
+ * {@link #bigIntegerFormat(NumberFormat) BigInteger format}.
+ *
+ * @param number
+ * The number which the input BigInteger must be greater than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints greaterEqual(BigInteger number) {
+ addValidator(BigIntegerRangeValidator.greaterEqual(number,
+ greaterEqualMessage, bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #greaterEqual(BigInteger)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greaterEqual(BigInteger)} constraint. Can be
+ * parameterized with the number ({0}) which the input BigInteger
+ * must be greater than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greaterEqual(BigInteger)
+ */
+ public BigIntegerConstraints greaterEqualMessage(String message) {
+ this.greaterEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is less than the
+ * given number. Uses the current {@link #lessMessage(String) validation
+ * message} and {@link #bigIntegerFormat(NumberFormat) BigInteger format}.
+ *
+ * @param number
+ * The number which the input BigInteger must be less than.
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints less(BigInteger number) {
+ addValidator(BigIntegerRangeValidator.less(number, lessMessage,
+ bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #less(BigInteger)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #less(BigInteger)} constraint. Can be parameterized
+ * with the number ({0}) which the input BigInteger must be less
+ * than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #less(BigInteger)
+ */
+ public BigIntegerConstraints lessMessage(String message) {
+ this.lessMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is less than or equal
+ * to the given number. Uses the current {@link #lessEqualMessage(String)
+ * validation message} and {@link #bigIntegerFormat(NumberFormat) BigInteger
+ * format}.
+ *
+ * @param number
+ * The number which the input BigInteger must be less than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints lessEqual(BigInteger number) {
+ addValidator(BigIntegerRangeValidator.greaterEqual(number,
+ lessEqualMessage, bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #lessEqual(BigInteger)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #lessEqual(BigInteger)} constraint. Can be
+ * parameterized with the number ({0}) which the input BigInteger
+ * must be less than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #lessEqual(BigInteger)
+ */
+ public BigIntegerConstraints lessEqualMessage(String message) {
+ this.lessEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is within the given
+ * range. Uses the current {@link #rangeMessage(String) validation message}
+ * and {@link #bigIntegerFormat(NumberFormat) BigInteger format}.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints range(BigInteger min, BigInteger max) {
+ addValidator(BigIntegerRangeValidator.range(min, max, rangeMessage,
+ bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #range(BigInteger, BigInteger)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #range(BigInteger, BigInteger)} constraint. Can be
+ * parameterized with the min ({0}) and max ({1}) values of the
+ * range in which the input BigInteger must lie.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #range(BigInteger, BigInteger)
+ */
+ public BigIntegerConstraints rangeMessage(String message) {
+ this.rangeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is positive. Uses the
+ * current {@link #positiveMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints positive() {
+ addValidator(BigIntegerRangeValidator.positive(positiveMessage,
+ bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #positive()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #positive()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #positive()
+ */
+ public BigIntegerConstraints positiveMessage(String message) {
+ this.positiveMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input BigInteger is non-negative. Uses
+ * the current {@link #nonNegativeMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BigIntegerConstraints nonNegative() {
+ addValidator(BigIntegerRangeValidator.nonNegative(nonNegativeMessage,
+ bigIntegerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #nonNegative()
+ */
+ public BigIntegerConstraints nonNegativeMessage(String message) {
+ this.nonNegativeMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/BooleanConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/BooleanConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/BooleanConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/BooleanConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+/**
+ * Provides a set of constraints to apply to Boolean
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to booleans. In addition, the validation messages to be issued for the + * individual constraints can be configured. + *
+ * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class BooleanConstraints extends Constraints { + + private String requiredMessage = null; + + /** + * Adds a validator ensuring that the boolean value is notnull
+ * . Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public BooleanConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public BooleanConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/CharacterConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/CharacterConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/CharacterConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/CharacterConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,199 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import org.eclipse.core.internal.databinding.validation.CharacterValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+/**
+ * Provides a set of constraints to apply to Character
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to characters. In addition, the validation messages to be issued for the + * individual constraints can be configured. + *
+ * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class CharacterConstraints extends Constraints { + + private String requiredMessage = null; + + private String noWhitespaceMessage = null; + + private String noSpaceMessage = null; + + private String letterMessage = null; + + private String digitMessage = null; + + private String letterOrDigitMessage = null; + + /** + * Adds a validator ensuring that the character is notnull
.
+ * Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public CharacterConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the character is no
+ * {@link Character#isWhitespace(char) whitespace}. Uses the current
+ * {@link #noWhitespaceMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints noWhitespace() {
+ addValidator(CharacterValidator.noWhitespace(noWhitespaceMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #noWhitespace()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #noWhitespace()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #noWhitespace()
+ */
+ public CharacterConstraints noWhitespaceMessage(String message) {
+ this.noWhitespaceMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the character is no
+ * {@link Character#isSpaceChar(char) space}. Uses the current
+ * {@link #noSpaceMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints noSpace() {
+ addValidator(CharacterValidator.noSpace(noSpaceMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #noSpace()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #noSpace()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #noSpace()
+ */
+ public CharacterConstraints noSpaceMessage(String message) {
+ this.noSpaceMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the character is a
+ * {@link Character#isLetter(char) letter}. Uses the current
+ * {@link #letterMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints letter() {
+ addValidator(CharacterValidator.letter(letterMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #letter()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #letter()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #letter()
+ */
+ public CharacterConstraints letterMessage(String message) {
+ this.letterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the character is a
+ * {@link Character#isDigit(char) digit}. Uses the current
+ * {@link #digitMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints digit() {
+ addValidator(CharacterValidator.digit(digitMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #digit()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #digit()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #digit()
+ */
+ public CharacterConstraints digitMessage(String message) {
+ this.digitMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the character is a
+ * {@link Character#isLetterOrDigit(char) letter or digit}. Uses the current
+ * {@link #letterOrDigitMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public CharacterConstraints letterOrDigit() {
+ addValidator(CharacterValidator.letterOrDigit(letterOrDigitMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #letterOrDigit()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #letterOrDigit()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #letterOrDigit()
+ */
+ public CharacterConstraints letterOrDigitMessage(String message) {
+ this.letterOrDigitMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/Constraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/Constraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/Constraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/Constraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,225 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.databinding.util.Policy;
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.MultiStatus;
+import org.eclipse.core.runtime.Status;
+
+/**
+ * This class allows to aggregate a set of {@link IValidator validators} and to
+ * combine their individual validation statuses in different ways.
+ *
+ * + * The set of validators to be applied can be {@link #addValidator(IValidator) + * added} to a constraints object from which an aggregate validator can then be + * {@link #createValidator() created} which combines the statuses of all the + * accumulated validators as dictated by the specified + * {@link #aggregationPolicy(int) aggregation policy}. + *
+ * + * @see IValidator + * + * @since 1.4 + */ +public class Constraints { + + /** + * Constant denoting an aggregation strategy that merges multiple non-OK + * status objects in a {@link MultiStatus}. Returns an OK status result if + * all statuses from the set of validators to apply are an OK status. + * Returns a single status if there is only one non-OK status. + * + * @see #aggregationPolicy(int) + */ + public static final int AGGREGATION_MERGED = 1; + + /** + * Constant denoting an aggregation strategy that always returns the + * most severe status from the set of validators to apply. If there + * is more than one status at the same severity level, it picks the first + * one it encounters. + * + * @see #aggregationPolicy(int) + */ + public static final int AGGREGATION_MAX_SEVERITY = 2; + + /** + * Constant denoting an aggregation strategy that always returns the + * least severe status from the set of validators to apply. If there + * is more than one status at the same severity level, it picks the first + * one it encounters. + * + * @see #aggregationPolicy(int) + */ + public static final int AGGREGATION_MIN_SEVERITY = 3; + + // Will be initialized lazily. + private List validators = null; + + private IValidator cachedValidator = null; + + private int aggregationPolicy = AGGREGATION_MAX_SEVERITY; + + /** + * Adds a custom validator to the set of constraints to apply. + * + * @param validator + * The custom validator to add to the set of constraints. + * @return This constraints instance for method chaining. + */ + public final Constraints addValidator(IValidator validator) { + if (validators == null) { + validators = new ArrayList(2); + } + validators.add(validator); + cachedValidator = null; + return this; + } + + /** + * Sets the aggregation policy to apply to the individual validations which + * constitute this set of constraints. + * + * @param policy + * The validation aggregation policy to set. Must be one of + * {@link #AGGREGATION_MERGED}, {@link #AGGREGATION_MAX_SEVERITY} + * or {@link #AGGREGATION_MIN_SEVERITY}. + * @return This constraints instance for method chaining. + * + * @see #AGGREGATION_MERGED + * @see #AGGREGATION_MAX_SEVERITY + * @see #AGGREGATION_MIN_SEVERITY + */ + public final Constraints aggregationPolicy(int policy) { + this.aggregationPolicy = policy; + cachedValidator = null; + return this; + } + + /** + * Creates a validator which enforces the current set of constraints. + * + *
+ * Note that this method will return null
in case the set of
+ * constraints to apply is empty.
+ *
null
.
+ */
+ public final IValidator createValidator() {
+ if (validators != null && !validators.isEmpty()) {
+ if (cachedValidator == null) {
+ if (validators.size() == 1) {
+ cachedValidator = (IValidator) validators.get(0);
+ } else {
+ IValidator[] currentValidators = (IValidator[]) validators
+ .toArray(new IValidator[validators.size()]);
+ cachedValidator = new ConstraintsValidator(
+ currentValidators, aggregationPolicy);
+ }
+ }
+ return cachedValidator;
+ }
+ return null;
+ }
+
+ private static final class ConstraintsValidator implements IValidator {
+
+ private final IValidator[] validators;
+
+ private final int aggregationPolicy;
+
+ public ConstraintsValidator(IValidator[] validators,
+ int aggregationPolicy) {
+ this.validators = validators;
+ this.aggregationPolicy = aggregationPolicy;
+ }
+
+ public IStatus validate(Object value) {
+ switch (aggregationPolicy) {
+ case AGGREGATION_MERGED:
+ return validateMerged(value);
+ case AGGREGATION_MAX_SEVERITY:
+ return validateMaxSeverity(value);
+ case AGGREGATION_MIN_SEVERITY:
+ return validateMinSeverity(value);
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported aggregationPolicy: " + aggregationPolicy); //$NON-NLS-1$
+ }
+ }
+
+ private IStatus validateMerged(Object value) {
+ List statuses = new ArrayList();
+ for (int i = 0; i < validators.length; i++) {
+ IValidator validator = validators[i];
+ IStatus status = validator.validate(value);
+ if (!status.isOK()) {
+ statuses.add(status);
+ }
+ }
+
+ if (statuses.size() == 1) {
+ return (IStatus) statuses.get(0);
+ }
+
+ if (!statuses.isEmpty()) {
+ MultiStatus result = new MultiStatus(Policy.JFACE_DATABINDING,
+ 0,
+ BindingMessages
+ .getString(BindingMessages.MULTIPLE_PROBLEMS),
+ null);
+ for (Iterator it = statuses.iterator(); it.hasNext();) {
+ IStatus status = (IStatus) it.next();
+ result.merge(status);
+ }
+ return result;
+ }
+
+ return Status.OK_STATUS;
+ }
+
+ private IStatus validateMaxSeverity(Object value) {
+ IStatus maxStatus = Status.OK_STATUS;
+ for (int i = 0; i < validators.length; i++) {
+ IValidator validator = validators[i];
+ IStatus status = validator.validate(value);
+ if (status.getSeverity() > maxStatus.getSeverity()) {
+ maxStatus = status;
+ }
+ }
+ return maxStatus;
+ }
+
+ private IStatus validateMinSeverity(Object value) {
+ IStatus minStatus = null;
+ for (int i = 0; i < validators.length; i++) {
+ IValidator validator = validators[i];
+ IStatus status = validator.validate(value);
+ if (minStatus == null
+ || status.getSeverity() < minStatus.getSeverity()) {
+ minStatus = status;
+ }
+ }
+ return minStatus != null ? minStatus : Status.OK_STATUS;
+ }
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/DateConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/DateConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/DateConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/DateConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,260 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import java.util.Date;
+
+import org.eclipse.core.internal.databinding.validation.DateRangeValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+import com.ibm.icu.text.DateFormat;
+
+/**
+ * Provides a set of constraints to apply to Date
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to dates. In addition, the validation messages to be issued for the + * individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + * @see Date + * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class DateConstraints extends Constraints { + + private DateFormat dateFormat = DateFormat.getDateInstance(); + + private String requiredMessage = null; + + private String afterMessage = null; + + private String afterEqualMessage = null; + + private String beforeMessage = null; + + private String beforeEqualMessage = null; + + private String rangeMessage = null; + + /** + * Sets the format to use when formatting a date to be included in any of + * the validation messages. + * + * @param format + * The format to use for displaying dates in validation messages. + * @return This constraints instance for method chaining. + */ + public DateConstraints dateFormat(DateFormat format) { + this.dateFormat = format; + return this; + } + + /** + * Adds a validator ensuring that the input date is notnull
.
+ * Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public DateConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public DateConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input date is after the given date.
+ * Uses the current {@link #afterMessage(String) validation message} and
+ * {@link #dateFormat(DateFormat) date format}.
+ *
+ * @param date
+ * The date which the input date must be after.
+ * @return This constraints instance for method chaining.
+ *
+ * @see Date#after(Date)
+ */
+ public DateConstraints after(Date date) {
+ addValidator(DateRangeValidator.after(date, afterMessage, dateFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #after(Date)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #after(Date)}
+ * constraint. Can be parameterized with the date ({0}) which the
+ * input date must be after.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #after(Date)
+ */
+ public DateConstraints afterMessage(String message) {
+ this.afterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input date is after or on the given
+ * date. Uses the current {@link #afterEqualMessage(String) validation
+ * message} and {@link #dateFormat(DateFormat) date format}.
+ *
+ * @param date
+ * The date which the input date must be after or on.
+ * @return This constraints instance for method chaining.
+ *
+ * @see Date#after(Date)
+ * @see Date#equals(Object)
+ */
+ public DateConstraints afterEqual(Date date) {
+ addValidator(DateRangeValidator.afterEqual(date, afterEqualMessage,
+ dateFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #afterEqual(Date)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #afterEqual(Date)} constraint. Can be parameterized
+ * with the date ({0}) which the input date must be after or on.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #afterEqual(Date)
+ */
+ public DateConstraints afterEqualMessage(String message) {
+ this.afterEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input date is before the given date.
+ * Uses the current {@link #beforeMessage(String) validation message} and
+ * {@link #dateFormat(DateFormat) date format}.
+ *
+ * @param date
+ * The date which the input date must be before
+ * @return This constraints instance for method chaining.
+ *
+ * @see Date#before(Date)
+ */
+ public DateConstraints before(Date date) {
+ addValidator(DateRangeValidator.before(date, beforeMessage, dateFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #before(Date)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #before(Date)}
+ * constraint. Can be parameterized with the date ({0}) which the
+ * input date must be before.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #before(Date)
+ */
+ public DateConstraints beforeMessage(String message) {
+ this.beforeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input date is before or on the given
+ * date. Uses the current {@link #beforeEqualMessage(String) validation
+ * message} and {@link #dateFormat(DateFormat) date format}.
+ *
+ * @param date
+ * The date which the input date must be before or on.
+ * @return This constraints instance for method chaining.
+ *
+ * @see Date#before(Date)
+ * @see Date#equals(Object)
+ */
+ public DateConstraints beforeEqual(Date date) {
+ addValidator(DateRangeValidator.beforeEqual(date, beforeEqualMessage,
+ dateFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #beforeEqual(Date)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #beforeEqual(Date)} constraint. Can be parameterized
+ * ({0}) with the date which the input date must be before or on.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #beforeEqual(Date)
+ */
+ public DateConstraints beforeEqualMessage(String message) {
+ this.beforeEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input date is within the given range.
+ * Uses the current {@link #rangeMessage(String) validation message} and
+ * {@link #dateFormat(DateFormat) date format}.
+ *
+ * @param minDate
+ * The lower bound of the range (inclusive).
+ * @param maxDate
+ * The upper bound of the range (inclusive).
+ * @return This constraints instance for method chaining.
+ */
+ public DateConstraints range(Date minDate, Date maxDate) {
+ addValidator(DateRangeValidator.range(minDate, maxDate, rangeMessage,
+ dateFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #range(Date, Date)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #range(Date, Date)} constraint. Can be parameterized
+ * with the min ({0}) and max ({1}) dates of the range in which
+ * the input date must lie.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #range(Date, Date)
+ */
+ public DateConstraints rangeMessage(String message) {
+ this.rangeMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/DecimalConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/DecimalConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/DecimalConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/DecimalConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,424 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import org.eclipse.core.internal.databinding.validation.DecimalPrecisionValidator;
+import org.eclipse.core.internal.databinding.validation.DecimalRangeValidator;
+import org.eclipse.core.internal.databinding.validation.DecimalScaleValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides a set of constraints to apply to decimal numbers.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to decimal numbers. In addition, the validation messages to be issued for the + * individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + *
+ * The decimal types supported by this class are double
, and
+ * float
.
+ *
null
+ * . Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public DecimalConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is greater than the
+ * given number. Uses the current {@link #greaterMessage(String) validation
+ * message} and {@link #decimalFormat(NumberFormat) decimal format}.
+ *
+ * @param number
+ * The number which the input decimal must be greater than.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints greater(double number) {
+ addValidator(DecimalRangeValidator.greater(number, greaterMessage,
+ decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greater(double)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greater(double)} constraint. Can be parameterized with
+ * the number ({0}) which the input decimal must be greater than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greater(double)
+ */
+ public DecimalConstraints greaterMessage(String message) {
+ this.greaterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is greater than or equal
+ * to the given number. Uses the current
+ * {@link #greaterEqualMessage(String) validation message} and
+ * {@link #decimalFormat(NumberFormat) decimal format}.
+ *
+ * @param number
+ * The number which the input decimal must be greater than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints greaterEqual(double number) {
+ addValidator(DecimalRangeValidator.greaterEqual(number,
+ greaterEqualMessage, decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greaterEqual(double)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greaterEqual(double)} constraint. Can be parameterized
+ * with the number ({0}) which the input decimal must be greater
+ * than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greaterEqual(double)
+ */
+ public DecimalConstraints greaterEqualMessage(String message) {
+ this.greaterEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is less than the given
+ * number. Uses the current {@link #lessMessage(String) validation message}
+ * and {@link #decimalFormat(NumberFormat) decimal format}.
+ *
+ * @param number
+ * The number which the input decimal must be less than.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints less(double number) {
+ addValidator(DecimalRangeValidator.less(number, lessMessage,
+ decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #less(double)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #less(double)}
+ * constraint. Can be parameterized with the number ({0}) which
+ * the input decimal must be less than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #less(double)
+ */
+ public DecimalConstraints lessMessage(String message) {
+ this.lessMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is less than or equal to
+ * the given number. Uses the current {@link #lessEqualMessage(String)
+ * validation message} and {@link #decimalFormat(NumberFormat) decimal
+ * format}.
+ *
+ * @param number
+ * The number which the input decimal must be less than or equal
+ * to.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints lessEqual(double number) {
+ addValidator(DecimalRangeValidator.greaterEqual(number,
+ lessEqualMessage, decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #lessEqual(double)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #lessEqual(double)} constraint. Can be parameterized
+ * with the number ({0}) which the input decimal must be less
+ * than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #lessEqual(double)
+ */
+ public DecimalConstraints lessEqualMessage(String message) {
+ this.lessEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is within the given
+ * range. Uses the current {@link #rangeMessage(String) validation message}
+ * and {@link #decimalFormat(NumberFormat) decimal format}.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints range(double min, double max) {
+ addValidator(DecimalRangeValidator.range(min, max, rangeMessage,
+ decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #range(double, double)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #range(double, double)} constraint. Can be
+ * parameterized with the min ({0}) and max ({1}) values of the
+ * range in which the input decimal must lie.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #range(double, double)
+ */
+ public DecimalConstraints rangeMessage(String message) {
+ this.rangeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is positive. Uses the
+ * current {@link #positiveMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints positive() {
+ addValidator(DecimalRangeValidator.positive(positiveMessage,
+ decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #positive()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #positive()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #positive()
+ */
+ public DecimalConstraints positiveMessage(String message) {
+ this.positiveMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal is non-negative. Uses
+ * the current {@link #nonNegativeMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints nonNegative() {
+ addValidator(DecimalRangeValidator.nonNegative(nonNegativeMessage,
+ decimalFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #nonNegative()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #nonNegative()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #nonNegative()
+ */
+ public DecimalConstraints nonNegativeMessage(String message) {
+ this.nonNegativeMessage = message;
+ return this;
+ }
+
+ /**
+ * Sets the format to use when formatting the scale in any of the validation
+ * messages stemming from a scale constraint.
+ *
+ * @param format
+ * The format to use for displaying the scale in validation
+ * messages stemming from a scale constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxScale(int)
+ */
+ public DecimalConstraints scaleFormat(NumberFormat format) {
+ this.scaleFormat = format;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal's scale is not greater
+ * than the given one. Uses the current {@link #maxScaleMessage(String)
+ * validation message} and {@link #scaleFormat(NumberFormat) scale format}.
+ *
+ * @param scale
+ * The maximum scale to enforce on the input decimal.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints maxScale(int scale) {
+ addValidator(DecimalScaleValidator.max(scale, maxScaleMessage,
+ scaleFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #maxScale(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #maxScale(int)}
+ * constraint. Can be parameterized with the maximum scale ({0})
+ * of the decimal.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxScale(int)
+ */
+ public DecimalConstraints maxScaleMessage(String message) {
+ this.maxScaleMessage = message;
+ return this;
+ }
+
+ /**
+ * Sets the format to use when formatting the precision in any of the
+ * validation messages stemming from a precision constraint.
+ *
+ * @param format
+ * The format to use for displaying the precision in validation
+ * messages stemming from a precision constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxPrecision(int)
+ */
+ public DecimalConstraints precisionFormat(NumberFormat format) {
+ this.precisionFormat = format;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input decimal's precision is not
+ * greater than the given one. Uses the current
+ * {@link #maxPrecisionMessage(String) validation message} and
+ * {@link #precisionFormat(NumberFormat) precision format}.
+ *
+ * @param precision
+ * The maximum precision to enforce on the input decimal.
+ * @return This constraints instance for method chaining.
+ */
+ public DecimalConstraints maxPrecision(int precision) {
+ addValidator(DecimalPrecisionValidator.max(precision,
+ maxPrecisionMessage, precisionFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #maxPrecision(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #maxPrecision(int)} constraint. Can be parameterized
+ * with the maximum precision ({0}) of the decimal.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxPrecision(int)
+ */
+ public DecimalConstraints maxPrecisionMessage(String message) {
+ this.maxPrecisionMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/IntegerConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/IntegerConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/IntegerConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/IntegerConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,322 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import org.eclipse.core.internal.databinding.validation.IntegerRangeValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides a set of constraints to apply to integer numbers.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to integer numbers. In addition, the validation messages to be issued for the + * individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + *
+ * The integer types supported by this class are long
,
+ * int
, short
, and byte
.
+ *
null
+ * . Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public IntegerConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is greater than the
+ * given number. Uses the current {@link #greaterMessage(String) validation
+ * message} and {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @param number
+ * The number which the input integer must be greater than.
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints greater(long number) {
+ addValidator(IntegerRangeValidator.greater(number, greaterMessage,
+ integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greater(long)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #greater(long)}
+ * constraint. Can be parameterized with the number ({0}) which
+ * the input integer must be greater than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greater(long)
+ */
+ public IntegerConstraints greaterMessage(String message) {
+ this.greaterMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is greater than or equal
+ * to the given number. Uses the current
+ * {@link #greaterEqualMessage(String) validation message} and
+ * {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @param number
+ * The number which the input integer must be greater than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints greaterEqual(long number) {
+ addValidator(IntegerRangeValidator.greaterEqual(number,
+ greaterEqualMessage, integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #greaterEqual(long)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #greaterEqual(long)} constraint. Can be parameterized
+ * with the number ({0}) which the input integer must be greater
+ * than or equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #greaterEqual(long)
+ */
+ public IntegerConstraints greaterEqualMessage(String message) {
+ this.greaterEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is less than the given
+ * number. Uses the current {@link #lessMessage(String) validation message}
+ * and {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @param number
+ * The number which the input integer must be less than.
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints less(long number) {
+ addValidator(IntegerRangeValidator.less(number, lessMessage,
+ integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #less(long)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #less(long)}
+ * constraint. Can be parameterized with the number ({0}) which
+ * the input integer must be less than.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #less(long)
+ */
+ public IntegerConstraints lessMessage(String message) {
+ this.lessMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is less than or equal to
+ * the given number. Uses the current {@link #lessEqualMessage(String)
+ * validation message} and {@link #integerFormat(NumberFormat) integer
+ * format}.
+ *
+ * @param number
+ * The number which the input integer must be less than or equal
+ * to.
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints lessEqual(long number) {
+ addValidator(IntegerRangeValidator.greaterEqual(number,
+ lessEqualMessage, integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #lessEqual(long)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #lessEqual(long)} constraint. Can be parameterized with
+ * the number ({0}) which the input integer must be less than or
+ * equal to.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #lessEqual(long)
+ */
+ public IntegerConstraints lessEqualMessage(String message) {
+ this.lessEqualMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is within the given
+ * range. Uses the current {@link #rangeMessage(String) validation message}
+ * and {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints range(long min, long max) {
+ addValidator(IntegerRangeValidator.range(min, max, rangeMessage,
+ integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #range(long, long)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #range(long, long)} constraint. Can be parameterized
+ * with the min ({0}) and max ({1}) values of the range in which
+ * the input integer must lie.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #range(long, long)
+ */
+ public IntegerConstraints rangeMessage(String message) {
+ this.rangeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is positive. Uses the
+ * current {@link #positiveMessage(String) validation message} and
+ * {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints positive() {
+ addValidator(IntegerRangeValidator.positive(positiveMessage,
+ integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #positive()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #positive()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #positive()
+ */
+ public IntegerConstraints positiveMessage(String message) {
+ this.positiveMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is non-negative. Uses
+ * the current {@link #nonNegativeMessage(String) validation message} and
+ * {@link #integerFormat(NumberFormat) integer format}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public IntegerConstraints nonNegative() {
+ addValidator(IntegerRangeValidator.nonNegative(nonNegativeMessage,
+ integerFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #nonNegative()}
+ * constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #nonNegative()
+ */
+ public IntegerConstraints nonNegativeMessage(String message) {
+ this.nonNegativeMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/databinding/validation/constraint/StringConstraints.java
===================================================================
RCS file: src/org/eclipse/core/databinding/validation/constraint/StringConstraints.java
diff -N src/org/eclipse/core/databinding/validation/constraint/StringConstraints.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/databinding/validation/constraint/StringConstraints.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,272 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.databinding.validation.constraint;
+
+import java.util.regex.Pattern;
+
+import org.eclipse.core.internal.databinding.validation.NonEmptyStringValidator;
+import org.eclipse.core.internal.databinding.validation.NonNullValidator;
+import org.eclipse.core.internal.databinding.validation.StringLengthValidator;
+import org.eclipse.core.internal.databinding.validation.StringRegexValidator;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides a set of constraints to apply to String
s.
+ *
+ * + * Instances of this class can be used to define a set of constraints to apply + * to strings. In addition, the validation messages to be issued for the + * individual constraints as well as the way in which model objects are + * formatted in the validation messages can be configured. + *
+ * + * @noextend This class is not intended to be subclassed by clients. + * @since 1.4 + */ +public class StringConstraints extends Constraints { + + private NumberFormat lengthFormat = NumberFormat.getIntegerInstance(); + + private String requiredMessage = null; + + private String nonEmptyMessage = null; + + private String minLengthMessage = null; + + private String maxLengthMessage = null; + + private String lengthRangeMessage = null; + + private String matchesMessage = null; + + /** + * Adds a validator ensuring that the input integer is notnull
+ * . Uses the current {@link #requiredMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public StringConstraints required() {
+ addValidator(new NonNullValidator(requiredMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #required()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #required()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #required()
+ */
+ public StringConstraints requiredMessage(String message) {
+ this.requiredMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input integer is not empty. Uses the
+ * current {@link #nonEmptyMessage(String) validation message}.
+ *
+ * @return This constraints instance for method chaining.
+ */
+ public StringConstraints nonEmpty() {
+ addValidator(new NonEmptyStringValidator(nonEmptyMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message for the {@link #nonEmpty()} constraint.
+ *
+ * @param message
+ * The validation message for the {@link #nonEmpty()} constraint.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #nonEmpty()
+ */
+ public StringConstraints nonEmptyMessage(String message) {
+ this.nonEmptyMessage = message;
+ return this;
+ }
+
+ /**
+ * Sets the format to use when formatting the string length in any of the
+ * validation messages stemming from a length constraint.
+ *
+ * @param format
+ * The format to use for displaying the string length in
+ * validation messages stemming from a length constraint.
+ * messages.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #minLength(int)
+ * @see #maxLength(int)
+ * @see #lengthRange(int, int)
+ */
+ public StringConstraints lengthFormat(NumberFormat format) {
+ this.lengthFormat = format;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input string has at least the
+ * specified amount of characters. Uses the current
+ * {@link #minLengthMessage(String) validation message} and
+ * {@link #lengthFormat(NumberFormat) length format}.
+ *
+ * @param minLength
+ * The minimal length to be enforced on the input string.
+ * @return This constraints instance for method chaining.
+ */
+ public StringConstraints minLength(int minLength) {
+ addValidator(StringLengthValidator.min(minLength, minLengthMessage,
+ lengthFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #minLength(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #minLength(int)}
+ * constraint. Can be parameterized with the minimum string
+ * length ({0}).
+ * @return This constraints instance for method chaining.
+ *
+ * @see #minLength(int)
+ */
+ public StringConstraints minLengthMessage(String message) {
+ this.minLengthMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input string has not more than the
+ * specified amount of characters. Uses the current
+ * {@link #maxLengthMessage(String) validation message} and
+ * {@link #lengthFormat(NumberFormat) length format}.
+ *
+ * @param maxLength
+ * The maximum length to be enforced on the input string.
+ * @return This constraints instance for method chaining.
+ */
+ public StringConstraints maxLength(int maxLength) {
+ addValidator(StringLengthValidator.max(maxLength, maxLengthMessage,
+ lengthFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #maxLength(int)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the {@link #maxLength(int)}
+ * constraint. Can be parameterized with the maximum string
+ * length ({0}).
+ * @return This constraints instance for method chaining.
+ *
+ * @see #maxLength(int)
+ */
+ public StringConstraints maxLengthMessage(String message) {
+ this.maxLengthMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the length of the input string is between
+ * the given bounds. Uses the current {@link #lengthRangeMessage(String)
+ * validation message} and {@link #lengthFormat(NumberFormat) length format}
+ * .
+ *
+ * @param minLength
+ * The minimal length to be enforced on the input string.
+ * @param maxLength
+ * The maximum length to be enforced on the input string.
+ * @return This constraints instance for method chaining.
+ */
+ public StringConstraints lengthRange(int minLength, int maxLength) {
+ addValidator(StringLengthValidator.range(minLength, maxLength,
+ lengthRangeMessage, lengthFormat));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the
+ * {@link #lengthRange(int, int)} constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #lengthRange(int, int)} constraint. Can be
+ * parameterized with the minimum ({0}) and maximum ({1}) string
+ * lengths.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #lengthRange(int, int)
+ */
+ public StringConstraints lengthRangeMessage(String message) {
+ this.lengthRangeMessage = message;
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input string
+ * {@link Pattern#matches(String, CharSequence) matches} the given regular
+ * expression. Uses the current {@link #matchesMessage(String) validation
+ * message}.
+ *
+ * @param regex
+ * The regular expression which the input string must match.
+ * @return This constraints instance for method chaining.
+ *
+ * @see Pattern#matches(String, CharSequence)
+ */
+ public StringConstraints matches(String regex) {
+ addValidator(new StringRegexValidator(regex, matchesMessage));
+ return this;
+ }
+
+ /**
+ * Adds a validator ensuring that the input string
+ * {@link Pattern#matches(String, CharSequence) matches} the given pattern.
+ * Uses the current {@link #matchesMessage(String) validation message}.
+ *
+ * @param pattern
+ * The pattern which the input string must match.
+ * @return This constraints instance for method chaining.
+ *
+ * @see Pattern#matches(String, CharSequence)
+ */
+ public StringConstraints matches(Pattern pattern) {
+ addValidator(new StringRegexValidator(pattern, matchesMessage));
+ return this;
+ }
+
+ /**
+ * Sets the validation message pattern for the {@link #matches(String)}
+ * constraint.
+ *
+ * @param message
+ * The validation message pattern for the
+ * {@link #matches(String)} constraint. Can be parameterized with
+ * the pattern string ({0}) which the input integer must match.
+ * @return This constraints instance for method chaining.
+ *
+ * @see #matches(String)
+ */
+ public StringConstraints matchesMessage(String message) {
+ this.matchesMessage = message;
+ return this;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/BindingMessages.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/BindingMessages.java,v
retrieving revision 1.6
diff -u -r1.6 BindingMessages.java
--- src/org/eclipse/core/internal/databinding/BindingMessages.java 9 May 2008 14:13:00 -0000 1.6
+++ src/org/eclipse/core/internal/databinding/BindingMessages.java 19 Sep 2010 15:12:43 -0000
@@ -8,6 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Tom Schindltrue
and
+ * false
value should be converted.
+ *
+ * @param trueValue
+ * The string to which to convert a true
value.
+ * @param falseValue
+ * The string to which to convert a false
value.
+ */
+ public final void setTargetStrings(String trueValue, String falseValue) {
+ this.trueValue = trueValue;
+ this.falseValue = falseValue;
+ }
+
+ public Object convert(Object source) {
+ Boolean value = (Boolean) source;
+ if (value != null) {
+ return value.booleanValue() ? trueValue : falseValue;
+ }
+ return ""; //$NON-NLS-1$
+ }
+}
Index: src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java,v
retrieving revision 1.5
diff -u -r1.5 DateConversionSupport.java
--- src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java 25 May 2009 20:52:20 -0000 1.5
+++ src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java 19 Sep 2010 15:12:44 -0000
@@ -10,6 +10,7 @@
* db4objects - Initial API and implementation
* Tom Schindltrue
and false
value, respectively.
+ *
+ * + * Note that the capitalization of the provided strings is ignored. + *
+ * + * @param trueValues + * The set of strings representing atrue
value.
+ * @param falseValues
+ * The set of strings representing a false
value.
+ */
+ public final void setSourceStrings(String[] trueValues, String[] falseValues) {
+ this.trueValues = new String[trueValues.length];
+ for (int i = 0; i < trueValues.length; i++) {
+ this.trueValues[i] = trueValues[i].toUpperCase();
+ }
+ Arrays.sort(this.trueValues); // for binary search
+
+ this.falseValues = new String[falseValues.length];
+ for (int i = 0; i < falseValues.length; i++) {
+ this.falseValues[i] = falseValues[i].toUpperCase();
+ }
+ Arrays.sort(this.falseValues); // for binary search
+ }
+
/*
* (non-Javadoc)
*
Index: src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java,v
retrieving revision 1.8
diff -u -r1.8 StringToNumberParser.java
--- src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java 1 Jun 2010 19:22:16 -0000 1.8
+++ src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java 19 Sep 2010 15:12:45 -0000
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Ovidio Mallo - bug 183055
*******************************************************************************/
package org.eclipse.core.internal.databinding.conversion;
@@ -115,12 +116,12 @@
.getErrorIndex() : position.getIndex();
if (errorIndex < value.length()) {
- return BindingMessages.formatString(
+ return BindingMessages.getFormattedString(
BindingMessages.VALIDATE_NUMBER_PARSE_ERROR, new Object[] {
value, new Integer(errorIndex + 1),
new Character(value.charAt(errorIndex)) });
}
- return BindingMessages.formatString(
+ return BindingMessages.getFormattedString(
BindingMessages.VALIDATE_NUMBER_PARSE_ERROR_NO_CHARACTER,
new Object[] { value, new Integer(errorIndex + 1) });
}
@@ -136,6 +137,23 @@
*/
public static String createOutOfRangeMessage(Number minValue,
Number maxValue, NumberFormat numberFormat) {
+ return createOutOfRangeMessage(BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_OUT_OF_RANGE_ERROR),
+ minValue, maxValue, numberFormat);
+ }
+
+ /**
+ * Formats an appropriate message for an out of range error.
+ *
+ * @param message
+ * @param minValue
+ * @param maxValue
+ * @param numberFormat
+ * when accessed method synchronizes on instance
+ * @return message
+ */
+ public static String createOutOfRangeMessage(String message,
+ Number minValue, Number maxValue, NumberFormat numberFormat) {
String min = null;
String max = null;
@@ -144,8 +162,8 @@
max = numberFormat.format(maxValue);
}
- return BindingMessages.formatString(
- "Validate_NumberOutOfRangeError", new Object[] { min, max }); //$NON-NLS-1$
+ return BindingMessages
+ .formatMessage(message, new Object[] { min, max });
}
/**
Index: src/org/eclipse/core/internal/databinding/conversion/StringTrimConverter.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/conversion/StringTrimConverter.java
diff -N src/org/eclipse/core/internal/databinding/conversion/StringTrimConverter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/conversion/StringTrimConverter.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.conversion;
+
+import org.eclipse.core.databinding.conversion.Converter;
+
+/**
+ * @since 1.3
+ */
+public class StringTrimConverter extends Converter {
+
+ private final boolean trimToNull;
+
+ /**
+ *
+ * @param trimToNull
+ */
+ public StringTrimConverter(boolean trimToNull) {
+ super(String.class, String.class);
+ this.trimToNull = trimToNull;
+ }
+
+ public Object convert(Object fromObject) {
+ String string = (String) fromObject;
+
+ if (string != null) {
+ string = string.trim();
+
+ if (trimToNull && string.length() == 0) {
+ string = null;
+ }
+ }
+
+ return string;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/messages.properties
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/messages.properties,v
retrieving revision 1.9
diff -u -r1.9 messages.properties
--- src/org/eclipse/core/internal/databinding/messages.properties 16 Apr 2008 02:51:53 -0000 1.9
+++ src/org/eclipse/core/internal/databinding/messages.properties 19 Sep 2010 15:12:45 -0000
@@ -43,6 +43,8 @@
TrueStringValues=yes,true
FalseStringValues=no,false
+Validate_InvalidBooleanString=The boolean string is invalid.
+
Validate_NumberOutOfRangeError=Please enter a value between [{0}] and [{1}] and with a similar format.
Validate_NumberParseError=Invalid character for value [{0}] at position [{1}] character [{2}].
Validate_NumberParseErrorNoCharacter=Missing character for value [{0}] at position [{1}].
@@ -53,4 +55,47 @@
Validate_NoChangeAllowedHelp=Changes are not allowed in this field
Validate_CharacterHelp=Please type a character
+#CharacterValidator
+Validate_CharacterNoWhitespace=The character must be no whitespace.
+Validate_CharacterNoSpace=The character must be no space.
+Validate_CharacterLetter=The character must be a letter.
+Validate_CharacterDigit=The character must be a digit.
+Validate_CharacterLetterOrDigit=The character must be a letter or digit.
+
+#NonNullValidator
+Validate_NonNull=The value must not be empty.
+
+#NonEmptyStringValidator
+Validate_NonEmptyString=The string must not be empty.
+
+#StringRegexValidator
+Validate_NonStringRegex=The string must match the following pattern: {0}.
+
+#NumberRangeValidator
+Validate_NumberRangeGreater=The value must be greater than {0}.
+Validate_NumberRangeGreaterEqual=The value must be greater than or equal to {0}.
+Validate_NumberRangeLess=The value must be less than {0}.
+Validate_NumberRangeLessEqual=The value must be less than or equal to {0}.
+Validate_NumberRangeWithinRange=The value must lie between {0} and {1}.
+Validate_NumberRangePositive=The value must be positive.
+Validate_NumberRangeNonNegative=The value must not be negative.
+
+#DateRangeValidator
+Validate_DateRangeAfter=The date must be after {0}.
+Validate_DateRangeAfterEqual=The date must be after or on {0}.
+Validate_DateRangeBefore=The date must be before {0}.
+Validate_DateRangeBeforeEqual=The date must be before or on {0}.
+Validate_DateRangeWithinRange=The date must lie between {0} and {1}.
+
+#StringLengthValidator
+Validate_StringLengthMin=The string must have at least {0} characters.
+Validate_StringLengthMax=The string must not have more than {0} characters.
+Validate_StringLengthRange=The string must have between {0} and {1} characters.
+
+#DecimalScaleValidator
+Validate_DecimalMaxScale=The decimal must not have more than {0} fractional digits.
+
+#DecimalPrecisionValidator
+Validate_DecimalMaxPrecision=The decimal must not have more than {0} significant digits.
+
Examples=Examples
\ No newline at end of file
Index: src/org/eclipse/core/internal/databinding/validation/AbstractStringToNumberValidator.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/AbstractStringToNumberValidator.java,v
retrieving revision 1.2
diff -u -r1.2 AbstractStringToNumberValidator.java
--- src/org/eclipse/core/internal/databinding/validation/AbstractStringToNumberValidator.java 4 Jul 2007 20:08:45 -0000 1.2
+++ src/org/eclipse/core/internal/databinding/validation/AbstractStringToNumberValidator.java 19 Sep 2010 15:12:45 -0000
@@ -7,12 +7,16 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Ovidio Mallo - bug 183055
******************************************************************************/
package org.eclipse.core.internal.databinding.validation;
+import java.text.ParsePosition;
+
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
import org.eclipse.core.runtime.IStatus;
@@ -31,7 +35,10 @@
private final Number min;
private final Number max;
- private String outOfRangeMessage;
+ private String parseErrorMessage;
+
+ private String outOfRangeMessage = BindingMessages.getString(BindingMessages.VALIDATE_NUMBER_OUT_OF_RANGE_ERROR);
+ private String formattedOutOfRangeMessage;
/**
* Constructs a new instance.
@@ -55,6 +62,34 @@
}
/**
+ * Sets the validation message pattern to be used in case the source string
+ * does not represent a valid number.
+ *
+ * @param message
+ * The validation message pattern to be used in case the source
+ * string does not represent a valid number.
+ */
+ public final void setParseErrorMessage(String message) {
+ this.parseErrorMessage = message;
+ }
+
+ /**
+ * Sets the validation message pattern to be used in case the parsed number
+ * lies outside the value range supported by the number type associated to
+ * this validator.
+ *
+ * @param message
+ * The validation message pattern to be used in case the parsed
+ * number lies outside the supported value range. Can be
+ * parameterized by the Integer.MIN_VALUE
and
+ * Integer.MAX_VALUE
values.
+ */
+ public final void setOutOfRangeMessage(String message) {
+ this.outOfRangeMessage = message;
+ this.formattedOutOfRangeMessage = null;
+ }
+
+ /**
* Validates the provided value
. An error status is returned if:
* true
if in range
*/
protected abstract boolean isInRange(Number number);
+
+ private String createParseErrorMessage(String input,
+ ParsePosition parsePosition) {
+ if (parseErrorMessage == null) {
+ return StringToNumberParser.createParseErrorMessage(input,
+ parsePosition);
+ }
+ return parseErrorMessage;
+ }
+
+ private String getFormattedOutOfRangeMessage() {
+ if (formattedOutOfRangeMessage == null) {
+ formattedOutOfRangeMessage = StringToNumberParser
+ .createOutOfRangeMessage(outOfRangeMessage, min, max,
+ converter.getNumberFormat());
+ }
+ return formattedOutOfRangeMessage;
+ }
}
Index: src/org/eclipse/core/internal/databinding/validation/BigDecimalRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/BigDecimalRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/BigDecimalRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/BigDecimalRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,206 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.math.BigDecimal;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for BigDecimals which must lie within an open/closed
+ * range.
+ *
+ * @since 1.3
+ */
+public class BigDecimalRangeValidator extends NumberRangeValidator {
+
+ private static final BigDecimal ZERO = new BigDecimal("0"); //$NON-NLS-1$
+ private static final BigDecimal ONE = new BigDecimal("1"); //$NON-NLS-1$
+
+ /**
+ * Single constructor which supports the individual integer range types.
+ *
+ * @param min
+ * The min BigDecimal of the range or null
in case
+ * no lower bound is defined.
+ * @param max
+ * The max BigDecimal of the range or null
in case
+ * no upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The BigDecimal format to use for formatting integers in the
+ * validation message.
+ */
+ private BigDecimalRangeValidator(BigDecimal min, BigDecimal max,
+ int minConstraint, int maxConstraint, String validationMessage,
+ NumberFormat format) {
+ super(min, max, minConstraint, maxConstraint, validationMessage, format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is greater than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the greater constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator greater(BigDecimal number,
+ String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(number, null, GREATER, UNDEFINED,
+ defaultIfNull(validationMessage, GREATER_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is greater than
+ * or equal to the given number.
+ *
+ * @param number
+ * The reference number of the greater-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator greaterEqual(BigDecimal number,
+ String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(number, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage,
+ GREATER_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is less than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the less constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator less(BigDecimal number,
+ String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(null, number, UNDEFINED, LESS,
+ defaultIfNull(validationMessage, LESS_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is less than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the less-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator lessEqual(BigDecimal number,
+ String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(null, number, UNDEFINED,
+ LESS_EQUAL,
+ defaultIfNull(validationMessage, LESS_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is within the
+ * given range.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the range's lower and upper bound.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator range(BigDecimal min,
+ BigDecimal max, String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(min, max, GREATER_EQUAL,
+ LESS_EQUAL, defaultIfNull(validationMessage,
+ WITHIN_RANGE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is positive.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator positive(String validationMessage,
+ NumberFormat format) {
+ return new BigDecimalRangeValidator(ONE, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage, POSITIVE_MESSAGE),
+ format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigDecimal is
+ * non-negative.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting BigDecimals in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigDecimalRangeValidator nonNegative(
+ String validationMessage, NumberFormat format) {
+ return new BigDecimalRangeValidator(ZERO, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage,
+ NON_NEGATIVE_MESSAGE), format);
+ }
+
+ protected int compare(Number number1, Number number2) {
+ BigDecimal bigDecimal1 = (BigDecimal) number1;
+ BigDecimal bigDecimal2 = (BigDecimal) number2;
+ return bigDecimal1.compareTo(bigDecimal2);
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/BigIntegerRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/BigIntegerRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/BigIntegerRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/BigIntegerRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,206 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.math.BigInteger;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for BigIntegers which must lie within an open/closed
+ * range.
+ *
+ * @since 1.3
+ */
+public class BigIntegerRangeValidator extends NumberRangeValidator {
+
+ private static final BigInteger ZERO = new BigInteger("0"); //$NON-NLS-1$
+ private static final BigInteger ONE = new BigInteger("1"); //$NON-NLS-1$
+
+ /**
+ * Single constructor which supports the individual integer range types.
+ *
+ * @param min
+ * The min BigInteger of the range or null
in case
+ * no lower bound is defined.
+ * @param max
+ * The max BigInteger of the range or null
in case
+ * no upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The BigInteger format to use for formatting integers in the
+ * validation message.
+ */
+ private BigIntegerRangeValidator(BigInteger min, BigInteger max,
+ int minConstraint, int maxConstraint, String validationMessage,
+ NumberFormat format) {
+ super(min, max, minConstraint, maxConstraint, validationMessage, format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is greater than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the greater constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator greater(BigInteger number,
+ String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(number, null, GREATER, UNDEFINED,
+ defaultIfNull(validationMessage, GREATER_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is greater than
+ * or equal to the given number.
+ *
+ * @param number
+ * The reference number of the greater-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator greaterEqual(BigInteger number,
+ String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(number, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage,
+ GREATER_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is less than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the less constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator less(BigInteger number,
+ String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(null, number, UNDEFINED, LESS,
+ defaultIfNull(validationMessage, LESS_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is less than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the less-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator lessEqual(BigInteger number,
+ String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(null, number, UNDEFINED,
+ LESS_EQUAL,
+ defaultIfNull(validationMessage, LESS_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is within the
+ * given range.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the range's lower and upper bound.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator range(BigInteger min,
+ BigInteger max, String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(min, max, GREATER_EQUAL,
+ LESS_EQUAL, defaultIfNull(validationMessage,
+ WITHIN_RANGE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is positive.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator positive(String validationMessage,
+ NumberFormat format) {
+ return new BigIntegerRangeValidator(ONE, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage, POSITIVE_MESSAGE),
+ format);
+ }
+
+ /**
+ * Creates a validator which checks that an input BigInteger is
+ * non-negative.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting BigIntegers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static BigIntegerRangeValidator nonNegative(
+ String validationMessage, NumberFormat format) {
+ return new BigIntegerRangeValidator(ZERO, null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage,
+ NON_NEGATIVE_MESSAGE), format);
+ }
+
+ protected int compare(Number number1, Number number2) {
+ BigInteger bigInteger1 = (BigInteger) number1;
+ BigInteger bigInteger2 = (BigInteger) number2;
+ return bigInteger1.compareTo(bigInteger2);
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/CharacterValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/CharacterValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/CharacterValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/CharacterValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+/**
+ * @since 1.3
+ */
+public class CharacterValidator implements IValidator {
+
+ private static final int VALIDATE_NO_WHITESPACE = 0;
+ private static final int VALIDATE_NO_SPACE = 1;
+ private static final int VALIDATE_LETTER = 2;
+ private static final int VALIDATE_DIGIT = 3;
+ private static final int VALIDATE_LETTER_OR_DIGIT = 4;
+
+ private static final String NO_WHITESPACE_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_CHARACTER_NO_WHITESPACE);
+ private static final String NO_SPACE_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_CHARACTER_NO_SPACE);
+ private static final String LETTER_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_CHARACTER_LETTER);
+ private static final String DIGIT_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_CHARACTER_DIGIT);
+ private static final String LETTER_OR_DIGIT_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_CHARACTER_LETTER_OR_DIGIT);
+
+ private final int validation;
+ private final String validationMessage;
+
+ private CharacterValidator(int validation, String validationMessage) {
+ this.validation = validation;
+ this.validationMessage = validationMessage;
+ }
+
+ /**
+ * Creates a validator which checks that an input character is no
+ * {@link Character#isWhitespace(char) whitespace}.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @return The validator instance.
+ */
+ public static CharacterValidator noWhitespace(String validationMessage) {
+ return new CharacterValidator(VALIDATE_NO_WHITESPACE, defaultIfNull(
+ NO_WHITESPACE_VALIDATION_MESSAGE, validationMessage));
+ }
+
+ /**
+ * Creates a validator which checks that an input character is no
+ * {@link Character#isSpaceChar(char) space}.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @return The validator instance.
+ */
+ public static CharacterValidator noSpace(String validationMessage) {
+ return new CharacterValidator(VALIDATE_NO_SPACE, defaultIfNull(
+ NO_SPACE_VALIDATION_MESSAGE, validationMessage));
+ }
+
+ /**
+ * Creates a validator which checks that an input character is a
+ * {@link Character#isLetter(char) letter}.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @return The validator instance.
+ */
+ public static CharacterValidator letter(String validationMessage) {
+ return new CharacterValidator(VALIDATE_LETTER, defaultIfNull(
+ LETTER_VALIDATION_MESSAGE, validationMessage));
+ }
+
+ /**
+ * Creates a validator which checks that an input character is a
+ * {@link Character#isDigit(char) digit}.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @return The validator instance.
+ */
+ public static CharacterValidator digit(String validationMessage) {
+ return new CharacterValidator(VALIDATE_DIGIT, defaultIfNull(
+ DIGIT_VALIDATION_MESSAGE, validationMessage));
+ }
+
+ /**
+ * Creates a validator which checks that an input character is a
+ * {@link Character#isLetterOrDigit(char) letter or ditig}.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @return The validator instance.
+ */
+ public static CharacterValidator letterOrDigit(String validationMessage) {
+ return new CharacterValidator(VALIDATE_LETTER_OR_DIGIT, defaultIfNull(
+ LETTER_OR_DIGIT_VALIDATION_MESSAGE, validationMessage));
+ }
+
+ public IStatus validate(Object value) {
+ if (value != null) {
+ char character = ((Character) value).charValue();
+ if (!isValid(character)) {
+ return ValidationStatus.error(validationMessage);
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private boolean isValid(char character) {
+ switch (validation) {
+ case VALIDATE_NO_WHITESPACE:
+ return !Character.isWhitespace(character);
+ case VALIDATE_NO_SPACE:
+ return !Character.isSpaceChar(character);
+ case VALIDATE_LETTER:
+ return Character.isLetter(character);
+ case VALIDATE_DIGIT:
+ return Character.isDigit(character);
+ case VALIDATE_LETTER_OR_DIGIT:
+ return Character.isLetterOrDigit(character);
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported validation: " + validation); //$NON-NLS-1$
+ }
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/DateRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/DateRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/DateRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/DateRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,243 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+import java.util.Date;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+import com.ibm.icu.text.DateFormat;
+
+/**
+ * Provides validations for date which must lie within an open/closed range.
+ *
+ * @since 1.3
+ */
+public class DateRangeValidator implements IValidator {
+
+ // The set of constraint types for the lower/upper bound of the date range.
+ private static final int UNDEFINED = -1;
+ private static final int AFTER = 0;
+ private static final int AFTER_EQUAL = 1;
+ private static final int BEFORE = 2;
+ private static final int BEFORE_EQUAL = 3;
+
+ // The default validation messages.
+ private static final String AFTER_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DATE_RANGE_AFTER);
+ private static final String AFTER_EQUAL_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DATE_RANGE_AFTER_EQUAL);
+ private static final String BEFORE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DATE_RANGE_BEFORE);
+ private static final String BEFORE_EQUAL_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DATE_RANGE_BEFORE_EQUAL);
+ private static final String WITHIN_RANGE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DATE_RANGE_WITHIN_RANGE);
+
+ private final Date min;
+ private final Date max;
+ private final int minConstraint;
+ private final int maxConstraint;
+ private final String validationMessage;
+ private String formattedValidationMessage;
+ private final DateFormat format;
+
+ /**
+ * Single constructor which supports the individual date range types.
+ *
+ * @param min
+ * The min date of the range or null
in case no
+ * lower bound is defined.
+ * @param max
+ * The max date of the range or null
in case no
+ * upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The date format to use for formatting dates in the validation
+ * message.
+ */
+ private DateRangeValidator(Date min, Date max, int minConstraint,
+ int maxConstraint, String validationMessage, DateFormat format) {
+ this.min = min;
+ this.max = max;
+ this.minConstraint = minConstraint;
+ this.maxConstraint = maxConstraint;
+ this.validationMessage = validationMessage;
+ this.format = format;
+ }
+
+ /**
+ * Creates a validator which checks that an input date is after the given
+ * date.
+ *
+ * @param date
+ * The reference date of the after constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference date.
+ * @param format
+ * The display format to use for formatting dates in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DateRangeValidator after(Date date, String validationMessage,
+ DateFormat format) {
+ return new DateRangeValidator(date, null, AFTER, UNDEFINED,
+ defaultIfNull(validationMessage, AFTER_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input date is after or on the
+ * given date.
+ *
+ * @param date
+ * The reference date of the after-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference date.
+ * @param format
+ * The display format to use for formatting dates in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DateRangeValidator afterEqual(Date date,
+ String validationMessage, DateFormat format) {
+ return new DateRangeValidator(date, null, AFTER_EQUAL, UNDEFINED,
+ defaultIfNull(validationMessage, AFTER_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input date is before the given
+ * date.
+ *
+ * @param date
+ * The reference date of the before constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference date.
+ * @param format
+ * The display format to use for formatting dates in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DateRangeValidator before(Date date,
+ String validationMessage, DateFormat format) {
+ return new DateRangeValidator(null, date, UNDEFINED, BEFORE,
+ defaultIfNull(validationMessage, BEFORE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input date is before or on the
+ * given date.
+ *
+ * @param date
+ * The reference date of the before-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference date.
+ * @param format
+ * The display format to use for formatting dates in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DateRangeValidator beforeEqual(Date date,
+ String validationMessage, DateFormat format) {
+ return new DateRangeValidator(null, date, UNDEFINED, BEFORE_EQUAL,
+ defaultIfNull(validationMessage, BEFORE_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input date is within the given
+ * range.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the range's lower and upper bound.
+ * @param format
+ * The display format to use for formatting dates in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DateRangeValidator range(Date min, Date max,
+ String validationMessage, DateFormat format) {
+ return new DateRangeValidator(min, max, AFTER_EQUAL, BEFORE_EQUAL,
+ defaultIfNull(validationMessage, WITHIN_RANGE_MESSAGE), format);
+ }
+
+ public IStatus validate(Object value) {
+ if (value != null) {
+ Date date = (Date) value;
+ if ((min != null && !fulfillsConstraint(date, min, minConstraint))
+ || (max != null && !fulfillsConstraint(date, max,
+ maxConstraint))) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private boolean fulfillsConstraint(Date date1, Date date2, int constraint) {
+ switch (constraint) {
+ case AFTER:
+ return date1.after(date2);
+ case AFTER_EQUAL:
+ return date1.after(date2) || date1.equals(date2);
+ case BEFORE:
+ return date1.before(date2);
+ case BEFORE_EQUAL:
+ return date1.before(date2) || date1.equals(date2);
+ case UNDEFINED:
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported constraint: " + constraint); //$NON-NLS-1$
+ }
+ }
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, getValidationMessageArguments());
+ }
+ return formattedValidationMessage;
+ }
+
+ private String[] getValidationMessageArguments() {
+ synchronized (format) {
+ if (min == null) {
+ return new String[] { format.format(max) };
+ } else if (max == null) {
+ return new String[] { format.format(min) };
+ }
+ return new String[] { format.format(min), format.format(max) };
+ }
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/DecimalPrecisionValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/DecimalPrecisionValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/DecimalPrecisionValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/DecimalPrecisionValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+import com.ibm.icu.math.BigDecimal;
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for the precision of decimal numbers.
+ *
+ * @since 1.3
+ */
+public class DecimalPrecisionValidator implements IValidator {
+
+ private static final String MAX_PRECISION_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DECIMAL_MAX_PRECISION);
+
+ private final int maxPrecision;
+
+ private final String validationMessage;
+
+ private String formattedValidationMessage;
+
+ private final NumberFormat precisionFormat;
+
+ private DecimalPrecisionValidator(int maxPrecision,
+ String validationMessage, NumberFormat precisionFormat) {
+ this.maxPrecision = maxPrecision;
+ this.validationMessage = validationMessage != null ? validationMessage
+ : MAX_PRECISION_VALIDATION_MESSAGE;
+ this.precisionFormat = precisionFormat;
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal has a precision
+ * which is less or equal to the given precision.
+ *
+ * @param maxPrecision
+ * The maximum precision to enforce on the input decimal.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the maximum precision to be enforced.
+ * @param precisionFormat
+ * The display format to use for formatting the precision in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalPrecisionValidator max(int maxPrecision,
+ String validationMessage, NumberFormat precisionFormat) {
+ return new DecimalPrecisionValidator(maxPrecision, validationMessage,
+ precisionFormat);
+ }
+
+ public IStatus validate(Object value) {
+ if (value != null) {
+ Number number = (Number) value;
+ final int precision;
+ if (number instanceof BigDecimal) {
+ precision = computePrecision(((BigDecimal) number));
+ } else {
+ precision = computePrecision(new BigDecimal(number
+ .doubleValue()));
+ }
+ if (precision > maxPrecision) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private static int computePrecision(BigDecimal bigDecimal) {
+ // In Java 1.4, there is no direct API for retrieving the precision of
+ // a BigDecimal so we use the length of the string representation of the
+ // unscaled BigInteger value of the BigDecimal to actually compute it.
+ return bigDecimal.unscaledValue().abs().toString().length();
+ }
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, getValidationMessageArguments());
+ }
+ return formattedValidationMessage;
+ }
+
+ private String[] getValidationMessageArguments() {
+ synchronized (precisionFormat) {
+ return new String[] { precisionFormat.format(new Integer(
+ maxPrecision)) };
+ }
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/DecimalRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/DecimalRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/DecimalRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/DecimalRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,201 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for decimal numbers which must lie within an open/closed
+ * range.
+ *
+ * @since 1.3
+ */
+public class DecimalRangeValidator extends NumberRangeValidator {
+
+ private static final Double ZERO = new Double(0);
+ private static final Double ONE = new Double(1);
+
+ /**
+ * Single constructor which supports the individual decimal range types.
+ *
+ * @param min
+ * The min decimal of the range or null
in case no
+ * lower bound is defined.
+ * @param max
+ * The max decimal of the range or null
in case no
+ * upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The decimal format to use for formatting integers in the
+ * validation message.
+ */
+ private DecimalRangeValidator(Number min, Number max, int minConstraint,
+ int maxConstraint, String validationMessage, NumberFormat format) {
+ super(min, max, minConstraint, maxConstraint, validationMessage, format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is greater than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the greater constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator greater(double number,
+ String validationMessage, NumberFormat format) {
+ return new DecimalRangeValidator(new Double(number), null, GREATER,
+ UNDEFINED, defaultIfNull(validationMessage, GREATER_MESSAGE),
+ format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is greater than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the greater-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator greaterEqual(double number,
+ String validationMessage, NumberFormat format) {
+ return new DecimalRangeValidator(new Double(number), null,
+ GREATER_EQUAL, UNDEFINED, defaultIfNull(validationMessage,
+ GREATER_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is less than the
+ * given number.
+ *
+ * @param number
+ * The reference number of the less constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator less(double number,
+ String validationMessage, NumberFormat format) {
+ return new DecimalRangeValidator(null, new Double(number), UNDEFINED,
+ LESS, defaultIfNull(validationMessage, LESS_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is less than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the less-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator lessEqual(double number,
+ String validationMessage, NumberFormat format) {
+ return new DecimalRangeValidator(null, new Double(number), UNDEFINED,
+ LESS_EQUAL,
+ defaultIfNull(validationMessage, LESS_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is within the
+ * given range.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the range's lower and upper bound.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator range(double min, double max,
+ String validationMessage, NumberFormat format) {
+ return new DecimalRangeValidator(new Double(min), new Double(max),
+ GREATER_EQUAL, LESS_EQUAL, defaultIfNull(validationMessage,
+ WITHIN_RANGE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is positive.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator positive(String validationMessage,
+ NumberFormat format) {
+ return new DecimalRangeValidator(ONE, null, GREATER_EQUAL, UNDEFINED,
+ defaultIfNull(validationMessage, POSITIVE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal is non-negative.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalRangeValidator nonNegative(String validationMessage,
+ NumberFormat format) {
+ return new DecimalRangeValidator(ZERO, null, GREATER_EQUAL, UNDEFINED,
+ defaultIfNull(validationMessage, NON_NEGATIVE_MESSAGE), format);
+ }
+
+ protected int compare(Number number1, Number number2) {
+ double val1 = number1.doubleValue();
+ double val2 = number2.doubleValue();
+ return (val1 < val2) ? -1 : ((val1 > val2) ? + 1 : 0);
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/DecimalScaleValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/DecimalScaleValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/DecimalScaleValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/DecimalScaleValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+import com.ibm.icu.math.BigDecimal;
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for the scale of decimal numbers.
+ *
+ * @since 1.3
+ */
+public class DecimalScaleValidator implements IValidator {
+
+ private static final String MAX_SCALE_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_DECIMAL_MAX_SCALE);
+
+ private final int maxScale;
+
+ private final String validationMessage;
+
+ private String formattedValidationMessage;
+
+ private final NumberFormat scaleFormat;
+
+ private DecimalScaleValidator(int maxScale, String validationMessage,
+ NumberFormat scaleFormat) {
+ this.maxScale = maxScale;
+ this.validationMessage = validationMessage != null ? validationMessage
+ : MAX_SCALE_VALIDATION_MESSAGE;
+ this.scaleFormat = scaleFormat;
+ }
+
+ /**
+ * Creates a validator which checks that an input decimal has a scale which
+ * is less or equal to the given scale.
+ *
+ * @param maxScale
+ * The maximum scale to enforce on the input decimal.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the maximum scale to be enforced.
+ * @param scaleFormat
+ * The display format to use for formatting the scale in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static DecimalScaleValidator max(int maxScale,
+ String validationMessage, NumberFormat scaleFormat) {
+ return new DecimalScaleValidator(maxScale, validationMessage,
+ scaleFormat);
+ }
+
+ public IStatus validate(Object value) {
+ if (value != null) {
+ Number number = (Number) value;
+ final int scale;
+ if (number instanceof BigDecimal) {
+ scale = ((BigDecimal) number).scale();
+ } else {
+ scale = new BigDecimal(number.doubleValue()).scale();
+ }
+ if (scale > maxScale) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, getValidationMessageArguments());
+ }
+ return formattedValidationMessage;
+ }
+
+ private String[] getValidationMessageArguments() {
+ synchronized (scaleFormat) {
+ return new String[] { scaleFormat.format(new Integer(maxScale)) };
+ }
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/IntegerRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/IntegerRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/IntegerRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/IntegerRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,201 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for integer numbers which must lie within an open/closed
+ * range.
+ *
+ * @since 1.3
+ */
+public class IntegerRangeValidator extends NumberRangeValidator {
+
+ private static final Long ZERO = new Long(0);
+ private static final Long ONE = new Long(1);
+
+ /**
+ * Single constructor which supports the individual integer range types.
+ *
+ * @param min
+ * The min integer of the range or null
in case no
+ * lower bound is defined.
+ * @param max
+ * The max integer of the range or null
in case no
+ * upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The integer format to use for formatting integers in the
+ * validation message.
+ */
+ private IntegerRangeValidator(Number min, Number max, int minConstraint,
+ int maxConstraint, String validationMessage, NumberFormat format) {
+ super(min, max, minConstraint, maxConstraint, validationMessage, format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is greater than
+ * the given number.
+ *
+ * @param number
+ * The reference number of the greater constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator greater(long number,
+ String validationMessage, NumberFormat format) {
+ return new IntegerRangeValidator(new Long(number), null, GREATER,
+ UNDEFINED, defaultIfNull(validationMessage, GREATER_MESSAGE),
+ format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is greater than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the greater-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator greaterEqual(long number,
+ String validationMessage, NumberFormat format) {
+ return new IntegerRangeValidator(new Long(number), null, GREATER_EQUAL,
+ UNDEFINED, defaultIfNull(validationMessage,
+ GREATER_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is less than the
+ * given number.
+ *
+ * @param number
+ * The reference number of the less constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator less(long number,
+ String validationMessage, NumberFormat format) {
+ return new IntegerRangeValidator(null, new Long(number), UNDEFINED,
+ LESS, defaultIfNull(validationMessage, LESS_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is less than or
+ * equal to the given number.
+ *
+ * @param number
+ * The reference number of the less-equal constraint.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given reference number.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator lessEqual(long number,
+ String validationMessage, NumberFormat format) {
+ return new IntegerRangeValidator(null, new Long(number), UNDEFINED,
+ LESS_EQUAL,
+ defaultIfNull(validationMessage, LESS_EQUAL_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is within the
+ * given range.
+ *
+ * @param min
+ * The lower bound of the range (inclusive).
+ * @param max
+ * The upper bound of the range (inclusive).
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the range's lower and upper bound.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator range(long min, long max,
+ String validationMessage, NumberFormat format) {
+ return new IntegerRangeValidator(new Long(min), new Long(max),
+ GREATER_EQUAL, LESS_EQUAL, defaultIfNull(validationMessage,
+ WITHIN_RANGE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is positive.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator positive(String validationMessage,
+ NumberFormat format) {
+ return new IntegerRangeValidator(ONE, null, GREATER_EQUAL, UNDEFINED,
+ defaultIfNull(validationMessage, POSITIVE_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input integer is non-negative.
+ *
+ * @param validationMessage
+ * The validation message to use.
+ * @param format
+ * The display format to use for formatting integers in the
+ * validation message.
+ * @return The validator instance.
+ */
+ public static IntegerRangeValidator nonNegative(String validationMessage,
+ NumberFormat format) {
+ return new IntegerRangeValidator(ZERO, null, GREATER_EQUAL, UNDEFINED,
+ defaultIfNull(validationMessage, NON_NEGATIVE_MESSAGE), format);
+ }
+
+ protected int compare(Number number1, Number number2) {
+ long val1 = number1.longValue();
+ long val2 = number2.longValue();
+ return (val1 < val2) ? -1 : ((val1 > val2) ? + 1 : 0);
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/NonEmptyStringValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/NonEmptyStringValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/NonEmptyStringValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/NonEmptyStringValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+/**
+ * @since 1.3
+ */
+public class NonEmptyStringValidator implements IValidator {
+
+ private static final String NON_EMPTY_STRING_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NON_EMPTY_STRING);
+
+ private final String validationMessage;
+
+ /**
+ *
+ */
+ public NonEmptyStringValidator() {
+ this(null);
+ }
+
+ /**
+ * @param validationMessage
+ */
+ public NonEmptyStringValidator(String validationMessage) {
+ this.validationMessage = validationMessage != null ? validationMessage
+ : NON_EMPTY_STRING_VALIDATION_MESSAGE;
+ }
+
+ public IStatus validate(Object value) {
+ String input = (String) value;
+ if (input == null || input.length() == 0) {
+ return ValidationStatus.error(validationMessage);
+ }
+ return ValidationStatus.ok();
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/NonNullValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/NonNullValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/NonNullValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/NonNullValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+/**
+ * @since 1.3
+ */
+public class NonNullValidator implements IValidator {
+
+ private static final String NON_NULL_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NON_NULL);
+
+ private final String validationMessage;
+
+ /**
+ * @param validationMessage
+ */
+ public NonNullValidator(String validationMessage) {
+ this.validationMessage = validationMessage != null ? validationMessage
+ : NON_NULL_VALIDATION_MESSAGE;
+ }
+
+ public IStatus validate(Object value) {
+ if (value == null) {
+ return ValidationStatus.error(validationMessage);
+ }
+ return ValidationStatus.ok();
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/NumberRangeValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/NumberRangeValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/NumberRangeValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/NumberRangeValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,156 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for numbers which must lie within an open/closed range.
+ *
+ * @since 1.3
+ */
+public abstract class NumberRangeValidator implements IValidator {
+
+ // The set of constraint types for the lower/upper bound of the date range.
+ protected static final int UNDEFINED = -1;
+ protected static final int GREATER = 0;
+ protected static final int GREATER_EQUAL = 1;
+ protected static final int LESS = 2;
+ protected static final int LESS_EQUAL = 3;
+
+ // The default validation messages.
+ protected static final String GREATER_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_GREATER);
+ protected static final String GREATER_EQUAL_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_GREATER_EQUAL);
+ protected static final String LESS_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_LESS);
+ protected static final String LESS_EQUAL_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_LESS_EQUAL);
+ protected static final String WITHIN_RANGE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_WITHIN_RANGE);
+ protected static final String POSITIVE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_POSITIVE);
+ protected static final String NON_NEGATIVE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_NUMBER_RANGE_NON_NEGATIVE);
+
+ private final Number min;
+ private final Number max;
+ private final int minConstraint;
+ private final int maxConstraint;
+ private final String validationMessage;
+ private String formattedValidationMessage;
+ private final NumberFormat format;
+
+ /**
+ * Single constructor which supports the individual number range types.
+ *
+ * @param min
+ * The min number of the range or null
in case no
+ * lower bound is defined.
+ * @param max
+ * The max number of the range or null
in case no
+ * upper bound is defined.
+ * @param minConstraint
+ * The type of constraint imposed by the lower bound of the
+ * range.
+ * @param maxConstraint
+ * The type of constraint imposed by the upper bound of the
+ * range.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The integer format to use for formatting numbers in the
+ * validation message.
+ */
+ protected NumberRangeValidator(Number min, Number max, int minConstraint,
+ int maxConstraint, String validationMessage, NumberFormat format) {
+ this.min = min;
+ this.max = max;
+ this.minConstraint = minConstraint;
+ this.maxConstraint = maxConstraint;
+ this.validationMessage = validationMessage;
+ this.format = format;
+ }
+
+ public IStatus validate(Object value) {
+ if (value != null) {
+ Number number = (Number) value;
+ if ((min != null && !fulfillsConstraint(number, min, minConstraint))
+ || (max != null && !fulfillsConstraint(number, max,
+ maxConstraint))) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private boolean fulfillsConstraint(Number number1, Number number2,
+ int constraint) {
+ int compareResult = compare(number1, number2);
+ switch (constraint) {
+ case GREATER:
+ return compareResult > 0;
+ case GREATER_EQUAL:
+ return compareResult >= 0;
+ case LESS:
+ return compareResult < 0;
+ case LESS_EQUAL:
+ return compareResult <= 0;
+ case UNDEFINED:
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported constraint: " + constraint); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Comparator method to be implemented by subclasses in order to compare two
+ * instances of the concrete number type.
+ *
+ * @param number1
+ * The first number to compare.
+ * @param number2
+ * The second number to compare.
+ * @return A negative number, zero, or a positive number in case the first
+ * number is smaller than, equal to, or greater than the second
+ * number, respectively.
+ */
+ protected abstract int compare(Number number1, Number number2);
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, getValidationMessageArguments());
+ }
+ return formattedValidationMessage;
+ }
+
+ private String[] getValidationMessageArguments() {
+ synchronized (format) {
+ if (min == null) {
+ return new String[] { format.format(max) };
+ } else if (max == null) {
+ return new String[] { format.format(min) };
+ }
+ return new String[] { format.format(min), format.format(max) };
+ }
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringLengthValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/StringLengthValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/StringLengthValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/StringLengthValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,167 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+import com.ibm.icu.text.NumberFormat;
+
+/**
+ * Provides validations for strings which must have a specified length.
+ *
+ * @since 1.3
+ */
+public class StringLengthValidator implements IValidator {
+
+ // The default validation messages.
+ private static final String MIN_LENGTH_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_STRING_LENGTH_MIN);
+ private static final String MAX_LENGTH_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_STRING_LENGTH_MAX);
+ private static final String LENGTH_RANGE_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_STRING_LENGTH_RANGE);
+
+ private final Integer minLength;
+ private final Integer maxLength;
+ private final String validationMessage;
+ private String formattedValidationMessage;
+ private final NumberFormat format;
+
+ /**
+ * Creates a string length validator defining a minimum and/or maximum
+ * length for a string.
+ *
+ * @param minLength
+ * The minimum length of the string or null
in case
+ * no minimum length is defined.
+ * @param maxLength
+ * The maximum length of the string or null
in case
+ * no maximum length is defined.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized by
+ * the lower and upper bound of the range, if defined.
+ * @param format
+ * The number format to use for formatting integers (the length
+ * bounds) in the validation message.
+ */
+ private StringLengthValidator(Integer minLength, Integer maxLength,
+ String validationMessage, NumberFormat format) {
+ this.minLength = minLength;
+ this.maxLength = maxLength;
+ this.validationMessage = validationMessage;
+ this.format = format;
+ }
+
+ /**
+ * Creates a validator which checks that an input string has the given
+ * minimum length.
+ *
+ * @param minLength
+ * The minimum length which the input string must have.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given minimum length.
+ * @param format
+ * The display format to use for formatting integers (the minimum
+ * length) in the validation message.
+ * @return The validator instance.
+ */
+ public static StringLengthValidator min(int minLength,
+ String validationMessage, NumberFormat format) {
+ return new StringLengthValidator(new Integer(minLength), null,
+ defaultIfNull(validationMessage, MIN_LENGTH_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that an input string has the given
+ * maximum length.
+ *
+ * @param maxLength
+ * The maximum length which the input string must have.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given maximum length.
+ * @param format
+ * The display format to use for formatting integers (the maximum
+ * length) in the validation message.
+ * @return The validator instance.
+ */
+ public static StringLengthValidator max(int maxLength,
+ String validationMessage, NumberFormat format) {
+ return new StringLengthValidator(null, new Integer(maxLength),
+ defaultIfNull(validationMessage, MAX_LENGTH_MESSAGE), format);
+ }
+
+ /**
+ * Creates a validator which checks that the length of an input string lies
+ * in the given range.
+ *
+ * @param minLength
+ * The minimum length which the input string must have.
+ * @param maxLength
+ * The maximum length which the input string must have.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given minimum and maximum lengths.
+ * @param format
+ * The display format to use for formatting integers (the
+ * minimum/maximum length) in the validation message.
+ * @return The validator instance.
+ */
+ public static StringLengthValidator range(int minLength, int maxLength,
+ String validationMessage, NumberFormat format) {
+ return new StringLengthValidator(new Integer(minLength), new Integer(
+ maxLength), defaultIfNull(validationMessage,
+ LENGTH_RANGE_MESSAGE), format);
+ }
+
+ public IStatus validate(Object value) {
+ String input = (String) value;
+ if (input != null) {
+ int inputLength = input.length();
+ if ((minLength != null && inputLength < minLength.intValue())
+ || maxLength != null && inputLength > maxLength.intValue()) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, getValidationMessageArguments());
+ }
+ return formattedValidationMessage;
+ }
+
+ private String[] getValidationMessageArguments() {
+ synchronized (format) {
+ if (minLength == null) {
+ return new String[] { format.format(maxLength) };
+ } else if (maxLength == null) {
+ return new String[] { format.format(minLength) };
+ }
+ return new String[] { format.format(minLength),
+ format.format(maxLength) };
+ }
+ }
+
+ private static String defaultIfNull(String string, String defaultString) {
+ return (string != null) ? string : defaultString;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringRegexValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/StringRegexValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/StringRegexValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/StringRegexValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.text.MessageFormat;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+/**
+ * Provides validations for strings which must match a given regular expression.
+ *
+ * @since 1.3
+ */
+public class StringRegexValidator implements IValidator {
+
+ private static final String REGEX_VALIDATION_MESSAGE = BindingMessages
+ .getString(BindingMessages.VALIDATE_STRING_REGEX);
+
+ private final Pattern pattern;
+ private final String validationMessage;
+ private String formattedValidationMessage;
+
+ /**
+ * Creates a new regex validator.
+ *
+ * @param regex
+ * The regular expression which the input string must match.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given regular expression string.
+ */
+ public StringRegexValidator(String regex, String validationMessage) {
+ this(Pattern.compile(regex), validationMessage);
+ }
+
+ /**
+ * Creates a new regex validator.
+ *
+ * @param pattern
+ * The pattern which the input string must match.
+ * @param validationMessage
+ * The validation message pattern to use. Can be parameterized
+ * with the given regular expression string.
+ */
+ public StringRegexValidator(Pattern pattern, String validationMessage) {
+ this.pattern = pattern;
+ this.validationMessage = validationMessage != null ? validationMessage
+ : REGEX_VALIDATION_MESSAGE;
+ }
+
+ public IStatus validate(Object value) {
+ String input = (String) value;
+ if (input != null) {
+ Matcher matcher = pattern.matcher(input);
+ if (!matcher.matches()) {
+ return ValidationStatus.error(getFormattedValidationMessage());
+ }
+ }
+ return ValidationStatus.ok();
+ }
+
+ private synchronized String getFormattedValidationMessage() {
+ if (formattedValidationMessage == null) {
+ formattedValidationMessage = MessageFormat.format(
+ validationMessage, new String[] { pattern.pattern() });
+ }
+ return formattedValidationMessage;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringToBigDecimalValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/StringToBigDecimalValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/StringToBigDecimalValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/StringToBigDecimalValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+/**
+ * Validates that a string is of the appropriate format for a BigDecimal.
+ *
+ * @since 1.3
+ */
+public class StringToBigDecimalValidator extends
+ AbstractStringToNumberValidator {
+
+ /**
+ * @param converter
+ */
+ public StringToBigDecimalValidator(NumberFormatConverter converter) {
+ super(converter, null, null);
+ }
+
+ protected boolean isInRange(Number number) {
+ return true;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringToBigIntegerValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/StringToBigIntegerValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/StringToBigIntegerValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/StringToBigIntegerValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+/**
+ * Validates that a string is of the appropriate format for a BigInteger.
+ *
+ * @since 1.3
+ */
+public class StringToBigIntegerValidator extends
+ AbstractStringToNumberValidator {
+
+ /**
+ * @param converter
+ */
+ public StringToBigIntegerValidator(NumberFormatConverter converter) {
+ super(converter, null, null);
+ }
+
+ protected boolean isInRange(Number number) {
+ return true;
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringToBooleanValidator.java
===================================================================
RCS file: src/org/eclipse/core/internal/databinding/validation/StringToBooleanValidator.java
diff -N src/org/eclipse/core/internal/databinding/validation/StringToBooleanValidator.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/core/internal/databinding/validation/StringToBooleanValidator.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Ovidio Mallo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ovidio Mallo - initial API and implementation (bug 183055)
+ ******************************************************************************/
+
+package org.eclipse.core.internal.databinding.validation;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.eclipse.core.databinding.validation.IValidator;
+import org.eclipse.core.databinding.validation.ValidationStatus;
+import org.eclipse.core.internal.databinding.BindingMessages;
+import org.eclipse.core.runtime.IStatus;
+
+/**
+ * @since 1.3
+ */
+public class StringToBooleanValidator implements IValidator {
+
+ // TODO: Some of this code is duplicated from StringToBooleanPrimitiveConverter!
+ private static final String[] DEFAULT_TRUE_VALUES;
+ private static final String[] DEFAULT_FALSE_VALUES;
+
+ static {
+ String delimiter = BindingMessages.getString(BindingMessages.VALUE_DELIMITER);
+ String values = BindingMessages.getString(BindingMessages.TRUE_STRING_VALUES);
+ DEFAULT_TRUE_VALUES = valuesToSortedArray(delimiter, values);
+
+ values = BindingMessages.getString(BindingMessages.FALSE_STRING_VALUES);
+ DEFAULT_FALSE_VALUES = valuesToSortedArray(delimiter, values);
+ }
+
+ private String[] trueValues = DEFAULT_TRUE_VALUES;
+ private String[] falseValues = DEFAULT_FALSE_VALUES;
+
+ private String parseErrorMessage = BindingMessages.getString(BindingMessages.VALIDATE_INVALID_BOOLEAN_STRING);
+
+ /**
+ * Returns a sorted array with all values converted to upper case.
+ *
+ * @param delimiter
+ * @param values
+ * @return sorted array of values
+ */
+ private static String[] valuesToSortedArray(String delimiter, String values) {
+ List list = new LinkedList();
+ StringTokenizer tokenizer = new StringTokenizer(values, delimiter);
+ while (tokenizer.hasMoreTokens()) {
+ list.add(tokenizer.nextToken().toUpperCase());
+ }
+
+ String[] array = (String[]) list.toArray(new String[list.size()]);
+ Arrays.sort(array);
+
+ return array;
+ }
+
+ /**
+ * Sets the validation message to be used in case the source string does not
+ * represent a valid boolean value.
+ *
+ * @param message
+ * The validation message to be used in case the source string
+ * does not represent a valid boolean value.
+ */
+ public final void setParseErrorMessage(String message) {
+ this.parseErrorMessage = message;
+ }
+
+ /**
+ * Sets the string values which are considered to represent a
+ * true
and false
value, respectively.
+ *
+ * + * Note that the capitalization of the provided strings is ignored. + *
+ * + * @param trueValues + * The set of strings representing atrue
value.
+ * @param falseValues
+ * The set of strings representing a false
value.
+ */
+ public final void setSourceStrings(String[] trueValues, String[] falseValues) {
+ this.trueValues = new String[trueValues.length];
+ for (int i = 0; i < trueValues.length; i++) {
+ this.trueValues[i] = trueValues[i].toUpperCase();
+ }
+ Arrays.sort(this.trueValues); // for binary search
+
+ this.falseValues = new String[falseValues.length];
+ for (int i = 0; i < falseValues.length; i++) {
+ this.falseValues[i] = falseValues[i].toUpperCase();
+ }
+ Arrays.sort(this.falseValues); // for binary search
+ }
+
+ public IStatus validate(Object value) {
+ String source = (String) value;
+ if (source != null && source.length() != 0) {
+ source = source.toUpperCase();
+ if (Arrays.binarySearch(trueValues, source) < 0
+ && Arrays.binarySearch(falseValues, source) < 0) {
+ return ValidationStatus.error(parseErrorMessage);
+ }
+ }
+ return ValidationStatus.ok();
+ }
+}
Index: src/org/eclipse/core/internal/databinding/validation/StringToCharacterValidator.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/StringToCharacterValidator.java,v
retrieving revision 1.3
diff -u -r1.3 StringToCharacterValidator.java
--- src/org/eclipse/core/internal/databinding/validation/StringToCharacterValidator.java 25 May 2009 20:52:19 -0000 1.3
+++ src/org/eclipse/core/internal/databinding/validation/StringToCharacterValidator.java 19 Sep 2010 15:12:46 -0000
@@ -8,6 +8,7 @@
* Contributors:
* Matt Carter - initial API and implementation
* Tom Schindl