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