ColorPropertyEditor.java
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.javafx.editors;
017 
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.core.formatters.Formatter;
020 import griffon.core.formatters.ParseException;
021 import griffon.javafx.formatters.ColorFormatter;
022 import griffon.metadata.PropertyEditorFor;
023 import javafx.scene.paint.Color;
024 
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  */
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((ColorgetValueInternal()) : 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((Listvalue);
055         else if (value instanceof Map) {
056             handleAsMap((Mapvalue);
057         else if (value instanceof Number) {
058             handleAsNumber((Numbervalue);
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         try {
077             super.setValueInternal(ColorFormatter.parseColor(str));
078         catch (ParseException e) {
079             throw illegalValue(str, Color.class, e);
080         }
081     }
082 
083     protected void handleAsList(List<?> list) {
084         if (list.isEmpty()) {
085             super.setValueInternal(null);
086             return;
087         }
088 
089         List<Object> values = new ArrayList<>();
090         values.addAll(list);
091         switch (list.size()) {
092             case 3:
093                 values.add(1d);
094                 break;
095             case 4:
096                 // ok
097                 break;
098             default:
099                 throw illegalValue(list, Color.class);
100         }
101         for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
102             Object val = values.get(i);
103             if (val instanceof Number) {
104                 values.set(i, parse((Numberval));
105             else if (val instanceof CharSequence) {
106                 values.set(i, parse(String.valueOf(val)));
107             else {
108                 throw illegalValue(list, Color.class);
109             }
110         }
111         super.setValueInternal(
112             new Color(
113                 (Doublevalues.get(0),
114                 (Doublevalues.get(1),
115                 (Doublevalues.get(2),
116                 (Doublevalues.get(3)
117             )
118         );
119     }
120 
121     protected void handleAsMap(Map<?, ?> map) {
122         if (map.isEmpty()) {
123             super.setValueInternal(null);
124             return;
125         }
126 
127         double r = getMapValue(map, "red"0d);
128         double g = getMapValue(map, "green"0d);
129         double b = getMapValue(map, "blue"0d);
130         double a = getMapValue(map, "alpha"1d);
131         super.setValueInternal(new Color(r, g, b, a));
132     }
133 
134     protected double parse(String val) {
135         try {
136             return (Integer.parseInt(String.valueOf(val).trim()160xFF255d;
137         catch (NumberFormatException e) {
138             throw illegalValue(val, Color.class, e);
139         }
140     }
141 
142     protected double parse(Number val) {
143         return val.doubleValue();
144     }
145 
146     protected double getMapValue(Map<?, ?> map, String key, double defaultValue) {
147         Object val = map.get(key);
148         if (null == valval = map.get(String.valueOf(key.charAt(0)));
149         if (null == val) {
150             return defaultValue;
151         else if (val instanceof CharSequence) {
152             return parse(String.valueOf(val));
153         else if (val instanceof Number) {
154             return parse((Numberval);
155         }
156         throw illegalValue(map, Color.class);
157     }
158 
159     protected void handleAsNumber(Number value) {
160         double c = parse(value);
161         super.setValueInternal(new Color(c, c, c, 1d));
162     }
163 }