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.swing.editors;
017
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020
021 import java.awt.Point;
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 * @author Alexander Klein
030 * @since 2.0.0
031 */
032 @PropertyEditorFor(Point.class)
033 public class PointPropertyEditor extends AbstractPropertyEditor {
034 public String getAsText() {
035 if (null == getValue()) return null;
036 Point p = (Point) getValue();
037 return p.getX() + ", " + p.getY();
038 }
039
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 Number) {
050 handleAsNumber((Number) value);
051 } else if (value instanceof Point) {
052 super.setValueInternal(value);
053 } else {
054 throw illegalValue(value, Point.class);
055 }
056 }
057
058 private 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 1:
067 int s = parseValue(parts[0]);
068 super.setValueInternal(new Point(s, s));
069 break;
070 case 2:
071 int x = parseValue(parts[0]);
072 int y = parseValue(parts[1]);
073 super.setValueInternal(new Point(x, y));
074 break;
075 default:
076 throw illegalValue(str, Point.class);
077 }
078 }
079
080 private void handleAsList(List<?> list) {
081 if(list.isEmpty()) {
082 super.setValueInternal(null);
083 return;
084 }
085
086 switch (list.size()) {
087 case 1:
088 int s = parseValue(list.get(0));
089 super.setValueInternal(new Point(s, s));
090 break;
091 case 2:
092 int x = parseValue(list.get(0));
093 int y = parseValue(list.get(1));
094 super.setValueInternal(new Point(x, y));
095 break;
096 default:
097 throw illegalValue(list, Point.class);
098 }
099 }
100
101 private void handleAsMap(Map<?, ?> map) {
102 if(map.isEmpty()) {
103 super.setValueInternal(null);
104 return;
105 }
106
107 int x = getMapValue(map, "x", 0);
108 int y = getMapValue(map, "y", 0);
109 super.setValueInternal(new Point(x, y));
110 }
111
112 private int parseValue(Object value) {
113 if (value instanceof CharSequence) {
114 return parse(String.valueOf(value));
115 } else if (value instanceof Number) {
116 return parse((Number) value);
117 }
118 throw illegalValue(value, Point.class);
119 }
120
121 private int parse(String val) {
122 try {
123 return Integer.parseInt(val.trim());
124 } catch (NumberFormatException e) {
125 throw illegalValue(val, Point.class, e);
126 }
127 }
128
129 private int parse(Number val) {
130 return val.intValue();
131 }
132
133 private int getMapValue(Map<?, ?> map, String key, int defaultValue) {
134 Object val = map.get(key);
135 if (null == val) {
136 return defaultValue;
137 } else if (val instanceof CharSequence) {
138 return parse(String.valueOf(val));
139 } else if (val instanceof Number) {
140 return parse((Number) val);
141 }
142 throw illegalValue(map, Point.class);
143 }
144
145 private void handleAsNumber(Number value) {
146 int s = parse(value);
147 super.setValueInternal(new Point(s, s));
148 }
149 }
|