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 82601 Details for
Bug 203492
[DataBinding] Binding builder API
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Builder Prototype
builder.patch (text/plain), 54.92 KB, created by
Brad Reynolds
on 2007-11-10 02:12:34 EST
(
hide
)
Description:
Builder Prototype
Filename:
MIME Type:
Creator:
Brad Reynolds
Created:
2007-11-10 02:12:34 EST
Size:
54.92 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.core.databinding >Index: src/org/eclipse/core/databinding/ValueBindingBuilderDelegate.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/ValueBindingBuilderDelegate.java >diff -N src/org/eclipse/core/databinding/ValueBindingBuilderDelegate.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/ValueBindingBuilderDelegate.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,79 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import org.eclipse.core.databinding.observable.value.IObservableValue; >+ >+/** >+ * Implementation of {@link IValueBindingBuilderDelegate} that determines >+ * converters and validators from an {@link IStrategyDefaultsProvider}. >+ */ >+public class ValueBindingBuilderDelegate implements >+ IValueBindingBuilderDelegate { >+ private IStrategyDefaultsProvider defaultsProvider; >+ >+ /** >+ * Constructs a new instance with the provided <code>defaultsProvider</code>. >+ * >+ * @param defaultsProvider >+ */ >+ public ValueBindingBuilderDelegate(IStrategyDefaultsProvider defaultsProvider) { >+ if (defaultsProvider == null) { >+ throw new IllegalArgumentException( >+ "Parameter 'defaultsProvider' was null."); //$NON-NLS-1$ >+ } >+ >+ this.defaultsProvider = defaultsProvider; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.core.databinding.IValueBindingBuilderDelegate#prepare(org.eclipse.core.databinding.observable.value.IObservableValue, >+ * org.eclipse.core.databinding.observable.value.IObservableValue, >+ * org.eclipse.core.databinding.UpdateValueStrategy, >+ * org.eclipse.core.databinding.UpdateValueStrategy) >+ */ >+ public void prepare(IObservableValue source, IObservableValue destination, >+ UpdateValueStrategy sourceToModelStrategy, >+ UpdateValueStrategy destinationToTargetStrategy) { >+ >+ Object sourceType = source.getValueType(); >+ Object destinationType = destination.getValueType(); >+ >+ if (sourceType != null && destinationType != null) { >+ StrategyDefaults defaults = defaultsProvider.getDefaults(sourceType, destinationType); >+ sourceToModelStrategy.setConverter(defaults.getConverter()); >+ sourceToModelStrategy.setAfterGetValidator(defaults.getValidator()); >+ >+ defaults = defaultsProvider.getDefaults(destinationType, sourceType); >+ destinationToTargetStrategy.setConverter(defaults.getConverter()); >+ destinationToTargetStrategy.setAfterGetValidator(defaults.getValidator()); >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.core.databinding.IValueBindingBuilderDelegate#createDestinationToSourceStrategy(int, org.eclipse.core.databinding.observable.value.IObservableValue, org.eclipse.core.databinding.observable.value.IObservableValue) >+ */ >+ public UpdateValueStrategy createDestinationToSourceStrategy(int policy, >+ IObservableValue source, IObservableValue destination) { >+ return new UpdateValueStrategy(policy); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.core.databinding.IValueBindingBuilderDelegate#createSourceToDestinationStrategy(int, org.eclipse.core.databinding.observable.value.IObservableValue, org.eclipse.core.databinding.observable.value.IObservableValue) >+ */ >+ public UpdateValueStrategy createSourceToDestinationStrategy(int policy, >+ IObservableValue source, IObservableValue destination) { >+ return new UpdateValueStrategy(policy); >+ } >+} >Index: src/org/eclipse/core/databinding/IStrategyDefaultsProvider.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/IStrategyDefaultsProvider.java >diff -N src/org/eclipse/core/databinding/IStrategyDefaultsProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/IStrategyDefaultsProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,30 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import org.eclipse.core.databinding.conversion.IConverter; >+import org.eclipse.core.databinding.validation.IValidator; >+ >+/** >+ * Provider of defaults (e.g. {@link IConverter converters} and >+ * {@link IValidator validators}) to the binding process. >+ */ >+public interface IStrategyDefaultsProvider { >+ /** >+ * Provides a default converter and validator for the provides types. >+ * >+ * @param sourceType >+ * @param destinationType >+ * @return defaults, <code>null</code> if none found >+ */ >+ public StrategyDefaults getDefaults(Object sourceType, Object destinationType); >+} >Index: src/org/eclipse/core/databinding/IValueBindingBuilderDelegate.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/IValueBindingBuilderDelegate.java >diff -N src/org/eclipse/core/databinding/IValueBindingBuilderDelegate.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/IValueBindingBuilderDelegate.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,58 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import org.eclipse.core.databinding.observable.value.IObservableValue; >+ >+/** >+ * A delegate for a {@link ValueBindingBuilder} to externalize the setup of the >+ * binding. >+ */ >+public interface IValueBindingBuilderDelegate { >+ /** >+ * Creates the update strategy to be used when updating from source to >+ * destination. >+ * >+ * @param policy >+ * >+ * @param source >+ * @param destination >+ * @return strategy >+ */ >+ public UpdateValueStrategy createSourceToDestinationStrategy(int policy, >+ IObservableValue source, IObservableValue destination); >+ >+ /** >+ * Creates the update strategy to be used when updating from destination to >+ * source. >+ * >+ * @param policy >+ * @param source >+ * @param destination >+ * @return strategy >+ */ >+ public UpdateValueStrategy createDestinationToSourceStrategy(int policy, >+ IObservableValue source, IObservableValue destination); >+ >+ /** >+ * Invoked for the customization of the provided strategies. This allows for >+ * the configuring of converters and validators in the strategies. >+ * >+ * @param source >+ * @param destination >+ * @param sourceToDestinationStrategy >+ * @param destinationToSourceStrategy >+ */ >+ public void prepare(IObservableValue source, IObservableValue destination, >+ UpdateValueStrategy sourceToDestinationStrategy, >+ UpdateValueStrategy destinationToSourceStrategy); >+} >Index: src/org/eclipse/core/databinding/StrategyDefaults.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/StrategyDefaults.java >diff -N src/org/eclipse/core/databinding/StrategyDefaults.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/StrategyDefaults.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,59 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import org.eclipse.core.databinding.conversion.IConverter; >+import org.eclipse.core.databinding.validation.IValidator; >+ >+/** >+ * >+ */ >+public class StrategyDefaults { >+ private IConverter converter; >+ private IValidator validator; >+ >+ public StrategyDefaults() { >+ } >+ >+ public StrategyDefaults(IConverter converter, IValidator validator) { >+ this.setConverter(converter); >+ this.setValidator(validator); >+ } >+ >+ /** >+ * @param converter The converter to set. >+ */ >+ public void setConverter(IConverter converter) { >+ this.converter = converter; >+ } >+ >+ /** >+ * @return Returns the converter. >+ */ >+ public IConverter getConverter() { >+ return converter; >+ } >+ >+ /** >+ * @param validator The validator to set. >+ */ >+ public void setValidator(IValidator validator) { >+ this.validator = validator; >+ } >+ >+ /** >+ * @return Returns the validator. >+ */ >+ public IValidator getValidator() { >+ return validator; >+ } >+} >Index: src/org/eclipse/core/databinding/ValueBindingBuilder.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/ValueBindingBuilder.java >diff -N src/org/eclipse/core/databinding/ValueBindingBuilder.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/ValueBindingBuilder.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,228 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import org.eclipse.core.databinding.observable.value.IObservableValue; >+import org.eclipse.core.runtime.Assert; >+ >+/** >+ * A builder to streamline the process of constructing {@link Binding bindings} >+ * between {@link IObservableValue observable values}. >+ */ >+public class ValueBindingBuilder { >+ private int sourceToDestinationPolicy = -1; >+ >+ private int destinationToSourcePolicy = -1; >+ >+ private DataBindingContext dbc; >+ >+ private IValueBindingBuilderDelegate delegate; >+ >+ /** >+ * Constructs a new instance. Public instantiation is not allowed. >+ * >+ * @see #withDefaults() >+ */ >+ private ValueBindingBuilder() { >+ } >+ >+ /** >+ * Sets the {@link UpdateValueStrategy#getUpdatePolicy() update policy} to >+ * be used for source to destination updates. >+ * >+ * @param updatePolicy >+ * @return this builder for chaining >+ */ >+ public ValueBindingBuilder sourceToDestinationPolicy(int updatePolicy) { >+ sourceToDestinationPolicy = updatePolicy; >+ return this; >+ } >+ >+ public int getSourceToDestinationPolicy() { >+ return sourceToDestinationPolicy; >+ } >+ >+ /** >+ * Sets the {@link UpdateValueStrategy#getUpdatePolicy() update policy} to >+ * be used for destination to source updates. >+ * >+ * @param updatePolicy >+ * @return this builder for chaining >+ */ >+ public ValueBindingBuilder destinationToSourcePolicy(int updatePolicy) { >+ destinationToSourcePolicy = updatePolicy; >+ return this; >+ } >+ >+ public int getDestinationToSourcePolicy() { >+ return destinationToSourcePolicy; >+ } >+ >+ /** >+ * Sets the {@link DataBindingContext} to be used when creating bindings. >+ * >+ * @param dbc >+ * @return this builder for chaining >+ */ >+ public ValueBindingBuilder dbc(DataBindingContext dbc) { >+ this.dbc = dbc; >+ return this; >+ } >+ >+ public DataBindingContext getDbc() { >+ return dbc; >+ } >+ >+ /** >+ * Creates a copy of the builder inheriting all state. >+ * >+ * @return new builder >+ */ >+ public ValueBindingBuilder copy() { >+ ValueBindingBuilder builder = new ValueBindingBuilder(); >+ builder.sourceToDestinationPolicy = sourceToDestinationPolicy; >+ builder.destinationToSourcePolicy = destinationToSourcePolicy; >+ builder.dbc = dbc; >+ builder.delegate = delegate; >+ >+ return builder; >+ } >+ >+ /** >+ * Sets the delegate to prepare strategies. >+ * >+ * @param delegate >+ * @return this builder for chaining >+ */ >+ public ValueBindingBuilder delegate(IValueBindingBuilderDelegate delegate) { >+ this.delegate = delegate; >+ return this; >+ } >+ >+ public IValueBindingBuilderDelegate getDelegate() { >+ return delegate; >+ } >+ >+ /** >+ * Binds the provided <code>source</code> to the provided >+ * <code>destination</code> using the {@link DataBindingContext} in >+ * context. >+ * >+ * @param source >+ * source to bind, does not become state in the builder >+ * @param destination >+ * destination to bind, does not become state in the builder >+ * @return binding >+ */ >+ public Binding bind(IObservableValue source, IObservableValue destination) { >+ return bind(dbc, source, destination); >+ } >+ >+ /** >+ * Binds the provided <code>source</code> to the provided >+ * <code>destination</code> using the provided <code>dbc</code>. >+ * >+ * @param dbc >+ * dbc to create the binding, does not become state in the >+ * builder >+ * @param source >+ * source to bind, does not become state in the builder >+ * @param destination >+ * destination to bind, does not become state in the builder >+ * @return binding >+ */ >+ public Binding bind(DataBindingContext dbc, IObservableValue source, >+ IObservableValue destination) { >+ return bind(dbc, sourceToDestinationPolicy, destinationToSourcePolicy, >+ source, destination); >+ } >+ >+ /** >+ * Binds the provided <code>source</code> to the provided >+ * <code>destination</code> using the provided <code>dbc</code>. >+ * >+ * @param dbc >+ * creates the binding, does not become state in the builder >+ * @param sourceToDestinationPolicy >+ * policy for source to destination updates, does not become >+ * state in the builder >+ * @param destinationToSourcePolicy >+ * policy for destination to source updates, does not become >+ * state in the builder >+ * @param source >+ * source to bind, does not become state in the builder >+ * @param destination >+ * destination to bind, does not become state in the builder >+ * @return binding >+ */ >+ public Binding bind(DataBindingContext dbc, int sourceToDestinationPolicy, >+ int destinationToSourcePolicy, IObservableValue source, >+ IObservableValue destination) { >+ if (dbc == null) { >+ throw new IllegalArgumentException("Parameter 'dbc' was null."); //$NON-NLS-1$ >+ } >+ if (source == null) { >+ throw new IllegalArgumentException("Parameter 'source' was null."); //$NON-NLS-1$ >+ } >+ if (destination == null) { >+ throw new IllegalArgumentException( >+ "Parameter 'destination' was null."); //$NON-NLS-1$ >+ } >+ >+ if (delegate == null) { >+ // Default a delegate if one hasn't been set yet. >+ delegate = new ValueBindingBuilderDelegate( >+ new CoreStrategyDefaultsProvider()); >+ } >+ >+ UpdateValueStrategy sourceToDestinationStrategy = delegate >+ .createSourceToDestinationStrategy(sourceToDestinationPolicy, >+ source, destination); >+ UpdateValueStrategy destinationToSourceStrategy = delegate >+ .createSourceToDestinationStrategy(destinationToSourcePolicy, >+ source, destination); >+ >+ Assert.isNotNull(sourceToDestinationStrategy, >+ "source to destination strategy"); //$NON-NLS-1$ >+ Assert.isNotNull(destinationToSourceStrategy, >+ "destination to source strategy"); //$NON-NLS-1$ >+ >+ >+ delegate.prepare(source, destination, sourceToDestinationStrategy, >+ destinationToSourceStrategy); >+ >+ // For backwards compability. >+ sourceToDestinationStrategy.fillDefaults(source, destination); >+ destinationToSourceStrategy.fillDefaults(destination, source); >+ >+ return dbc.bindValue(source, destination, sourceToDestinationStrategy, >+ destinationToSourceStrategy); >+ } >+ >+ /** >+ * Constructs a builder with the following defaults. >+ * <ul> >+ * <li>Source to destination policy = >+ * {@link UpdateValueStrategy#POLICY_UPDATE}</li> >+ * <li>Destination to source policy = >+ * {@link UpdateValueStrategy#POLICY_UPDATE}</li> >+ * </ul> >+ * >+ * @return builder >+ */ >+ public static ValueBindingBuilder withDefaults() { >+ ValueBindingBuilder builder = new ValueBindingBuilder(); >+ builder.sourceToDestinationPolicy = builder.destinationToSourcePolicy = UpdateValueStrategy.POLICY_UPDATE; >+ >+ return builder; >+ } >+} >Index: src/org/eclipse/core/databinding/CoreStrategyDefaultsProvider.java >=================================================================== >RCS file: src/org/eclipse/core/databinding/CoreStrategyDefaultsProvider.java >diff -N src/org/eclipse/core/databinding/CoreStrategyDefaultsProvider.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/core/databinding/CoreStrategyDefaultsProvider.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,939 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.core.databinding; >+ >+import java.math.BigDecimal; >+import java.math.BigInteger; >+import java.util.Date; >+import java.util.HashMap; >+import java.util.Map; >+ >+import org.eclipse.core.databinding.conversion.IConverter; >+import org.eclipse.core.databinding.conversion.NumberToStringConverter; >+import org.eclipse.core.databinding.conversion.StringToNumberConverter; >+import org.eclipse.core.databinding.util.Policy; >+import org.eclipse.core.databinding.validation.IValidator; >+import org.eclipse.core.internal.databinding.ClassLookupSupport; >+import org.eclipse.core.internal.databinding.Pair; >+import org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter; >+import org.eclipse.core.internal.databinding.conversion.IdentityConverter; >+import org.eclipse.core.internal.databinding.conversion.IntegerToStringConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToBigDecimalConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToBigIntegerConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToByteConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToDoubleConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToFloatConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter; >+import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter; >+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.StringToDateConverter; >+import org.eclipse.core.internal.databinding.conversion.StringToShortConverter; >+import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; >+import org.eclipse.core.internal.databinding.validation.NumberToByteValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToFloatValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToIntegerValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToLongValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToShortValidator; >+import org.eclipse.core.internal.databinding.validation.NumberToUnboundedNumberValidator; >+import org.eclipse.core.internal.databinding.validation.ObjectToPrimitiveValidator; >+import org.eclipse.core.internal.databinding.validation.StringToByteValidator; >+import org.eclipse.core.internal.databinding.validation.StringToCharacterValidator; >+import org.eclipse.core.internal.databinding.validation.StringToDateValidator; >+import org.eclipse.core.internal.databinding.validation.StringToDoubleValidator; >+import org.eclipse.core.internal.databinding.validation.StringToFloatValidator; >+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.runtime.IStatus; >+import org.eclipse.core.runtime.Status; >+ >+import com.ibm.icu.text.NumberFormat; >+ >+/** >+ * @since 3.3 >+ */ >+public class CoreStrategyDefaultsProvider implements IStrategyDefaultsProvider { >+ private static ValidatorRegistry validatorRegistry = new ValidatorRegistry(); >+ >+ private static HashMap validatorsByConverter = new HashMap(); >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.core.databinding.IStrategyDefaultsProvider#getDefaults(java.lang.Object, >+ * java.lang.Object, org.eclipse.core.databinding.StrategyDefaults) >+ */ >+ public StrategyDefaults getDefaults(Object sourceType, Object destinationType) { >+ IConverter converter = createConverter(sourceType, destinationType); >+ >+ if (converter != null) { >+ IValidator validator = createValidator(converter, sourceType, >+ destinationType); >+ return new StrategyDefaults(converter, validator); >+ } >+ >+ return null; >+ } >+ >+ /** >+ * Tries to create a validator that can validate values of type fromType. >+ * Returns <code>null</code> if no validator could be created. Either >+ * toType or modelDescription can be <code>null</code>, but not both. >+ * >+ * @param converter >+ * @param fromType >+ * @param toType >+ * @return an IValidator, or <code>null</code> if unsuccessful >+ */ >+ private IValidator createValidator(IConverter converter, Object fromType, >+ Object toType) { >+ if (fromType == null || toType == null) { >+ return new IValidator() { >+ >+ public IStatus validate(Object value) { >+ return Status.OK_STATUS; >+ } >+ }; >+ } >+ >+ return findValidator(converter, fromType, toType); >+ } >+ >+ private IValidator findValidator(IConverter converter, Object fromType, >+ Object toType) { >+ IValidator result = null; >+ >+ if (String.class.equals(fromType)) { >+ result = (IValidator) validatorsByConverter.get(converter); >+ >+ if (result == null) { >+ // TODO sring based lookup >+ if (Integer.class.equals(toType) || Integer.TYPE.equals(toType)) { >+ result = new StringToIntegerValidator( >+ (NumberFormatConverter) converter); >+ } else if (Long.class.equals(toType) >+ || Long.TYPE.equals(toType)) { >+ result = new StringToLongValidator( >+ (NumberFormatConverter) converter); >+ } else if (Float.class.equals(toType) >+ || Float.TYPE.equals(toType)) { >+ result = new StringToFloatValidator( >+ (NumberFormatConverter) converter); >+ } else if (Double.class.equals(toType) >+ || Double.TYPE.equals(toType)) { >+ result = new StringToDoubleValidator( >+ (NumberFormatConverter) converter); >+ } else if (Byte.class.equals(toType) >+ || Byte.TYPE.equals(toType)) { >+ result = new StringToByteValidator( >+ (NumberFormatConverter) converter); >+ } else if (Short.class.equals(toType) >+ || Short.TYPE.equals(toType)) { >+ result = new StringToShortValidator( >+ (NumberFormatConverter) converter); >+ } else if (Character.class.equals(toType) >+ || Character.TYPE.equals(toType) >+ && converter instanceof StringToCharacterConverter) { >+ result = new StringToCharacterValidator( >+ (StringToCharacterConverter) converter); >+ } else if (Date.class.equals(toType) >+ && converter instanceof StringToDateConverter) { >+ result = new StringToDateValidator( >+ (StringToDateConverter) converter); >+ } >+ >+ if (result != null) { >+ validatorsByConverter.put(converter, result); >+ } >+ } >+ } else if (converter instanceof NumberToNumberConverter) { >+ result = (IValidator) validatorsByConverter.get(converter); >+ >+ if (result == null) { >+ if (converter instanceof NumberToByteConverter) { >+ result = new NumberToByteValidator( >+ (NumberToByteConverter) converter); >+ } else if (converter instanceof NumberToShortConverter) { >+ result = new NumberToShortValidator( >+ (NumberToShortConverter) converter); >+ } else if (converter instanceof NumberToIntegerConverter) { >+ result = new NumberToIntegerValidator( >+ (NumberToIntegerConverter) converter); >+ } else if (converter instanceof NumberToLongConverter) { >+ result = new NumberToLongValidator( >+ (NumberToLongConverter) converter); >+ } else if (converter instanceof NumberToFloatConverter) { >+ result = new NumberToFloatValidator( >+ (NumberToFloatConverter) converter); >+ } else if (converter instanceof NumberToDoubleConverter) { >+ result = new NumberToDoubleValidator( >+ (NumberToDoubleConverter) converter); >+ } else if (converter instanceof NumberToBigIntegerConverter >+ || converter instanceof NumberToBigDecimalConverter) { >+ result = new NumberToUnboundedNumberValidator( >+ (NumberToNumberConverter) converter); >+ } >+ } >+ } >+ >+ if (result == null) { >+ // TODO string based lookup >+ result = validatorRegistry.get(fromType, toType); >+ } >+ >+ return result; >+ } >+ >+ private static class ValidatorRegistry { >+ >+ private HashMap validators = new HashMap(); >+ >+ /** >+ * Adds the system-provided validators to the current validator >+ * registry. This is done automatically for the validator registry >+ * singleton. >+ */ >+ private ValidatorRegistry() { >+ // Standalone validators here... >+ associate(Integer.class, Integer.TYPE, >+ new ObjectToPrimitiveValidator(Integer.TYPE)); >+ associate(Byte.class, Byte.TYPE, new ObjectToPrimitiveValidator( >+ Byte.TYPE)); >+ associate(Short.class, Short.TYPE, new ObjectToPrimitiveValidator( >+ Short.TYPE)); >+ associate(Long.class, Long.TYPE, new ObjectToPrimitiveValidator( >+ Long.TYPE)); >+ associate(Float.class, Float.TYPE, new ObjectToPrimitiveValidator( >+ Float.TYPE)); >+ associate(Double.class, Double.TYPE, >+ new ObjectToPrimitiveValidator(Double.TYPE)); >+ associate(Boolean.class, Boolean.TYPE, >+ new ObjectToPrimitiveValidator(Boolean.TYPE)); >+ >+ associate(Object.class, Integer.TYPE, >+ new ObjectToPrimitiveValidator(Integer.TYPE)); >+ associate(Object.class, Byte.TYPE, new ObjectToPrimitiveValidator( >+ Byte.TYPE)); >+ associate(Object.class, Short.TYPE, new ObjectToPrimitiveValidator( >+ Short.TYPE)); >+ associate(Object.class, Long.TYPE, new ObjectToPrimitiveValidator( >+ Long.TYPE)); >+ associate(Object.class, Float.TYPE, new ObjectToPrimitiveValidator( >+ Float.TYPE)); >+ associate(Object.class, Double.TYPE, >+ new ObjectToPrimitiveValidator(Double.TYPE)); >+ associate(Object.class, Boolean.TYPE, >+ new ObjectToPrimitiveValidator(Boolean.TYPE)); >+ } >+ >+ /** >+ * Associate a particular validator that can validate the conversion >+ * (fromClass, toClass) >+ * >+ * @param fromClass >+ * The Class to convert from >+ * @param toClass >+ * The Class to convert to >+ * @param validator >+ * The IValidator >+ */ >+ private void associate(Object fromClass, Object toClass, >+ IValidator validator) { >+ validators.put(new Pair(fromClass, toClass), validator); >+ } >+ >+ /** >+ * Return an IValidator for a specific fromClass and toClass. >+ * >+ * @param fromClass >+ * The Class to convert from >+ * @param toClass >+ * The Class to convert to >+ * @return An appropriate IValidator >+ */ >+ private IValidator get(Object fromClass, Object toClass) { >+ IValidator result = (IValidator) validators.get(new Pair(fromClass, >+ toClass)); >+ if (result != null) >+ return result; >+ if (fromClass != null && toClass != null && fromClass == toClass) { >+ return new IValidator() { >+ public IStatus validate(Object value) { >+ return Status.OK_STATUS; >+ } >+ }; >+ } >+ return new IValidator() { >+ public IStatus validate(Object value) { >+ return Status.OK_STATUS; >+ } >+ }; >+ } >+ } >+ >+ private static final String BOOLEAN_TYPE = "java.lang.Boolean.TYPE"; //$NON-NLS-1$ >+ >+ private static final String SHORT_TYPE = "java.lang.Short.TYPE"; //$NON-NLS-1$ >+ >+ private static final String BYTE_TYPE = "java.lang.Byte.TYPE"; //$NON-NLS-1$ >+ >+ private static final String DOUBLE_TYPE = "java.lang.Double.TYPE"; //$NON-NLS-1$ >+ >+ private static final String FLOAT_TYPE = "java.lang.Float.TYPE"; //$NON-NLS-1$ >+ >+ private static final String INTEGER_TYPE = "java.lang.Integer.TYPE"; //$NON-NLS-1$ >+ >+ private static final String LONG_TYPE = "java.lang.Long.TYPE"; //$NON-NLS-1$ >+ >+ private static final String CHARACTER_TYPE = "java.lang.Character.TYPE"; //$NON-NLS-1$ >+ >+ private static Map converterMap; >+ >+ private static Class autoboxed(Class clazz) { >+ if (clazz == Float.TYPE) >+ return Float.class; >+ else if (clazz == Double.TYPE) >+ return Double.class; >+ else if (clazz == Short.TYPE) >+ return Short.class; >+ else if (clazz == Integer.TYPE) >+ return Integer.class; >+ else if (clazz == Long.TYPE) >+ return Long.class; >+ else if (clazz == Byte.TYPE) >+ return Byte.class; >+ else if (clazz == Boolean.TYPE) >+ return Boolean.class; >+ else if (clazz == Character.TYPE) >+ return Character.class; >+ return clazz; >+ } >+ >+ final protected void checkAssignable(Object toType, Object fromType, >+ String errorString) { >+ Boolean assignableFromModelToModelConverter = isAssignableFromTo( >+ fromType, toType); >+ if (assignableFromModelToModelConverter != null >+ && !assignableFromModelToModelConverter.booleanValue()) { >+ throw new BindingException(errorString >+ + " Expected: " + fromType + ", actual: " + toType); //$NON-NLS-1$//$NON-NLS-2$ >+ } >+ } >+ >+ /** >+ * Tries to create a converter that can convert from values of type >+ * fromType. Returns <code>null</code> if no converter could be created. >+ * Either toType or modelDescription can be <code>null</code>, but not >+ * both. >+ * >+ * @param fromType >+ * @param toType >+ * @return an IConverter, or <code>null</code> if unsuccessful >+ */ >+ private IConverter createConverter(Object fromType, Object toType) { >+ if (!(fromType instanceof Class) || !(toType instanceof Class)) { >+ return new DefaultConverter(fromType, toType); >+ } >+ Class toClass = (Class) toType; >+ Class originalToClass = toClass; >+ if (toClass.isPrimitive()) { >+ toClass = autoboxed(toClass); >+ } >+ Class fromClass = (Class) fromType; >+ Class originalFromClass = fromClass; >+ if (fromClass.isPrimitive()) { >+ fromClass = autoboxed(fromClass); >+ } >+ if (!((Class) toType).isPrimitive() >+ && toClass.isAssignableFrom(fromClass)) { >+ return new IdentityConverter(originalFromClass, originalToClass); >+ } >+ if (((Class) fromType).isPrimitive() && ((Class) toType).isPrimitive() >+ && fromType.equals(toType)) { >+ return new IdentityConverter(originalFromClass, originalToClass); >+ } >+ Map converterMap = getConverterMap(); >+ Class[] supertypeHierarchyFlattened = ClassLookupSupport >+ .getTypeHierarchyFlattened(fromClass); >+ for (int i = 0; i < supertypeHierarchyFlattened.length; i++) { >+ Class currentFromClass = supertypeHierarchyFlattened[i]; >+ if (currentFromClass == toType) { >+ // converting to toType is just a widening >+ return new IdentityConverter(fromClass, toClass); >+ } >+ Pair key = new Pair(getKeyForClass(fromType, currentFromClass), >+ getKeyForClass(toType, toClass)); >+ Object converterOrClassname = converterMap.get(key); >+ if (converterOrClassname instanceof IConverter) { >+ return (IConverter) converterOrClassname; >+ } else if (converterOrClassname instanceof String) { >+ String classname = (String) converterOrClassname; >+ Class converterClass; >+ try { >+ converterClass = Class.forName(classname); >+ IConverter result = (IConverter) converterClass >+ .newInstance(); >+ converterMap.put(key, result); >+ return result; >+ } catch (Exception e) { >+ Policy >+ .getLog() >+ .log( >+ new Status( >+ IStatus.ERROR, >+ Policy.JFACE_DATABINDING, >+ 0, >+ "Error while instantiating default converter", e)); //$NON-NLS-1$ >+ } >+ } >+ } >+ // Since we found no converter yet, try a "downcast" converter; >+ // the IdentityConverter will automatically check the actual types at >+ // runtime. >+ if (fromClass.isAssignableFrom(toClass)) { >+ return new IdentityConverter(originalFromClass, originalToClass); >+ } >+ return new DefaultConverter(fromType, toType); >+ } >+ >+ private static Map getConverterMap() { >+ // using string-based lookup avoids loading of too many classes >+ if (converterMap == null) { >+ // NumberFormat to be shared across converters for the formatting of >+ // integer values >+ NumberFormat integerFormat = NumberFormat.getIntegerInstance(); >+ // NumberFormat to be shared across converters for formatting non >+ // integer values >+ NumberFormat numberFormat = NumberFormat.getNumberInstance(); >+ >+ converterMap = new HashMap(); >+ // Standard and Boxed Types >+ converterMap >+ .put( >+ new Pair("java.util.Date", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.DateToStringConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Boolean"), "org.eclipse.core.internal.databinding.conversion.StringToBooleanConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Byte"), StringToByteConverter.toByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.util.Date"), "org.eclipse.core.internal.databinding.conversion.StringToDateConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Short"), StringToShortConverter.toShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Character"), StringToCharacterConverter.toCharacter(false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Integer"), StringToNumberConverter.toInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Double"), StringToNumberConverter.toDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Long"), StringToNumberConverter.toLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.lang.Float"), StringToNumberConverter.toFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.String", "java.math.BigInteger"), StringToNumberConverter.toBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.Integer", "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.Long", "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.Double", "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair("java.lang.Float", "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$ >+ 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.lang.Character", "java.lang.String"), CharacterToStringConverter.fromCharacter(false)); //$NON-NLS-1$//$NON-NLS-2$ >+ >+ converterMap >+ .put( >+ new Pair("java.lang.Object", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ >+ >+ // Integer.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", INTEGER_TYPE), StringToNumberConverter.toInteger(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(INTEGER_TYPE, "java.lang.Integer"), new IdentityConverter(Integer.TYPE, Integer.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(INTEGER_TYPE, "java.lang.Object"), new IdentityConverter(Integer.TYPE, Object.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(INTEGER_TYPE, "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, true)); //$NON-NLS-1$ >+ >+ // Byte.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", BYTE_TYPE), StringToByteConverter.toByte(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(BYTE_TYPE, "java.lang.Byte"), new IdentityConverter(Byte.TYPE, Byte.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(BYTE_TYPE, "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(BYTE_TYPE, "java.lang.Object"), new IdentityConverter(Byte.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Double.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", DOUBLE_TYPE), StringToNumberConverter.toDouble(numberFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(DOUBLE_TYPE, "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, true)); //$NON-NLS-1$ >+ >+ converterMap >+ .put( >+ new Pair(DOUBLE_TYPE, "java.lang.Double"), new IdentityConverter(Double.TYPE, Double.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(DOUBLE_TYPE, "java.lang.Object"), new IdentityConverter(Double.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Boolean.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", BOOLEAN_TYPE), "org.eclipse.core.internal.databinding.conversion.StringToBooleanPrimitiveConverter"); //$NON-NLS-1$ //$NON-NLS-2$ >+ converterMap >+ .put( >+ new Pair(BOOLEAN_TYPE, "java.lang.Boolean"), new IdentityConverter(Boolean.TYPE, Boolean.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(BOOLEAN_TYPE, "java.lang.String"), new ObjectToStringConverter(Boolean.TYPE)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(BOOLEAN_TYPE, "java.lang.Object"), new IdentityConverter(Boolean.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Float.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", FLOAT_TYPE), StringToNumberConverter.toFloat(numberFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(FLOAT_TYPE, "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(FLOAT_TYPE, "java.lang.Float"), new IdentityConverter(Float.TYPE, Float.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(FLOAT_TYPE, "java.lang.Object"), new IdentityConverter(Float.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Short.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", SHORT_TYPE), StringToShortConverter.toShort(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(SHORT_TYPE, "java.lang.Short"), new IdentityConverter(Short.TYPE, Short.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(SHORT_TYPE, "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(SHORT_TYPE, "java.lang.Object"), new IdentityConverter(Short.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Long.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", LONG_TYPE), StringToNumberConverter.toLong(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(LONG_TYPE, "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(LONG_TYPE, "java.lang.Long"), new IdentityConverter(Long.TYPE, Long.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(LONG_TYPE, "java.lang.Object"), new IdentityConverter(Long.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Character.TYPE >+ converterMap >+ .put( >+ new Pair("java.lang.String", CHARACTER_TYPE), StringToCharacterConverter.toCharacter(true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(CHARACTER_TYPE, "java.lang.Character"), new IdentityConverter(Character.TYPE, Character.class)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(CHARACTER_TYPE, "java.lang.String"), CharacterToStringConverter.fromCharacter(true)); //$NON-NLS-1$ >+ converterMap >+ .put( >+ new Pair(CHARACTER_TYPE, "java.lang.Object"), new IdentityConverter(Character.TYPE, Object.class)); //$NON-NLS-1$ >+ >+ // Miscellaneous >+ converterMap >+ .put( >+ new Pair( >+ "org.eclipse.core.runtime.IStatus", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.StatusToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ >+ >+ addNumberToByteConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToByteConverters(converterMap, numberFormat, floatClasses); >+ >+ addNumberToShortConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToShortConverters(converterMap, numberFormat, floatClasses); >+ >+ addNumberToIntegerConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToIntegerConverters(converterMap, numberFormat, >+ floatClasses); >+ >+ addNumberToLongConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToLongConverters(converterMap, numberFormat, floatClasses); >+ >+ addNumberToFloatConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToFloatConverters(converterMap, numberFormat, floatClasses); >+ >+ addNumberToDoubleConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToDoubleConverters(converterMap, numberFormat, >+ floatClasses); >+ >+ addNumberToBigIntegerConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToBigIntegerConverters(converterMap, numberFormat, >+ floatClasses); >+ >+ addNumberToBigDecimalConverters(converterMap, integerFormat, >+ integerClasses); >+ addNumberToBigDecimalConverters(converterMap, numberFormat, >+ floatClasses); >+ } >+ >+ return converterMap; >+ } >+ >+ private static final Class[] integerClasses = new Class[] { Byte.TYPE, >+ Byte.class, Short.TYPE, Short.class, Integer.TYPE, Integer.class, >+ Long.TYPE, Long.class, BigInteger.class }; >+ >+ private static final Class[] floatClasses = new Class[] { Float.TYPE, >+ Float.class, Double.TYPE, Double.class, BigDecimal.class }; >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToByteConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Byte.class) && !fromType.equals(Byte.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, BYTE_TYPE), >+ new NumberToByteConverter(numberFormat, >+ fromType, true)); >+ map >+ .put(new Pair(fromName, Byte.class.getName()), >+ new NumberToByteConverter(numberFormat, >+ fromType, false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToShortConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Short.class) && !fromType.equals(Short.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, SHORT_TYPE), >+ new NumberToShortConverter(numberFormat, >+ fromType, true)); >+ map.put(new Pair(fromName, Short.class.getName()), >+ new NumberToShortConverter(numberFormat, fromType, >+ false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToIntegerConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Integer.class) >+ && !fromType.equals(Integer.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map.put(new Pair(fromName, INTEGER_TYPE), >+ new NumberToIntegerConverter(numberFormat, fromType, >+ true)); >+ map.put(new Pair(fromName, Integer.class.getName()), >+ new NumberToIntegerConverter(numberFormat, fromType, >+ false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToLongConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Long.class) && !fromType.equals(Long.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, LONG_TYPE), >+ new NumberToLongConverter(numberFormat, >+ fromType, true)); >+ map >+ .put(new Pair(fromName, Long.class.getName()), >+ new NumberToLongConverter(numberFormat, >+ fromType, false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToFloatConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Float.class) && !fromType.equals(Float.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, FLOAT_TYPE), >+ new NumberToFloatConverter(numberFormat, >+ fromType, true)); >+ map.put(new Pair(fromName, Float.class.getName()), >+ new NumberToFloatConverter(numberFormat, fromType, >+ false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToDoubleConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(Double.class) && !fromType.equals(Double.TYPE)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map.put(new Pair(fromName, DOUBLE_TYPE), >+ new NumberToDoubleConverter(numberFormat, fromType, >+ true)); >+ map.put(new Pair(fromName, Double.class.getName()), >+ new NumberToDoubleConverter(numberFormat, fromType, >+ false)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToBigIntegerConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(BigInteger.class)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, BigInteger.class.getName()), >+ new NumberToBigIntegerConverter(numberFormat, >+ fromType)); >+ } >+ } >+ } >+ >+ /** >+ * Registers converters to boxed and unboxed types from a list of from >+ * classes. >+ * >+ * @param map >+ * @param numberFormat >+ * @param fromTypes >+ */ >+ private static void addNumberToBigDecimalConverters(Map map, >+ NumberFormat numberFormat, Class[] fromTypes) { >+ for (int i = 0; i < fromTypes.length; i++) { >+ Class fromType = fromTypes[i]; >+ if (!fromType.equals(BigDecimal.class)) { >+ String fromName = (fromType.isPrimitive()) ? getKeyForClass( >+ fromType, null) : fromType.getName(); >+ >+ map >+ .put(new Pair(fromName, BigDecimal.class.getName()), >+ new NumberToBigDecimalConverter(numberFormat, >+ fromType)); >+ } >+ } >+ } >+ >+ private static String getKeyForClass(Object originalValue, >+ Class filteredValue) { >+ if (originalValue instanceof Class) { >+ Class originalClass = (Class) originalValue; >+ if (originalClass.equals(Integer.TYPE)) { >+ return INTEGER_TYPE; >+ } else if (originalClass.equals(Byte.TYPE)) { >+ return BYTE_TYPE; >+ } else if (originalClass.equals(Boolean.TYPE)) { >+ return BOOLEAN_TYPE; >+ } else if (originalClass.equals(Double.TYPE)) { >+ return DOUBLE_TYPE; >+ } else if (originalClass.equals(Float.TYPE)) { >+ return FLOAT_TYPE; >+ } else if (originalClass.equals(Long.TYPE)) { >+ return LONG_TYPE; >+ } else if (originalClass.equals(Short.TYPE)) { >+ return SHORT_TYPE; >+ } >+ } >+ return filteredValue.getName(); >+ } >+ >+ /** >+ * @param fromType >+ * @param toType >+ * @return whether fromType is assignable to toType >+ */ >+ protected Boolean isAssignableFromTo(Object fromType, Object toType) { >+ if (fromType instanceof Class && toType instanceof Class) { >+ Class toClass = (Class) toType; >+ if (toClass.isPrimitive()) { >+ toClass = autoboxed(toClass); >+ } >+ Class fromClass = (Class) fromType; >+ if (fromClass.isPrimitive()) { >+ fromClass = autoboxed(fromClass); >+ } >+ return toClass.isAssignableFrom(fromClass) ? Boolean.TRUE >+ : Boolean.FALSE; >+ } >+ return null; >+ } >+ >+ /* >+ * Default converter implementation, does not perform any conversion. >+ */ >+ protected static final class DefaultConverter implements IConverter { >+ >+ private final Object toType; >+ >+ private final Object fromType; >+ >+ /** >+ * @param fromType >+ * @param toType >+ */ >+ DefaultConverter(Object fromType, Object toType) { >+ this.toType = toType; >+ this.fromType = fromType; >+ } >+ >+ public Object convert(Object fromObject) { >+ return fromObject; >+ } >+ >+ public Object getFromType() { >+ return fromType; >+ } >+ >+ public Object getToType() { >+ return toType; >+ } >+ } >+} >#P org.eclipse.jface.examples.databinding >Index: src/org/eclipse/jface/examples/databinding/snippets/Snippet000HelloWorld.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet000HelloWorld.java,v >retrieving revision 1.12 >diff -u -r1.12 Snippet000HelloWorld.java >--- src/org/eclipse/jface/examples/databinding/snippets/Snippet000HelloWorld.java 13 Mar 2007 04:12:19 -0000 1.12 >+++ src/org/eclipse/jface/examples/databinding/snippets/Snippet000HelloWorld.java 10 Nov 2007 06:56:08 -0000 >@@ -13,7 +13,8 @@ > package org.eclipse.jface.examples.databinding.snippets; > > import org.eclipse.core.databinding.DataBindingContext; >-import org.eclipse.core.databinding.beans.BeansObservables; >+import org.eclipse.core.databinding.ValueBindingBuilder; >+import org.eclipse.core.databinding.beans.PojoObservables; > import org.eclipse.core.databinding.observable.Realm; > import org.eclipse.jface.databinding.swt.SWTObservables; > import org.eclipse.swt.SWT; >@@ -34,19 +35,18 @@ > Display display = new Display(); > final ViewModel viewModel = new ViewModel(); > >- Realm.runWithDefault(SWTObservables.getRealm(display), >- new Runnable() { >- public void run() { >- final Shell shell = new View(viewModel).createShell(); >- // The SWT event loop >- Display display = Display.getCurrent(); >- while (!shell.isDisposed()) { >- if (!display.readAndDispatch()) { >- display.sleep(); >- } >- } >+ Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { >+ public void run() { >+ final Shell shell = new View(viewModel).createShell(); >+ // The SWT event loop >+ Display display = Display.getCurrent(); >+ while (!shell.isDisposed()) { >+ if (!display.readAndDispatch()) { >+ display.sleep(); > } >- }); >+ } >+ } >+ }); > // Print the results > System.out.println("person.getName() = " > + viewModel.getPerson().getName()); >@@ -90,6 +90,7 @@ > // The GUI view > static class View { > private ViewModel viewModel; >+ > private Text name; > > public View(ViewModel viewModel) { >@@ -104,12 +105,12 @@ > name = new Text(shell, SWT.BORDER); > > // Bind it >- DataBindingContext bindingContext = new DataBindingContext(); >+ ValueBindingBuilder builder = ValueBindingBuilder.withDefaults() >+ .dbc(new DataBindingContext()); > Person person = viewModel.getPerson(); > >- bindingContext.bindValue(SWTObservables.observeText(name, >- SWT.Modify), BeansObservables.observeValue(person, "name"), >- null, null); >+ builder.bind(SWTObservables.observeText(name, SWT.Modify), >+ PojoObservables.observeValue(person, "name")); > > // Open and return the Shell > shell.pack();
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 203492
:
80312
| 82601