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