PointPropertyEditor.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.Point;
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(Point.class)
033 public class PointPropertyEditor extends AbstractPropertyEditor {
034     @Override
035     public String getAsText() {
036         if (null == getValue()) return null;
037         Point p = (PointgetValue();
038         return p.getX() ", " + p.getY();
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 Number) {
052             handleAsNumber((Numbervalue);
053         else if (value instanceof Point) {
054             super.setValueInternal(value);
055         else {
056             throw illegalValue(value, Point.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 1:
069                 int s = parseValue(parts[0]);
070                 super.setValueInternal(new Point(s, s));
071                 break;
072             case 2:
073                 int x = parseValue(parts[0]);
074                 int y = parseValue(parts[1]);
075                 super.setValueInternal(new Point(x, y));
076                 break;
077             default:
078                 throw illegalValue(str, Point.class);
079         }
080     }
081 
082     protected void handleAsList(List<?> list) {
083         if(list.isEmpty()) {
084             super.setValueInternal(null);
085             return;
086         }
087 
088         switch (list.size()) {
089             case 1:
090                 int s = parseValue(list.get(0));
091                 super.setValueInternal(new Point(s, s));
092                 break;
093             case 2:
094                 int x = parseValue(list.get(0));
095                 int y = parseValue(list.get(1));
096                 super.setValueInternal(new Point(x, y));
097                 break;
098             default:
099                 throw illegalValue(list, Point.class);
100         }
101     }
102 
103     protected void handleAsMap(Map<?, ?> map) {
104         if(map.isEmpty()) {
105             super.setValueInternal(null);
106             return;
107         }
108 
109         int x = getMapValue(map, "x"0);
110         int y = getMapValue(map, "y"0);
111         super.setValueInternal(new Point(x, y));
112     }
113 
114     protected int parseValue(Object value) {
115         if (value instanceof CharSequence) {
116             return parse(String.valueOf(value));
117         else if (value instanceof Number) {
118             return parse((Numbervalue);
119         }
120         throw illegalValue(value, Point.class);
121     }
122 
123     protected int parse(String val) {
124         try {
125             return Integer.parseInt(val.trim());
126         catch (NumberFormatException e) {
127             throw illegalValue(val, Point.class, e);
128         }
129     }
130 
131     protected int parse(Number val) {
132         return val.intValue();
133     }
134 
135     protected int getMapValue(Map<?, ?> map, String key, int defaultValue) {
136         Object val = map.get(key);
137         if (null == val) {
138             return defaultValue;
139         else if (val instanceof CharSequence) {
140             return parse(String.valueOf(val));
141         else if (val instanceof Number) {
142             return parse((Numberval);
143         }
144         throw illegalValue(map, Point.class);
145     }
146 
147     protected void handleAsNumber(Number value) {
148         int s = parse(value);
149         super.setValueInternal(new Point(s, s));
150     }
151 }