DimensionPropertyEditor.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.Dimension;
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(Dimension.class)
035 public class DimensionPropertyEditor extends AbstractPropertyEditor {
036     @Override
037     public String getAsText() {
038         if (null == getValue()) return null;
039         Dimension dimension = (DimensiongetValue();
040         return dimension.getWidth() ", " + dimension.getHeight();
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 Dimension) {
056             super.setValueInternal(value);
057         else {
058             throw illegalValue(value, Dimension.class);
059         }
060     }
061 
062     protected void handleAsString(String str) {
063         if (isBlank(str)) {
064             super.setValueInternal(null);
065             return;
066         }
067         String[] parts = str.split(",");
068         switch (parts.length) {
069             case 1:
070                 int s = parseValue(parts[0]);
071                 super.setValueInternal(new Dimension(s, s));
072                 break;
073             case 2:
074                 int w = parseValue(parts[0]);
075                 int h = parseValue(parts[1]);
076                 super.setValueInternal(new Dimension(w, h));
077                 break;
078             default:
079                 throw illegalValue(str, Dimension.class);
080         }
081     }
082 
083     protected void handleAsList(List<?> list) {
084         if(list.isEmpty()) {
085             super.setValueInternal(null);
086             return;
087         }
088 
089         switch (list.size()) {
090             case 1:
091                 int s = parseValue(list.get(0));
092                 super.setValueInternal(new Dimension(s, s));
093                 break;
094             case 2:
095                 int w = parseValue(list.get(0));
096                 int h = parseValue(list.get(1));
097                 super.setValueInternal(new Dimension(w, h));
098                 break;
099             default:
100                 throw illegalValue(list, Dimension.class);
101         }
102     }
103 
104     protected void handleAsMap(Map<?, ?> map) {
105         if(map.isEmpty()) {
106             super.setValueInternal(null);
107             return;
108         }
109 
110         int w = getMapValue(map, "width"0);
111         int h = getMapValue(map, "height"0);
112         super.setValueInternal(new Dimension(w, h));
113     }
114 
115     protected int parseValue(Object value) {
116         if (value instanceof CharSequence) {
117             return parse(String.valueOf(value));
118         else if (value instanceof Number) {
119             return parse((Numbervalue);
120         }
121         throw illegalValue(value, Dimension.class);
122     }
123 
124     protected int parse(String val) {
125         try {
126             return Integer.parseInt(val.trim());
127         catch (NumberFormatException e) {
128             throw illegalValue(val, Dimension.class, e);
129         }
130     }
131 
132     protected int parse(Number val) {
133         return val.intValue();
134     }
135 
136     protected int getMapValue(Map<?, ?> map, String key, int defaultValue) {
137         Object val = map.get(key);
138         if (null == valval = map.get(String.valueOf(key.charAt(0)));
139         if (null == val) {
140             return defaultValue;
141         else if (val instanceof CharSequence) {
142             return parse(String.valueOf(val));
143         else if (val instanceof Number) {
144             return parse((Numberval);
145         }
146         throw illegalValue(map, Dimension.class);
147     }
148 
149     protected void handleAsNumber(Number value) {
150         int s = parse(value);
151         super.setValueInternal(new Dimension(s, s));
152     }
153 }