001 /*
002 * Copyright 2008-2014 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 public String getFormat() {
040 return format;
041 }
042
043 public void setFormat(@Nullable String format) {
044 this.format = format;
045 }
046
047 @Override
048 public String getAsText() {
049 return isBlank(getFormat()) ? getAsTextInternal() : getFormattedValue();
050 }
051
052 @Override
053 public void setAsText(String str) throws IllegalArgumentException {
054 if (isBlank(getFormat())) {
055 setAsTextInternal(str);
056 } else {
057 setFormattedValue(str);
058 }
059 }
060
061 @Override
062 public void setValue(Object value) {
063 if (value instanceof CharSequence) {
064 setFormattedValue(String.valueOf(value));
065 } else {
066 setValueInternal(value);
067 }
068 }
069
070 protected void setValueInternal(Object value) {
071 super.setValue(value);
072 }
073
074 protected Object getValueInternal() {
075 return super.getValue();
076 }
077
078 protected void setAsTextInternal(String str) throws IllegalArgumentException {
079 setValueInternal(str);
080 }
081
082 protected String getAsTextInternal() {
083 return super.getAsText();
084 }
085
086 @SuppressWarnings({"unchecked", "rawtypes"})
087 public String getFormattedValue() {
088 Object value = getValueInternal();
089 Formatter formatter = resolveFormatter();
090 if (formatter != null) {
091 return formatter.format(value);
092 }
093 return value != null ? value.toString() : null;
094 }
095
096 @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
097 public void setFormattedValue(String value) {
098 Formatter<?> formatter = resolveFormatter();
099 if (formatter != null) {
100 try {
101 setValueInternal(formatter.parse(value));
102 } catch (ParseException e) {
103 e = (ParseException) sanitize(e);
104 if (LOG.isTraceEnabled()) {
105 LOG.trace("Cannot parse value " + value, e);
106 }
107
108 throw new ValueConversionException(value, e);
109 }
110 } else {
111 setValueInternal(value);
112 }
113 }
114
115 protected Formatter<?> resolveFormatter() {
116 return null;
117 }
118
119 protected ValueConversionException illegalValue(Object value, Class<?> klass) {
120 throw new ValueConversionException(value, klass);
121 }
122
123 protected ValueConversionException illegalValue(Object value, Class<?> klass, Exception e) {
124 throw new ValueConversionException(value, klass, e);
125 }
126 }
|