diff options
Diffstat (limited to 'bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion')
12 files changed, 636 insertions, 0 deletions
diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java new file mode 100755 index 00000000000..c4851aec366 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateConversionSupport.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import com.ibm.icu.text.DateFormat; +import java.text.ParsePosition; +import com.ibm.icu.text.SimpleDateFormat; +import java.util.Date; + +/** + * Base support for date/string conversion handling according to the + * default locale or in plain long milliseconds. + * + * NOTE: parse(format(date)) will generally *not* be equal to date, since the + * string representation may not cover the sub-second range, time-only string + * representations will be counted from the beginning of the era, etc. + */ +public abstract class DateConversionSupport { + private final static int DATE_FORMAT=DateFormat.SHORT; + private final static int DEFAULT_FORMATTER_INDEX=0; + + private final static int NUM_VIRTUAL_FORMATTERS=1; + + /** + * Alternative formatters for date, time and date/time. + * Raw milliseconds are covered as a special case. + */ + // TODO: These could be shared, but would have to be synchronized. + private DateFormat[] formatters={ + new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS Z"), //$NON-NLS-1$ + new SimpleDateFormat("HH:mm:ss.SSS"), //$NON-NLS-1$ + DateFormat.getDateTimeInstance(DATE_FORMAT,DateFormat.SHORT), + DateFormat.getDateInstance(DATE_FORMAT), + DateFormat.getTimeInstance(DateFormat.SHORT), + DateFormat.getDateTimeInstance(DATE_FORMAT,DateFormat.MEDIUM), + DateFormat.getTimeInstance(DateFormat.MEDIUM) + }; + + /** + * 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. + * + * @param str A string specifying a date according to the default locale or in raw milliseconds + * @return The parsed date, or null, if no available formatter could interpret the input string + */ + protected Date parse(String str) { + for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) { + Date parsed=parse(str,formatterIdx); + if(parsed!=null) { + return parsed; + } + } + return null; + } + + protected Date parse(String str,int formatterIdx) { + if(formatterIdx>=0) { + ParsePosition pos=new ParsePosition(0); + if (str == null) { + return null; + } + Date date=formatters[formatterIdx].parse(str,pos); + if(pos.getErrorIndex()!=-1||pos.getIndex()!=str.length()) { + return null; + } + return date; + } + try { + long millisecs=Long.parseLong(str); + return new Date(millisecs); + } + catch(NumberFormatException exc) { + } + return null; + } + + /** + * Formats the given date with the default formatter according to the default locale. + * @param date a date + * @return a string representation of the given date according to the default locale + */ + protected String format(Date date) { + return format(date,DEFAULT_FORMATTER_INDEX); + } + + protected String format(Date date,int formatterIdx) { + if(formatterIdx>=0) { + return formatters[formatterIdx].format(date); + } + return String.valueOf(date.getTime()); + } + + protected int numFormatters() { + return formatters.length+NUM_VIRTUAL_FORMATTERS; + } +}
\ No newline at end of file diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateToStringConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateToStringConverter.java new file mode 100755 index 00000000000..95a3e295129 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/DateToStringConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import java.util.Date; + +import org.eclipse.core.databinding.conversion.IConverter; + + +/** + * Converts a Java.util.Date to a String using the current locale. Null date + * values are converted to an empty string. + * + * @since 1.0 + */ +public class DateToStringConverter extends DateConversionSupport implements IConverter { + public Object convert(Object source) { + if (source != null) + return format((Date)source); + return ""; //$NON-NLS-1$ + } + + public Object getFromType() { + return Date.class; + } + + public Object getToType() { + return String.class; + } +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.java new file mode 100644 index 00000000000..a582c6075f1 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.BindingException; +import org.eclipse.core.databinding.conversion.IConverter; + +/** + * TheIdentityConverter. Returns the source value (the identity function). + */ +public class IdentityConverter implements IConverter { + + private Class fromType; + + private Class toType; + + /** + * @param type + */ + public IdentityConverter(Class type) { + this.fromType = type; + this.toType = type; + } + + /** + * @param fromType + * @param toType + */ + public IdentityConverter(Class fromType, Class toType) { + this.fromType = fromType; + this.toType = toType; + } + + private Class[][] primitiveMap = new Class[][] { + { Integer.TYPE, Integer.class }, { Short.TYPE, Short.class }, + { Long.TYPE, Long.class }, { Double.TYPE, Double.class }, + { Byte.TYPE, Byte.class }, { Float.TYPE, Float.class }, + { Boolean.TYPE, Boolean.class }, }; + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + if (toType.isPrimitive()) { + if (source == null) { + throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$ + } + } + if (source != null) { + Class sourceClass = source.getClass(); + if (toType.isPrimitive() || sourceClass.isPrimitive()) { + if (sourceClass.equals(toType) + || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) { + return source; + } + throw new BindingException( + "Boxed and unboxed types do not match"); //$NON-NLS-1$ + } + if (!toType.isAssignableFrom(sourceClass)) { + throw new BindingException(sourceClass.getName() + + " is not assignable to " + toType.getName()); //$NON-NLS-1$ + } + } + return source; + } + + /** + * (Non-API) isPrimitiveTypeMatchedWithBoxed. + * + * @param sourceClass + * @param toClass + * @return true if sourceClass and toType are matched primitive/boxed types + */ + public boolean isPrimitiveTypeMatchedWithBoxed(Class sourceClass, + Class toClass) { + for (int i = 0; i < primitiveMap.length; i++) { + if (toClass.equals(primitiveMap[i][0]) + && sourceClass.equals(primitiveMap[i][1])) { + return true; + } + if (sourceClass.equals(primitiveMap[i][0]) + && toClass.equals(primitiveMap[i][1])) { + return true; + } + } + return false; + } + + public Object getFromType() { + return fromType; + } + + public Object getToType() { + return toType; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/ObjectToStringConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/ObjectToStringConverter.java new file mode 100644 index 00000000000..76b69d45c0b --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/ObjectToStringConverter.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.conversion.IConverter; + +/** + * Converts any object to a string by calling its toString() method. + */ +public class ObjectToStringConverter implements IConverter { + private final Class fromClass; + + /** + * + */ + public ObjectToStringConverter() { + this(Object.class); + } + + /** + * @param fromClass + */ + public ObjectToStringConverter(Class fromClass) { + this.fromClass = fromClass; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + if (source == null) { + return ""; //$NON-NLS-1$ + } + return source.toString(); + } + + public Object getFromType() { + return fromClass; + } + + public Object getToType() { + return String.class; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanConverter.java new file mode 100755 index 00000000000..7f67e768fa2 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +/** + * StringToBooleanConverter. + */ +public class StringToBooleanConverter extends StringToBooleanPrimitiveConverter { + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String sourceString = (String) source; + if ("".equals(sourceString.trim())) { //$NON-NLS-1$ + return null; + } + return super.convert(source); + } + + public Object getToType() { + return Boolean.class; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java new file mode 100644 index 00000000000..4ea7fef484b --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.internal.databinding.BindingMessages; + + + +/** + * StringToBooleanPrimitiveConverter. + */ +public class StringToBooleanPrimitiveConverter implements IConverter { + + /* (non-Javadoc) + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String s = (String) source; + if (s.equals(BindingMessages.getString("Yes")) || s.equals(BindingMessages.getString("yes")) || s.equals(BindingMessages.getString("true")) || s.equals(BindingMessages.getString("True"))) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + return Boolean.TRUE; + if (s.equals(BindingMessages.getString("No")) || s.equals(BindingMessages.getString("no")) || s.equals(BindingMessages.getString("false")) || s.equals(BindingMessages.getString("False"))) //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ + return Boolean.FALSE; + + throw new IllegalArgumentException(s + " is not a legal boolean value"); //$NON-NLS-1$ + } + + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Boolean.TYPE; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToByteConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToByteConverter.java new file mode 100755 index 00000000000..a0ccdf70eb7 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToByteConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +/** + * StringToByteConverter. + */ +public class StringToByteConverter extends StringToBytePrimitiveConverter { + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String sourceString = (String) source; + if ("".equals(sourceString.trim())) { //$NON-NLS-1$ + return null; + } + return super.convert(source); + } + + public Object getToType() { + return Byte.class; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBytePrimitiveConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBytePrimitiveConverter.java new file mode 100644 index 00000000000..49e68ba52b7 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBytePrimitiveConverter.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.conversion.IConverter; + + + + +/** + * StringToBytePrimitiveConverter. + */ +public class StringToBytePrimitiveConverter implements IConverter { + + /* (non-Javadoc) + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + try { + return new Byte(Byte.parseByte((String) source)); + } catch (Exception e) { + throw new IllegalArgumentException("String2Byte: " + e.getMessage() + ": " + source); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Byte.TYPE; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToCharacterConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToCharacterConverter.java new file mode 100644 index 00000000000..10065b5850e --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToCharacterConverter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.conversion.IConverter; + +/** + * StringToCharacterConverter. + */ +public class StringToCharacterConverter implements IConverter { + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String s = (String) source; + Character result; + + if (s.length() > 1) + throw new IllegalArgumentException( + "String2Character: string too long: " + s); //$NON-NLS-1$ + + try { + result = new Character(s.charAt(0)); + } catch (Exception e) { + throw new IllegalArgumentException( + "String2Character: " + e.getMessage() + ": " + s); //$NON-NLS-1$ //$NON-NLS-2$ + } + + return result; + } + + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Character.class; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToDateConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToDateConverter.java new file mode 100644 index 00000000000..47b55cee73c --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToDateConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import java.util.Date; + +import org.eclipse.core.databinding.conversion.IConverter; + + +/** + * Convert a String to a java.util.Date, respecting the current locale + * + * @since 1.0 + */ +public class StringToDateConverter extends DateConversionSupport implements IConverter { + public Object convert(Object source) { + return parse(source.toString()); + } + + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Date.class; + } +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortConverter.java new file mode 100755 index 00000000000..11a18df0e2c --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +/** + * ConvertString2Long. + */ +public class StringToShortConverter extends StringToShortPrimitiveConverter { + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String sourceString = (String) source; + if ("".equals(sourceString.trim())) { //$NON-NLS-1$ + return null; + } + return super.convert(source); + } + + public Object getToType() { + return Short.class; + } + +} diff --git a/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortPrimitiveConverter.java b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortPrimitiveConverter.java new file mode 100644 index 00000000000..b3ed66a6305 --- /dev/null +++ b/bundles/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToShortPrimitiveConverter.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2005 db4objects Inc. http://www.db4o.com + * + * 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: + * db4objects - Initial API and implementation + */ +package org.eclipse.core.internal.databinding.conversion; + +import org.eclipse.core.databinding.conversion.IConverter; + + + + +/** + * StringToShortPrimitiveConverter. + */ +public class StringToShortPrimitiveConverter implements IConverter { + + /* (non-Javadoc) + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + try { + return new Short(Short.parseShort((String) source)); + } catch (Exception e) { + throw new IllegalArgumentException("String2Short: " + e.getMessage() + ": " + source); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + public Object getFromType() { + return String.class; + } + + public Object getToType() { + return Short.TYPE; + } + +} |