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