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