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