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.swing.editors;
019
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022
023 import java.awt.Color;
024 import java.awt.GradientPaint;
025 import java.util.List;
026 import java.util.Map;
027
028 import static griffon.util.GriffonNameUtils.isBlank;
029
030 /**
031 * @author Andres Almiray
032 * @author Alexander Klein
033 * @since 2.0.0
034 */
035 @PropertyEditorFor(GradientPaint.class)
036 public class GradientPaintPropertyEditor extends AbstractPropertyEditor {
037 @Override
038 public String getAsText() {
039 if (null == getValue()) return null;
040 GradientPaint p = (GradientPaint) getValue();
041 return new StringBuilder()
042 .append(p.getPoint1().getX())
043 .append(", ")
044 .append(p.getPoint1().getY())
045 .append(", ")
046 .append(ColorPropertyEditor.format(p.getColor1()))
047 .append(", ")
048 .append(p.getPoint2().getX())
049 .append(", ")
050 .append(p.getPoint2().getY())
051 .append(", ")
052 .append(ColorPropertyEditor.format(p.getColor2()))
053 .append(", ")
054 .append(p.isCyclic())
055 .toString();
056 }
057
058 @Override
059 protected void setValueInternal(Object value) {
060 if (null == value) {
061 super.setValueInternal(null);
062 } else if (value instanceof CharSequence) {
063 handleAsString(String.valueOf(value));
064 } else if (value instanceof List) {
065 handleAsList((List) value);
066 } else if (value instanceof Map) {
067 handleAsMap((Map) value);
068 } else if (value instanceof GradientPaint) {
069 super.setValueInternal(value);
070 } else {
071 throw illegalValue(value, GradientPaint.class);
072 }
073 }
074
075 protected void handleAsString(String str) {
076 if (isBlank(str)) {
077 super.setValueInternal(null);
078 return;
079 }
080
081 float x1 = 0;
082 float y1 = 0;
083 float x2 = 0;
084 float y2 = 0;
085 Color c1 = Color.WHITE;
086 Color c2 = Color.BLACK;
087 boolean cyclic = false;
088
089 if (str.contains("|")) {
090 String[] parts = str.split("\\|");
091 switch (parts.length) {
092 case 4:
093 cyclic = parseBoolean(parts[3]);
094 case 3:
095 ColorPropertyEditor colorEditor = new ColorPropertyEditor();
096 // point1
097 String[] p1 = parts[0].split(",");
098 if (p1.length != 2) {
099 throw illegalValue(str, GradientPaint.class);
100 }
101 x1 = parseValue(p1[0]);
102 y1 = parseValue(p1[1]);
103
104 // point2
105 String[] p2 = parts[1].split(",");
106 if (p2.length != 2) {
107 throw illegalValue(str, GradientPaint.class);
108 }
109 x2 = parseValue(p2[0]);
110 y2 = parseValue(p2[1]);
111
112 String[] colors = parts[2].split(",");
113 try {
114 colorEditor.setAsText(colors[0]);
115 c1 = (Color) colorEditor.getValue();
116 } catch (Exception e) {
117 throw illegalValue(colors[0], GradientPaint.class);
118 }
119
120 try {
121 colorEditor.setAsText(colors[1]);
122 c2 = (Color) colorEditor.getValue();
123 } catch (Exception e) {
124 throw illegalValue(colors[1], GradientPaint.class);
125 }
126 super.setValueInternal(new GradientPaint(x1, y1, c1, x2, y2, c2, cyclic));
127 break;
128 default:
129 throw illegalValue(str, GradientPaint.class);
130 }
131 } else {
132 String[] parts = str.split(",");
133 switch (parts.length) {
134 case 7:
135 cyclic = parseBoolean(parts[6]);
136 case 6:
137 ColorPropertyEditor colorEditor = new ColorPropertyEditor();
138 x1 = parseValue(parts[0]);
139 y1 = parseValue(parts[1]);
140 x2 = parseValue(parts[3]);
141 y2 = parseValue(parts[4]);
142 try {
143 colorEditor.setAsText(parts[2]);
144 c1 = (Color) colorEditor.getValue();
145 } catch (Exception e) {
146 throw illegalValue(parts[2], GradientPaint.class);
147 }
148 try {
149 colorEditor.setAsText(parts[5]);
150 c2 = (Color) colorEditor.getValue();
151 } catch (Exception e) {
152 throw illegalValue(parts[5], GradientPaint.class);
153 }
154 super.setValueInternal(new GradientPaint(x1, y1, c1, x2, y2, c2, cyclic));
155 break;
156 default:
157 throw illegalValue(str, GradientPaint.class);
158 }
159 }
160
161 }
162
163 protected void handleAsList(List<?> list) {
164 if(list.isEmpty()) {
165 super.setValueInternal(null);
166 return;
167 }
168
169 float x1 = 0;
170 float y1 = 0;
171 float x2 = 0;
172 float y2 = 0;
173 Color c1 = Color.WHITE;
174 Color c2 = Color.BLACK;
175 boolean cyclic = false;
176 switch (list.size()) {
177 case 7:
178 cyclic = parseBoolean(String.valueOf(list.get(6)));
179 case 6:
180 ColorPropertyEditor colorEditor = new ColorPropertyEditor();
181 x1 = parseValue(list.get(0));
182 y1 = parseValue(list.get(1));
183 x2 = parseValue(list.get(3));
184 y2 = parseValue(list.get(4));
185 try {
186 colorEditor.setValueInternal(list.get(2));
187 c1 = (Color) colorEditor.getValue();
188 } catch (Exception e) {
189 throw illegalValue(list.get(2), GradientPaint.class);
190 }
191 try {
192 colorEditor.setValueInternal(list.get(5));
193 c2 = (Color) colorEditor.getValue();
194 } catch (Exception e) {
195 throw illegalValue(list.get(5), GradientPaint.class);
196 }
197 super.setValueInternal(new GradientPaint(x1, y1, c1, x2, y2, c2, cyclic));
198 break;
199 default:
200 throw illegalValue(list, GradientPaint.class);
201 }
202 }
203
204 protected void handleAsMap(Map<?, ?> map) {
205 if(map.isEmpty()) {
206 super.setValueInternal(null);
207 return;
208 }
209
210 float x1 = (Float) getMapValue(map, "x1", 0f);
211 float y1 = (Float) getMapValue(map, "y1", 0f);
212 float x2 = (Float) getMapValue(map, "x2", 0f);
213 float y2 = (Float) getMapValue(map, "y2", 0f);
214 Color c1 = Color.WHITE;
215 Color c2 = Color.BLACK;
216 boolean cyclic = false;
217
218 ColorPropertyEditor colorEditor = new ColorPropertyEditor();
219 Object colorValue = map.get("c1");
220 try {
221 if (null != colorValue) {
222 colorEditor.setValueInternal(colorValue);
223 c1 = (Color) colorEditor.getValue();
224 } else {
225 c1 = Color.WHITE;
226 }
227 } catch (Exception e) {
228 throw illegalValue(colorValue, GradientPaint.class);
229 }
230 colorValue = map.get("c2");
231 try {
232 if (null != colorValue) {
233 colorEditor.setValueInternal(colorValue);
234 c2 = (Color) colorEditor.getValue();
235 } else {
236 c2 = Color.BLACK;
237 }
238 } catch (Exception e) {
239 throw illegalValue(colorValue, GradientPaint.class);
240 }
241 Object cyclicValue = map.get("cyclic");
242 if (null != cyclicValue) {
243 cyclic = parseBoolean(String.valueOf(cyclicValue));
244 }
245
246 super.setValueInternal(new GradientPaint(x1, y1, c1, x2, y2, c2, cyclic));
247 }
248
249 protected float parse(String val) {
250 try {
251 return Float.parseFloat(val.trim());
252 } catch (NumberFormatException e) {
253 throw illegalValue(val, GradientPaint.class, e);
254 }
255 }
256
257 protected boolean parseBoolean(String val) {
258 try {
259 return Boolean.parseBoolean(val.trim());
260 } catch (Exception e) {
261 throw illegalValue(val, GradientPaint.class, e);
262 }
263 }
264
265 protected float parseValue(Object value) {
266 if (value instanceof CharSequence) {
267 return parse(String.valueOf(value));
268 } else if (value instanceof Number) {
269 return parse((Number) value);
270 }
271 throw illegalValue(value, GradientPaint.class);
272 }
273
274 protected float parse(Number val) {
275 return val.floatValue();
276 }
277
278 protected Object getMapValue(Map<?, ?> map, String key, Object defaultValue) {
279 Object val = map.get(key);
280 if (null == val) {
281 return defaultValue;
282 } else if (val instanceof CharSequence) {
283 return parse(String.valueOf(val));
284 } else if (val instanceof Number) {
285 return parse((Number) val);
286 }
287 throw illegalValue(map, GradientPaint.class);
288 }
289 }
|