View | Details | Raw Unified | Return to bug 197798
Collapse All | Expand All

(-)src/org/eclipse/core/internal/databinding/messages.properties (+2 lines)
Lines 34-43 Link Here
34
Validate_RangeStart=Please enter a number between
34
Validate_RangeStart=Please enter a number between
35
Validate_Like=Please enter a number like
35
Validate_Like=Please enter a number like
36
Validate_Number_Examples= 1.234, 256E-15, 42
36
Validate_Number_Examples= 1.234, 256E-15, 42
37
Validate_TimestampHelp=Not a valid timestamp. Should be in the format YYYY-MM-DD hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds.
37
38
38
IndexOutOfRange=Index out of Range.
39
IndexOutOfRange=Index out of Range.
39
MultipleProblems=Multiple Problems.
40
MultipleProblems=Multiple Problems.
40
41
42
DateFormat_Timestamp_WithoutNanos=yyyy-MM-dd HH:mm:ss.
41
DateFormat_DateTime=dd.MM.yyyy HH:mm:ss.SSS Z
43
DateFormat_DateTime=dd.MM.yyyy HH:mm:ss.SSS Z
42
DateFormat_Time=HH:mm:ss.SSS
44
DateFormat_Time=HH:mm:ss.SSS
43
45
(-)src/org/eclipse/core/databinding/UpdateStrategy.java (+3 lines)
Lines 200-205 Link Here
200
			converterMap.put(new Pair("java.math.BigInteger", "java.lang.String"), NumberToStringConverter.fromBigInteger(integerFormat));  //$NON-NLS-1$//$NON-NLS-2$
200
			converterMap.put(new Pair("java.math.BigInteger", "java.lang.String"), NumberToStringConverter.fromBigInteger(integerFormat));  //$NON-NLS-1$//$NON-NLS-2$
201
			converterMap.put(new Pair("java.lang.Byte", "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, false));   //$NON-NLS-1$//$NON-NLS-2$
201
			converterMap.put(new Pair("java.lang.Byte", "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, false));   //$NON-NLS-1$//$NON-NLS-2$
202
			converterMap.put(new Pair("java.lang.Short", "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, false));  //$NON-NLS-1$//$NON-NLS-2$
202
			converterMap.put(new Pair("java.lang.Short", "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, false));  //$NON-NLS-1$//$NON-NLS-2$
203
204
			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$
205
			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$
203
			
206
			
204
			converterMap
207
			converterMap
205
					.put(
208
					.put(
(-)src/org/eclipse/core/databinding/UpdateValueStrategy.java (+7 lines)
Lines 11-16 Link Here
11
11
12
package org.eclipse.core.databinding;
12
package org.eclipse.core.databinding;
13
13
14
import java.sql.Timestamp;
14
import java.util.Date;
15
import java.util.Date;
15
import java.util.HashMap;
16
import java.util.HashMap;
16
17
Lines 30-35 Link Here
30
import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter;
31
import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter;
31
import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter;
32
import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter;
32
import org.eclipse.core.internal.databinding.conversion.StringToDateConverter;
33
import org.eclipse.core.internal.databinding.conversion.StringToDateConverter;
34
import org.eclipse.core.internal.databinding.conversion.StringToTimestampConverter;
33
import org.eclipse.core.internal.databinding.validation.NumberFormatConverter;
35
import org.eclipse.core.internal.databinding.validation.NumberFormatConverter;
34
import org.eclipse.core.internal.databinding.validation.NumberToByteValidator;
36
import org.eclipse.core.internal.databinding.validation.NumberToByteValidator;
35
import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator;
37
import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator;
Lines 46-51 Link Here
46
import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator;
48
import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator;
47
import org.eclipse.core.internal.databinding.validation.StringToLongValidator;
49
import org.eclipse.core.internal.databinding.validation.StringToLongValidator;
48
import org.eclipse.core.internal.databinding.validation.StringToShortValidator;
50
import org.eclipse.core.internal.databinding.validation.StringToShortValidator;
51
import org.eclipse.core.internal.databinding.validation.StringToTimestampValidator;
49
import org.eclipse.core.runtime.IStatus;
52
import org.eclipse.core.runtime.IStatus;
50
import org.eclipse.core.runtime.Status;
53
import org.eclipse.core.runtime.Status;
51
54
Lines 304-309 Link Here
304
					} else if (Short.class.equals(toType)
307
					} else if (Short.class.equals(toType)
305
							|| Short.TYPE.equals(toType)) {
308
							|| Short.TYPE.equals(toType)) {
306
						result = new StringToShortValidator((NumberFormatConverter) converter);
309
						result = new StringToShortValidator((NumberFormatConverter) converter);
310
					} else if (Timestamp.class.equals(toType)
311
							&& converter instanceof StringToTimestampConverter) {
312
						result = new StringToTimestampValidator(
313
								(StringToTimestampConverter) converter);
307
					} else if (Date.class.equals(toType)
314
					} else if (Date.class.equals(toType)
308
							&& converter instanceof StringToDateConverter) {
315
							&& converter instanceof StringToDateConverter) {
309
						result = new StringToDateValidator(
316
						result = new StringToDateValidator(
(-)src/org/eclipse/core/internal/databinding/validation/StringToTimestampValidator.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 Matt Carter and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matt Carter - Initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.validation;
13
14
import org.eclipse.core.databinding.validation.IValidator;
15
import org.eclipse.core.databinding.validation.ValidationStatus;
16
import org.eclipse.core.internal.databinding.BindingMessages;
17
import org.eclipse.core.internal.databinding.conversion.StringToTimestampConverter;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
21
/**
22
 * @since 1.0
23
 */
24
public class StringToTimestampValidator implements IValidator {
25
26
	private final StringToTimestampConverter converter;
27
28
	/**
29
	 * @param converter
30
	 */
31
	public StringToTimestampValidator(StringToTimestampConverter converter) {
32
		this.converter = converter;
33
	}
34
35
	/*
36
	 * (non-Javadoc)
37
	 * 
38
	 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
39
	 */
40
	public IStatus validate(Object value) {
41
		Object convertedValue = converter.convert(value);
42
		//The StringToTimestampConverter returns null if it can't parse the timestamp.
43
		if (convertedValue == null) {
44
			return ValidationStatus.error(getErrorMessage());
45
		}
46
		return Status.OK_STATUS;
47
	}
48
49
	/*
50
	 * Returns the error message for an invalid date.
51
	 */
52
	protected String getErrorMessage() {
53
		return BindingMessages.getString("Validate_TimestampHelp"); //$NON-NLS-1$
54
	}
55
	
56
}
(-)src/org/eclipse/core/internal/databinding/conversion/StringToTimestampConverter.java (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 Matt Carter and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matt Carter - Initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.conversion;
13
14
import java.sql.Timestamp;
15
import java.text.ParseException;
16
import java.util.Date;
17
18
import org.eclipse.core.databinding.conversion.IConverter;
19
import org.eclipse.core.internal.databinding.BindingMessages;
20
21
import com.ibm.icu.text.DateFormat;
22
import com.ibm.icu.text.SimpleDateFormat;
23
24
/**
25
 * Convert a String to a java.sql.Timestamp.
26
 * 
27
 * Accepts JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds,
28
 * and the same format adapted for the current locale. 
29
 * 
30
 * Empty strings are converted to a null Timestamp.
31
 * 
32
 * @since 1.0
33
 */
34
public class StringToTimestampConverter implements IConverter {
35
36
	private DateFormat df = new SimpleDateFormat(
37
			BindingMessages.getString("DateFormat_Timestamp_WithoutNanos")); //$NON-NLS-1$
38
	
39
	public Object convert(Object fromObject) {
40
		if(fromObject == null) return ""; //$NON-NLS-1$
41
		String s = (String) fromObject;
42
		int p = s.lastIndexOf('.');
43
		if(p == -1) return tryNative(fromObject);
44
		int nanos;
45
		try { 
46
			nanos = Integer.parseInt(s.substring(p+1));
47
		} catch(NumberFormatException e) {
48
			return tryNative(fromObject);
49
		}
50
		Date date;
51
		try {
52
			date = df.parse(s.substring(0, p));
53
		} catch(ParseException e) {
54
			return tryNative(fromObject);
55
		}
56
		if(date == null) return tryNative(fromObject);
57
58
		Timestamp t = new Timestamp(date.getTime());
59
		t.setNanos(nanos);
60
		return t;
61
	}
62
63
	private Object tryNative(Object fromObject) {
64
		try {
65
			return Timestamp.valueOf((String) fromObject);
66
		} catch(IllegalArgumentException e) {
67
			return null;
68
		} catch(Exception e) {
69
			return null;
70
		}
71
	}
72
	public Object getFromType() {
73
		return String.class;
74
	}
75
76
	public Object getToType() {
77
		return Timestamp.class;
78
	}
79
80
}
(-)src/org/eclipse/core/internal/databinding/conversion/TimestampToStringConverter.java (+60 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 Matt Carter and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matt Carter - Initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.conversion;
13
14
import java.sql.Timestamp;
15
16
import org.eclipse.core.databinding.conversion.IConverter;
17
import org.eclipse.core.internal.databinding.BindingMessages;
18
19
import com.ibm.icu.text.DateFormat;
20
import com.ibm.icu.text.SimpleDateFormat;
21
22
/**
23
 * Convert a java.sql.Timestamp to a String using the current locale. 
24
 * 
25
 * Converts to a String in JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, 
26
 * where ffffffffff indicates nanoseconds, with delimiters adapted for the current locale. 
27
 * 
28
 * Null timestamp values are converted to an empty string.
29
 * 
30
 * @since 1.0
31
 */
32
public class TimestampToStringConverter implements IConverter {
33
34
	private DateFormat df = new SimpleDateFormat(
35
			BindingMessages.getString("DateFormat_Timestamp_WithoutNanos")); //$NON-NLS-1$
36
	
37
	public Object convert(Object fromObject) {
38
		if(fromObject == null) return ""; //$NON-NLS-1$
39
		Timestamp t = (Timestamp) fromObject;
40
		return df.format(t)+toLZ(t.getNanos(), 9);
41
	}
42
43
	public Object getFromType() {
44
		return java.sql.Timestamp.class;
45
	}
46
47
	public Object getToType() {
48
		return String.class;
49
	}
50
51
	private static String toLZ(int i, int len)
52
	{
53
		// Converts integer to left-zero padded string, len chars long.
54
		String s = Integer.toString(i);
55
		if (s.length() > len) return s.substring(0,len);
56
		else if ( s.length() < len ) // pad on left with zeros
57
			return "000000000000000000000000000".substring(0, len - s.length ()) + s; //$NON-NLS-1$
58
		else return s;
59
	}
60
}

Return to bug 197798