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.Rectangle;
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(Rectangle.class)
033 public class RectanglePropertyEditor extends AbstractPropertyEditor {
034 public String getAsText() {
035 if (null == getValue()) return null;
036 Rectangle r = (Rectangle) getValue();
037 return r.getX() + ", " + r.getY() + ", " + r.getWidth() + ", " + r.getHeight();
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 Rectangle) {
050 super.setValueInternal(value);
051 } else {
052 throw illegalValue(value, Rectangle.class);
053 }
054 }
055
056 private 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 int x = parseValue(parts[0]);
066 int y = parseValue(parts[1]);
067 int w = parseValue(parts[2]);
068 int h = parseValue(parts[3]);
069 super.setValueInternal(new Rectangle(x, y, w, h));
070 break;
071 default:
072 throw illegalValue(str, Rectangle.class);
073 }
074 }
075
076 private void handleAsList(List<?> list) {
077 if(list.isEmpty()) {
078 super.setValueInternal(null);
079 return;
080 }
081
082 switch (list.size()) {
083 case 4:
084 int x = parseValue(list.get(0));
085 int y = parseValue(list.get(1));
086 int w = parseValue(list.get(2));
087 int h = parseValue(list.get(3));
088 super.setValueInternal(new Rectangle(x, y, w, h));
089 break;
090 default:
091 throw illegalValue(list, Rectangle.class);
092 }
093 }
094
095 private void handleAsMap(Map<?, ?> map) {
096 if(map.isEmpty()) {
097 super.setValueInternal(null);
098 return;
099 }
100
101 int x = getMapValue(map, "x", 0);
102 int y = getMapValue(map, "y", 0);
103 int w = getMapValue(map, "width", 0);
104 int h = getMapValue(map, "height", 0);
105 super.setValueInternal(new Rectangle(x, y, w, h));
106 }
107
108 private int 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, Rectangle.class);
115 }
116
117 private int parse(String val) {
118 try {
119 return Integer.parseInt(val.trim());
120 } catch (NumberFormatException e) {
121 throw illegalValue(val, Rectangle.class, e);
122 }
123 }
124
125 private int parse(Number val) {
126 return val.intValue();
127 }
128
129 private int getMapValue(Map<?, ?> map, String key, int 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, Rectangle.class);
140 }
141 }
|