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