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