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