RadialGradientPropertyEditor.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.metadata.PropertyEditorFor;
020 import javafx.scene.paint.CycleMethod;
021 import javafx.scene.paint.RadialGradient;
022 import javafx.scene.paint.Stop;
023 
024 import java.lang.reflect.Field;
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.4.0
034  */
035 @PropertyEditorFor(RadialGradient.class)
036 public class RadialGradientPropertyEditor extends AbstractPropertyEditor {
037     @Override
038     public String getAsText() {
039         if (null == getValue()) return null;
040         return getValueInternal().toString();
041     }
042 
043     @Override
044     protected void setValueInternal(Object value) {
045         if (null == value) {
046             super.setValueInternal(null);
047         else if (value instanceof CharSequence) {
048             handleAsString(String.valueOf(value).trim());
049         else if (value instanceof List) {
050             handleAsList((Listvalue);
051         else if (value instanceof Map) {
052             handleAsMap((Mapvalue);
053         else if (value instanceof RadialGradient) {
054             super.setValueInternal(value);
055         else {
056             throw illegalValue(value, RadialGradient.class);
057         }
058     }
059 
060     protected void handleAsString(String str) {
061         if (isBlank(str)) {
062             super.setValueInternal(null);
063             return;
064         }
065         try {
066             super.setValueInternal(RadialGradient.valueOf(str));
067         catch (Exception e) {
068             throw illegalValue(str, RadialGradient.class, e);
069         }
070     }
071 
072     protected void handleAsList(List<?> list) {
073         if (list.isEmpty()) {
074             super.setValueInternal(null);
075             return;
076         }
077 
078         double fa = 0;
079         double fd = 0;
080         double cx = 0;
081         double cy = 0;
082         double r = 0;
083         boolean proportional = false;
084         List<Stop> stops = new ArrayList<>();
085         CycleMethod cyclicMethod = CycleMethod.NO_CYCLE;
086 
087         switch (list.size()) {
088             case 8:
089                 cyclicMethod = parseCyclicMethod(list, String.valueOf(list.get(7)).trim());
090             case 7:
091                 fa = parseValue(list.get(0));
092                 fd = parseValue(list.get(1));
093                 cx = parseValue(list.get(2));
094                 cy = parseValue(list.get(3));
095                 r = parseValue(list.get(4));
096                 proportional = (Booleanlist.get(5);
097                 stops = (List<Stop>list.get(6);
098                 super.setValueInternal(new RadialGradient(fa, fd, cx, cy, r, proportional, cyclicMethod, stops));
099                 break;
100             default:
101                 throw illegalValue(list, RadialGradient.class);
102         }
103     }
104 
105     protected void handleAsMap(Map<?, ?> map) {
106         if (map.isEmpty()) {
107             super.setValueInternal(null);
108             return;
109         }
110 
111         double fa = (DoublegetMapValue(map, "fa"0d);
112         double fd = (DoublegetMapValue(map, "fd"0d);
113         double cx = (DoublegetMapValue(map, "cx"0d);
114         double cy = (DoublegetMapValue(map, "cy"0d);
115         double r = (DoublegetMapValue(map, "r"0d);
116         boolean proportional = (BooleangetMapValue(map, "p"false);
117         List<Stop> stops = new ArrayList<>();
118         CycleMethod cyclicMethod = CycleMethod.NO_CYCLE;
119 
120         if (map.containsKey("stops")) {
121             stops = (List<Stop>map.get("stops");
122         }
123         Object cyclicValue = map.get("cycle");
124         if (null != cyclicValue) {
125             cyclicMethod = parseCyclicMethod(map, String.valueOf(cyclicValue));
126         }
127 
128         super.setValueInternal(new RadialGradient(fa, fd, cx, cy, r, proportional, cyclicMethod, stops));
129     }
130 
131     protected CycleMethod parseCyclicMethod(Object source, String str) {
132         try {
133             Field cyclicMethodField = CycleMethod.class.getDeclaredField(str.toUpperCase().trim());
134             return (CycleMethodcyclicMethodField.get(null);
135         catch (NoSuchFieldException | IllegalAccessException e) {
136             throw illegalValue(source, RadialGradient.class, e);
137         }
138     }
139 
140     protected double parse(String val) {
141         try {
142             return Float.parseFloat(val.trim());
143         catch (NumberFormatException e) {
144             throw illegalValue(val, RadialGradient.class, e);
145         }
146     }
147 
148     protected double parseValue(Object value) {
149         if (value instanceof CharSequence) {
150             return parse(String.valueOf(value));
151         else if (value instanceof Number) {
152             return parse((Numbervalue);
153         }
154         throw illegalValue(value, RadialGradient.class);
155     }
156 
157     protected double parse(Number val) {
158         return val.doubleValue();
159     }
160 
161     protected Object getMapValue(Map<?, ?> map, String key, Object defaultValue) {
162         Object val = map.get(key);
163         if (null == val) {
164             return defaultValue;
165         else if (val instanceof CharSequence) {
166             return parse(String.valueOf(val));
167         else if (val instanceof Number) {
168             return parse((Numberval);
169         }
170         throw illegalValue(map, RadialGradient.class);
171     }
172 }