AbstractPropertyEditor.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.core.editors;
019 
020 import griffon.core.formatters.Formatter;
021 import griffon.core.formatters.ParseException;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 
025 import javax.annotation.Nullable;
026 import java.beans.PropertyEditorSupport;
027 
028 import static griffon.core.GriffonExceptionHandler.sanitize;
029 import static griffon.util.GriffonNameUtils.isBlank;
030 
031 /**
032  @author Andres Almiray
033  @since 2.0.0
034  */
035 public abstract class AbstractPropertyEditor extends PropertyEditorSupport implements ExtendedPropertyEditor {
036     private static final Logger LOG = LoggerFactory.getLogger(AbstractPropertyEditor.class);
037 
038     private String format;
039 
040     @Nullable
041     @Override
042     public String getFormat() {
043         return format;
044     }
045 
046     @Override
047     public void setFormat(@Nullable String format) {
048         this.format = format;
049     }
050 
051     @Override
052     public String getAsText() {
053         return isBlank(getFormat()) ? getAsTextInternal() : getFormattedValue();
054     }
055 
056     @Override
057     public void setAsText(String strthrows IllegalArgumentException {
058         if (isBlank(getFormat())) {
059             setAsTextInternal(str);
060         else {
061             setFormattedValue(str);
062         }
063     }
064 
065     @Override
066     public void setValue(Object value) {
067         if (value instanceof CharSequence) {
068             setFormattedValue(String.valueOf(value));
069         else {
070             setValueInternal(value);
071         }
072     }
073 
074     protected void setValueInternal(Object value) {
075         super.setValue(value);
076     }
077 
078     protected Object getValueInternal() {
079         return super.getValue();
080     }
081 
082     protected void setAsTextInternal(String strthrows IllegalArgumentException {
083         setValueInternal(str);
084     }
085 
086     protected String getAsTextInternal() {
087         return super.getAsText();
088     }
089 
090     @Override
091     @SuppressWarnings({"unchecked""rawtypes"})
092     public String getFormattedValue() {
093         Object value = getValueInternal();
094         Formatter formatter = resolveFormatter();
095         if (formatter != null) {
096             return formatter.format(value);
097         }
098         return value != null ? value.toString() null;
099     }
100 
101     @Override
102     @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
103     public void setFormattedValue(String value) {
104         Formatter<?> formatter = resolveFormatter();
105         if (formatter != null) {
106             try {
107                 setValueInternal(formatter.parse(value));
108             catch (ParseException e) {
109                 e = (ParseExceptionsanitize(e);
110                 if (LOG.isTraceEnabled()) {
111                     LOG.trace("Cannot parse value " + value, e);
112                 }
113 
114                 throw new ValueConversionException(value, e);
115             }
116         else {
117             setValueInternal(value);
118         }
119     }
120 
121     protected Formatter<?> resolveFormatter() {
122         return null;
123     }
124 
125     protected ValueConversionException illegalValue(Object value, Class<?> klass) {
126         throw new ValueConversionException(value, klass);
127     }
128 
129     protected ValueConversionException illegalValue(Object value, Class<?> klass, Exception e) {
130         throw new ValueConversionException(value, klass, e);
131     }
132 }