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.javafx.editors;
019
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.core.formatters.Formatter;
022 import griffon.core.formatters.ParseException;
023 import griffon.javafx.formatters.ColorFormatter;
024 import griffon.metadata.PropertyEditorFor;
025 import javafx.scene.paint.Color;
026
027 import java.util.ArrayList;
028 import java.util.List;
029 import java.util.Map;
030
031 import static griffon.util.GriffonNameUtils.isBlank;
032 import static griffon.util.GriffonNameUtils.isNotBlank;
033
034 /**
035 * @author Andres Almiray
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 isNotBlank(getFormat()) ? ColorFormatter.getInstance(getFormat()) : null;
071 }
072
073 protected void handleAsString(String str) {
074 if (isBlank(str)) {
075 super.setValueInternal(null);
076 return;
077 }
078 try {
079 super.setValueInternal(ColorFormatter.parseColor(str));
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(1d);
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 (Double) values.get(0),
116 (Double) values.get(1),
117 (Double) values.get(2),
118 (Double) 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 double r = getMapValue(map, "red", 0d);
130 double g = getMapValue(map, "green", 0d);
131 double b = getMapValue(map, "blue", 0d);
132 double a = getMapValue(map, "alpha", 1d);
133 super.setValueInternal(new Color(r, g, b, a));
134 }
135
136 protected double parse(String val) {
137 try {
138 return (Integer.parseInt(String.valueOf(val).trim(), 16) & 0xFF) / 255d;
139 } catch (NumberFormatException e) {
140 throw illegalValue(val, Color.class, e);
141 }
142 }
143
144 protected double parse(Number val) {
145 return val.doubleValue();
146 }
147
148 protected double getMapValue(Map<?, ?> map, String key, double 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 double c = parse(value);
163 super.setValueInternal(new Color(c, c, c, 1d));
164 }
165 }
|