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.Dimension;
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(Dimension.class)
033 public class DimensionPropertyEditor extends AbstractPropertyEditor {
034 public String getAsText() {
035 if (null == getValue()) return null;
036 Dimension dimension = (Dimension) getValue();
037 return dimension.getWidth() + ", " + dimension.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 Number) {
050 handleAsNumber((Number) value);
051 } else if (value instanceof Dimension) {
052 super.setValueInternal(value);
053 } else {
054 throw illegalValue(value, Dimension.class);
055 }
056 }
057
058 private void handleAsString(String str) {
059 if (isBlank(str)) {
060 super.setValueInternal(null);
061 return;
062 }
063 String[] parts = str.split(",");
064 switch (parts.length) {
065 case 1:
066 int s = parseValue(parts[0]);
067 super.setValueInternal(new Dimension(s, s));
068 break;
069 case 2:
070 int w = parseValue(parts[0]);
071 int h = parseValue(parts[1]);
072 super.setValueInternal(new Dimension(w, h));
073 break;
074 default:
075 throw illegalValue(str, Dimension.class);
076 }
077 }
078
079 private void handleAsList(List<?> list) {
080 if(list.isEmpty()) {
081 super.setValueInternal(null);
082 return;
083 }
084
085 switch (list.size()) {
086 case 1:
087 int s = parseValue(list.get(0));
088 super.setValueInternal(new Dimension(s, s));
089 break;
090 case 2:
091 int w = parseValue(list.get(0));
092 int h = parseValue(list.get(1));
093 super.setValueInternal(new Dimension(w, h));
094 break;
095 default:
096 throw illegalValue(list, Dimension.class);
097 }
098 }
099
100 private void handleAsMap(Map<?, ?> map) {
101 if(map.isEmpty()) {
102 super.setValueInternal(null);
103 return;
104 }
105
106 int w = getMapValue(map, "width", 0);
107 int h = getMapValue(map, "height", 0);
108 super.setValueInternal(new Dimension(w, h));
109 }
110
111 private int parseValue(Object value) {
112 if (value instanceof CharSequence) {
113 return parse(String.valueOf(value));
114 } else if (value instanceof Number) {
115 return parse((Number) value);
116 }
117 throw illegalValue(value, Dimension.class);
118 }
119
120 private int parse(String val) {
121 try {
122 return Integer.parseInt(val.trim());
123 } catch (NumberFormatException e) {
124 throw illegalValue(val, Dimension.class, e);
125 }
126 }
127
128 private int parse(Number val) {
129 return val.intValue();
130 }
131
132 private int getMapValue(Map<?, ?> map, String key, int defaultValue) {
133 Object val = map.get(key);
134 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
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, Dimension.class);
143 }
144
145 private void handleAsNumber(Number value) {
146 int s = parse(value);
147 super.setValueInternal(new Dimension(s, s));
148 }
149 }
|