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