Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 147262 Details for
Bug 183055
[DataBinding] New Editing and Constraints API
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
ongoing Editing/Constraints API patch
patch_editing.txt (text/plain), 209.44 KB, created by
Ovidio Mallo
on 2009-09-15 18:18:13 EDT
(
hide
)
Description:
ongoing Editing/Constraints API patch
Filename:
MIME Type:
Creator:
Ovidio Mallo
Created:
2009-09-15 18:18:13 EDT
Size:
209.44 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.core.databinding >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.6 >diff -u -r1.6 StringToNumberParser.java >--- src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java 28 Oct 2008 19:54:39 -0000 1.6 >+++ src/org/eclipse/core/internal/databinding/conversion/StringToNumberParser.java 15 Sep 2009 22:13:54 -0000 >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Ovidio Mallo - bug 183055 > *******************************************************************************/ > > package org.eclipse.core.internal.databinding.conversion; >@@ -115,11 +116,11 @@ > .getErrorIndex() : position.getIndex(); > > if (errorIndex < value.length()) { >- return BindingMessages.formatString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR, >+ return BindingMessages.getFormattedString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR, > new Object[] { value, new Integer(errorIndex + 1), > new Character(value.charAt(errorIndex)) }); > } >- return BindingMessages.formatString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR_NO_CHARACTER, >+ return BindingMessages.getFormattedString(BindingMessages.VALIDATE_NUMBER_PARSE_ERROR_NO_CHARACTER, > new Object[] { value, new Integer(errorIndex + 1) }); > } > >@@ -133,6 +134,22 @@ > */ > 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; > >@@ -141,8 +158,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/StringToBooleanPrimitiveConverter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java,v >retrieving revision 1.4 >diff -u -r1.4 StringToBooleanPrimitiveConverter.java >--- src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java 25 May 2009 20:52:20 -0000 1.4 >+++ src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java 15 Sep 2009 22:13:54 -0000 >@@ -9,6 +9,7 @@ > * Contributors: > * db4objects - Initial API and implementation > * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 >+ * Ovidio Mallo - bug 183055 > */ > package org.eclipse.core.internal.databinding.conversion; > >@@ -24,19 +25,22 @@ > * StringToBooleanPrimitiveConverter. > */ > public class StringToBooleanPrimitiveConverter implements IConverter { >- private static final String[] trueValues; > >- private static final String[] falseValues; >+ 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); >- trueValues = valuesToSortedArray(delimiter, values); >+ DEFAULT_TRUE_VALUES = valuesToSortedArray(delimiter, values); > > values = BindingMessages.getString(BindingMessages.FALSE_STRING_VALUES); >- falseValues = valuesToSortedArray(delimiter, values); >+ DEFAULT_FALSE_VALUES = valuesToSortedArray(delimiter, values); > } > >+ private String[] trueValues = DEFAULT_TRUE_VALUES; >+ private String[] falseValues = DEFAULT_FALSE_VALUES; >+ > /** > * Returns a sorted array with all values converted to upper case. > * >@@ -57,6 +61,33 @@ > return array; > } > >+ /** >+ * Sets the string values which are considered to represent a >+ * <code>true</code> and <code>false</code> value, respectively. >+ * >+ * <p> >+ * Note that the capitalization of the provided strings is ignored. >+ * </p> >+ * >+ * @param trueValues >+ * The set of strings representing a <code>true</code> value. >+ * @param falseValues >+ * The set of strings representing a <code>false</code> 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/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 15 Sep 2009 22:13:54 -0000 >@@ -10,6 +10,7 @@ > * db4objects - Initial API and implementation > * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 > * Matthew Hall - bug 121110 >+ * Ovidio Mallo - bug 183055 > ******************************************************************************/ > package org.eclipse.core.internal.databinding.conversion; > >@@ -52,6 +53,20 @@ > }; > > /** >+ * Sets the {@link DateFormat}s to be used for parsing/formatting dates. Any >+ * of the supplied formats will be used for {@link #parse(String) parsing} a >+ * date from a string while the first format in the array will be used for >+ * {@link #format(Date) formatting} a date as a string. >+ * >+ * @param formatters >+ * The {@link DateFormat}s to be used for parsing/formatting >+ * dates. >+ */ >+ public final void setFormatters(DateFormat[] formatters) { >+ this.formatters = formatters; >+ } >+ >+ /** > * Tries all available formatters to parse the given string according to the > * default locale or as a raw millisecond value and returns the result of the > * first successful run. >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 15 Sep 2009 22:13:54 -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/internal/databinding/validation/StringToDateValidator.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.java,v >retrieving revision 1.5 >diff -u -r1.5 StringToDateValidator.java >--- src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.java 9 May 2008 14:13:00 -0000 1.5 >+++ src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.java 15 Sep 2009 22:13:54 -0000 >@@ -8,6 +8,7 @@ > * Contributors: > * IBM Corporation - initial API and implementation > * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 >+ * Ovidio Mallo - bug 183055 > *******************************************************************************/ > > package org.eclipse.core.internal.databinding.validation; >@@ -28,6 +29,8 @@ > public class StringToDateValidator implements IValidator { > private final StringToDateConverter converter; > >+ private String parseErrorMessage = null; >+ > /** > * @param converter > */ >@@ -35,6 +38,18 @@ > this.converter = converter; > } > >+ /** >+ * Sets the validation message to be used in case the source string does not >+ * represent a valid date. >+ * >+ * @param message >+ * The validation message to be used in case the source string >+ * does not represent a valid date. >+ */ >+ public final void setParseErrorMessage(String message) { >+ this.parseErrorMessage = message; >+ } >+ > /* > * (non-Javadoc) > * >@@ -47,18 +62,17 @@ > Object convertedValue = converter.convert(value); > //The StringToDateConverter returns null if it can't parse the date. > if (convertedValue == null) { >- return ValidationStatus.error(getErrorMessage()); >+ return ValidationStatus.error(getParseErrorMessage()); > } > > return Status.OK_STATUS; > } > >- /* >- * (non-Javadoc) >- * >- * @see org.eclipse.core.internal.databinding.validation.WrappedConverterValidator#getErrorMessage() >- */ >- protected String getErrorMessage() { >+ private String getParseErrorMessage() { >+ if (parseErrorMessage != null) { >+ return parseErrorMessage; >+ } >+ > Date sampleDate = new Date(); > > // FIXME We need to use the information from the >@@ -73,7 +87,9 @@ > samples.append('\''); > samples.append(util.format(sampleDate, 0)); > samples.append('\''); >- return BindingMessages.getString(BindingMessages.EXAMPLES) + ": " + samples + ",..."; //$NON-NLS-1$//$NON-NLS-2$ >+ parseErrorMessage = BindingMessages.getString(BindingMessages.EXAMPLES) + ": " + samples + ",..."; //$NON-NLS-1$//$NON-NLS-2$ >+ >+ return parseErrorMessage; > } > > private static class FormatUtil extends DateConversionSupport { >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 15 Sep 2009 22:13:54 -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,32 @@ > } > > /** >+ * Sets the validation message to be used in case the source string does not >+ * represent a valid number. >+ * >+ * @param message >+ * The validation message 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 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 to be used in case the parsed number >+ * lies outside the supported value range. >+ */ >+ public final void setOutOfRangeMessage(String message) { >+ this.outOfRangeMessage = message; >+ this.formattedOutOfRangeMessage = null; >+ } >+ >+ /** > * Validates the provided <code>value</code>. An error status is returned if: > * <ul> > * <li>The value cannot be parsed.</li> >@@ -69,16 +102,10 @@ > > if (result.getNumber() != null) { > if (!isInRange(result.getNumber())) { >- if (outOfRangeMessage == null) { >- outOfRangeMessage = StringToNumberParser >- .createOutOfRangeMessage(min, max, converter >- .getNumberFormat()); >- } >- >- return ValidationStatus.error(outOfRangeMessage); >+ return ValidationStatus.error(getFormattedOutOfRangeMessage()); > } > } else if (result.getPosition() != null) { >- String parseErrorMessage = StringToNumberParser.createParseErrorMessage( >+ String parseErrorMessage = createParseErrorMessage( > (String) value, result.getPosition()); > > return ValidationStatus.error(parseErrorMessage); >@@ -94,4 +121,22 @@ > * @return <code>true</code> 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: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/META-INF/MANIFEST.MF,v >retrieving revision 1.21 >diff -u -r1.21 MANIFEST.MF >--- META-INF/MANIFEST.MF 25 Aug 2009 04:57:27 -0000 1.21 >+++ META-INF/MANIFEST.MF 15 Sep 2009 22:13:53 -0000 >@@ -8,7 +8,9 @@ > 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/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 15 Sep 2009 22:13:54 -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,34 @@ > Validate_NoChangeAllowedHelp=Changes are not allowed in this field > Validate_CharacterHelp=Please type a character > >+#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. >+ > Examples=Examples >\ No newline at end of file >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 15 Sep 2009 22:13:54 -0000 >@@ -8,6 +8,7 @@ > * Contributors: > * IBM Corporation - initial API and implementation > * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 >+ * Ovidio Mallo - bug 183055 > *******************************************************************************/ > package org.eclipse.core.internal.databinding; > >@@ -69,6 +70,11 @@ > public static final String FALSE_STRING_VALUES = "FalseStringValues"; //$NON-NLS-1$ > > /** >+ * Key to be used for a "Validate_InvalidBooleanString" message >+ */ >+ public static final String VALIDATE_INVALID_BOOLEAN_STRING = "Validate_InvalidBooleanString"; //$NON-NLS-1$ >+ >+ /** > * Key to be used for a "Validate_NumberOutOfRangeError" message > */ > public static final String VALIDATE_NUMBER_OUT_OF_RANGE_ERROR = "Validate_NumberOutOfRangeError"; //$NON-NLS-1$ >@@ -109,10 +115,100 @@ > public static final String VALIDATE_NUMBER_PARSE_ERROR_NO_CHARACTER = "Validate_NumberParseErrorNoCharacter"; //$NON-NLS-1$ > > /** >+ * Key to be used for a "Validate_NonNull" message >+ */ >+ public static final String VALIDATE_NON_NULL = "Validate_NonNull"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NonEmptyString" message >+ */ >+ public static final String VALIDATE_NON_EMPTY_STRING = "Validate_NonEmptyString"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NonStringRegex" message >+ */ >+ public static final String VALIDATE_STRING_REGEX = "Validate_NonStringRegex"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeGreater" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_GREATER = "Validate_NumberRangeGreater"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeGreaterEqual" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_GREATER_EQUAL = "Validate_NumberRangeGreaterEqual"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeLess" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_LESS = "Validate_NumberRangeLess"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeLessEqual" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_LESS_EQUAL = "Validate_NumberRangeLessEqual"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeWithinRange" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_WITHIN_RANGE = "Validate_NumberRangeWithinRange"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangePositive" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_POSITIVE = "Validate_NumberRangePositive"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_NumberRangeNonNegative" message >+ */ >+ public static final String VALIDATE_NUMBER_RANGE_NON_NEGATIVE = "Validate_NumberRangeNonNegative"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_DateRangeAfter" message >+ */ >+ public static final String VALIDATE_DATE_RANGE_AFTER = "Validate_DateRangeAfter"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_DateRangeAfterEqual" message >+ */ >+ public static final String VALIDATE_DATE_RANGE_AFTER_EQUAL = "Validate_DateRangeAfterEqual"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_DateRangeBefore" message >+ */ >+ public static final String VALIDATE_DATE_RANGE_BEFORE = "Validate_DateRangeBefore"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_DateRangeBeforeEqual" message >+ */ >+ public static final String VALIDATE_DATE_RANGE_BEFORE_EQUAL = "Validate_DateRangeBeforeEqual"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_DateRangeWithinRange" message >+ */ >+ public static final String VALIDATE_DATE_RANGE_WITHIN_RANGE = "Validate_DateRangeWithinRange"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_StringLengthMin" message >+ */ >+ public static final String VALIDATE_STRING_LENGTH_MIN = "Validate_StringLengthMin"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_StringLengthMax" message >+ */ >+ public static final String VALIDATE_STRING_LENGTH_MAX = "Validate_StringLengthMax"; //$NON-NLS-1$ >+ >+ /** >+ * Key to be used for a "Validate_StringLengthRange" message >+ */ >+ public static final String VALIDATE_STRING_LENGTH_RANGE = "Validate_StringLengthRange"; //$NON-NLS-1$ >+ >+ /** > * Returns the resource object with the given key in the resource bundle for > * JFace Data Binding. If there isn't any value under the given key, the key > * is returned. >- * >+ * > * @param key > * the resource name > * @return the string >@@ -128,16 +224,27 @@ > /** > * Returns a formatted string with the given key in the resource bundle for > * JFace Data Binding. >- * >+ * > * @param key > * @param arguments > * @return formatted string, the key if the key is invalid > */ >- public static String formatString(String key, Object[] arguments) { >+ public static String getFormattedString(String key, Object[] arguments) { > try { >- return MessageFormat.format(bundle.getString(key), arguments); >+ return formatMessage(getString(key), arguments); > } catch (MissingResourceException e) { > return key; > } > } >+ >+ /** >+ * Formats the given message pattern with the provided arguments. >+ * >+ * @param pattern >+ * @param arguments >+ * @return formatted string >+ */ >+ public static String formatMessage(String pattern, Object[] arguments) { >+ return MessageFormat.format(pattern, arguments); >+ } > } >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,140 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public class IntegerRangeValidator extends NumberRangeValidator { >+ >+ private static final Integer ZERO = new Integer(0); >+ private static final Integer ONE = new Integer(1); >+ >+ /** >+ * @param min >+ * @param max >+ * @param minConstraint >+ * @param maxConstraint >+ * @param validationMessage >+ * @param format >+ */ >+ private IntegerRangeValidator(Number min, Number max, int minConstraint, >+ int maxConstraint, String validationMessage, NumberFormat format) { >+ super(min, max, minConstraint, maxConstraint, validationMessage, format); >+ } >+ >+ /** >+ * >+ * @param min >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator greater(int min, >+ String validationMessage, NumberFormat format) { >+ return new IntegerRangeValidator(new Integer(min), null, GREATER, >+ UNDEFINED, defaultIfNull(validationMessage, GREATER_MESSAGE), >+ format); >+ } >+ >+ /** >+ * >+ * @param min >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator greaterEqual(int min, >+ String validationMessage, NumberFormat format) { >+ return new IntegerRangeValidator(new Integer(min), null, GREATER_EQUAL, >+ UNDEFINED, defaultIfNull(validationMessage, >+ GREATER_EQUAL_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator less(int max, String validationMessage, >+ NumberFormat format) { >+ return new IntegerRangeValidator(null, new Integer(max), UNDEFINED, >+ LESS, defaultIfNull(validationMessage, LESS_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator lessEqual(int max, >+ String validationMessage, NumberFormat format) { >+ return new IntegerRangeValidator(null, new Integer(max), UNDEFINED, >+ LESS_EQUAL, >+ defaultIfNull(validationMessage, LESS_EQUAL_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param min >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator range(int min, int max, >+ String validationMessage, NumberFormat format) { >+ return new IntegerRangeValidator(new Integer(min), new Integer(max), >+ GREATER_EQUAL, LESS_EQUAL, defaultIfNull(validationMessage, >+ WITHIN_RANGE_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static IntegerRangeValidator positive(String validationMessage, >+ NumberFormat format) { >+ return new IntegerRangeValidator(ONE, null, GREATER_EQUAL, UNDEFINED, >+ defaultIfNull(validationMessage, POSITIVE_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ 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) { >+ Integer integer1 = (Integer) number1; >+ Integer integer2 = (Integer) number2; >+ return integer1.compareTo(integer2); >+ } >+ >+ private static String defaultIfNull(String string, String defaultString) { >+ return (string != null) ? string : defaultString; >+ } >+} >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,232 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class StringEditing extends Editing { >+ >+ private 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. >+ * >+ * @param stripToNull >+ * Whether to convert the input string to <code>null</code> in >+ * case stripping the input string results in an empty string. >+ * @return The new editing object which strips whitespace from both ends of >+ * the input string. >+ * >+ * @see Character#isWhitespace(char) >+ */ >+ public static StringEditing stripped(boolean stripToNull) { >+ return new StringEditing(new StringStripConverter(stripToNull)); >+ } >+ >+ /** >+ * Creates a new editing object which {@link String#trim()}s the input >+ * string. >+ * >+ * @param trimToNull >+ * Whether to convert the input string to <code>null</code> in >+ * case trimming the input string results in an empty string. >+ * @return The new editing object which trims whitespace from both ends of >+ * the input string. >+ * >+ * @see String#trim() >+ */ >+ public static StringEditing trimmed(boolean trimToNull) { >+ return new StringEditing(new StringTrimConverter(trimToNull)); >+ } >+ >+ /** >+ * Returns the target constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link StringConstraints >+ * string target constraints} of this editing object and is equivalent to >+ * {@code (StringConstraints) targetConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #targetConstraints() >+ * @see StringConstraints >+ */ >+ public StringConstraints targetStringConstraints() { >+ return (StringConstraints) targetConstraints(); >+ } >+ >+ /** >+ * Returns the model constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link StringConstraints >+ * string model constraints} of this editing object and is equivalent to >+ * {@code (StringConstraints) modelConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #modelConstraints() >+ * @see StringConstraints >+ */ >+ public StringConstraints modelStringConstraints() { >+ return (StringConstraints) modelConstraints(); >+ } >+ >+ /** >+ * Returns the before-set model constraints to apply. >+ * >+ * <p> >+ * 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()}. >+ * </p> >+ * >+ * @return The target 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(); >+ } >+ >+ /** >+ * Convenience method which adds a {@link StringConstraints#required() >+ * required constraint} to the set of {@link #modelStringConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#required() >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing required() { >+ modelStringConstraints().required(); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link StringConstraints#nonEmpty() >+ * non-empty constraint} to the set of {@link #modelStringConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#nonEmpty() >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing nonEmpty() { >+ modelStringConstraints().nonEmpty(); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link StringConstraints#minLength(int) >+ * min-length constraint} to the set of {@link #modelStringConstraints()}. >+ * >+ * @param minLength >+ * The min length of the min-length constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#minLength(int) >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing minLength(int minLength) { >+ modelStringConstraints().minLength(minLength); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link StringConstraints#maxLength(int) >+ * max-length constraint} to the set of {@link #modelStringConstraints()}. >+ * >+ * @param maxLength >+ * The max length of the max-length constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#maxLength(int) >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing maxLength(int maxLength) { >+ modelStringConstraints().maxLength(maxLength); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a >+ * {@link StringConstraints#lengthRange(int, int) length-range constraint} >+ * to the set of {@link #modelStringConstraints()}. >+ * >+ * @param minLength >+ * The min length of the length-range constraint. >+ * @param maxLength >+ * The max length of the length-range constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#lengthRange(int, int) >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing lengthRange(int minLength, int maxLength) { >+ modelStringConstraints().lengthRange(minLength, maxLength); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link StringConstraints#matches(String) >+ * matches constraint} to the set of {@link #modelStringConstraints()}. >+ * >+ * @param regex >+ * The regular expression of the matches constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see StringConstraints#matches(String) >+ * @see #modelStringConstraints() >+ */ >+ public StringEditing matches(String regex) { >+ modelStringConstraints().matches(regex); >+ return this; >+ } >+} >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,120 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public abstract class NumberRangeValidator implements IValidator { >+ >+ 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; >+ >+ 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; >+ >+ /** >+ * >+ * @param min >+ * @param max >+ * @param minConstraint >+ * @param maxConstraint >+ * @param validationMessage >+ * @param format >+ */ >+ 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$ >+ } >+ } >+ >+ protected abstract int compare(Number number1, Number number2); >+ >+ private 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/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,185 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public class DateRangeValidator implements IValidator { >+ >+ 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; >+ >+ 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; >+ >+ /** >+ * >+ * @param min >+ * @param max >+ * @param minConstraint >+ * @param maxConstraint >+ * @param validationMessage >+ * @param format >+ */ >+ 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; >+ } >+ >+ /** >+ * >+ * @param min >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static DateRangeValidator after(Date min, String validationMessage, >+ DateFormat format) { >+ return new DateRangeValidator(min, null, AFTER, UNDEFINED, >+ defaultIfNull(validationMessage, AFTER_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param min >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static DateRangeValidator afterEqual(Date min, >+ String validationMessage, DateFormat format) { >+ return new DateRangeValidator(min, null, AFTER_EQUAL, UNDEFINED, >+ defaultIfNull(validationMessage, AFTER_EQUAL_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static DateRangeValidator before(Date max, String validationMessage, >+ DateFormat format) { >+ return new DateRangeValidator(null, max, UNDEFINED, BEFORE, >+ defaultIfNull(validationMessage, BEFORE_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static DateRangeValidator beforeEqual(Date max, >+ String validationMessage, DateFormat format) { >+ return new DateRangeValidator(null, max, UNDEFINED, BEFORE_EQUAL, >+ defaultIfNull(validationMessage, BEFORE_EQUAL_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param min >+ * @param max >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ 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 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/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,129 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public class StringLengthValidator implements IValidator { >+ >+ 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; >+ >+ /** >+ * >+ * @param minLength >+ * @param maxLength >+ * @param validationMessage >+ * @param format >+ */ >+ private StringLengthValidator(Integer minLength, Integer maxLength, >+ String validationMessage, NumberFormat format) { >+ this.minLength = minLength; >+ this.maxLength = maxLength; >+ this.validationMessage = validationMessage; >+ this.format = format; >+ } >+ >+ /** >+ * >+ * @param minLength >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static StringLengthValidator minLength(int minLength, >+ String validationMessage, NumberFormat format) { >+ return new StringLengthValidator(new Integer(minLength), null, >+ defaultIfNull(validationMessage, MIN_LENGTH_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param maxLength >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static StringLengthValidator maxLength(int maxLength, >+ String validationMessage, NumberFormat format) { >+ return new StringLengthValidator(null, new Integer(maxLength), >+ defaultIfNull(validationMessage, MAX_LENGTH_MESSAGE), format); >+ } >+ >+ /** >+ * >+ * @param minLength >+ * @param maxLength >+ * @param validationMessage >+ * @param format >+ * @return . >+ */ >+ public static StringLengthValidator lengthRange(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 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/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,214 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class DateConstraints extends Constraints { >+ >+ private DateFormat displayFormat = DateFormat.getDateInstance(); >+ >+ private String requiredMessage = null; >+ >+ private String afterMessage = null; >+ >+ private String afterEqualMessage = null; >+ >+ private String beforeMessage = null; >+ >+ private String beforeEqualMessage = null; >+ >+ /** >+ * Sets the format to use when formatting a date to be included in any of >+ * the validation messages. >+ * >+ * <p> >+ * This date format will be used for any subsequent constraints which are >+ * applied. >+ * </p> >+ * >+ * @param format >+ * The format to use for displaying dates in validation messages. >+ * @return This constraints instance for method chaining. >+ */ >+ public DateConstraints displayFormat(DateFormat format) { >+ this.displayFormat = format; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed date is not <code>null</code>. >+ * 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 to be issued in case the parsed date is >+ * <code>null</code>. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed date is >+ * <code>null</code>. >+ * @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 parsed date is after the given date. >+ * Uses the current {@link #afterMessage(String) validation message}. >+ * >+ * @param date >+ * The date which must be before or on the parsed date. >+ * @return This constraints instance for method chaining. >+ * >+ * @see Date#after(Date) >+ */ >+ public DateConstraints after(Date date) { >+ addValidator(DateRangeValidator >+ .after(date, afterMessage, displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed date is not >+ * after a given date. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed date is >+ * not after a given date. >+ * @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 parsed date is after or on the given >+ * date. Uses the current {@link #afterEqualMessage(String) validation >+ * message}. >+ * >+ * @param date >+ * The date which must be before the parsed date. >+ * @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, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed date is not >+ * after or on a given date. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed date is >+ * not after or on a given date. >+ * @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 parsed date is before the given date. >+ * Uses the current {@link #beforeMessage(String) validation message}. >+ * >+ * @param date >+ * The date which must be after or on the parsed date. >+ * @return This constraints instance for method chaining. >+ * >+ * @see Date#before(Date) >+ */ >+ public DateConstraints before(Date date) { >+ addValidator(DateRangeValidator.before(date, beforeMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed date is not >+ * before a given date. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed date is >+ * not before a given date. >+ * @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 parsed date is before or on the given >+ * date. Uses the current {@link #beforeEqualMessage(String) validation >+ * message}. >+ * >+ * @param date >+ * The date which must be after the parsed date. >+ * @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, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed date is not >+ * before or on a given date. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed date is >+ * not before or on a given date. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #beforeEqual(Date) >+ */ >+ public DateConstraints beforeEqualMessage(String message) { >+ this.beforeEqualMessage = message; >+ return this; >+ } >+} >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.4 >+ */ >+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/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,203 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class BooleanEditing extends Editing { >+ >+ private 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 new BooleanEditing(null, null, 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 an 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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @param trueValues >+ * The set of strings representing a <code>true</code> value. >+ * @param falseValues >+ * The set of strings representing a <code>false</code> 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 new BooleanEditing(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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @param trueValues >+ * The set of strings representing a <code>true</code> value. >+ * @param falseValues >+ * The set of strings representing a <code>false</code> value. >+ * @param parseErrorMessage >+ * The validation message issued in case the input string cannot >+ * be parsed to an 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. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link StringConstraints >+ * string target constraints} of this editing object and is equivalent to >+ * {@code (StringConstraints) targetConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #targetConstraints() >+ * @see StringConstraints >+ */ >+ public StringConstraints targetStringConstraints() { >+ return (StringConstraints) targetConstraints(); >+ } >+ >+ /** >+ * Returns the model constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link BooleanConstraints >+ * bool model constraints} of this editing object and is equivalent to >+ * {@code (BooleanConstraints) modelConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #modelConstraints() >+ * @see BooleanConstraints >+ */ >+ public BooleanConstraints modelBooleanConstraints() { >+ return (BooleanConstraints) modelConstraints(); >+ } >+ >+ /** >+ * Returns the before-set model constraints to apply. >+ * >+ * <p> >+ * 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()}. >+ * </p> >+ * >+ * @return The target 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(); >+ } >+ >+ /** >+ * Convenience method which adds a {@link BooleanConstraints#required() >+ * required constraint} to the set of {@link #modelBooleanConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see BooleanConstraints#required() >+ * @see #modelBooleanConstraints() >+ */ >+ public BooleanEditing required() { >+ modelBooleanConstraints().required(); >+ return this; >+ } >+} >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,49 @@ >+/******************************************************************************* >+ * 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.4 >+ */ >+public class NonNullValidator implements IValidator { >+ >+ private static final String NON_NULL_VALIDATION_MESSAGE = BindingMessages.getString(BindingMessages.VALIDATE_NON_NULL); >+ >+ private final String validationMessage; >+ >+ /** >+ * >+ */ >+ public NonNullValidator() { >+ this(null); >+ } >+ >+ /** >+ * @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/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,49 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class BooleanConstraints extends Constraints { >+ >+ private String requiredMessage = null; >+ >+ /** >+ * Adds a validator ensuring that the boolean value is not <code>null</code> >+ * . 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 to be issued in case the boolean value is >+ * <code>null</code>. >+ * >+ * @param message >+ * The validation message to be issued in case the boolean value >+ * is <code>null</code>. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #required() >+ */ >+ public BooleanConstraints requiredMessage(String message) { >+ this.requiredMessage = message; >+ return this; >+ } >+} >Index: src/org/eclipse/core/internal/databinding/conversion/BooleanToStringConverter.java >=================================================================== >RCS file: src/org/eclipse/core/internal/databinding/conversion/BooleanToStringConverter.java >diff -N src/org/eclipse/core/internal/databinding/conversion/BooleanToStringConverter.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/internal/databinding/conversion/BooleanToStringConverter.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,60 @@ >+/******************************************************************************* >+ * 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.4 >+ */ >+public class BooleanToStringConverter extends Converter { >+ >+ // Those are the defaults used up to now in UpdateStrategy where a simple >+ // ObjectToStringConverter was used for converting booleans to strings. >+ // FIXME: Should/may we change this to convert to a localized string? >+ private String trueValue = Boolean.TRUE.toString(); >+ private String falseValue = Boolean.FALSE.toString(); >+ >+ /** >+ * Creates a new converter which converts a boolean to its default string >+ * representation. >+ * >+ * @param booleanType >+ * The boolean type. Must be one of {@code Boolean.TYPE} or >+ * {@code Boolean.class}. >+ */ >+ public BooleanToStringConverter(Class booleanType) { >+ super(booleanType, String.class); >+ } >+ >+ /** >+ * Sets the string values to which a <code>true</code> and >+ * <code>false</code> value should be converted. >+ * >+ * @param trueValue >+ * The string to which to convert a <code>true</code> value. >+ * @param falseValue >+ * The string to which to convert a <code>false</code> 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/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.4 >+ */ >+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 >+ * <code>true</code> and <code>false</code> value, respectively. >+ * >+ * <p> >+ * Note that the capitalization of the provided strings is ignored. >+ * </p> >+ * >+ * @param trueValues >+ * The set of strings representing a <code>true</code> value. >+ * @param falseValues >+ * The set of strings representing a <code>false</code> 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/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,180 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @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; >+ >+ // 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} or >+ * {@link #AGGREGATION_MAX_SEVERITY}. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #AGGREGATION_MERGED >+ * @see #AGGREGATION_MAX_SEVERITY >+ */ >+ public final Constraints aggregationPolicy(int policy) { >+ this.aggregationPolicy = policy; >+ cachedValidator = null; >+ return this; >+ } >+ >+ /** >+ * Creates a validator which enforces the current set of constraints. >+ * >+ * <p> >+ * Note that this method will return <code>null</code> in case the set of >+ * constraints to apply is empty. >+ * </p> >+ * >+ * @return A validator which enforces the current set of constraints. May be >+ * <code>null</code>. >+ */ >+ 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) { >+ return (aggregationPolicy == AGGREGATION_MERGED) ? validateMerged(value) >+ : validateMaxSeverity(value); >+ } >+ >+ 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) { >+ int maxSeverity = IStatus.OK; >+ 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() > maxSeverity) { >+ maxSeverity = status.getSeverity(); >+ maxStatus = status; >+ } >+ } >+ return maxStatus; >+ } >+ } >+} >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,244 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class StringConstraints extends Constraints { >+ >+ private NumberFormat integerDisplayFormat = 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; >+ >+ /** >+ * Sets the format to use when formatting an integer to be included in any >+ * of the validation messages. >+ * >+ * <p> >+ * This integer format will be used for any subsequent constraints which are >+ * applied. >+ * </p> >+ * >+ * @param format >+ * The format to use for displaying integers in validation >+ * messages. >+ * @return This constraints instance for method chaining. >+ */ >+ public StringConstraints integerDisplayFormat(NumberFormat format) { >+ this.integerDisplayFormat = format; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is not >+ * <code>null</code>. 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 to be issued in case the input string is >+ * <code>null</code>. >+ * >+ * @param message >+ * The validation message to be issued in case the input string >+ * is <code>null</code>. >+ * @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 parsed 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 to be issued in case the input string is >+ * empty. >+ * >+ * @param message >+ * The validation message to be issued in case the input string >+ * is empty. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #nonEmpty() >+ */ >+ public StringConstraints nonEmptyMessage(String message) { >+ this.nonEmptyMessage = message; >+ 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}. >+ * >+ * @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.minLength(minLength, >+ minLengthMessage, integerDisplayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the input string does >+ * not have at least a given number of characters. >+ * >+ * @param message >+ * The validation message to be issued in case the input string >+ * does not have at least a given number of characters. >+ * @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}. >+ * >+ * @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.maxLength(maxLength, >+ maxLengthMessage, integerDisplayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the input string has >+ * more than a given number of characters. >+ * >+ * @param message >+ * The validation message to be issued in case the input string >+ * has more than a given number of characters. >+ * @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}. >+ * >+ * @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.lengthRange(minLength, maxLength, >+ lengthRangeMessage, integerDisplayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the length of the input >+ * is not between the given bounds. >+ * >+ * @param message >+ * The validation message to be issued in case the length of the >+ * input is not between the given bounds. >+ * @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; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the input string does >+ * not match a given regular expression. >+ * >+ * @param message >+ * The validation message to be issued in case the input string >+ * does not match a given regular expression. >+ * @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/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,1010 @@ >+/******************************************************************************* >+ * 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 be <code>null</code> >+ * . >+ * @param m2tConverter >+ * The model-to-target converter to set. May be <code>null</code> >+ * . >+ * @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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #targetConstraints() >+ */ >+ protected Constraints createTargetConstraints() { >+ return new Constraints(); >+ } >+ >+ /** >+ * Creates the model constraints to apply. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @return The model constraints to apply. >+ * >+ * @see #modelConstraints() >+ */ >+ protected Constraints createModelConstraints() { >+ return new Constraints(); >+ } >+ >+ /** >+ * Creates the before-set model constraints to apply. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @return The before-set model constraints to apply. >+ * >+ * @see #beforeSetModelConstraints() >+ */ >+ protected Constraints createBeforeSetModelConstraints() { >+ return new Constraints(); >+ } >+ >+ /** >+ * Convenience method which {@link Constraints#addValidator(IValidator) >+ * adds} the given validator to the set of {@link #targetConstraints()}. >+ * >+ * @param validator >+ * The validator to add to the target constraints. >+ * @return This editing instance for method chaining. >+ * >+ * @see Constraints#addValidator(IValidator) >+ * @see #targetConstraints() >+ */ >+ public final Editing addTargetValidator(IValidator validator) { >+ targetConstraints().addValidator(validator); >+ return this; >+ } >+ >+ /** >+ * Convenience method which {@link Constraints#addValidator(IValidator) >+ * adds} the given validator to the set of {@link #modelConstraints()}. >+ * >+ * @param validator >+ * The validator to add to the model constraints. >+ * @return This editing instance for method chaining. >+ * >+ * @see Constraints#addValidator(IValidator) >+ * @see #modelConstraints() >+ */ >+ public final Editing addModelValidator(IValidator validator) { >+ modelConstraints().addValidator(validator); >+ return this; >+ } >+ >+ /** >+ * Convenience method which {@link Constraints#addValidator(IValidator) >+ * adds} the given validator to the set of >+ * {@link #beforeSetModelConstraints()}. >+ * >+ * @param validator >+ * The validator to add to the before-set model constraints. >+ * @return This editing instance for method chaining. >+ * >+ * @see Constraints#addValidator(IValidator) >+ * @see #beforeSetModelConstraints >+ */ >+ public final Editing addBeforeSetModelValidator(IValidator validator) { >+ beforeSetModelConstraints().addValidator(validator); >+ return this; >+ } >+ >+ /** >+ * 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 #applyToT2MValueStrategy(UpdateValueStrategy) >+ */ >+ public final UpdateValueStrategy createT2MValueStrategy() { >+ return applyToT2MValueStrategy(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 #applyToT2MValueStrategy(UpdateValueStrategy) >+ */ >+ public final UpdateValueStrategy createT2MValueStrategy(int updatePolicy) { >+ return applyToT2MValueStrategy(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 #applyToM2TValueStrategy(UpdateValueStrategy) >+ */ >+ public final UpdateValueStrategy createM2TValueStrategy() { >+ return applyToM2TValueStrategy(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 #applyToM2TValueStrategy(UpdateValueStrategy) >+ */ >+ public final UpdateValueStrategy createM2TValueStrategy(int updatePolicy) { >+ return applyToM2TValueStrategy(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 #applyToT2MListStrategy(UpdateListStrategy) >+ */ >+ public final UpdateListStrategy createT2MListStrategy() { >+ return applyToT2MListStrategy(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 #applyToT2MListStrategy(UpdateListStrategy) >+ */ >+ public final UpdateListStrategy createT2MListStrategy(int updatePolicy) { >+ return applyToT2MListStrategy(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 #applyToM2TListStrategy(UpdateListStrategy) >+ */ >+ public final UpdateListStrategy createM2TListStrategy() { >+ return applyToM2TListStrategy(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 #applyToM2TListStrategy(UpdateListStrategy) >+ */ >+ public final UpdateListStrategy createM2TListStrategy(int updatePolicy) { >+ return applyToM2TListStrategy(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 #applyToT2MSetStrategy(UpdateSetStrategy) >+ */ >+ public final UpdateSetStrategy createT2MSetStrategy() { >+ return applyToT2MSetStrategy(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 #applyToT2MSetStrategy(UpdateSetStrategy) >+ */ >+ public final UpdateSetStrategy createT2MSetStrategy(int updatePolicy) { >+ return applyToT2MSetStrategy(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 #applyToM2TSetStrategy(UpdateSetStrategy) >+ */ >+ public final UpdateSetStrategy createM2TSetStrategy() { >+ return applyToM2TSetStrategy(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 #applyToM2TSetStrategy(UpdateSetStrategy) >+ */ >+ public final UpdateSetStrategy createM2TSetStrategy(int updatePolicy) { >+ return applyToM2TSetStrategy(new UpdateSetStrategy(updatePolicy)); >+ } >+ >+ /** >+ * Configures an existing target-to-model {@link UpdateValueStrategy} with >+ * the current state of this editing object. >+ * >+ * <p> >+ * The configuration is done as follows: >+ * <ul> >+ * <li> >+ * The {@link Constraints#createValidator() validator} of the >+ * {@link #targetConstraints() target constraints} is set as the >+ * {@link UpdateValueStrategy#setAfterGetValidator(IValidator) after-get >+ * validator} of the update strategy.</li> >+ * <li>The {@link #setTargetConverter(IConverter) target converter} is set >+ * as the {@link UpdateValueStrategy#setConverter(IConverter) converter} of >+ * the update strategy.</li> >+ * <li> >+ * The {@link Constraints#createValidator() validator} of the >+ * {@link #modelConstraints() model constraints} is set as the >+ * {@link UpdateValueStrategy#setAfterConvertValidator(IValidator) >+ * after-convert validator} of the update strategy.</li> >+ * The {@link Constraints#createValidator() validator} of the >+ * {@link #beforeSetModelConstraints() before-set model constraints} is set >+ * as the {@link UpdateValueStrategy#setBeforeSetValidator(IValidator) >+ * before-set validator} of the update strategy.</li> >+ * </ul> >+ * </p> >+ * >+ * @param updateStrategy >+ * The {@link UpdateValueStrategy} to configure. >+ * @return The passed-in, configured target-to-model >+ * {@link UpdateValueStrategy}. >+ * >+ * @see UpdateValueStrategy#setAfterGetValidator(IValidator) >+ * @see UpdateValueStrategy#setConverter(IConverter) >+ * @see UpdateValueStrategy#setAfterConvertValidator(IValidator) >+ * @see UpdateValueStrategy#setBeforeSetValidator(IValidator) >+ */ >+ public final UpdateValueStrategy applyToT2MValueStrategy( >+ UpdateValueStrategy updateStrategy) { >+ updateStrategy.setAfterGetValidator(createValidator(targetConstraints)); >+ updateStrategy.setConverter(targetConverter); >+ updateStrategy >+ .setAfterConvertValidator(createValidator(modelConstraints)); >+ updateStrategy >+ .setBeforeSetValidator(createValidator(beforeSetModelConstraints)); >+ return updateStrategy; >+ } >+ >+ /** >+ * Configures an existing model-to-target {@link UpdateValueStrategy} with >+ * the current state of this editing object by setting the >+ * {@link #setModelConverter(IConverter) model converter} as the >+ * {@link UpdateValueStrategy#setConverter(IConverter) converter} of the >+ * update strategy. >+ * >+ * @param updateStrategy >+ * The {@link UpdateValueStrategy} to configure. >+ * @return The passed-in, configured model-to-target >+ * {@link UpdateValueStrategy}. >+ * >+ * @see UpdateValueStrategy#setConverter(IConverter) >+ */ >+ public final UpdateValueStrategy applyToM2TValueStrategy( >+ UpdateValueStrategy updateStrategy) { >+ updateStrategy.setConverter(modelConverter); >+ return updateStrategy; >+ } >+ >+ /** >+ * Configures an existing target-to-model {@link UpdateListStrategy} with >+ * the current state of this editing object by setting the >+ * {@link #setTargetConverter(IConverter) target converter} as the >+ * {@link UpdateListStrategy#setConverter(IConverter) converter} of the >+ * update strategy. >+ * >+ * @param updateStrategy >+ * The {@link UpdateListStrategy} to configure. >+ * @return The passed-in, configured target-to-model >+ * {@link UpdateListStrategy}. >+ * >+ * @see UpdateListStrategy#setConverter(IConverter) >+ */ >+ public final UpdateListStrategy applyToT2MListStrategy( >+ UpdateListStrategy updateStrategy) { >+ updateStrategy.setConverter(targetConverter); >+ return updateStrategy; >+ } >+ >+ /** >+ * Configures an existing model-to-target {@link UpdateListStrategy} with >+ * the current state of this editing object by setting the >+ * {@link #setModelConverter(IConverter) model converter} as the >+ * {@link UpdateListStrategy#setConverter(IConverter) converter} of the >+ * update strategy. >+ * >+ * @param updateStrategy >+ * The {@link UpdateListStrategy} to configure. >+ * @return The passed-in, configured model-to-target >+ * {@link UpdateListStrategy}. >+ * >+ * @see UpdateListStrategy#setConverter(IConverter) >+ */ >+ public final UpdateListStrategy applyToM2TListStrategy( >+ UpdateListStrategy updateStrategy) { >+ updateStrategy.setConverter(modelConverter); >+ return updateStrategy; >+ } >+ >+ /** >+ * Configures an existing target-to-model {@link UpdateListStrategy} with >+ * the current state of this editing object by setting the >+ * {@link #setTargetConverter(IConverter) target converter} as the >+ * {@link UpdateSetStrategy#setConverter(IConverter) converter} of the >+ * update strategy. >+ * >+ * @param updateStrategy >+ * The {@link UpdateSetStrategy} to configure. >+ * @return The passed-in, configured target-to-model >+ * {@link UpdateSetStrategy}. >+ * >+ * @see UpdateSetStrategy#setConverter(IConverter) >+ */ >+ public final UpdateSetStrategy applyToT2MSetStrategy( >+ UpdateSetStrategy updateStrategy) { >+ updateStrategy.setConverter(targetConverter); >+ return updateStrategy; >+ } >+ >+ /** >+ * Configures an existing model-to-target {@link UpdateSetStrategy} with the >+ * current state of this editing object by setting the >+ * {@link #setModelConverter(IConverter) model converter} as the >+ * {@link UpdateSetStrategy#setConverter(IConverter) converter} of the >+ * update strategy. >+ * >+ * @param updateStrategy >+ * The {@link UpdateSetStrategy} to configure. >+ * @return The passed-in, configured model-to-target >+ * {@link UpdateSetStrategy}. >+ * >+ * @see UpdateSetStrategy#setConverter(IConverter) >+ */ >+ public final UpdateSetStrategy applyToM2TSetStrategy( >+ UpdateSetStrategy updateStrategy) { >+ updateStrategy.setConverter(modelConverter); >+ return updateStrategy; >+ } >+ >+ /** >+ * Converts a target value to a model value by performing the following >+ * steps: >+ * <ul> >+ * <li> >+ * Applying all the {@link #targetConstraints() target constraints} to the >+ * given target value.</li> >+ * <li> >+ * {@link #setTargetConverter(IConverter) Converting} the target value to >+ * the model value.</li> >+ * <li> >+ * Applying all the {@link #modelConstraints() model constraints} to the >+ * converted model value.</li> >+ * <li> >+ * Applying all the {@link #beforeSetModelConstraints() before-set model >+ * constraints} to the converted model value.</li> >+ * </ul> >+ * >+ * <p> >+ * The conversion process will be aborted by returning <code>null</code> >+ * whenever any of the applied validators produces a {@link IStatus >+ * validation status} having {@link IStatus#getSeverity() severity} >+ * <code>IStatus.ERROR</code> or <code>IStatus.CANCEL</code>. During the >+ * conversion process, any validation status whose severity is different >+ * from <code>IStatus.OK</code> will be {@link MultiStatus#merge(IStatus) >+ * aggregated} on the given <code>validationStatus</code>. >+ * </p> >+ * >+ * @param targetValue >+ * The target value to be converted to a model value. >+ * @param validationStatus >+ * Aggregate validation status to which to add the validations >+ * produced during the conversion process. >+ * @return The converted model value or <code>null</code> in case the >+ * conversion has been aborted due to a validation error. >+ */ >+ public final Object convertToModel(Object targetValue, >+ MultiStatus validationStatus) { >+ IValidator targetValidator = createValidator(targetConstraints); >+ if (!applyValidator(targetValidator, targetValue, validationStatus)) { >+ return null; >+ } >+ >+ Object modelValue = (targetConverter != null) ? targetConverter >+ .convert(targetValue) : targetValue; >+ >+ IValidator modelValidator = createValidator(modelConstraints); >+ if (!applyValidator(modelValidator, modelValue, validationStatus)) { >+ return null; >+ } >+ >+ IValidator beforeSetModelValidator = createValidator(beforeSetModelConstraints); >+ if (!applyValidator(beforeSetModelValidator, 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 (modelConverter != null) ? modelConverter.convert(modelValue) >+ : 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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @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 #applyToT2MValueStrategy(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 #applyToM2TValueStrategy(UpdateValueStrategy) >+ * configured} with the state of this editing object before >+ * passing it to the binding. >+ * @return The new value binding. >+ * >+ * @see #applyToT2MValueStrategy(UpdateValueStrategy) >+ * @see #applyToM2TValueStrategy(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, >+ applyToT2MValueStrategy(t2mUpdateStrategy), >+ applyToM2TValueStrategy(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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @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 #applyToT2MListStrategy(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 #applyToM2TListStrategy(UpdateListStrategy) >+ * configured} with the state of this editing object before >+ * passing it to the binding. >+ * @return The new list binding. >+ * >+ * @see #applyToT2MListStrategy(UpdateListStrategy) >+ * @see #applyToM2TListStrategy(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, >+ applyToT2MListStrategy(t2mUpdateStrategy), >+ applyToM2TListStrategy(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. >+ * >+ * <p> >+ * 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. >+ * </p> >+ * >+ * @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 #applyToT2MSetStrategy(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 #applyToM2TSetStrategy(UpdateSetStrategy) >+ * configured} with the state of this editing object before >+ * passing it to the binding. >+ * @return The new set binding. >+ * >+ * @see #applyToT2MSetStrategy(UpdateSetStrategy) >+ * @see #applyToM2TSetStrategy(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, >+ applyToT2MSetStrategy(t2mUpdateStrategy), >+ applyToM2TSetStrategy(m2tUpdateStrategy)); >+ } >+ >+ private static IValidator createValidator(Constraints constraints) { >+ return constraints != null ? constraints.createValidator() : null; >+ } >+ >+ private static boolean applyValidator(IValidator validator, Object value, >+ MultiStatus aggregateStatus) { >+ if (validator != null) { >+ IStatus validationStatus = validator.validate(value); >+ if (!validationStatus.isOK()) { >+ aggregateStatus.merge(validationStatus); >+ } >+ return isValid(validationStatus); >+ } >+ return true; >+ } >+ >+ private static boolean isValid(IStatus status) { >+ return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); >+ } >+} >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,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.validation.constraint; >+ >+import org.eclipse.core.internal.databinding.validation.IntegerRangeValidator; >+import org.eclipse.core.internal.databinding.validation.NonNullValidator; >+ >+import com.ibm.icu.text.NumberFormat; >+ >+/** >+ * @since 1.4 >+ */ >+public final class IntegerConstraints extends Constraints { >+ >+ private NumberFormat displayFormat = 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 an integer to be included in any >+ * of the validation messages. >+ * >+ * <p> >+ * This integer format will be used for any subsequent constraints which are >+ * applied. >+ * </p> >+ * >+ * @param format >+ * The format to use for displaying integers in validation >+ * messages. >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints displayFormat(NumberFormat format) { >+ this.displayFormat = format; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is not >+ * <code>null</code>. 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 to be issued in case the parsed integer is >+ * <code>null</code>. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is <code>null</code>. >+ * @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 parsed integer is greater than the >+ * given number. Uses the current {@link #greaterMessage(String) validation >+ * message}. >+ * >+ * @param number >+ * The number which the parsed integer must be greater than. >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints greater(int number) { >+ addValidator(IntegerRangeValidator.greater(number, greaterMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not greater than a given number. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not greater than a given number. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #greater(int) >+ */ >+ public IntegerConstraints greaterMessage(String message) { >+ this.greaterMessage = message; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is greater than or >+ * equal to the given number. Uses the current >+ * {@link #greaterEqualMessage(String) validation message}. >+ * >+ * @param number >+ * The number which the parsed integer must be greater than or >+ * equal to. >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints greaterEqual(int number) { >+ addValidator(IntegerRangeValidator.greaterEqual(number, >+ greaterEqualMessage, displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not greater than or equal to a given number. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not greater than or equal to a given number. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #greaterEqual(int) >+ */ >+ public IntegerConstraints greaterEqualMessage(String message) { >+ this.greaterEqualMessage = message; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is less than the given >+ * number. Uses the current {@link #lessMessage(String) validation message}. >+ * >+ * @param number >+ * The number which the parsed integer must be less than. >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints less(int number) { >+ addValidator(IntegerRangeValidator.less(number, lessMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not less than a given number. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not less than a given number. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #less(int) >+ */ >+ public IntegerConstraints lessMessage(String message) { >+ this.lessMessage = message; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is less than or equal >+ * to the given number. Uses the current {@link #lessEqualMessage(String) >+ * validation message}. >+ * >+ * @param number >+ * The number which the parsed integer must be less than or equal >+ * to. >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints lessEqual(int number) { >+ addValidator(IntegerRangeValidator.greaterEqual(number, >+ lessEqualMessage, displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not less than or equal to a given number. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not less than or equal to a given number. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #lessEqual(int) >+ */ >+ public IntegerConstraints lessEqualMessage(String message) { >+ this.lessEqualMessage = message; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is within the given >+ * range. Uses the current {@link #rangeMessage(String) validation message}. >+ * >+ * @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(int min, int max) { >+ addValidator(IntegerRangeValidator.range(min, max, rangeMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not within a given range. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not within a given range. >+ * @return This constraints instance for method chaining. >+ * >+ * @see #range(int, int) >+ */ >+ public IntegerConstraints rangeMessage(String message) { >+ this.rangeMessage = message; >+ return this; >+ } >+ >+ /** >+ * Adds a validator ensuring that the parsed integer is positive. Uses the >+ * current {@link #positiveMessage(String) validation message}. >+ * >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints positive() { >+ addValidator(IntegerRangeValidator.positive(positiveMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * not positive. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is not positive. >+ * @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 parsed integer is non-negative. Uses >+ * the current {@link #nonNegativeMessage(String) validation message}. >+ * >+ * @return This constraints instance for method chaining. >+ */ >+ public IntegerConstraints nonNegative() { >+ addValidator(IntegerRangeValidator.nonNegative(nonNegativeMessage, >+ displayFormat)); >+ return this; >+ } >+ >+ /** >+ * Sets the validation message to be issued in case the parsed integer is >+ * negative. >+ * >+ * @param message >+ * The validation message to be issued in case the parsed integer >+ * is negative. >+ * @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/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,320 @@ >+/******************************************************************************* >+ * 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.StringToIntegerValidator; >+ >+import com.ibm.icu.text.NumberFormat; >+ >+/** >+ * @since 1.4 >+ */ >+public final class IntegerEditing extends Editing { >+ >+ private final NumberFormat displayFormat; >+ >+ private IntegerEditing(NumberFormat format, String parseErrorMessage, >+ String outOfRangeMessage) { >+ this.displayFormat = format; >+ >+ StringToNumberConverter targetConverter = StringToNumberConverter >+ .toInteger(format, false); >+ NumberToStringConverter modelConverter = NumberToStringConverter >+ .fromInteger(format, false); >+ >+ StringToIntegerValidator targetValidator = new StringToIntegerValidator( >+ targetConverter); >+ if (parseErrorMessage != null) { >+ targetValidator.setParseErrorMessage(parseErrorMessage); >+ } >+ if (outOfRangeMessage != null) { >+ targetValidator.setOutOfRangeMessage(outOfRangeMessage); >+ } >+ >+ targetConstraints().addValidator(targetValidator); >+ setTargetConverter(targetConverter); >+ setModelConverter(modelConverter); >+ } >+ >+ /** >+ * Creates a new editing object 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 withDefaults() { >+ return withDefaults(null, null); >+ } >+ >+ /** >+ * Creates a new editing object 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 <code>Integer.MIN_VALUE</code> and >+ * <code>Integer.MAX_VALUE</code> values. >+ * @return The new editing object using the default validations and >+ * conversions for editing. >+ * >+ * @see NumberFormat#getIntegerInstance() >+ */ >+ public static IntegerEditing withDefaults(String parseErrorMessage, >+ String outOfRangeMessage) { >+ return new IntegerEditing(NumberFormat.getIntegerInstance(), >+ parseErrorMessage, outOfRangeMessage); >+ } >+ >+ /** >+ * Creates a new editing object 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 forFormat(NumberFormat format) { >+ return forFormat(format, null, null); >+ } >+ >+ /** >+ * Creates a new editing object 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 <code>Integer.MIN_VALUE</code> and >+ * <code>Integer.MAX_VALUE</code> values. >+ * @return The new editing object configured by the given integer format. >+ */ >+ public static IntegerEditing forFormat(NumberFormat format, >+ String parseErrorMessage, String outOfRangeMessage) { >+ return new IntegerEditing(format, parseErrorMessage, outOfRangeMessage); >+ } >+ >+ /** >+ * Returns the target constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link StringConstraints >+ * string target constraints} of this editing object and is equivalent to >+ * {@code (StringConstraints) targetConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #targetConstraints() >+ * @see StringConstraints >+ */ >+ public StringConstraints targetStringConstraints() { >+ return (StringConstraints) targetConstraints(); >+ } >+ >+ /** >+ * Returns the model constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link IntegerConstraints >+ * integer model constraints} of this editing object and is equivalent to >+ * {@code (IntegerConstraints) modelConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #modelConstraints() >+ * @see IntegerConstraints >+ */ >+ public IntegerConstraints modelIntegerConstraints() { >+ return (IntegerConstraints) modelConstraints(); >+ } >+ >+ /** >+ * Returns the before-set model constraints to apply. >+ * >+ * <p> >+ * 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()}. >+ * </p> >+ * >+ * @return The target 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().displayFormat(displayFormat); >+ } >+ >+ protected Constraints createBeforeSetModelConstraints() { >+ return new IntegerConstraints().displayFormat(displayFormat); >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#required() >+ * required constraint} to the set of {@link #modelIntegerConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#required() >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing required() { >+ modelIntegerConstraints().required(); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#greater(int) >+ * greater constraint} to the set of {@link #modelIntegerConstraints()}. >+ * >+ * @param number >+ * The number of the greater constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#greater(int) >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing greater(int number) { >+ modelIntegerConstraints().greater(number); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a >+ * {@link IntegerConstraints#greaterEqual(int) greater-equal constraint} to >+ * the set of {@link #modelIntegerConstraints()}. >+ * >+ * @param number >+ * The number of the greater-equal constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#greaterEqual(int) >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing greaterEqual(int number) { >+ modelIntegerConstraints().greaterEqual(number); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#less(int) less >+ * constraint} to the set of {@link #modelIntegerConstraints()}. >+ * >+ * @param number >+ * The number of the less constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#less(int) >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing less(int number) { >+ modelIntegerConstraints().less(number); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#lessEqual(int) >+ * less-equal constraint} to the set of {@link #modelIntegerConstraints()}. >+ * >+ * @param number >+ * The number of the less-equal constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#lessEqual(int) >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing lessEqual(int number) { >+ modelIntegerConstraints().lessEqual(number); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a >+ * {@link IntegerConstraints#range(int, int) range constraint} to the set of >+ * {@link #modelIntegerConstraints()}. >+ * >+ * @param min >+ * The min number of the range constraint. >+ * @param max >+ * The max number of the range constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#range(int, int) >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing range(int min, int max) { >+ modelIntegerConstraints().range(min, max); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#positive() >+ * positive constraint} to the set of {@link #modelIntegerConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#positive() >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing positive() { >+ modelIntegerConstraints().positive(); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link IntegerConstraints#nonNegative() >+ * non-negative constraint} to the set of {@link #modelIntegerConstraints()} >+ * . >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see IntegerConstraints#nonNegative() >+ * @see #modelIntegerConstraints() >+ */ >+ public IntegerEditing nonNegative() { >+ modelIntegerConstraints().nonNegative(); >+ return this; >+ } >+} >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,261 @@ >+/******************************************************************************* >+ * 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.util.Date; >+ >+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; >+ >+/** >+ * @since 1.4 >+ */ >+public final class DateEditing extends Editing { >+ >+ private final DateFormat displayFormat; >+ >+ private 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. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link StringConstraints >+ * string target constraints} of this editing object and is equivalent to >+ * {@code (StringConstraints) targetConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #targetConstraints() >+ * @see StringConstraints >+ */ >+ public StringConstraints targetStringConstraints() { >+ return (StringConstraints) targetConstraints(); >+ } >+ >+ /** >+ * Returns the model constraints to apply. >+ * >+ * <p> >+ * This method provides a typesafe access to the {@link DateConstraints date >+ * model constraints} of this editing object and is equivalent to {@code >+ * (DateConstraints) modelConstraints()}. >+ * </p> >+ * >+ * @return The target constraints to apply. >+ * >+ * @see #modelConstraints() >+ * @see DateConstraints >+ */ >+ public DateConstraints modelDateConstraints() { >+ return (DateConstraints) modelConstraints(); >+ } >+ >+ /** >+ * Returns the before-set model constraints to apply. >+ * >+ * <p> >+ * 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()}. >+ * </p> >+ * >+ * @return The target 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().displayFormat(displayFormat); >+ } >+ >+ protected Constraints createBeforeSetModelConstraints() { >+ return new DateConstraints().displayFormat(displayFormat); >+ } >+ >+ /** >+ * Convenience method which adds a {@link DateConstraints#required() >+ * required constraint} to the set of {@link #modelDateConstraints()}. >+ * >+ * @return This editing instance for method chaining. >+ * >+ * @see DateConstraints#required() >+ * @see #modelDateConstraints() >+ */ >+ public DateEditing required() { >+ modelDateConstraints().required(); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link DateConstraints#after(Date) after >+ * constraint} to the set of {@link #modelDateConstraints()}. >+ * >+ * @param date >+ * The date of the after constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see DateConstraints#after(Date) >+ * @see #modelDateConstraints() >+ */ >+ public DateEditing after(Date date) { >+ modelDateConstraints().after(date); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link DateConstraints#afterEqual(Date) >+ * after-equal constraint} to the set of {@link #modelDateConstraints()}. >+ * >+ * @param date >+ * The date of the after-equal constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see DateConstraints#afterEqual(Date) >+ * @see #modelDateConstraints() >+ */ >+ public DateEditing afterEqual(Date date) { >+ modelDateConstraints().afterEqual(date); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link DateConstraints#before(Date) >+ * before constraint} to the set of {@link #modelDateConstraints()}. >+ * >+ * @param date >+ * The date of the before constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see DateConstraints#before(Date) >+ * @see #modelDateConstraints() >+ */ >+ public DateEditing before(Date date) { >+ modelDateConstraints().before(date); >+ return this; >+ } >+ >+ /** >+ * Convenience method which adds a {@link DateConstraints#beforeEqual(Date) >+ * after-equal constraint} to the set of {@link #modelDateConstraints()}. >+ * >+ * @param date >+ * The date of the after-equal constraint. >+ * @return This editing instance for method chaining. >+ * >+ * @see DateConstraints#afterEqual(Date) >+ * @see #modelDateConstraints() >+ */ >+ public DateEditing beforeEqual(Date date) { >+ modelDateConstraints().beforeEqual(date); >+ return this; >+ } >+} >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,50 @@ >+/******************************************************************************* >+ * 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.4 >+ */ >+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 ("".equals(input)) { //$NON-NLS-1$ >+ return ValidationStatus.error(validationMessage); >+ } >+ return ValidationStatus.ok(); >+ } >+} >Index: src/org/eclipse/core/internal/databinding/conversion/StringStripConverter.java >=================================================================== >RCS file: src/org/eclipse/core/internal/databinding/conversion/StringStripConverter.java >diff -N src/org/eclipse/core/internal/databinding/conversion/StringStripConverter.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/internal/databinding/conversion/StringStripConverter.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,61 @@ >+/******************************************************************************* >+ * 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.4 >+ */ >+public class StringStripConverter extends Converter { >+ >+ private final boolean stripToNull; >+ >+ /** >+ * >+ * @param stripToNull >+ */ >+ public StringStripConverter(boolean stripToNull) { >+ super(String.class, String.class); >+ this.stripToNull = stripToNull; >+ } >+ >+ public Object convert(Object fromObject) { >+ String string = (String) fromObject; >+ >+ if (string != null && string.length() != 0) { >+ int stripStart = 0; >+ while (stripStart < string.length() >+ && Character.isWhitespace(string.charAt(stripStart))) { >+ stripStart++; >+ } >+ >+ int stripEnd = string.length() - 1; >+ while (stripEnd >= 0 >+ && Character.isWhitespace(string.charAt(stripEnd))) { >+ stripEnd--; >+ } >+ >+ if (stripStart <= stripEnd) { >+ string = string.substring(stripStart, stripEnd + 1); >+ } else { >+ string = ""; //$NON-NLS-1$ >+ } >+ } >+ >+ if (stripToNull && string != null && string.length() == 0) { >+ string = null; >+ } >+ >+ return string; >+ } >+} >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,71 @@ >+/******************************************************************************* >+ * 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; >+ >+/** >+ * @since 1.4 >+ */ >+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; >+ >+ /** >+ * >+ * @param regex >+ */ >+ public StringRegexValidator(String regex) { >+ this(regex, null); >+ } >+ >+ /** >+ * >+ * @param regex >+ * @param validationMessage >+ */ >+ public StringRegexValidator(String regex, String validationMessage) { >+ this.pattern = Pattern.compile(regex); >+ 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 String getFormattedValidationMessage() { >+ if (formattedValidationMessage == null) { >+ formattedValidationMessage = MessageFormat.format( >+ validationMessage, new String[] { pattern.pattern() }); >+ } >+ return formattedValidationMessage; >+ } >+} >#P org.eclipse.jface.tests.databinding >Index: src/org/eclipse/core/tests/internal/databinding/BindingMessagesTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/internal/databinding/BindingMessagesTest.java,v >retrieving revision 1.1 >diff -u -r1.1 BindingMessagesTest.java >--- src/org/eclipse/core/tests/internal/databinding/BindingMessagesTest.java 1 Apr 2007 20:58:10 -0000 1.1 >+++ src/org/eclipse/core/tests/internal/databinding/BindingMessagesTest.java 15 Sep 2009 22:13:58 -0000 >@@ -12,6 +12,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Ovidio Mallo - bug 183055 > ******************************************************************************/ > > /** >@@ -21,13 +22,13 @@ > public class BindingMessagesTest extends TestCase { > public void testFormatString() throws Exception { > String key = "Validate_NumberOutOfRangeError"; >- String result = BindingMessages.formatString(key, new Object[] {"1", "2"}); >+ String result = BindingMessages.getFormattedString(key, new Object[] {"1", "2"}); > assertFalse("key should not be returned", key.equals(result)); > } > > public void testFormatStringForKeyNotFound() throws Exception { > String key = "key_that_does_not_exist"; >- String result = BindingMessages.formatString(key, null); >+ String result = BindingMessages.getFormattedString(key, null); > assertTrue(key.equals(result)); > } > } >#P org.eclipse.jface.examples.databinding >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.examples.databinding/META-INF/MANIFEST.MF,v >retrieving revision 1.21 >diff -u -r1.21 MANIFEST.MF >--- META-INF/MANIFEST.MF 24 Aug 2009 21:33:42 -0000 1.21 >+++ META-INF/MANIFEST.MF 15 Sep 2009 22:14:00 -0000 >@@ -17,5 +17,6 @@ > org.eclipse.jface.examples.databinding.mask.internal;x-internal:=true, > org.eclipse.jface.examples.databinding.model;x-internal:=false, > org.eclipse.jface.examples.databinding.radioGroup;x-internal:=false >-Import-Package: com.ibm.icu.text >+Import-Package: com.ibm.icu.math, >+ com.ibm.icu.text > Bundle-RequiredExecutionEnvironment: J2SE-1.5 >Index: src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingSupport.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingSupport.java >diff -N src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingSupport.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingSupport.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,88 @@ >+/******************************************************************************* >+ * 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.jface.examples.databinding.util; >+ >+import org.eclipse.core.databinding.editing.Editing; >+import org.eclipse.core.databinding.observable.map.IObservableMap; >+import org.eclipse.core.databinding.util.Policy; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.MultiStatus; >+import org.eclipse.jface.dialogs.MessageDialog; >+import org.eclipse.jface.viewers.CellEditor; >+import org.eclipse.jface.viewers.ColumnViewer; >+import org.eclipse.jface.viewers.EditingSupport; >+import org.eclipse.jface.viewers.TextCellEditor; >+import org.eclipse.swt.widgets.Composite; >+ >+/** >+ * @since 3.2 >+ */ >+public class ObservableMapEditingSupport extends EditingSupport { >+ >+ private final IObservableMap attributeMap; >+ >+ private final Editing editing; >+ >+ private final CellEditor cellEditor; >+ >+ public ObservableMapEditingSupport(ColumnViewer viewer, IObservableMap attributeMap, Editing editing) { >+ super(viewer); >+ this.attributeMap = attributeMap; >+ this.editing = editing; >+ this.cellEditor = new TextCellEditor((Composite) getViewer().getControl()); >+ } >+ >+ protected boolean canEdit(Object element) { >+ return true; >+ } >+ >+ protected CellEditor getCellEditor(Object element) { >+ return cellEditor; >+ } >+ >+ protected Object getValue(Object element) { >+ return editing.convertToTarget(attributeMap.get(element)); >+ } >+ >+ protected void setValue(Object element, Object value) { >+ MultiStatus validationStatus = new MultiStatus(Policy.JFACE_DATABINDING, 0, null, null); >+ Object modelValue = editing.convertToModel(value, validationStatus); >+ if (handleValidation(validationStatus)) { >+ attributeMap.put(element, modelValue); >+ } >+ } >+ >+ private boolean handleValidation(IStatus validationStatus) { >+ if (validationStatus.matches(IStatus.ERROR | IStatus.CANCEL)) { >+ MessageDialog.openError(getViewer().getControl().getShell(), "Validation Error", getValidationMessage(validationStatus)); >+ return false; >+ } >+ return true; >+ } >+ >+ private static String getValidationMessage(IStatus validationStatus) { >+ if (!validationStatus.isMultiStatus()) { >+ return validationStatus.getMessage(); >+ } >+ >+ MultiStatus multiStatus = (MultiStatus) validationStatus; >+ StringBuffer sb = new StringBuffer(); >+ IStatus[] childStatus = multiStatus.getChildren(); >+ for (int i = 0; i < childStatus.length; i++) { >+ if (i > 0) { >+ sb.append('\n'); >+ } >+ sb.append(getValidationMessage(childStatus[i])); >+ } >+ return sb.toString(); >+ } >+} >Index: src/org/eclipse/jface/examples/databinding/snippets/Snippet035Editing.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/snippets/Snippet035Editing.java >diff -N src/org/eclipse/jface/examples/databinding/snippets/Snippet035Editing.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/snippets/Snippet035Editing.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,217 @@ >+/******************************************************************************* >+ * Copyright (c) 2006, 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.jface.examples.databinding.snippets; >+ >+import java.util.Calendar; >+import java.util.Date; >+ >+import org.eclipse.core.databinding.Binding; >+import org.eclipse.core.databinding.DataBindingContext; >+import org.eclipse.core.databinding.editing.BooleanEditing; >+import org.eclipse.core.databinding.editing.DateEditing; >+import org.eclipse.core.databinding.editing.Editing; >+import org.eclipse.core.databinding.editing.IntegerEditing; >+import org.eclipse.core.databinding.observable.Realm; >+import org.eclipse.core.databinding.observable.list.WritableList; >+import org.eclipse.core.databinding.observable.value.IObservableValue; >+import org.eclipse.core.databinding.observable.value.WritableValue; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.jface.databinding.swt.ISWTObservableValue; >+import org.eclipse.jface.databinding.swt.SWTObservables; >+import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; >+import org.eclipse.jface.examples.databinding.util.EditingFactory; >+import org.eclipse.jface.internal.databinding.provisional.fieldassist.ControlDecorationSupport; >+import org.eclipse.jface.layout.GridDataFactory; >+import org.eclipse.jface.layout.GridLayoutFactory; >+import org.eclipse.jface.viewers.LabelProvider; >+import org.eclipse.jface.viewers.ListViewer; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.FocusAdapter; >+import org.eclipse.swt.events.FocusEvent; >+import org.eclipse.swt.events.SelectionAdapter; >+import org.eclipse.swt.events.SelectionEvent; >+import org.eclipse.swt.layout.GridLayout; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Display; >+import org.eclipse.swt.widgets.Group; >+import org.eclipse.swt.widgets.Label; >+import org.eclipse.swt.widgets.Shell; >+import org.eclipse.swt.widgets.Text; >+ >+public class Snippet035Editing { >+ >+ private DataBindingContext dbc; >+ >+ public static void main(String[] args) { >+ Display display = new Display(); >+ >+ Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { >+ public void run() { >+ Shell shell = new Snippet035Editing().createShell(); >+ Display display = Display.getCurrent(); >+ while (!shell.isDisposed()) { >+ if (!display.readAndDispatch()) { >+ display.sleep(); >+ } >+ } >+ } >+ }); >+ } >+ >+ private Shell createShell() { >+ Display display = Display.getCurrent(); >+ Shell shell = new Shell(display); >+ shell.setText("Editing"); >+ shell.setLayout(new GridLayout(1, false)); >+ >+ dbc = new DataBindingContext(); >+ >+ createValueSection(shell); >+ createListSection(shell); >+ >+ shell.pack(); >+ shell.open(); >+ >+ return shell; >+ } >+ >+ private void createValueSection(Composite parent) { >+ Group section = createSectionGroup(parent, "Value bindings", false); >+ >+ // Edit an e-mail address. >+ Text emailText = createTextField(section, "E-Mail*"); >+ Editing emailEditing = EditingFactory.forEmailString().required(); >+ bindTextField(emailText, new WritableValue(), emailEditing); >+ >+ // Edit a required, positive integer using the default validation message. >+ Text positiveText = createTextField(section, "Positive value*"); >+ IntegerEditing positiveEditing = EditingFactory.forInteger().required().positive(); >+ bindTextField(positiveText, new WritableValue(), positiveEditing); >+ >+ // Edit an integer within the range [1, 100] using the default >+ // validation message. >+ Text rangeText = createTextField(section, "Value in [1, 100]"); >+ IntegerEditing rangeEditing = EditingFactory.forInteger().range(1, 100); >+ bindTextField(rangeText, new WritableValue(new Integer(0), null), rangeEditing); >+ >+ // Edit a percentage value which must lie within [0, 100] using a custom >+ // validation message which indicates that the value actually represents >+ // a percentage value. >+ Text percentText = createTextField(section, "Value [%]"); >+ IntegerEditing percentEditing = EditingFactory.forInteger(); >+ percentEditing.modelIntegerConstraints().rangeMessage("Please specify a percentage value within [{0}, {1}].").range(0, 100); >+ bindTextField(percentText, new WritableValue(new Integer(-1), null), percentEditing); >+ >+ // Edit a hex integer within the range [0x00, 0xff]. The range validation >+ // message will display the range boundaries in hex format as well. >+ Text hexText = createTextField(section, "Hex value in [0x00, 0xff]"); >+ IntegerEditing hexEditing = EditingFactory.forHexInteger(2).range(0x00, 0xff); >+ bindTextField(hexText, new WritableValue(new Integer(0), null), hexEditing); >+ >+ // Edit a boolean value. >+ Text boolText = createTextField(section, "Boolean value"); >+ BooleanEditing boolEditing = EditingFactory.forBoolean(); >+ bindTextField(boolText, new WritableValue(Boolean.TRUE, null), boolEditing); >+ } >+ >+ private void createListSection(Composite parent) { >+ Group section = createSectionGroup(parent, "List bindings", true); >+ >+ // Our date should be >= 01.01.1990. >+ Calendar year1990Calendar = Calendar.getInstance(); >+ year1990Calendar.clear(); >+ year1990Calendar.set(1990, 0, 1); >+ Date year1990 = year1990Calendar.getTime(); >+ >+ // Edit a date supporting the default input/display patterns. >+ Text dateText = createTextField(section, "Date"); >+ DateEditing dateEditing = EditingFactory.forDate().afterEqual(year1990); >+ final WritableValue dateObservable = new WritableValue(); >+ final Binding dateBinding = bindTextField(dateText, dateObservable, dateEditing); >+ >+ // Create a list to which the dates are added when the user hits ENTER. >+ new Label(section, SWT.LEFT); >+ ListViewer dateListViewer = new ListViewer(section); >+ GridDataFactory.fillDefaults().grab(true, true).hint(150, 200).applyTo(dateListViewer.getList()); >+ >+ dateListViewer.setContentProvider(new ObservableListContentProvider()); >+ dateListViewer.setLabelProvider(new LabelProvider()); >+ >+ // We use the same DateEditing object as for the text field above to >+ // create a list binding which maps the entered dates to their string >+ // representation which is then set as input on the ListViewer. >+ final WritableList targetDateList = new WritableList(); >+ final WritableList modelDateList = new WritableList(); >+ dateEditing.bindList(dbc, targetDateList, modelDateList); >+ >+ // Set the list containing the string representation of the dates as input. >+ dateListViewer.setInput(targetDateList); >+ >+ // Add the current date in the text field when the user hits ENTER. >+ dateText.addSelectionListener(new SelectionAdapter() { >+ public void widgetDefaultSelected(SelectionEvent e) { >+ IStatus dateValidationStatus = (IStatus) dateBinding.getValidationStatus().getValue(); >+ Date date = (Date) dateObservable.getValue(); >+ if (dateValidationStatus.isOK() && date != null) { >+ modelDateList.add(date); >+ } >+ } >+ }); >+ } >+ >+ private Binding bindTextField(Text text, IObservableValue modelValue, Editing editing) { >+ // Create the binding using the editing object. >+ ISWTObservableValue textObservable = SWTObservables.observeText(text, SWT.Modify); >+ Binding binding = editing.bindValue(dbc, textObservable, modelValue); >+ >+ // Decorate the control with the validation status. >+ ControlDecorationSupport.create(binding, SWT.TOP); >+ >+ // Re-format when the text field looses the focus in order to always >+ // display the model in the default format in case multiple input formats >+ // are supported. >+ formatOnFocusOut(text, binding); >+ >+ return binding; >+ } >+ >+ private static void formatOnFocusOut(final Control control, final Binding binding) { >+ control.addFocusListener(new FocusAdapter() { >+ public void focusLost(FocusEvent e) { >+ IStatus dateValidationStatus = (IStatus) binding.getValidationStatus().getValue(); >+ if (dateValidationStatus.isOK()) { >+ binding.updateModelToTarget(); >+ } >+ } >+ }); >+ } >+ >+ private static Group createSectionGroup(Composite parent, String groupText, boolean grabVertical) { >+ Group section = new Group(parent, SWT.SHADOW_ETCHED_IN); >+ section.setText(groupText); >+ GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).margins(5, 5).spacing(15, 5).applyTo(section); >+ GridDataFactory.fillDefaults().grab(true, grabVertical).applyTo(section); >+ return section; >+ } >+ >+ private static Text createTextField(Composite parent, String labelText) { >+ Label label = new Label(parent, SWT.LEFT); >+ label.setText(labelText); >+ GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(label); >+ >+ Text text = new Text(parent, SWT.BORDER); >+ GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).applyTo(text); >+ >+ return text; >+ } >+} >Index: src/org/eclipse/jface/examples/databinding/util/EditingFactory.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/util/EditingFactory.java >diff -N src/org/eclipse/jface/examples/databinding/util/EditingFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/util/EditingFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,140 @@ >+/******************************************************************************* >+ * 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.jface.examples.databinding.util; >+ >+import org.eclipse.core.databinding.editing.BooleanEditing; >+import org.eclipse.core.databinding.editing.DateEditing; >+import org.eclipse.core.databinding.editing.IntegerEditing; >+import org.eclipse.core.databinding.editing.StringEditing; >+ >+import com.ibm.icu.text.DateFormat; >+import com.ibm.icu.text.SimpleDateFormat; >+ >+/** >+ * @since 3.2 >+ */ >+public final class EditingFactory { >+ >+ private static final String REQUIRED_MESSAGE = "Please specify a value."; >+ >+ private static final String INTEGER_PARSE_ERROR_MESSAGE = "The input is invalid."; >+ >+ private static final String INTEGER_OUT_OF_RANGE_MESSAGE = "The value lies outside the supported range."; >+ >+ private static final String INTEGER_RANGE_MESSAGE = "The value must lie between {0} and {1}."; >+ >+ private static final String[] BOOLEAN_TRUE_VALUES = new String[] { >+ "yes", "y", "true", "1" >+ }; >+ >+ private static final String[] BOOLEAN_FALSE_VALUES = new String[] { >+ "no", "n", "false", "0" >+ }; >+ >+ private static final String[] DATE_INPUT_PATTERNS = new String[] { >+ "dd.MM.yy", >+ "dd/MM/yy", >+ "dd.MM.yyyy", >+ "dd/MM/yyyy" >+ }; >+ >+ private static final String DATE_DISPLAY_PATTERN = "dd.MM.yyyy"; >+ >+ private static final String DATE_PARSE_ERROR_MESSAGE = createDateParseErrorMessage(DATE_INPUT_PATTERNS); >+ >+ private static final String EMAIL_REGEX = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b"; >+ >+ private static final String EMAIL_ERROR_MESSAGE = "Please specify a valid e-mail address."; >+ >+ private EditingFactory() { >+ // prevent instantiation >+ } >+ >+ public static StringEditing forString() { >+ return StringEditing.stripped(true); >+ } >+ >+ public static StringEditing forEmailString() { >+ StringEditing editing = StringEditing.stripped(true); >+ editing.modelStringConstraints() >+ .matchesMessage(EMAIL_ERROR_MESSAGE) >+ .matches(EMAIL_REGEX); >+ return editing; >+ } >+ >+ public static IntegerEditing forInteger() { >+ IntegerEditing editing = IntegerEditing.withDefaults( >+ INTEGER_PARSE_ERROR_MESSAGE, >+ INTEGER_OUT_OF_RANGE_MESSAGE); >+ configure(editing); >+ return editing; >+ } >+ >+ public static IntegerEditing forHexInteger(int digits) { >+ IntegerEditing editing = IntegerEditing.forFormat( >+ RadixNumberFormat.getHexInstance("0x", digits), >+ INTEGER_PARSE_ERROR_MESSAGE, >+ INTEGER_OUT_OF_RANGE_MESSAGE); >+ configure(editing); >+ return editing; >+ } >+ >+ private static void configure(IntegerEditing editing) { >+ editing.modelIntegerConstraints() >+ .requiredMessage(REQUIRED_MESSAGE) >+ .rangeMessage(INTEGER_RANGE_MESSAGE); >+ } >+ >+ public static BooleanEditing forBoolean() { >+ BooleanEditing editing = >+ BooleanEditing.forStringValues( >+ BOOLEAN_TRUE_VALUES, >+ BOOLEAN_FALSE_VALUES); >+ configure(editing); >+ return editing; >+ } >+ >+ private static void configure(BooleanEditing editing) { >+ editing.modelBooleanConstraints() >+ .requiredMessage(REQUIRED_MESSAGE); >+ } >+ >+ public static DateEditing forDate() { >+ DateEditing editing = DateEditing >+ .forFormats( >+ createDateFormats(DATE_INPUT_PATTERNS), >+ DATE_PARSE_ERROR_MESSAGE, >+ new SimpleDateFormat(DATE_DISPLAY_PATTERN)); >+ editing.modelDateConstraints().requiredMessage(REQUIRED_MESSAGE); >+ return editing; >+ } >+ >+ private static DateFormat[] createDateFormats(String[] datePatterns) { >+ DateFormat[] dateFormats = new DateFormat[datePatterns.length]; >+ for (int i = 0; i < dateFormats.length; i++) { >+ dateFormats[i] = new SimpleDateFormat(datePatterns[i]); >+ } >+ return dateFormats; >+ } >+ >+ private static String createDateParseErrorMessage(String[] datePatterns) { >+ StringBuffer messageSb = new StringBuffer(); >+ messageSb.append("Supported formats: "); >+ for (int i = 0; i < datePatterns.length; i++) { >+ if (i > 0) { >+ messageSb.append(", "); >+ } >+ messageSb.append(datePatterns[i]); >+ } >+ return messageSb.toString(); >+ } >+} >Index: src/org/eclipse/jface/examples/databinding/util/RadixNumberFormat.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/util/RadixNumberFormat.java >diff -N src/org/eclipse/jface/examples/databinding/util/RadixNumberFormat.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/util/RadixNumberFormat.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,100 @@ >+/******************************************************************************* >+ * 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.jface.examples.databinding.util; >+ >+import java.math.BigInteger; >+import java.text.FieldPosition; >+import java.text.ParsePosition; >+ >+import com.ibm.icu.math.BigDecimal; >+import com.ibm.icu.text.NumberFormat; >+ >+/** >+ * @since 3.2 >+ * >+ */ >+public class RadixNumberFormat extends NumberFormat { >+ >+ private static final long serialVersionUID = 411884077848863891L; >+ >+ private final int radix; >+ >+ private final String prefix; >+ >+ private final int digits; >+ >+ private RadixNumberFormat(int radix, String prefix, int digits) { >+ this.radix = radix; >+ this.prefix = prefix != null ? prefix : ""; >+ this.digits = digits; >+ } >+ >+ public static RadixNumberFormat getHexInstance() { >+ return getHexInstance(null, 0); >+ } >+ >+ public static RadixNumberFormat getHexInstance(String prefix, int digits) { >+ return new RadixNumberFormat(16, prefix, digits); >+ } >+ >+ public StringBuffer format(double number, StringBuffer toAppendTo, >+ FieldPosition pos) { >+ // TODO omallo: Is this necessary due to a bug in NumberFormat.format(Object, ...)? >+ return format((long) number, toAppendTo, pos); >+ } >+ >+ public StringBuffer format(long number, StringBuffer toAppendTo, >+ FieldPosition pos) { >+ return toAppendTo.append(prefix).append(addPadding(Long.toString(number, radix))); >+ } >+ >+ public StringBuffer format(BigInteger number, StringBuffer toAppendTo, >+ FieldPosition pos) { >+ return toAppendTo.append(prefix).append(addPadding(number.toString(radix))); >+ } >+ >+ public StringBuffer format(BigDecimal number, StringBuffer toAppendTo, >+ FieldPosition pos) { >+ throw new UnsupportedOperationException(); >+ } >+ >+ public Number parse(String text, ParsePosition parsePosition) { >+ if (text.length() == 0) { >+ return null; >+ } >+ >+ parsePosition.setIndex(parsePosition.getIndex() + text.length()); >+ >+ try { >+ if (text.startsWith(prefix)) { >+ return Integer.parseInt(text.substring(prefix.length()), radix); >+ } >+ return Integer.parseInt(text, radix); >+ } catch (NumberFormatException e) { >+ parsePosition.setErrorIndex(0); >+ return null; >+ } >+ } >+ >+ private String addPadding(String numberText) { >+ if (numberText.length() >= digits) { >+ return numberText; >+ } >+ >+ StringBuffer sb = new StringBuffer(); >+ for (int i = numberText.length(); i < digits; i++) { >+ sb.append('0'); >+ } >+ sb.append(numberText); >+ return sb.toString(); >+ } >+} >Index: src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingCellLabelProvider.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingCellLabelProvider.java >diff -N src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingCellLabelProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/util/ObservableMapEditingCellLabelProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,40 @@ >+/******************************************************************************* >+ * 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.jface.examples.databinding.util; >+ >+import org.eclipse.core.databinding.editing.Editing; >+import org.eclipse.core.databinding.observable.map.IObservableMap; >+import org.eclipse.jface.databinding.viewers.ObservableMapCellLabelProvider; >+import org.eclipse.jface.viewers.ViewerCell; >+ >+/** >+ * @since 3.2 >+ */ >+public class ObservableMapEditingCellLabelProvider extends >+ ObservableMapCellLabelProvider { >+ >+ private final IObservableMap attributeMap; >+ >+ private final Editing editing; >+ >+ public ObservableMapEditingCellLabelProvider(IObservableMap attributeMap, Editing editing) { >+ super(attributeMap); >+ this.attributeMap = attributeMap; >+ this.editing = editing; >+ } >+ >+ public void update(ViewerCell cell) { >+ Object element = cell.getElement(); >+ Object attribute = attributeMap.get(element); >+ cell.setText((String) editing.convertToTarget(attribute)); >+ } >+} >Index: src/org/eclipse/jface/examples/databinding/snippets/Snippet036EditingTable.java >=================================================================== >RCS file: src/org/eclipse/jface/examples/databinding/snippets/Snippet036EditingTable.java >diff -N src/org/eclipse/jface/examples/databinding/snippets/Snippet036EditingTable.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/examples/databinding/snippets/Snippet036EditingTable.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,245 @@ >+/******************************************************************************* >+ * Copyright (c) 2006, 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.jface.examples.databinding.snippets; >+ >+import java.util.Date; >+ >+import org.eclipse.core.databinding.Binding; >+import org.eclipse.core.databinding.DataBindingContext; >+import org.eclipse.core.databinding.beans.BeansObservables; >+import org.eclipse.core.databinding.editing.DateEditing; >+import org.eclipse.core.databinding.editing.Editing; >+import org.eclipse.core.databinding.editing.IntegerEditing; >+import org.eclipse.core.databinding.editing.StringEditing; >+import org.eclipse.core.databinding.observable.Realm; >+import org.eclipse.core.databinding.observable.list.WritableList; >+import org.eclipse.core.databinding.observable.map.IObservableMap; >+import org.eclipse.core.databinding.observable.set.IObservableSet; >+import org.eclipse.core.databinding.observable.value.IObservableValue; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.jface.databinding.swt.ISWTObservableValue; >+import org.eclipse.jface.databinding.swt.SWTObservables; >+import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; >+import org.eclipse.jface.databinding.viewers.ViewersObservables; >+import org.eclipse.jface.examples.databinding.ModelObject; >+import org.eclipse.jface.examples.databinding.util.EditingFactory; >+import org.eclipse.jface.examples.databinding.util.ObservableMapEditingCellLabelProvider; >+import org.eclipse.jface.examples.databinding.util.ObservableMapEditingSupport; >+import org.eclipse.jface.internal.databinding.provisional.fieldassist.ControlDecorationSupport; >+import org.eclipse.jface.layout.GridDataFactory; >+import org.eclipse.jface.layout.GridLayoutFactory; >+import org.eclipse.jface.viewers.StructuredSelection; >+import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.TableViewerColumn; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.FocusAdapter; >+import org.eclipse.swt.events.FocusEvent; >+import org.eclipse.swt.layout.GridLayout; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Display; >+import org.eclipse.swt.widgets.Group; >+import org.eclipse.swt.widgets.Label; >+import org.eclipse.swt.widgets.Shell; >+import org.eclipse.swt.widgets.Text; >+ >+public class Snippet036EditingTable { >+ >+ private final StringEditing nameEditing = EditingFactory.forString().required(); >+ >+ private final IntegerEditing ageEditing = EditingFactory.forInteger().required().nonNegative(); >+ >+ private final DateEditing bdayEditing = EditingFactory.forDate().before(new Date()); >+ >+ private DataBindingContext dbc; >+ >+ private TableViewer tableViewer; >+ >+ public static void main(String[] args) { >+ Display display = new Display(); >+ >+ Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { >+ public void run() { >+ Shell shell = new Snippet036EditingTable().createShell(); >+ Display display = Display.getCurrent(); >+ while (!shell.isDisposed()) { >+ if (!display.readAndDispatch()) { >+ display.sleep(); >+ } >+ } >+ } >+ }); >+ } >+ >+ private Shell createShell() { >+ Display display = Display.getCurrent(); >+ Shell shell = new Shell(display); >+ shell.setText("Editing"); >+ shell.setLayout(new GridLayout(2, false)); >+ >+ dbc = new DataBindingContext(); >+ >+ createTableSection(shell); >+ createFieldSection(shell); >+ >+ shell.pack(); >+ shell.open(); >+ >+ return shell; >+ } >+ >+ private void createTableSection(Composite parent) { >+ Group section = createSectionGroup(parent, 1); >+ >+ tableViewer = new TableViewer(section, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION); >+ GridDataFactory.fillDefaults().grab(true, true).hint(350, 250).applyTo(tableViewer.getTable()); >+ tableViewer.getTable().setHeaderVisible(true); >+ tableViewer.getTable().setLinesVisible(true); >+ >+ ObservableListContentProvider contentProvider = new ObservableListContentProvider(); >+ tableViewer.setContentProvider(contentProvider); >+ IObservableSet contentElements = contentProvider.getKnownElements(); >+ >+ IObservableMap nameMap = BeansObservables.observeMap(contentElements, "name"); >+ createColumn("Name*", 150, nameMap, nameEditing); >+ >+ IObservableMap ageMap = BeansObservables.observeMap(contentElements, "age"); >+ createColumn("Age*", 50, ageMap, ageEditing); >+ >+ IObservableMap bdayMap = BeansObservables.observeMap(contentElements, "birthday"); >+ createColumn("Birthday", 80, bdayMap, bdayEditing); >+ >+ WritableList people = new WritableList(); >+ people.add(new Person("John Doe", 27)); >+ people.add(new Person("Steve Northover", 33)); >+ people.add(new Person("Grant Gayed", 54)); >+ people.add(new Person("Veronika Irvine", 25)); >+ people.add(new Person("Mike Wilson", 44)); >+ people.add(new Person("Christophe Cornu", 37)); >+ people.add(new Person("Lynne Kues", 65)); >+ people.add(new Person("Silenio Quarti", 15)); >+ >+ tableViewer.setInput(people); >+ >+ tableViewer.setSelection(new StructuredSelection(people.get(0))); >+ } >+ >+ private TableViewerColumn createColumn(String text, int width, IObservableMap attributeMap, Editing modelEditing) { >+ TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE); >+ column.getColumn().setText(text); >+ column.getColumn().setWidth(width); >+ column.setLabelProvider(new ObservableMapEditingCellLabelProvider(attributeMap, modelEditing)); >+ column.setEditingSupport(new ObservableMapEditingSupport(tableViewer, attributeMap, modelEditing)); >+ return column; >+ } >+ >+ private void createFieldSection(Composite parent) { >+ final Group section = createSectionGroup(parent, 2); >+ >+ final IObservableValue personObservable = ViewersObservables.observeSingleSelection(tableViewer); >+ >+ Text nameText = createTextField(section, "Name*"); >+ IObservableValue nameObservable = BeansObservables.observeDetailValue(personObservable, "name", null); >+ bindTextField(nameText, nameObservable, nameEditing); >+ >+ Text ageText = createTextField(section, "Age*"); >+ IObservableValue ageObservable = BeansObservables.observeDetailValue(personObservable, "age", null); >+ bindTextField(ageText, ageObservable, ageEditing); >+ >+ Text bdayText = createTextField(section, "Birthday"); >+ IObservableValue bdayObservable = BeansObservables.observeDetailValue(personObservable, "birthday", null); >+ bindTextField(bdayText, bdayObservable, bdayEditing); >+ } >+ >+ private Binding bindTextField( >+ Text text, >+ IObservableValue modelValue, >+ Editing editing) { >+ // Create the binding using the editing object. >+ ISWTObservableValue textObservable = SWTObservables.observeText(text, SWT.Modify); >+ Binding binding = editing.bindValue(dbc, textObservable, modelValue); >+ >+ // Decorate the control with the validation status. >+ ControlDecorationSupport.create(binding, SWT.TOP); >+ >+ formatOnFocusOut(text, binding); >+ >+ return binding; >+ } >+ >+ private static void formatOnFocusOut(final Control control, final Binding binding) { >+ control.addFocusListener(new FocusAdapter() { >+ public void focusLost(FocusEvent e) { >+ IStatus dateValidationStatus = (IStatus) binding.getValidationStatus().getValue(); >+ if (dateValidationStatus.isOK()) { >+ binding.updateModelToTarget(); >+ } >+ } >+ }); >+ } >+ >+ private Group createSectionGroup(Composite parent, int numColumns) { >+ Group section = new Group(parent, SWT.SHADOW_ETCHED_IN); >+ GridLayoutFactory.fillDefaults().numColumns(numColumns).equalWidth(false).margins(5, 5).spacing(15, 5).applyTo(section); >+ GridDataFactory.fillDefaults().grab(true, true).applyTo(section); >+ return section; >+ } >+ >+ private static Text createTextField(Composite parent, String labelText) { >+ Label label = new Label(parent, SWT.LEFT); >+ label.setText(labelText); >+ GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(label); >+ >+ Text text = new Text(parent, SWT.BORDER); >+ GridDataFactory.fillDefaults().grab(true, false).hint(200, SWT.DEFAULT).applyTo(text); >+ >+ return text; >+ } >+ >+ public static final class Person extends ModelObject { >+ >+ private String name; >+ >+ private int age; >+ >+ private Date birthday; >+ >+ public Person(String name, int age) { >+ this.name = name; >+ this.age = age; >+ } >+ >+ public String getName() { >+ return name; >+ } >+ >+ public void setName(String name) { >+ firePropertyChange("name", this.name, this.name = name); >+ } >+ >+ public int getAge() { >+ return age; >+ } >+ >+ public void setAge(int age) { >+ firePropertyChange("age", this.age, this.age = age); >+ } >+ >+ public Date getBirthday() { >+ return birthday; >+ } >+ >+ public void setBirthday(Date birthday) { >+ firePropertyChange("birthday", this.birthday, this.birthday = birthday); >+ } >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 183055
:
139097
|
142074
|
145997
|
146005
|
146551
|
147262
|
149314
|
149326
|
151361
|
152082
|
153890
|
156294
|
156446
|
157017
|
157180
|
157378
|
157469
|
160440
|
160542
|
179211
|
179212