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