Java Code Examples for org.springframework.beans.PropertyEditorRegistry#registerCustomEditor()

The following examples show how to use org.springframework.beans.PropertyEditorRegistry#registerCustomEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Registers all known
 *
 * @param registry
 * @param locale
 */
public static void registerCustomEditors(PropertyEditorRegistry registry, Locale locale) {
	// Formatters for the different number types.
	NumberFormat floatFormat = NumberFormat.getInstance(locale);
	NumberFormat integerFormat = NumberFormat.getIntegerInstance(locale);

	DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, locale);

	registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	registry.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, floatFormat, true));
	registry.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, floatFormat, true));
	registry.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(Date.class, new StructuredDateEditor(dateFormat, true));
	registry.registerCustomEditor(Calendar.class, new StructuredDateEditor(dateFormat, true));

	registerCustomEditors(registry);
}
 
Example 2
Source File: AbstractBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the given PropertyEditorRegistry with the custom editors
 * that have been registered with this BeanFactory.
 * <p>To be called for BeanWrappers that will create and populate bean
 * instances, and for SimpleTypeConverter used for constructor argument
 * and factory method type conversion.
 * @param registry the PropertyEditorRegistry to initialize
 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
	PropertyEditorRegistrySupport registrySupport =
			(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
	if (registrySupport != null) {
		registrySupport.useConfigValueEditors();
	}
	if (!this.propertyEditorRegistrars.isEmpty()) {
		for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
			try {
				registrar.registerCustomEditors(registry);
			}
			catch (BeanCreationException ex) {
				Throwable rootCause = ex.getMostSpecificCause();
				if (rootCause instanceof BeanCurrentlyInCreationException) {
					BeanCreationException bce = (BeanCreationException) rootCause;
					if (isCurrentlyInCreation(bce.getBeanName())) {
						if (logger.isDebugEnabled()) {
							logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
									"] failed because it tried to obtain currently created bean '" +
									ex.getBeanName() + "': " + ex.getMessage());
						}
						onSuppressedException(ex);
						continue;
					}
				}
				throw ex;
			}
		}
	}
	if (!this.customEditors.isEmpty()) {
		for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
			Class<?> requiredType = entry.getKey();
			Class<? extends PropertyEditor> editorClass = entry.getValue();
			registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
		}
	}
}
 
Example 3
Source File: JseDefaultNumberPropertyEditorRegistrar.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(Byte.class, new HalfwidthNumberEditor(Byte.class, allowEmpty, message));
    registry.registerCustomEditor(Short.class, new HalfwidthNumberEditor(Short.class, allowEmpty, message));
    registry.registerCustomEditor(Integer.class, new HalfwidthNumberEditor(Integer.class, allowEmpty, message));
    registry.registerCustomEditor(Long.class, new HalfwidthNumberEditor(Long.class, allowEmpty, message));
    registry.registerCustomEditor(BigInteger.class, new HalfwidthNumberEditor(BigInteger.class, allowEmpty, message));
    registry.registerCustomEditor(BigDecimal.class, new HalfwidthDecimalEditor(BigDecimal.class, allowEmpty, message));
}
 
Example 4
Source File: ResourceEditorRegistrar.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 5
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the given PropertyEditorRegistry with the custom editors
 * that have been registered with this BeanFactory.
 * <p>To be called for BeanWrappers that will create and populate bean
 * instances, and for SimpleTypeConverter used for constructor argument
 * and factory method type conversion.
 * @param registry the PropertyEditorRegistry to initialize
 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
	PropertyEditorRegistrySupport registrySupport =
			(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
	if (registrySupport != null) {
		registrySupport.useConfigValueEditors();
	}
	if (!this.propertyEditorRegistrars.isEmpty()) {
		for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
			try {
				registrar.registerCustomEditors(registry);
			}
			catch (BeanCreationException ex) {
				Throwable rootCause = ex.getMostSpecificCause();
				if (rootCause instanceof BeanCurrentlyInCreationException) {
					BeanCreationException bce = (BeanCreationException) rootCause;
					if (isCurrentlyInCreation(bce.getBeanName())) {
						if (logger.isDebugEnabled()) {
							logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
									"] failed because it tried to obtain currently created bean '" +
									ex.getBeanName() + "': " + ex.getMessage());
						}
						onSuppressedException(ex);
						continue;
					}
				}
				throw ex;
			}
		}
	}
	if (!this.customEditors.isEmpty()) {
		for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
			Class<?> requiredType = entry.getKey();
			Class<? extends PropertyEditor> editorClass = entry.getValue();
			registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
		}
	}
}
 
Example 6
Source File: ResourceEditorRegistrar.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 7
Source File: ObjectPropertyUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Gets a property editor given a specific bean and property path.
 * 
 * @param bean The bean instance.
 * @param path The property path.
 * @return property editor
 */
public static PropertyEditor getPropertyEditor(Object bean, String path) {
    Class<?> propertyType = getPrimitiveType(getPropertyType(bean, path));
    
    PropertyEditor editor = null;

    PropertyEditorRegistry registry = getPropertyEditorRegistry();
    if (registry != null) {
        editor = registry.findCustomEditor(propertyType, path);
        
        if (editor != null && editor != registry.findCustomEditor(propertyType, null)) {
            return editor;
        }
        
        if (registry instanceof BeanWrapper
                && bean == ((BeanWrapper) registry).getWrappedInstance()
                && (bean instanceof ViewModel)) {
            
            ViewModel viewModel = (ViewModel) bean;
            ViewPostMetadata viewPostMetadata = viewModel.getViewPostMetadata();
            PropertyEditor editorFromView = viewPostMetadata == null ? null : viewPostMetadata.getFieldEditor(path);

            if (editorFromView != null) {
                registry.registerCustomEditor(propertyType, path, editorFromView);
                editor = registry.findCustomEditor(propertyType, path);
            }
        }
    }

    if (editor != null) {
        return editor;
    }
    
    return getPropertyEditor(propertyType);
}
 
Example 8
Source File: ResourceEditorRegistrar.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 9
Source File: CustomPropertyEditorRegistrar.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry)
{
    // add custom QName editor
    registry.registerCustomEditor(QName.class, new QNameTypeEditor(namespaceService));
}
 
Example 10
Source File: ResourceEditorRegistrar.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 11
Source File: PropertyEditorUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Register the property editors provided by uimaFIT in the given property editor registry.
 * 
 * @param aRegistry a property editor registry
 */
public static void registerUimaFITEditors(PropertyEditorRegistry aRegistry) {
  aRegistry.registerCustomEditor(Charset.class, new CharsetEditor());
  aRegistry.registerCustomEditor(Locale.class, new LocaleEditor());
  aRegistry.registerCustomEditor(String.class, new GetAsTextStringEditor(aRegistry));
  aRegistry.registerCustomEditor(LinkedList.class, new CustomCollectionEditor(LinkedList.class));
}
 
Example 12
Source File: LocalDatePropertyEditorRegistrar.java    From thinking-in-spring-boot-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    // 为 LocalDate 类型属性注册 LocalDateEditor
    registry.registerCustomEditor(LocalDate.class, new LocalDateEditor());

}
 
Example 13
Source File: JseDefaultDatePropertyEditorRegistrar.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(Date.class, new DateEditor(allowEmpty, true, pattern, message));
}
 
Example 14
Source File: SocketAddressEditor.java    From canal with Apache License 2.0 4 votes vote down vote up
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(InetSocketAddress.class, this);
}
 
Example 15
Source File: DateConverter.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(Date.class,
            new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
 
Example 16
Source File: CustomDateEditorRegistrar.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true));
}
 
Example 17
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	registry.registerCustomEditor(Set.class, new ListEditor());
}
 
Example 18
Source File: CustomDateEditorRegistrar.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true));
}
 
Example 19
Source File: SocketAddressEditor.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public void registerCustomEditors(PropertyEditorRegistry registry) {
    registry.registerCustomEditor(InetSocketAddress.class, this);
}
 
Example 20
Source File: CustomizedPropertyEditorRegistrar.java    From geekbang-lessons with Apache License 2.0 4 votes vote down vote up
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    // 1. 通用类型转换
    // 2. Java Bean 属性类型转换
    registry.registerCustomEditor(User.class, "context", new StringToPropertiesPropertyEditor());
}