001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.support;
018
019import java.beans.PropertyEditor;
020import java.beans.PropertyEditorSupport;
021
022import org.springframework.context.i18n.LocaleContextHolder;
023import org.springframework.format.Formatter;
024import org.springframework.util.Assert;
025import org.springframework.util.StringUtils;
026
027/**
028 * Adapter that bridges between {@link Formatter} and {@link PropertyEditor}.
029 *
030 * @author Juergen Hoeller
031 * @since 4.2
032 */
033public class FormatterPropertyEditorAdapter extends PropertyEditorSupport {
034
035        private final Formatter<Object> formatter;
036
037
038        /**
039         * Create a new {@code FormatterPropertyEditorAdapter} for the given {@link Formatter}.
040         * @param formatter the {@link Formatter} to wrap
041         */
042        @SuppressWarnings("unchecked")
043        public FormatterPropertyEditorAdapter(Formatter<?> formatter) {
044                Assert.notNull(formatter, "Formatter must not be null");
045                this.formatter = (Formatter<Object>) formatter;
046        }
047
048
049        /**
050         * Determine the {@link Formatter}-declared field type.
051         * @return the field type declared in the wrapped {@link Formatter} implementation
052         * (never {@code null})
053         * @throws IllegalArgumentException if the {@link Formatter}-declared field type
054         * cannot be inferred
055         */
056        public Class<?> getFieldType() {
057                return FormattingConversionService.getFieldType(this.formatter);
058        }
059
060
061        @Override
062        public void setAsText(String text) throws IllegalArgumentException {
063                if (StringUtils.hasText(text)) {
064                        try {
065                                setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
066                        }
067                        catch (IllegalArgumentException ex) {
068                                throw ex;
069                        }
070                        catch (Throwable ex) {
071                                throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
072                        }
073                }
074                else {
075                        setValue(null);
076                }
077        }
078
079        @Override
080        public String getAsText() {
081                Object value = getValue();
082                return (value != null ? this.formatter.print(value, LocaleContextHolder.getLocale()) : "");
083        }
084
085}