001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.javafx.editors;
019
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 import javafx.geometry.Rectangle2D;
023
024 import java.util.List;
025 import java.util.Map;
026
027 import static griffon.util.GriffonNameUtils.isBlank;
028
029 /**
030 * @author Andres Almiray
031 */
032 @PropertyEditorFor(Rectangle2D.class)
033 public class Rectangle2DPropertyEditor extends AbstractPropertyEditor {
034 @Override
035 public String getAsText() {
036 if (null == getValue()) return null;
037 Rectangle2D r = (Rectangle2D) getValue();
038 return r.getMinX() + ", " + r.getMinY() + ", " + r.getWidth() + ", " + r.getHeight();
039 }
040
041 @Override
042 protected void setValueInternal(Object value) {
043 if (null == value) {
044 super.setValueInternal(null);
045 } else if (value instanceof CharSequence) {
046 handleAsString(String.valueOf(value));
047 } else if (value instanceof List) {
048 handleAsList((List) value);
049 } else if (value instanceof Map) {
050 handleAsMap((Map) value);
051 } else if (value instanceof Rectangle2D) {
052 super.setValueInternal(value);
053 } else {
054 throw illegalValue(value, Rectangle2D.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 4:
067 double x = parseValue(parts[0]);
068 double y = parseValue(parts[1]);
069 double w = parseValue(parts[2]);
070 double h = parseValue(parts[3]);
071 super.setValueInternal(new Rectangle2D(x, y, w, h));
072 break;
073 default:
074 throw illegalValue(str, Rectangle2D.class);
075 }
076 }
077
078 protected void handleAsList(List<?> list) {
079 if (list.isEmpty()) {
080 super.setValueInternal(null);
081 return;
082 }
083
084 switch (list.size()) {
085 case 4:
086 double x = parseValue(list.get(0));
087 double y = parseValue(list.get(1));
088 double w = parseValue(list.get(2));
089 double h = parseValue(list.get(3));
090 super.setValueInternal(new Rectangle2D(x, y, w, h));
091 break;
092 default:
093 throw illegalValue(list, Rectangle2D.class);
094 }
095 }
096
097 protected void handleAsMap(Map<?, ?> map) {
098 if (map.isEmpty()) {
099 super.setValueInternal(null);
100 return;
101 }
102
103 double x = getMapValue(map, "x", 0);
104 double y = getMapValue(map, "y", 0);
105 double w = getMapValue(map, "width", 0);
106 double h = getMapValue(map, "height", 0);
107 super.setValueInternal(new Rectangle2D(x, y, w, h));
108 }
109
110 protected double parseValue(Object value) {
111 if (value instanceof CharSequence) {
112 return parse(String.valueOf(value));
113 } else if (value instanceof Number) {
114 return parse((Number) value);
115 }
116 throw illegalValue(value, Rectangle2D.class);
117 }
118
119 protected double parse(String val) {
120 try {
121 return Double.parseDouble(val.trim());
122 } catch (NumberFormatException e) {
123 throw illegalValue(val, Rectangle2D.class, e);
124 }
125 }
126
127 protected double parse(Number val) {
128 return val.doubleValue();
129 }
130
131 protected double getMapValue(Map<?, ?> map, String key, double defaultValue) {
132 Object val = map.get(key);
133 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
134 if (null == val) {
135 return defaultValue;
136 } else if (val instanceof CharSequence) {
137 return parse(String.valueOf(val));
138 } else if (val instanceof Number) {
139 return parse((Number) val);
140 }
141 throw illegalValue(map, Rectangle2D.class);
142 }
143 }
|