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