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