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