001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.pivot.editors;
019
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 import org.apache.pivot.wtk.Dimensions;
023
024 import java.util.List;
025 import java.util.Map;
026
027 import static griffon.util.GriffonNameUtils.isBlank;
028
029 /**
030 * @author Andres Almiray
031 * @since 2.0.0
032 */
033 @PropertyEditorFor(Dimensions.class)
034 public class DimensionsPropertyEditor extends AbstractPropertyEditor {
035 @Override
036 public String getAsText() {
037 if (null == getValue()) return null;
038 Dimensions dimension = (Dimensions) getValue();
039 return dimension.width + ", " + dimension.height;
040 }
041
042 @Override
043 protected void setValueInternal(Object value) {
044 if (null == value) {
045 super.setValueInternal(null);
046 } else if (value instanceof CharSequence) {
047 handleAsString(String.valueOf(value));
048 } else if (value instanceof List) {
049 handleAsList((List) value);
050 } else if (value instanceof Map) {
051 handleAsMap((Map) value);
052 } else if (value instanceof Number) {
053 handleAsNumber((Number) value);
054 } else if (value instanceof Dimensions) {
055 super.setValueInternal(value);
056 } else {
057 throw illegalValue(value, Dimensions.class);
058 }
059 }
060
061 protected void handleAsString(String str) {
062 if (isBlank(str)) {
063 super.setValueInternal(null);
064 return;
065 }
066
067 String[] parts = str.split(",");
068 switch (parts.length) {
069 case 1:
070 int s = parseValue(parts[0]);
071 super.setValueInternal(new Dimensions(s, s));
072 break;
073 case 2:
074 int w = parseValue(parts[0]);
075 int h = parseValue(parts[1]);
076 super.setValueInternal(new Dimensions(w, h));
077 break;
078 default:
079 throw illegalValue(str, Dimensions.class);
080 }
081 }
082
083 protected void handleAsList(List<?> list) {
084 if(list.isEmpty()) {
085 super.setValueInternal(null);
086 return;
087 }
088
089 switch (list.size()) {
090 case 1:
091 int s = parseValue(list.get(0));
092 super.setValueInternal(new Dimensions(s, s));
093 break;
094 case 2:
095 int w = parseValue(list.get(0));
096 int h = parseValue(list.get(1));
097 super.setValueInternal(new Dimensions(w, h));
098 break;
099 default:
100 throw illegalValue(list, Dimensions.class);
101 }
102 }
103
104 protected void handleAsMap(Map<?, ?> map) {
105 if(map.isEmpty()) {
106 super.setValueInternal(null);
107 return;
108 }
109
110 int w = getMapValue(map, "width", 0);
111 int h = getMapValue(map, "height", 0);
112 super.setValueInternal(new Dimensions(w, h));
113 }
114
115 protected int parseValue(Object value) {
116 if (value instanceof CharSequence) {
117 return parse(String.valueOf(value));
118 } else if (value instanceof Number) {
119 return parse((Number) value);
120 }
121 throw illegalValue(value, Dimensions.class);
122 }
123
124 protected int parse(String val) {
125 try {
126 return Integer.parseInt(val.trim());
127 } catch (NumberFormatException e) {
128 throw illegalValue(val, Dimensions.class, e);
129 }
130 }
131
132 protected int parse(Number val) {
133 return val.intValue();
134 }
135
136 protected int getMapValue(Map<?, ?> map, String key, int defaultValue) {
137 Object val = map.get(key);
138 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
139 if (null == val) {
140 return defaultValue;
141 } else if (val instanceof CharSequence) {
142 return parse(String.valueOf(val));
143 } else if (val instanceof Number) {
144 return parse((Number) val);
145 }
146 throw illegalValue(map, Dimensions.class);
147 }
148
149 protected void handleAsNumber(Number value) {
150 int s = parse(value);
151 super.setValueInternal(new Dimensions(s, s));
152 }
153 }
|