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