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.Insets;
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(Insets.class)
032 public class InsetsPropertyEditor extends AbstractPropertyEditor {
033 @Override
034 public String getAsText() {
035 if (null == getValue()) return null;
036 Insets i = (Insets) getValue();
037 return i.top + ", " + i.left + ", " + i.bottom + ", " + i.right;
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 Insets) {
053 super.setValueInternal(value);
054 } else {
055 throw illegalValue(value, Insets.class);
056 }
057 }
058
059 protected void handleAsString(String str) {
060 if (isBlank(str)) {
061 super.setValueInternal(null);
062 return;
063 }
064
065 int t = 0;
066 int l = 0;
067 int r = 0;
068 int b = 0;
069 String[] parts = str.split(",");
070 switch (parts.length) {
071 case 4:
072 b = parseValue(parts[3]);
073 case 3:
074 r = parseValue(parts[2]);
075 case 2:
076 l = parseValue(parts[1]);
077 case 1:
078 t = parseValue(parts[0]);
079 super.setValueInternal(new Insets(t, l, r, b));
080 break;
081 default:
082 throw illegalValue(str, Insets.class);
083 }
084 }
085
086 protected void handleAsList(List<?> list) {
087 if(list.isEmpty()) {
088 super.setValueInternal(null);
089 return;
090 }
091
092 int t = 0;
093 int l = 0;
094 int r = 0;
095 int b = 0;
096 switch (list.size()) {
097 case 4:
098 b = parseValue(list.get(3));
099 case 3:
100 r = parseValue(list.get(2));
101 case 2:
102 l = parseValue(list.get(1));
103 case 1:
104 t = parseValue(list.get(0));
105 super.setValueInternal(new Insets(t, l, r, b));
106 break;
107 default:
108 throw illegalValue(list, Insets.class);
109 }
110 }
111
112 protected void handleAsMap(Map<?, ?> map) {
113 if(map.isEmpty()) {
114 super.setValueInternal(null);
115 return;
116 }
117
118 int t = getMapValue(map, "top", 0);
119 int l = getMapValue(map, "left", 0);
120 int r = getMapValue(map, "right", 0);
121 int b = getMapValue(map, "bottom", 0);
122 super.setValueInternal(new Insets(t, l, r, b));
123 }
124
125 protected int parseValue(Object value) {
126 if (value instanceof CharSequence) {
127 return parse(String.valueOf(value));
128 } else if (value instanceof Number) {
129 return parse((Number) value);
130 }
131 throw illegalValue(value, Insets.class);
132 }
133
134 protected int parse(String val) {
135 try {
136 return Integer.parseInt(val.trim());
137 } catch (NumberFormatException e) {
138 throw illegalValue(val, Insets.class, e);
139 }
140 }
141
142 protected int parse(Number val) {
143 return val.intValue();
144 }
145
146 protected int getMapValue(Map<?, ?> map, String key, int defaultValue) {
147 Object val = map.get(key);
148 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
149 if (null == val) {
150 return defaultValue;
151 } else if (val instanceof CharSequence) {
152 return parse(String.valueOf(val));
153 } else if (val instanceof Number) {
154 return parse((Number) val);
155 }
156 throw illegalValue(map, Insets.class);
157 }
158
159 protected void handleAsNumber(Number value) {
160 int c = parse(value);
161 super.setValueInternal(new Insets(c, c, c, c));
162 }
163 }
|