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.RadialGradient;
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(RadialGradient.class)
038 public class RadialGradientPropertyEditor 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((List) value);
053 } else if (value instanceof Map) {
054 handleAsMap((Map) value);
055 } else if (value instanceof RadialGradient) {
056 super.setValueInternal(value);
057 } else {
058 throw illegalValue(value, RadialGradient.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(RadialGradient.valueOf(str));
069 } catch (Exception e) {
070 throw illegalValue(str, RadialGradient.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 fa = 0;
081 double fd = 0;
082 double cx = 0;
083 double cy = 0;
084 double r = 0;
085 boolean proportional = false;
086 List<Stop> stops = new ArrayList<>();
087 CycleMethod cyclicMethod = CycleMethod.NO_CYCLE;
088
089 switch (list.size()) {
090 case 8:
091 cyclicMethod = parseCyclicMethod(list, String.valueOf(list.get(7)).trim());
092 case 7:
093 fa = parseValue(list.get(0));
094 fd = parseValue(list.get(1));
095 cx = parseValue(list.get(2));
096 cy = parseValue(list.get(3));
097 r = parseValue(list.get(4));
098 proportional = (Boolean) list.get(5);
099 stops = (List<Stop>) list.get(6);
100 super.setValueInternal(new RadialGradient(fa, fd, cx, cy, r, proportional, cyclicMethod, stops));
101 break;
102 default:
103 throw illegalValue(list, RadialGradient.class);
104 }
105 }
106
107 protected void handleAsMap(Map<?, ?> map) {
108 if (map.isEmpty()) {
109 super.setValueInternal(null);
110 return;
111 }
112
113 double fa = (Double) getMapValue(map, "fa", 0d);
114 double fd = (Double) getMapValue(map, "fd", 0d);
115 double cx = (Double) getMapValue(map, "cx", 0d);
116 double cy = (Double) getMapValue(map, "cy", 0d);
117 double r = (Double) getMapValue(map, "r", 0d);
118 boolean proportional = (Boolean) getMapValue(map, "p", false);
119 List<Stop> stops = new ArrayList<>();
120 CycleMethod cyclicMethod = CycleMethod.NO_CYCLE;
121
122 if (map.containsKey("stops")) {
123 stops = (List<Stop>) map.get("stops");
124 }
125 Object cyclicValue = map.get("cycle");
126 if (null != cyclicValue) {
127 cyclicMethod = parseCyclicMethod(map, String.valueOf(cyclicValue));
128 }
129
130 super.setValueInternal(new RadialGradient(fa, fd, cx, cy, r, proportional, cyclicMethod, stops));
131 }
132
133 protected CycleMethod parseCyclicMethod(Object source, String str) {
134 try {
135 Field cyclicMethodField = CycleMethod.class.getDeclaredField(str.toUpperCase().trim());
136 return (CycleMethod) cyclicMethodField.get(null);
137 } catch (NoSuchFieldException | IllegalAccessException e) {
138 throw illegalValue(source, RadialGradient.class, e);
139 }
140 }
141
142 protected double parse(String val) {
143 try {
144 return Float.parseFloat(val.trim());
145 } catch (NumberFormatException e) {
146 throw illegalValue(val, RadialGradient.class, e);
147 }
148 }
149
150 protected double parseValue(Object value) {
151 if (value instanceof CharSequence) {
152 return parse(String.valueOf(value));
153 } else if (value instanceof Number) {
154 return parse((Number) value);
155 }
156 throw illegalValue(value, RadialGradient.class);
157 }
158
159 protected double parse(Number val) {
160 return val.doubleValue();
161 }
162
163 protected Object getMapValue(Map<?, ?> map, String key, Object defaultValue) {
164 Object val = map.get(key);
165 if (null == val) {
166 return defaultValue;
167 } else if (val instanceof CharSequence) {
168 return parse(String.valueOf(val));
169 } else if (val instanceof Number) {
170 return parse((Number) val);
171 }
172 throw illegalValue(map, RadialGradient.class);
173 }
174 }
|