### Eclipse Workspace Patch 1.0 #P org.eclipse.core.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.6 diff -u -r1.6 messages.properties --- src/org/eclipse/core/internal/databinding/messages.properties 7 Apr 2007 16:48:50 -0000 1.6 +++ src/org/eclipse/core/internal/databinding/messages.properties 25 Jul 2007 16:00:41 -0000 @@ -34,10 +34,12 @@ Validate_RangeStart=Please enter a number between Validate_Like=Please enter a number like Validate_Number_Examples= 1.234, 256E-15, 42 +Validate_TimestampHelp=Not a valid timestamp. Should be in the format YYYY-MM-DD hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds. IndexOutOfRange=Index out of Range. MultipleProblems=Multiple Problems. +DateFormat_Timestamp_WithoutNanos=yyyy-MM-dd HH:mm:ss. DateFormat_DateTime=dd.MM.yyyy HH:mm:ss.SSS Z DateFormat_Time=HH:mm:ss.SSS 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.7 diff -u -r1.7 UpdateStrategy.java --- src/org/eclipse/core/databinding/UpdateStrategy.java 29 Apr 2007 04:12:55 -0000 1.7 +++ src/org/eclipse/core/databinding/UpdateStrategy.java 25 Jul 2007 16:00:41 -0000 @@ -200,6 +200,9 @@ converterMap.put(new Pair("java.math.BigInteger", "java.lang.String"), NumberToStringConverter.fromBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$ converterMap.put(new Pair("java.lang.Byte", "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ converterMap.put(new Pair("java.lang.Short", "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ + + converterMap.put(new Pair("java.sql.Timestamp", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.TimestampToStringConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + converterMap.put(new Pair("java.lang.String", "java.sql.Timestamp"), "org.eclipse.core.internal.databinding.conversion.StringToTimestampConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ converterMap .put( Index: src/org/eclipse/core/databinding/UpdateValueStrategy.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.core.databinding/src/org/eclipse/core/databinding/UpdateValueStrategy.java,v retrieving revision 1.8 diff -u -r1.8 UpdateValueStrategy.java --- src/org/eclipse/core/databinding/UpdateValueStrategy.java 29 Apr 2007 04:12:55 -0000 1.8 +++ src/org/eclipse/core/databinding/UpdateValueStrategy.java 25 Jul 2007 16:00:41 -0000 @@ -11,6 +11,7 @@ package org.eclipse.core.databinding; +import java.sql.Timestamp; import java.util.Date; import java.util.HashMap; @@ -30,6 +31,7 @@ import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter; import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter; import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; +import org.eclipse.core.internal.databinding.conversion.StringToTimestampConverter; import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; import org.eclipse.core.internal.databinding.validation.NumberToByteValidator; import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator; @@ -46,6 +48,7 @@ import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator; import org.eclipse.core.internal.databinding.validation.StringToLongValidator; import org.eclipse.core.internal.databinding.validation.StringToShortValidator; +import org.eclipse.core.internal.databinding.validation.StringToTimestampValidator; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -304,6 +307,10 @@ } else if (Short.class.equals(toType) || Short.TYPE.equals(toType)) { result = new StringToShortValidator((NumberFormatConverter) converter); + } else if (Timestamp.class.equals(toType) + && converter instanceof StringToTimestampConverter) { + result = new StringToTimestampValidator( + (StringToTimestampConverter) converter); } else if (Date.class.equals(toType) && converter instanceof StringToDateConverter) { result = new StringToDateValidator( Index: src/org/eclipse/core/internal/databinding/validation/StringToTimestampValidator.java =================================================================== RCS file: src/org/eclipse/core/internal/databinding/validation/StringToTimestampValidator.java diff -N src/org/eclipse/core/internal/databinding/validation/StringToTimestampValidator.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/internal/databinding/validation/StringToTimestampValidator.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2007 Matt Carter 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: + * Matt Carter - Initial API and implementation + ******************************************************************************/ + +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.internal.databinding.conversion.StringToTimestampConverter; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + +/** + * @since 1.0 + */ +public class StringToTimestampValidator implements IValidator { + + private final StringToTimestampConverter converter; + + /** + * @param converter + */ + public StringToTimestampValidator(StringToTimestampConverter converter) { + this.converter = converter; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object) + */ + public IStatus validate(Object value) { + Object convertedValue = converter.convert(value); + //The StringToTimestampConverter returns null if it can't parse the timestamp. + if (convertedValue == null) { + return ValidationStatus.error(getErrorMessage()); + } + return Status.OK_STATUS; + } + + /* + * Returns the error message for an invalid date. + */ + protected String getErrorMessage() { + return BindingMessages.getString("Validate_TimestampHelp"); //$NON-NLS-1$ + } + +} Index: src/org/eclipse/core/internal/databinding/conversion/StringToTimestampConverter.java =================================================================== RCS file: src/org/eclipse/core/internal/databinding/conversion/StringToTimestampConverter.java diff -N src/org/eclipse/core/internal/databinding/conversion/StringToTimestampConverter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/internal/databinding/conversion/StringToTimestampConverter.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2007 Matt Carter 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: + * Matt Carter - Initial API and implementation + ******************************************************************************/ + +package org.eclipse.core.internal.databinding.conversion; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.util.Date; + +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.internal.databinding.BindingMessages; + +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.SimpleDateFormat; + +/** + * Convert a String to a java.sql.Timestamp. + * + * Accepts JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds, + * and the same format adapted for the current locale. + * + * Empty strings are converted to a null Timestamp. + * + * @since 1.0 + */ +public class StringToTimestampConverter implements IConverter { + + private DateFormat df = new SimpleDateFormat( + BindingMessages.getString("DateFormat_Timestamp_WithoutNanos")); //$NON-NLS-1$ + + public Object convert(Object fromObject) { + if(fromObject == null) return ""; //$NON-NLS-1$ + String s = (String) fromObject; + int p = s.lastIndexOf('.'); + if(p == -1) return tryNative(fromObject); + int nanos; + try { + nanos = Integer.parseInt(s.substring(p+1)); + } catch(NumberFormatException e) { + return tryNative(fromObject); + } + Date date; + try { + date = df.parse(s.substring(0, p)); + } catch(ParseException e) { + return tryNative(fromObject); + } + if(date == null) return tryNative(fromObject); + + Timestamp t = new Timestamp(date.getTime()); + t.setNanos(nanos); + return t; + } + + private Object tryNative(Object fromObject) { + try { + return Timestamp.valueOf((String) fromObject); + } catch(IllegalArgumentException e) { + return null; + } catch(Exception e) { + return null; + } + } + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Timestamp.class; + } + +} Index: src/org/eclipse/core/internal/databinding/conversion/TimestampToStringConverter.java =================================================================== RCS file: src/org/eclipse/core/internal/databinding/conversion/TimestampToStringConverter.java diff -N src/org/eclipse/core/internal/databinding/conversion/TimestampToStringConverter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/core/internal/databinding/conversion/TimestampToStringConverter.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2007 Matt Carter 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: + * Matt Carter - Initial API and implementation + ******************************************************************************/ + +package org.eclipse.core.internal.databinding.conversion; + +import java.sql.Timestamp; + +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.internal.databinding.BindingMessages; + +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.SimpleDateFormat; + +/** + * Convert a java.sql.Timestamp to a String using the current locale. + * + * Converts to a String in JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, + * where ffffffffff indicates nanoseconds, with delimiters adapted for the current locale. + * + * Null timestamp values are converted to an empty string. + * + * @since 1.0 + */ +public class TimestampToStringConverter implements IConverter { + + private DateFormat df = new SimpleDateFormat( + BindingMessages.getString("DateFormat_Timestamp_WithoutNanos")); //$NON-NLS-1$ + + public Object convert(Object fromObject) { + if(fromObject == null) return ""; //$NON-NLS-1$ + Timestamp t = (Timestamp) fromObject; + return df.format(t)+toLZ(t.getNanos(), 9); + } + + public Object getFromType() { + return java.sql.Timestamp.class; + } + + public Object getToType() { + return String.class; + } + + private static String toLZ(int i, int len) + { + // Converts integer to left-zero padded string, len chars long. + String s = Integer.toString(i); + if (s.length() > len) return s.substring(0,len); + else if ( s.length() < len ) // pad on left with zeros + return "000000000000000000000000000".substring(0, len - s.length ()) + s; //$NON-NLS-1$ + else return s; + } +}