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