BoundsPropertyEditor.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.pivot.editors;
019 
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 import org.apache.pivot.wtk.Bounds;
023 
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(Bounds.class)
034 public class BoundsPropertyEditor extends AbstractPropertyEditor {
035     @Override
036     public String getAsText() {
037         if (null == getValue()) return null;
038         Bounds r = (BoundsgetValue();
039         return r.x + ", " + r.y + ", " + r.width + ", " + r.height;
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 Bounds) {
053             super.setValueInternal(value);
054         else {
055             throw illegalValue(value, Bounds.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                 int x = parseValue(parts[0]);
069                 int y = parseValue(parts[1]);
070                 int w = parseValue(parts[2]);
071                 int h = parseValue(parts[3]);
072                 super.setValueInternal(new Bounds(x, y, w, h));
073                 break;
074             default:
075                 throw illegalValue(str, Bounds.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                 int x = parseValue(list.get(0));
088                 int y = parseValue(list.get(1));
089                 int w = parseValue(list.get(2));
090                 int h = parseValue(list.get(3));
091                 super.setValueInternal(new Bounds(x, y, w, h));
092                 break;
093             default:
094                 throw illegalValue(list, Bounds.class);
095         }
096     }
097 
098     protected void handleAsMap(Map<?, ?> map) {
099         if(map.isEmpty()) {
100             super.setValueInternal(null);
101             return;
102         }
103 
104         int x = getMapValue(map, "x"0);
105         int y = getMapValue(map, "y"0);
106         int w = getMapValue(map, "width"0);
107         int h = getMapValue(map, "height"0);
108         super.setValueInternal(new Bounds(x, y, w, h));
109     }
110 
111     protected int 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, Bounds.class);
118     }
119 
120     protected int parse(String val) {
121         try {
122             return Integer.parseInt(val.trim());
123         catch (NumberFormatException e) {
124             throw illegalValue(val, Bounds.class, e);
125         }
126     }
127 
128     protected int parse(Number val) {
129         return val.intValue();
130     }
131 
132     protected int getMapValue(Map<?, ?> map, String key, int 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, Bounds.class);
143     }
144 }