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