| 
001 /*002  * Copyright 2008-2015 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.pivot.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.pivot.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  * @author Andres Almiray
 033  * @since 2.0.0
 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((Color) getValueInternal()) : getFormattedValue();
 045     }
 046
 047     protected void setValueInternal(Object value) {
 048         if (null == value) {
 049             super.setValueInternal(null);
 050         } else if (value instanceof CharSequence) {
 051             handleAsString(String.valueOf(value).trim());
 052         } else if (value instanceof List) {
 053             handleAsList((List) value);
 054         } else if (value instanceof Map) {
 055             handleAsMap((Map) value);
 056         } else if (value instanceof Number) {
 057             handleAsNumber((Number) value);
 058         } else if (value instanceof Color) {
 059             super.setValueInternal(value);
 060         } else {
 061             throw illegalValue(value, Color.class);
 062         }
 063     }
 064
 065     @Override
 066     protected Formatter<Color> resolveFormatter() {
 067         return !isBlank(getFormat()) ? ColorFormatter.getInstance(getFormat()) : null;
 068     }
 069
 070     private void handleAsString(String str) {
 071         if (isBlank(str)) {
 072             super.setValueInternal(null);
 073             return;
 074         }
 075
 076         try {
 077             super.setValueInternal(ColorFormatter.parseColor(str));
 078         } catch (ParseException e) {
 079             throw illegalValue(str, Color.class, e);
 080         }
 081     }
 082
 083     private 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(255);
 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((Number) val));
 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                 (Integer) values.get(0),
 114                 (Integer) values.get(1),
 115                 (Integer) values.get(2),
 116                 (Integer) values.get(3)
 117             )
 118         );
 119     }
 120
 121     private void handleAsMap(Map<?, ?> map) {
 122         if (map.isEmpty()) {
 123             super.setValueInternal(null);
 124             return;
 125         }
 126
 127         int r = getMapValue(map, "red", 0);
 128         int g = getMapValue(map, "green", 0);
 129         int b = getMapValue(map, "blue", 0);
 130         int a = getMapValue(map, "alpha", 255);
 131         super.setValueInternal(new Color(r, g, b, a));
 132     }
 133
 134     private int parse(String val) {
 135         try {
 136             return Integer.parseInt(String.valueOf(val).trim(), 16) & 0xFF;
 137         } catch (NumberFormatException e) {
 138             throw illegalValue(val, Color.class, e);
 139         }
 140     }
 141
 142     private int parse(Number val) {
 143         return val.intValue() & 0xFF;
 144     }
 145
 146     private int getMapValue(Map<?, ?> map, String key, int defaultValue) {
 147         Object val = map.get(key);
 148         if (null == val) val = 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((Number) val);
 155         }
 156         throw illegalValue(map, Color.class);
 157     }
 158
 159     private void handleAsNumber(Number value) {
 160         int c = parse(value);
 161         super.setValueInternal(new Color(c, c, c, 255));
 162     }
 163 }
 |