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