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