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