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