| CODENOTIFIER | HelpYou are not signed inSign in |
Project: UFace
Revision: 2262
Author: kenneth.westelinck
Date: 17 Aug 2008 05:28:29
Changes:Javadoc
Files:| ... | ...@@ -13,26 +13,71 @@ | |
| 13 | 13 | import org.eclipse.core.databinding.validation.IValidator; |
| 14 | 14 | import org.ufacekit.core.databinding.common.strategy.UpdateValueStrategy; |
| 15 | 15 | |
| 16 | /** | |
| 17 | * An {@link org.eclipse.core.databinding.UpdateValueStrategy} can have three | |
| 18 | * different validators ({@link IValidator}) that are be applied in different | |
| 19 | * stages in the update strategy. | |
| 20 | * <ul> | |
| 21 | * <li>afterGetValidator: validator to be invoked after the source value is | |
| 22 | * retrieved at the beginning of the synchronization process</li> | |
| 23 | * <li>afterConvertValidator: validator to be invoked after the source value is | |
| 24 | * converted to the type of the destination observable</li> | |
| 25 | * <li>beforeSetValidator: validator to be invoked before the value is to be set | |
| 26 | * on the destination at the end of the synchronization process.</li> | |
| 27 | * </ul> | |
| 28 | * | |
| 29 | * @version 1.0 | |
| 30 | * @since 1.0 | |
| 31 | */ | |
| 16 | 32 | public class ValidatorSource { |
| 17 | 33 | private IValidator afterGetValidator; |
| 18 | 34 | private IValidator afterConvertValidator; |
| 19 | 35 | private IValidator beforeSetValidator; |
| 20 | 36 | |
| 37 | /** | |
| 38 | * @see org.eclipse.core.databinding.UpdateValueStrategy#setAfterGetValidator(IValidator) | |
| 39 | * @param afterGetValidator | |
| 40 | * @return this | |
| 41 | * @since 1.0 | |
| 42 | */ | |
| 21 | 43 | public ValidatorSource setAfterGetValidator(IValidator afterGetValidator) { |
| 22 | 44 | this.afterGetValidator = afterGetValidator; |
| 23 | 45 | return this; |
| 24 | 46 | } |
| 25 | 47 | |
| 48 | /** | |
| 49 | * @see org.eclipse.core.databinding.UpdateValueStrategy#setAfterConvertValidator(IValidator) | |
| 50 | * @param afterConvertValidator | |
| 51 | * @return this | |
| 52 | * @since 1.0 | |
| 53 | */ | |
| 26 | 54 | public ValidatorSource setAfterConvertValidator(IValidator afterConvertValidator) { |
| 27 | 55 | this.afterConvertValidator = afterConvertValidator; |
| 28 | 56 | return this; |
| 29 | 57 | } |
| 30 | 58 | |
| 59 | /** | |
| 60 | * @see org.eclipse.core.databinding.UpdateValueStrategy#setBeforeSetValidator(IValidator) | |
| 61 | * @param beforeSetValidator | |
| 62 | * @return this | |
| 63 | * @since 1.0 | |
| 64 | */ | |
| 31 | 65 | public ValidatorSource setBeforeSetValidator(IValidator beforeSetValidator) { |
| 32 | 66 | this.beforeSetValidator = beforeSetValidator; |
| 33 | 67 | return this; |
| 34 | 68 | } |
| 35 | 69 | |
| 70 | /** | |
| 71 | * Will set the necessary validators on the provided | |
| 72 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} and return the | |
| 73 | * adapted result. | |
| 74 | * | |
| 75 | * @param updateValueStrategy | |
| 76 | * @return the updateValueStrategy from the input parameter with the | |
| 77 | * necessary validators set, that were configured on this | |
| 78 | * {@link ValidatorSource}. | |
| 79 | * @since 1.0 | |
| 80 | */ | |
| 36 | 81 | public org.eclipse.core.databinding.UpdateValueStrategy adapt(org.eclipse.core.databinding.UpdateValueStrategy updateValueStrategy) { |
| 37 | 82 | if (afterGetValidator != null) { |
| 38 | 83 | updateValueStrategy.setAfterGetValidator(afterGetValidator); |
| ... | ...@@ -49,6 +94,21 @@ | |
| 49 | 94 | return updateValueStrategy; |
| 50 | 95 | } |
| 51 | 96 | |
| 97 | /** | |
| 98 | * Will set the necessary validators on the provided | |
| 99 | * {@link UpdateValueStrategy} and return the adapted result. | |
| 100 | * | |
| 101 | * @param updateValueStrategy | |
| 102 | * @return the updateValueStrategy from the input parameter with the | |
| 103 | * necessary validators set, that were configured on this | |
| 104 | * {@link ValidatorSource}. Please note that this will only affect | |
| 105 | * the wrapped model-to-target | |
| 106 | * {@link org.eclipse.core.databinding.UpdateValueStrategy}. At this | |
| 107 | * time, it is not possible to set the configured converters on the | |
| 108 | * wrapped target-to-model | |
| 109 | * {@link org.eclipse.core.databinding.UpdateValueStrategy}. | |
| 110 | * @since 1.0 | |
| 111 | */ | |
| 52 | 112 | public UpdateValueStrategy adapt(UpdateValueStrategy updateValueStrategy) { |
| 53 | 113 | adapt(updateValueStrategy.getTargetToModelStrategy()); |
| 54 | 114 | return updateValueStrategy; |
| ... | ...@@ -12,12 +12,37 @@ | |
| 12 | 12 | |
| 13 | 13 | import org.ufacekit.core.databinding.common.conversion.Converter; |
| 14 | 14 | |
| 15 | /** | |
| 16 | * Implementations of this interface serve as a wrapper for a model-to-target | |
| 17 | * and a target-to-model update strategy ( | |
| 18 | * {@link org.eclipse.core.databinding.UpdateValueStrategy}). This strategy is | |
| 19 | * responsible for: | |
| 20 | * <ul> | |
| 21 | * <li>Validation</li> | |
| 22 | * <li>Conversion: Use {@link #setConverter(Converter)} to set the converter for | |
| 23 | * this strategy</li> | |
| 24 | * </ul> | |
| 25 | * | |
| 26 | * @version 1.0 | |
| 27 | * @since 1.0 | |
| 28 | */ | |
| 15 | 29 | public interface UpdateValueStrategy { |
| 30 | /** | |
| 31 | * This is an empty update strategy, backed by _no_ | |
| 32 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} and no | |
| 33 | * {@link Converter}. | |
| 34 | */ | |
| 16 | 35 | public static final UpdateValueStrategy EMPTY = new UpdateValueStrategy() { |
| 36 | /** | |
| 37 | * @return null | |
| 38 | */ | |
| 17 | 39 | public org.eclipse.core.databinding.UpdateValueStrategy getModelToTargetStrategy() { |
| 18 | 40 | return null; |
| 19 | 41 | } |
| 20 | 42 | |
| 43 | /** | |
| 44 | * @return null | |
| 45 | */ | |
| 21 | 46 | public org.eclipse.core.databinding.UpdateValueStrategy getTargetToModelStrategy() { |
| 22 | 47 | return null; |
| 23 | 48 | } |
| ... | ...@@ -26,9 +51,34 @@ | |
| 26 | 51 | } |
| 27 | 52 | }; |
| 28 | 53 | |
| 54 | /** | |
| 55 | * Will return the wrapped | |
| 56 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} responsible for | |
| 57 | * updating the target from the model. | |
| 58 | * | |
| 59 | * @return the wrapped model-to-target | |
| 60 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} | |
| 61 | * @since 1.0 | |
| 62 | */ | |
| 29 | 63 | org.eclipse.core.databinding.UpdateValueStrategy getModelToTargetStrategy(); |
| 30 | 64 | |
| 65 | /** | |
| 66 | * Will return the wrapped | |
| 67 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} responsible for | |
| 68 | * updating the model from the target. | |
| 69 | * | |
| 70 | * @return the wrapped target-to-model | |
| 71 | * {@link org.eclipse.core.databinding.UpdateValueStrategy} | |
| 72 | * @since 1.0 | |
| 73 | */ | |
| 31 | 74 | org.eclipse.core.databinding.UpdateValueStrategy getTargetToModelStrategy(); |
| 32 | 75 | |
| 76 | /** | |
| 77 | * Set the converter on this strategy that will be responsible for | |
| 78 | * converting the model to the UI component and vice versa. | |
| 79 | * | |
| 80 | * @param converter | |
| 81 | * @since 1.0 | |
| 82 | */ | |
| 33 | 83 | void setConverter(Converter converter); |
| 34 | 84 | } |
| ... | ...@@ -18,30 +18,41 @@ | |
| 18 | 18 | import org.ufacekit.core.databinding.common.AttributeDescriptor; |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | * @version $Revision: 1.1 $ | |
| 22 | */ | |
| 21 | * Convert a {@link Number} to a {@link String}. | |
| 22 | * | |
| 23 | * @version 1.0 | |
| 24 | * @since 1.0 | |
| 25 | */ | |
| 23 | 26 | public class NumberToStringConverter implements IConverter { |
| 24 | private DecimalFormat format; | |
| 25 | private AttributeDescriptor descriptor; | |
| 27 | private DecimalFormat format; | |
| 28 | private AttributeDescriptor descriptor; | |
| 26 | 29 | |
| 27 | public NumberToStringConverter(String pattern, AttributeDescriptor descriptor) { | |
| 28 | format = new DecimalFormat(pattern); | |
| 29 | this.descriptor = descriptor; | |
| 30 | } | |
| 30 | /** | |
| 31 | * Construct the converter. | |
| 32 | * | |
| 33 | * @param pattern | |
| 34 | * The pattern that will be used to display the number. | |
| 35 | * @param descriptor | |
| 36 | * @since 1.0 | |
| 37 | */ | |
| 38 | public NumberToStringConverter(String pattern, AttributeDescriptor descriptor) { | |
| 39 | format = new DecimalFormat(pattern); | |
| 40 | this.descriptor = descriptor; | |
| 41 | } | |
| 31 | 42 | |
| 32 | public Object convert(Object fromObject) { | |
| 33 | if (fromObject instanceof Number) { | |
| 34 | Number number = (Number) fromObject; | |
| 35 | return format.format(number.doubleValue()); | |
| 36 | } | |
| 37 | return null; | |
| 38 | } | |
| 43 | public Object convert(Object fromObject) { | |
| 44 | if (fromObject instanceof Number) { | |
| 45 | Number number = (Number) fromObject; | |
| 46 | return format.format(number.doubleValue()); | |
| 47 | } | |
| 48 | return null; | |
| 49 | } | |
| 39 | 50 | |
| 40 | public Object getFromType() { | |
| 41 | return descriptor.getPropertyType(); | |
| 42 | } | |
| 51 | public Object getFromType() { | |
| 52 | return descriptor.getPropertyType(); | |
| 53 | } | |
| 43 | 54 | |
| 44 | public Object getToType() { | |
| 45 | return String.class; | |
| 46 | } | |
| 55 | public Object getToType() { | |
| 56 | return String.class; | |
| 57 | } | |
| 47 | 58 | } |
| ... | ...@@ -18,83 +18,88 @@ | |
| 18 | 18 | import org.ufacekit.core.databinding.common.AttributeDescriptor; |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | * @version $Revision: 1.1 $ | |
| 22 | */ | |
| 21 | * Convert a {@link String} to a {@link Number}. | |
| 22 | * | |
| 23 | * @version 1.0 | |
| 24 | * @since 1.0 | |
| 25 | */ | |
| 23 | 26 | public class StringToNumberConverter implements IConverter { |
| 24 | private AttributeDescriptor descriptor; | |
| 25 | private DecimalFormat[] parsers; | |
| 26 | private int targetType; | |
| 27 | public static final int SHORT_TARGET = 1; | |
| 28 | public static final int INTEGER_TARGET = 2; | |
| 29 | public static final int LONG_TARGET = 3; | |
| 30 | public static final int FLOAT_TARGET = 4; | |
| 31 | public static final int DOUBLE_TARGET = 5; | |
| 32 | ||
| 33 | public StringToNumberConverter(String[] patterns, AttributeDescriptor descriptor) { | |
| 34 | this.descriptor = descriptor; | |
| 35 | parsers = new DecimalFormat[patterns.length]; | |
| 36 | ||
| 37 | for (int i = 0; i < patterns.length; i++) { | |
| 38 | parsers[i] = new DecimalFormat(patterns[i]); | |
| 39 | } | |
| 40 | ||
| 41 | if (this.descriptor.getPropertyType() == Short.class) { | |
| 42 | targetType = SHORT_TARGET; | |
| 43 | } | |
| 44 | else if (this.descriptor.getPropertyType() == Double.class) { | |
| 45 | targetType = INTEGER_TARGET; | |
| 46 | } | |
| 47 | else if (this.descriptor.getPropertyType() == Long.class) { | |
| 48 | targetType = LONG_TARGET; | |
| 49 | } | |
| 50 | else if (this.descriptor.getPropertyType() == Float.class) { | |
| 51 | targetType = FLOAT_TARGET; | |
| 52 | } | |
| 53 | else if (this.descriptor.getPropertyType() == Double.class) { | |
| 54 | targetType = DOUBLE_TARGET; | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | public Object convert(Object fromObject) { | |
| 59 | if (fromObject != null && fromObject.toString().trim().length() > 0) { | |
| 60 | String s = fromObject.toString(); | |
| 61 | Number rv = new Double(0.0); | |
| 62 | ||
| 63 | for (int i = 0; i < parsers.length; i++) { | |
| 64 | try { | |
| 65 | rv = parsers[i].parse(s); | |
| 66 | } | |
| 67 | catch (Exception e) { | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | if (targetType == SHORT_TARGET) { | |
| 72 | return new Short(rv.shortValue()); | |
| 73 | } | |
| 74 | else if (targetType == INTEGER_TARGET) { | |
| 75 | return new Integer(rv.intValue()); | |
| 76 | } | |
| 77 | else if (targetType == LONG_TARGET) { | |
| 78 | return new Long(rv.longValue()); | |
| 79 | } | |
| 80 | else if (targetType == FLOAT_TARGET) { | |
| 81 | return new Float(rv.floatValue()); | |
| 82 | } | |
| 83 | else if (targetType == DOUBLE_TARGET) { | |
| 84 | return new Double(rv.doubleValue()); | |
| 85 | } | |
| 86 | ||
| 87 | throw new RuntimeException("Unknown target type: " + targetType); | |
| 88 | } | |
| 89 | ||
| 90 | return null; | |
| 91 | } | |
| 92 | ||
| 93 | public Object getFromType() { | |
| 94 | return null; | |
| 95 | } | |
| 96 | ||
| 97 | public Object getToType() { | |
| 98 | return descriptor.getPropertyType(); | |
| 99 | } | |
| 27 | private AttributeDescriptor descriptor; | |
| 28 | private DecimalFormat[] parsers; | |
| 29 | private int targetType; | |
| 30 | public static final int SHORT_TARGET = 1; | |
| 31 | public static final int INTEGER_TARGET = 2; | |
| 32 | public static final int LONG_TARGET = 3; | |
| 33 | public static final int FLOAT_TARGET = 4; | |
| 34 | public static final int DOUBLE_TARGET = 5; | |
| 35 | ||
| 36 | /** | |
| 37 | * Construct the converter. | |
| 38 | * | |
| 39 | * @param patterns | |
| 40 | * The pattern used to parse the string representation of the | |
| 41 | * number to a {@link Number} object. | |
| 42 | * @param descriptor | |
| 43 | * The {@link AttributeDescriptor} which describes the attribute | |
| 44 | * on the model bean. | |
| 45 | * @since 1.0 | |
| 46 | */ | |
| 47 | public StringToNumberConverter(String[] patterns, AttributeDescriptor descriptor) { | |
| 48 | this.descriptor = descriptor; | |
| 49 | parsers = new DecimalFormat[patterns.length]; | |
| 50 | ||
| 51 | for (int i = 0; i < patterns.length; i++) { | |
| 52 | parsers[i] = new DecimalFormat(patterns[i]); | |
| 53 | } | |
| 54 | ||
| 55 | if (this.descriptor.getPropertyType() == Short.class) { | |
| 56 | targetType = SHORT_TARGET; | |
| 57 | } else if (this.descriptor.getPropertyType() == Double.class) { | |
| 58 | targetType = INTEGER_TARGET; | |
| 59 | } else if (this.descriptor.getPropertyType() == Long.class) { | |
| 60 | targetType = LONG_TARGET; | |
| 61 | } else if (this.descriptor.getPropertyType() == Float.class) { | |
| 62 | targetType = FLOAT_TARGET; | |
| 63 | } else if (this.descriptor.getPropertyType() == Double.class) { | |
| 64 | targetType = DOUBLE_TARGET; | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | public Object convert(Object fromObject) { | |
| 69 | if (fromObject != null && fromObject.toString().trim().length() > 0) { | |
| 70 | String s = fromObject.toString(); | |
| 71 | Number rv = new Double(0.0); | |
| 72 | ||
| 73 | for (int i = 0; i < parsers.length; i++) { | |
| 74 | try { | |
| 75 | rv = parsers[i].parse(s); | |
| 76 | } catch (Exception e) { | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | if (targetType == SHORT_TARGET) { | |
| 81 | return new Short(rv.shortValue()); | |
| 82 | } else if (targetType == INTEGER_TARGET) { | |
| 83 | return new Integer(rv.intValue()); | |
| 84 | } else if (targetType == LONG_TARGET) { | |
| 85 | return new Long(rv.longValue()); | |
| 86 | } else if (targetType == FLOAT_TARGET) { | |
| 87 | return new Float(rv.floatValue()); | |
| 88 | } else if (targetType == DOUBLE_TARGET) { | |
| 89 | return new Double(rv.doubleValue()); | |
| 90 | } | |
| 91 | ||
| 92 | throw new RuntimeException("Unknown target type: " + targetType); | |
| 93 | } | |
| 94 | ||
| 95 | return null; | |
| 96 | } | |
| 97 | ||
| 98 | public Object getFromType() { | |
| 99 | return null; | |
| 100 | } | |
| 101 | ||
| 102 | public Object getToType() { | |
| 103 | return descriptor.getPropertyType(); | |
| 104 | } | |
| 100 | 105 | } |
| ... | ...@@ -19,28 +19,40 @@ | |
| 19 | 19 | import org.ufacekit.core.databinding.common.AttributeDescriptor; |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | * @version $Revision: 1.1 $ | |
| 23 | */ | |
| 22 | * Convert a {@link Date} to a {@link String}. | |
| 23 | * | |
| 24 | * @version 1.0 | |
| 25 | * @since 1.0 | |
| 26 | */ | |
| 24 | 27 | public class DateToStringConverter implements IConverter { |
| 25 | private SimpleDateFormat format; | |
| 28 | private SimpleDateFormat format; | |
| 26 | 29 | |
| 27 | public DateToStringConverter(String pattern, AttributeDescriptor descriptor) { | |
| 28 | this.format = new SimpleDateFormat(pattern); | |
| 29 | } | |
| 30 | /** | |
| 31 | * Construct the converter. | |
| 32 | * | |
| 33 | * @param pattern | |
| 34 | * The pattern that will be used to display the date. E.g. | |
| 35 | * dd-MM-yyyy | |
| 36 | * @param descriptor | |
| 37 | * @since 1.0 | |
| 38 | */ | |
| 39 | public DateToStringConverter(String pattern, AttributeDescriptor descriptor) { | |
| 40 | this.format = new SimpleDateFormat(pattern); | |
| 41 | } | |
| 30 | 42 | |
| 31 | public Object convert(Object fromObject) { | |
| 32 | if (fromObject instanceof Date) { | |
| 33 | Date value = (Date) fromObject; | |
| 34 | return format.format(value); | |
| 35 | } | |
| 36 | return null; | |
| 37 | } | |
| 43 | public Object convert(Object fromObject) { | |
| 44 | if (fromObject instanceof Date) { | |
| 45 | Date value = (Date) fromObject; | |
| 46 | return format.format(value); | |
| 47 | } | |
| 48 | return null; | |
| 49 | } | |
| 38 | 50 | |
| 39 | public Object getFromType() { | |
| 40 | return Date.class; | |
| 41 | } | |
| 51 | public Object getFromType() { | |
| 52 | return Date.class; | |
| 53 | } | |
| 42 | 54 | |
| 43 | public Object getToType() { | |
| 44 | return String.class; | |
| 45 | } | |
| 55 | public Object getToType() { | |
| 56 | return String.class; | |
| 57 | } | |
| 46 | 58 | } |
| ... | ...@@ -13,54 +13,66 @@ | |
| 13 | 13 | package org.ufacekit.core.databinding.common.conversion.internal; |
| 14 | 14 | |
| 15 | 15 | import java.text.SimpleDateFormat; |
| 16 | import java.util.Date; | |
| 16 | 17 | |
| 17 | 18 | import org.eclipse.core.databinding.conversion.IConverter; |
| 18 | 19 | import org.ufacekit.core.databinding.common.AttributeDescriptor; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | * @version $Revision: 1.1 $ | |
| 22 | */ | |
| 22 | * Convert a {@link String} to a {@link Date}. | |
| 23 | * | |
| 24 | * @version 1.0 | |
| 25 | * @since 1.0 | |
| 26 | */ | |
| 23 | 27 | public class StringToDateConverter implements IConverter { |
| 24 | private SimpleDateFormat[] parsers; | |
| 25 | private AttributeDescriptor descriptor; | |
| 28 | private SimpleDateFormat[] parsers; | |
| 29 | private AttributeDescriptor descriptor; | |
| 26 | 30 | |
| 27 | public StringToDateConverter(String[] patterns, AttributeDescriptor descriptor) { | |
| 28 | this.descriptor = descriptor; | |
| 29 | parsers = new SimpleDateFormat[patterns.length]; | |
| 30 | ||
| 31 | for (int i = 0; i < patterns.length; i++) { | |
| 32 | parsers[i] = new SimpleDateFormat(patterns[i]); | |
| 33 | } | |
| 34 | } | |
| 35 | ||
| 36 | public Object convert(Object fromObject) { | |
| 37 | if (fromObject != null && fromObject.toString().trim().length() > 0) { | |
| 38 | String s = fromObject.toString(); | |
| 39 | Object rv = null; | |
| 40 | ||
| 41 | for (int i = 0; i < parsers.length; i++) { | |
| 42 | try { | |
| 43 | rv = parsers[i].parse(s); | |
| 44 | } | |
| 45 | catch (Exception e) { | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | if (rv == null) { | |
| 50 | throw new RuntimeException("Could not parse date!"); | |
| 51 | } | |
| 52 | ||
| 53 | return rv; | |
| 54 | } | |
| 55 | ||
| 56 | return null; | |
| 57 | } | |
| 58 | ||
| 59 | public Object getFromType() { | |
| 60 | return String.class; | |
| 61 | } | |
| 62 | ||
| 63 | public Object getToType() { | |
| 64 | return descriptor.getPropertyType(); | |
| 65 | } | |
| 31 | /** | |
| 32 | * Construct the converter. | |
| 33 | * | |
| 34 | * @param patterns | |
| 35 | * The pattern used to parse the string representation of the | |
| 36 | * date to a {@link Date} object. | |
| 37 | * @param descriptor | |
| 38 | * @since 1.0 | |
| 39 | */ | |
| 40 | public StringToDateConverter(String[] patterns, AttributeDescriptor descriptor) { | |
| 41 | this.descriptor = descriptor; | |
| 42 | parsers = new SimpleDateFormat[patterns.length]; | |
| 43 | ||
| 44 | for (int i = 0; i < patterns.length; i++) { | |
| 45 | parsers[i] = new SimpleDateFormat(patterns[i]); | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | public Object convert(Object fromObject) { | |
| 50 | if (fromObject != null && fromObject.toString().trim().length() > 0) { | |
| 51 | String s = fromObject.toString(); | |
| 52 | Object rv = null; | |
| 53 | ||
| 54 | for (int i = 0; i < parsers.length; i++) { | |
| 55 | try { | |
| 56 | rv = parsers[i].parse(s); | |
| 57 | } catch (Exception e) { | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | if (rv == null) { | |
| 62 | throw new RuntimeException("Could not parse date!"); | |
| 63 | } | |
| 64 | ||
| 65 | return rv; | |
| 66 | } | |
| 67 | ||
| 68 | return null; | |
| 69 | } | |
| 70 | ||
| 71 | public Object getFromType() { | |
| 72 | return String.class; | |
| 73 | } | |
| 74 | ||
| 75 | public Object getToType() { | |
| 76 | return descriptor.getPropertyType(); | |
| 77 | } | |
| 66 | 78 | } |
| ... | ...@@ -12,14 +12,35 @@ | |
| 12 | 12 | import org.ufacekit.core.databinding.common.conversion.Converter; |
| 13 | 13 | import org.ufacekit.core.databinding.common.strategy.UpdateValueStrategy; |
| 14 | 14 | |
| 15 | /** | |
| 16 | * Default implementation of an {@link UpdateValueStrategy}. | |
| 17 | * | |
| 18 | * @version 1.0 | |
| 19 | * @since 1.0 | |
| 20 | */ | |
| 15 | 21 | public class DefaultUpdateValueStrategy implements UpdateValueStrategy { |
| 16 | 22 | private org.eclipse.core.databinding.UpdateValueStrategy modelToTarget = null; |
| 17 | 23 | private org.eclipse.core.databinding.UpdateValueStrategy targetToModel = null; |
| 18 | 24 | |
| 25 | /** | |
| 26 | * Empty constructor. Will create an update strategy backed by a default | |
| 27 | * model-to-target and target-to-model | |
| 28 | * {@link org.eclipse.core.databinding.UpdateValueStrategy}. | |
| 29 | * | |
| 30 | * @since 1.0 | |
| 31 | */ | |
| 19 | 32 | public DefaultUpdateValueStrategy() { |
| 20 | 33 | this(new org.eclipse.core.databinding.UpdateValueStrategy(), new org.eclipse.core.databinding.UpdateValueStrategy()); |
| 21 | 34 | } |
| 22 | 35 | |
| 36 | /** | |
| 37 | * Alternate constructor which will wrap the provided model-to-target and | |
| 38 | * target-to-model {@link org.eclipse.core.databinding.UpdateValueStrategy}. | |
| 39 | * | |
| 40 | * @param modelToTarget | |
| 41 | * @param targetToModel | |
| 42 | * @since 1.0 | |
| 43 | */ | |
| 23 | 44 | public DefaultUpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy modelToTarget, |
| 24 | 45 | org.eclipse.core.databinding.UpdateValueStrategy targetToModel) { |
| 25 | 46 | this.modelToTarget = modelToTarget; |
| ... | ...@@ -10,12 +10,38 @@ | |
| 10 | 10 | *******************************************************************************/ |
| 11 | 11 | package org.ufacekit.core.databinding.common.conversion; |
| 12 | 12 | |
| 13 | import java.text.SimpleDateFormat; | |
| 14 | import java.util.Date; | |
| 15 | ||
| 13 | 16 | import org.ufacekit.core.databinding.common.AttributeDescriptor; |
| 14 | 17 | import org.ufacekit.core.databinding.common.conversion.impl.DefaultConverter; |
| 15 | 18 | import org.ufacekit.core.databinding.common.conversion.internal.StringToDateConverter; |
| 16 | 19 | |
| 20 | /** | |
| 21 | * Converts a {@link Date} to a {@link String} and vice versa. | |
| 22 | * | |
| 23 | * @version 1.0 | |
| 24 | * @since 1.0 | |
| 25 | */ | |
| 17 | 26 | public class DateToStringConverter extends DefaultConverter { |
| 18 | public DateToStringConverter(String[] inputPatterns, String displayPattern, AttributeDescriptor att) { | |
| 19 | super(new org.ufacekit.core.databinding.common.conversion.internal.DateToStringConverter(displayPattern, att), new StringToDateConverter(inputPatterns, att)); | |
| 20 | } | |
| 27 | /** | |
| 28 | * Create a converter that will convert a {@link Date} to a {@link String} | |
| 29 | * and vice versa, using the provided input and display patterns. A | |
| 30 | * {@link SimpleDateFormat} is used for the conversion. | |
| 31 | * | |
| 32 | * @see SimpleDateFormat | |
| 33 | * @param inputPatterns | |
| 34 | * List of possible patterns that will be applied on the input. | |
| 35 | * E.g. April 21st 1978 can be entered as 21/04/1978 (dd/MM/yyyy) | |
| 36 | * or 21-04-1978 (dd-MM-yyyy) or ... | |
| 37 | * @param displayPattern | |
| 38 | * The single pattern that will be used to display the date. E.g. | |
| 39 | * dd-MM-yyyy | |
| 40 | * @param att | |
| 41 | * @since 1.0 | |
| 42 | */ | |
| 43 | public DateToStringConverter(String[] inputPatterns, String displayPattern, AttributeDescriptor att) { | |
| 44 | super(new org.ufacekit.core.databinding.common.conversion.internal.DateToStringConverter(displayPattern, att), new StringToDateConverter( | |
| 45 | inputPatterns, att)); | |
| 46 | } | |
| 21 | 47 | } |