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