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