001 /*
002 * Copyright 2008-2017 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 * http://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 package griffon.core.editors;
017
018 import griffon.core.formatters.Formatter;
019 import griffon.core.formatters.ParseException;
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022
023 import javax.annotation.Nullable;
024 import java.beans.PropertyEditorSupport;
025
026 import static griffon.core.GriffonExceptionHandler.sanitize;
027 import static griffon.util.GriffonNameUtils.isBlank;
028
029 /**
030 * @author Andres Almiray
031 * @since 2.0.0
032 */
033 public abstract class AbstractPropertyEditor extends PropertyEditorSupport implements ExtendedPropertyEditor {
034 private static final Logger LOG = LoggerFactory.getLogger(AbstractPropertyEditor.class);
035
036 private String format;
037
038 @Nullable
039 @Override
040 public String getFormat() {
041 return format;
042 }
043
044 @Override
045 public void setFormat(@Nullable String format) {
046 this.format = format;
047 }
048
049 @Override
050 public String getAsText() {
051 return isBlank(getFormat()) ? getAsTextInternal() : getFormattedValue();
052 }
053
054 @Override
055 public void setAsText(String str) throws IllegalArgumentException {
056 if (isBlank(getFormat())) {
057 setAsTextInternal(str);
058 } else {
059 setFormattedValue(str);
060 }
061 }
062
063 @Override
064 public void setValue(Object value) {
065 if (value instanceof CharSequence) {
066 setFormattedValue(String.valueOf(value));
067 } else {
068 setValueInternal(value);
069 }
070 }
071
072 protected void setValueInternal(Object value) {
073 super.setValue(value);
074 }
075
076 protected Object getValueInternal() {
077 return super.getValue();
078 }
079
080 protected void setAsTextInternal(String str) throws IllegalArgumentException {
081 setValueInternal(str);
082 }
083
084 protected String getAsTextInternal() {
085 return super.getAsText();
086 }
087
088 @Override
089 @SuppressWarnings({"unchecked", "rawtypes"})
090 public String getFormattedValue() {
091 Object value = getValueInternal();
092 Formatter formatter = resolveFormatter();
093 if (formatter != null) {
094 return formatter.format(value);
095 }
096 return value != null ? value.toString() : null;
097 }
098
099 @Override
100 @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
101 public void setFormattedValue(String value) {
102 Formatter<?> formatter = resolveFormatter();
103 if (formatter != null) {
104 try {
105 setValueInternal(formatter.parse(value));
106 } catch (ParseException e) {
107 e = (ParseException) sanitize(e);
108 if (LOG.isTraceEnabled()) {
109 LOG.trace("Cannot parse value " + value, e);
110 }
111
112 throw new ValueConversionException(value, e);
113 }
114 } else {
115 setValueInternal(value);
116 }
117 }
118
119 protected Formatter<?> resolveFormatter() {
120 return null;
121 }
122
123 protected ValueConversionException illegalValue(Object value, Class<?> klass) {
124 throw new ValueConversionException(value, klass);
125 }
126
127 protected ValueConversionException illegalValue(Object value, Class<?> klass, Exception e) {
128 throw new ValueConversionException(value, klass, e);
129 }
130 }
|