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