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