Point2DPropertyEditor.java
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.geom.Point2D;
022 import java.util.List;
023 import java.util.Map;
024 
025 import static griffon.util.GriffonNameUtils.isBlank;
026 
027 /**
028  @author Andres Almiray
029  @since 2.0.0
030  */
031 @PropertyEditorFor(Point2D.class)
032 public class Point2DPropertyEditor extends AbstractPropertyEditor {
033     public String getAsText() {
034         if (null == getValue()) return null;
035         Point2D p = (Point2DgetValue();
036         return p.getX() ", " + p.getY();
037     }
038 
039     protected void setValueInternal(Object value) {
040         if (null == value) {
041             super.setValueInternal(null);
042         else if (value instanceof CharSequence) {
043             handleAsString(String.valueOf(value));
044         else if (value instanceof List) {
045             handleAsList((Listvalue);
046         else if (value instanceof Map) {
047             handleAsMap((Mapvalue);
048         else if (value instanceof Number) {
049             handleAsNumber((Numbervalue);
050         else if (value instanceof Point2D) {
051             super.setValueInternal(value);
052         else {
053             throw illegalValue(value, Point2D.class);
054         }
055     }
056 
057     private void handleAsString(String str) {
058         if (isBlank(str)) {
059             super.setValueInternal(null);
060             return;
061         }
062 
063         String[] parts = str.split(",");
064         switch (parts.length) {
065             case 1:
066                 double s = parseValue(parts[0]);
067                 super.setValueInternal(new Point2D.Double(s, s));
068                 break;
069             case 2:
070                 double x = parseValue(parts[0]);
071                 double y = parseValue(parts[1]);
072                 super.setValueInternal(new Point2D.Double(x, y));
073                 break;
074             default:
075                 throw illegalValue(str, Point2D.class);
076         }
077     }
078 
079     private void handleAsList(List<?> list) {
080         if(list.isEmpty()) {
081             super.setValueInternal(null);
082             return;
083         }
084 
085         switch (list.size()) {
086             case 1:
087                 double s = parseValue(list.get(0));
088                 super.setValueInternal(new Point2D.Double(s, s));
089                 break;
090             case 2:
091                 double x = parseValue(list.get(0));
092                 double y = parseValue(list.get(1));
093                 super.setValueInternal(new Point2D.Double(x, y));
094                 break;
095             default:
096                 throw illegalValue(list, Point2D.class);
097         }
098     }
099 
100     private void handleAsMap(Map<?, ?> map) {
101         if(map.isEmpty()) {
102             super.setValueInternal(null);
103             return;
104         }
105 
106         double x = getMapValue(map, "x"0);
107         double y = getMapValue(map, "y"0);
108         super.setValueInternal(new Point2D.Double(x, y));
109     }
110 
111     private double parseValue(Object value) {
112         if (value instanceof CharSequence) {
113             return parse(String.valueOf(value));
114         else if (value instanceof Number) {
115             return parse((Numbervalue);
116         }
117         throw illegalValue(value, Point2D.class);
118     }
119 
120     private double parse(String val) {
121         try {
122             return Double.parseDouble(val.trim());
123         catch (NumberFormatException e) {
124             throw illegalValue(val, Point2D.class, e);
125         }
126     }
127 
128     private double parse(Number val) {
129         return val.doubleValue();
130     }
131 
132     private double getMapValue(Map<?, ?> map, String key, int defaultValue) {
133         Object val = map.get(key);
134         if (null == val) {
135             return defaultValue;
136         else if (val instanceof CharSequence) {
137             return parse(String.valueOf(val));
138         else if (val instanceof Number) {
139             return parse((Numberval);
140         }
141         throw illegalValue(map, Point2D.class);
142     }
143 
144     private void handleAsNumber(Number value) {
145         double s = parse(value);
146         super.setValueInternal(new Point2D.Double(s, s));
147     }
148 }