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.pivot.editors;
017
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.core.formatters.Formatter;
020 import griffon.core.formatters.ParseException;
021 import griffon.metadata.PropertyEditorFor;
022 import griffon.pivot.formatters.ColorFormatter;
023
024 import java.awt.Color;
025 import java.util.ArrayList;
026 import java.util.List;
027 import java.util.Map;
028
029 import static griffon.util.GriffonNameUtils.isBlank;
030
031 /**
032 * @author Andres Almiray
033 * @since 2.0.0
034 */
035 @PropertyEditorFor(Color.class)
036 public class ColorPropertyEditor extends AbstractPropertyEditor {
037 public static String format(Color color) {
038 return ColorFormatter.LONG.format(color);
039 }
040
041 @Override
042 public String getAsText() {
043 if (null == getValue()) return null;
044 return isBlank(getFormat()) ? format((Color) getValueInternal()) : getFormattedValue();
045 }
046
047 @Override
048 protected void setValueInternal(Object value) {
049 if (null == value) {
050 super.setValueInternal(null);
051 } else if (value instanceof CharSequence) {
052 handleAsString(String.valueOf(value).trim());
053 } else if (value instanceof List) {
054 handleAsList((List) value);
055 } else if (value instanceof Map) {
056 handleAsMap((Map) value);
057 } else if (value instanceof Number) {
058 handleAsNumber((Number) value);
059 } else if (value instanceof Color) {
060 super.setValueInternal(value);
061 } else {
062 throw illegalValue(value, Color.class);
063 }
064 }
065
066 @Override
067 protected Formatter<Color> resolveFormatter() {
068 return !isBlank(getFormat()) ? ColorFormatter.getInstance(getFormat()) : null;
069 }
070
071 protected void handleAsString(String str) {
072 if (isBlank(str)) {
073 super.setValueInternal(null);
074 return;
075 }
076
077 try {
078 super.setValueInternal(ColorFormatter.parseColor(str));
079 } catch (ParseException e) {
080 throw illegalValue(str, Color.class, e);
081 }
082 }
083
084 protected void handleAsList(List<?> list) {
085 if (list.isEmpty()) {
086 super.setValueInternal(null);
087 return;
088 }
089
090 List<Object> values = new ArrayList<>();
091 values.addAll(list);
092 switch (list.size()) {
093 case 3:
094 values.add(255);
095 break;
096 case 4:
097 // ok
098 break;
099 default:
100 throw illegalValue(list, Color.class);
101 }
102 for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
103 Object val = values.get(i);
104 if (val instanceof Number) {
105 values.set(i, parse((Number) val));
106 } else if (val instanceof CharSequence) {
107 values.set(i, parse(String.valueOf(val)));
108 } else {
109 throw illegalValue(list, Color.class);
110 }
111 }
112 super.setValueInternal(
113 new Color(
114 (Integer) values.get(0),
115 (Integer) values.get(1),
116 (Integer) values.get(2),
117 (Integer) values.get(3)
118 )
119 );
120 }
121
122 protected void handleAsMap(Map<?, ?> map) {
123 if (map.isEmpty()) {
124 super.setValueInternal(null);
125 return;
126 }
127
128 int r = getMapValue(map, "red", 0);
129 int g = getMapValue(map, "green", 0);
130 int b = getMapValue(map, "blue", 0);
131 int a = getMapValue(map, "alpha", 255);
132 super.setValueInternal(new Color(r, g, b, a));
133 }
134
135 protected int parse(String val) {
136 try {
137 return Integer.parseInt(String.valueOf(val).trim(), 16) & 0xFF;
138 } catch (NumberFormatException e) {
139 throw illegalValue(val, Color.class, e);
140 }
141 }
142
143 protected int parse(Number val) {
144 return val.intValue() & 0xFF;
145 }
146
147 protected int getMapValue(Map<?, ?> map, String key, int defaultValue) {
148 Object val = map.get(key);
149 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
150 if (null == val) {
151 return defaultValue;
152 } else if (val instanceof CharSequence) {
153 return parse(String.valueOf(val));
154 } else if (val instanceof Number) {
155 return parse((Number) val);
156 }
157 throw illegalValue(map, Color.class);
158 }
159
160 protected void handleAsNumber(Number value) {
161 int c = parse(value);
162 super.setValueInternal(new Color(c, c, c, 255));
163 }
164 }
|