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