LinearGradientPropertyEditor.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.javafx.editors;
019 
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 import javafx.scene.paint.CycleMethod;
023 import javafx.scene.paint.LinearGradient;
024 import javafx.scene.paint.Stop;
025 
026 import java.lang.reflect.Field;
027 import java.util.ArrayList;
028 import java.util.List;
029 import java.util.Map;
030 
031 import static griffon.util.GriffonNameUtils.isBlank;
032 
033 /**
034  @author Andres Almiray
035  @since 2.4.0
036  */
037 @PropertyEditorFor(LinearGradient.class)
038 public class LinearGradientPropertyEditor extends AbstractPropertyEditor {
039     @Override
040     public String getAsText() {
041         if (null == getValue()) return null;
042         return getValueInternal().toString();
043     }
044 
045     @Override
046     protected void setValueInternal(Object value) {
047         if (null == value) {
048             super.setValueInternal(null);
049         else if (value instanceof CharSequence) {
050             handleAsString(String.valueOf(value).trim());
051         else if (value instanceof List) {
052             handleAsList((Listvalue);
053         else if (value instanceof Map) {
054             handleAsMap((Mapvalue);
055         else if (value instanceof LinearGradient) {
056             super.setValueInternal(value);
057         else {
058             throw illegalValue(value, LinearGradient.class);
059         }
060     }
061 
062     protected void handleAsString(String str) {
063         if (isBlank(str)) {
064             super.setValueInternal(null);
065             return;
066         }
067         try {
068             super.setValueInternal(LinearGradient.valueOf(str));
069         catch (Exception e) {
070             throw illegalValue(str, LinearGradient.class, e);
071         }
072     }
073 
074     protected void handleAsList(List<?> list) {
075         if (list.isEmpty()) {
076             super.setValueInternal(null);
077             return;
078         }
079 
080         double sx = 0;
081         double sy = 0;
082         double ex = 0;
083         double ey = 0;
084         boolean proportional = false;
085         List<Stop> stops = new ArrayList<>();
086         CycleMethod cyclicMethod = CycleMethod.NO_CYCLE;
087 
088         switch (list.size()) {
089             case 7:
090                 cyclicMethod = parseCyclicMethod(list, String.valueOf(list.get(6)).trim());
091             case 6:
092                 sx = parseValue(list.get(0));
093                 sy = parseValue(list.get(1));
094                 ex = parseValue(list.get(2));
095                 ey = parseValue(list.get(3));
096                 proportional = (Booleanlist.get(4);
097                 stops = (List<Stop>list.get(5);
098                 super.setValueInternal(new LinearGradient(sx, sy, ex, ey, proportional, cyclicMethod, stops));
099                 break;
100             default:
101                 throw illegalValue(list, LinearGradient.class);
102         }
103     }
104 
105     protected void handleAsMap(Map<?, ?> map) {
106         if (map.isEmpty()) {
107             super.setValueInternal(null);
108             return;
109         }
110 
111         double sx = (DoublegetMapValue(map, "sx"0d);
112         double sy = (DoublegetMapValue(map, "sy"0d);
113         double ex = (DoublegetMapValue(map, "ex"0d);
114         double ey = (DoublegetMapValue(map, "ey"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 LinearGradient(sx, sy, ex, ey, 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, LinearGradient.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, LinearGradient.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, LinearGradient.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, LinearGradient.class);
170     }
171 }