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