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