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