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.javafx.editors;
017
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 import javafx.geometry.Rectangle2D;
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(Rectangle2D.class)
031 public class Rectangle2DPropertyEditor extends AbstractPropertyEditor {
032 public String getAsText() {
033 if (null == getValue()) return null;
034 Rectangle2D r = (Rectangle2D) getValue();
035 return r.getMinX() + ", " + r.getMinY() + ", " + r.getWidth() + ", " + r.getHeight();
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((List) value);
045 } else if (value instanceof Map) {
046 handleAsMap((Map) value);
047 } else if (value instanceof Rectangle2D) {
048 super.setValueInternal(value);
049 } else {
050 throw illegalValue(value, Rectangle2D.class);
051 }
052 }
053
054 private void handleAsString(String str) {
055 if (isBlank(str)) {
056 super.setValueInternal(null);
057 return;
058 }
059
060 String[] parts = str.split(",");
061 switch (parts.length) {
062 case 4:
063 double x = parseValue(parts[0]);
064 double y = parseValue(parts[1]);
065 double w = parseValue(parts[2]);
066 double h = parseValue(parts[3]);
067 super.setValueInternal(new Rectangle2D(x, y, w, h));
068 break;
069 default:
070 throw illegalValue(str, Rectangle2D.class);
071 }
072 }
073
074 private void handleAsList(List<?> list) {
075 if (list.isEmpty()) {
076 super.setValueInternal(null);
077 return;
078 }
079
080 switch (list.size()) {
081 case 4:
082 double x = parseValue(list.get(0));
083 double y = parseValue(list.get(1));
084 double w = parseValue(list.get(2));
085 double h = parseValue(list.get(3));
086 super.setValueInternal(new Rectangle2D(x, y, w, h));
087 break;
088 default:
089 throw illegalValue(list, Rectangle2D.class);
090 }
091 }
092
093 private void handleAsMap(Map<?, ?> map) {
094 if (map.isEmpty()) {
095 super.setValueInternal(null);
096 return;
097 }
098
099 double x = getMapValue(map, "x", 0);
100 double y = getMapValue(map, "y", 0);
101 double w = getMapValue(map, "width", 0);
102 double h = getMapValue(map, "height", 0);
103 super.setValueInternal(new Rectangle2D(x, y, w, h));
104 }
105
106 private double parseValue(Object value) {
107 if (value instanceof CharSequence) {
108 return parse(String.valueOf(value));
109 } else if (value instanceof Number) {
110 return parse((Number) value);
111 }
112 throw illegalValue(value, Rectangle2D.class);
113 }
114
115 private double parse(String val) {
116 try {
117 return Double.parseDouble(val.trim());
118 } catch (NumberFormatException e) {
119 throw illegalValue(val, Rectangle2D.class, e);
120 }
121 }
122
123 private double parse(Number val) {
124 return val.doubleValue();
125 }
126
127 private double getMapValue(Map<?, ?> map, String key, double defaultValue) {
128 Object val = map.get(key);
129 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
130 if (null == val) {
131 return defaultValue;
132 } else if (val instanceof CharSequence) {
133 return parse(String.valueOf(val));
134 } else if (val instanceof Number) {
135 return parse((Number) val);
136 }
137 throw illegalValue(map, Rectangle2D.class);
138 }
139 }
|