001 /*
002 * Copyright 2008-2014 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.javafx.editors;
017
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 import javafx.geometry.Insets;
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 */
030 @PropertyEditorFor(Insets.class)
031 public class InsetsPropertyEditor extends AbstractPropertyEditor {
032 public String getAsText() {
033 if (null == getValue()) return null;
034 Insets i = (Insets) getValue();
035 return i.getTop() + ", " + i.getRight() + ", " + i.getBottom() + ", " + i.getLeft();
036 }
037
038 protected void setValueInternal(Object value) {
039 if (null == value) {
040 super.setValueInternal(null);
041 } else if (value instanceof CharSequence) {
042 handleAsString(String.valueOf(value));
043 } else if (value instanceof List) {
044 handleAsList((List) value);
045 } else if (value instanceof Map) {
046 handleAsMap((Map) value);
047 } else if (value instanceof Number) {
048 handleAsNumber((Number) value);
049 } else if (value instanceof Insets) {
050 super.setValueInternal(value);
051 } else {
052 throw illegalValue(value, Insets.class);
053 }
054 }
055
056 private void handleAsString(String str) {
057 if (isBlank(str)) {
058 super.setValueInternal(null);
059 return;
060 }
061
062 double t = 0;
063 double l = 0;
064 double r = 0;
065 double b = 0;
066 String[] parts = str.split(",");
067 switch (parts.length) {
068 case 4:
069 l = parseValue(parts[3]);
070 case 3:
071 b = parseValue(parts[2]);
072 case 2:
073 r = parseValue(parts[1]);
074 case 1:
075 t = parseValue(parts[0]);
076 super.setValueInternal(new Insets(t, r, b, l));
077 break;
078 default:
079 throw illegalValue(str, Insets.class);
080 }
081 }
082
083 private void handleAsList(List<?> list) {
084 if (list.isEmpty()) {
085 super.setValueInternal(null);
086 return;
087 }
088
089 double t = 0;
090 double l = 0;
091 double r = 0;
092 double b = 0;
093 switch (list.size()) {
094 case 4:
095 l = parseValue(list.get(3));
096 case 3:
097 b = parseValue(list.get(2));
098 case 2:
099 r = parseValue(list.get(1));
100 case 1:
101 t = parseValue(list.get(0));
102 super.setValueInternal(new Insets(t, r, b, l));
103 break;
104 default:
105 throw illegalValue(list, Insets.class);
106 }
107 }
108
109 private void handleAsMap(Map<?, ?> map) {
110 if (map.isEmpty()) {
111 super.setValueInternal(null);
112 return;
113 }
114
115 double t = getMapValue(map, "top", 0);
116 double l = getMapValue(map, "left", 0);
117 double r = getMapValue(map, "right", 0);
118 double b = getMapValue(map, "bottom", 0);
119 super.setValueInternal(new Insets(t, r, b, l));
120 }
121
122 private double parseValue(Object value) {
123 if (value instanceof CharSequence) {
124 return parse(String.valueOf(value));
125 } else if (value instanceof Number) {
126 return parse((Number) value);
127 }
128 throw illegalValue(value, Insets.class);
129 }
130
131 private double parse(String val) {
132 try {
133 return Double.parseDouble(String.valueOf(val).trim());
134 } catch (NumberFormatException e) {
135 throw illegalValue(val, Insets.class, e);
136 }
137 }
138
139 private double parse(Number val) {
140 return val.doubleValue();
141 }
142
143 private double getMapValue(Map<?, ?> map, String key, double defaultValue) {
144 Object val = map.get(key);
145 if (null == val) val = map.get(String.valueOf(key.charAt(0)));
146 if (null == val) {
147 return defaultValue;
148 } else if (val instanceof CharSequence) {
149 return parse(String.valueOf(val));
150 } else if (val instanceof Number) {
151 return parse((Number) val);
152 }
153 throw illegalValue(map, Insets.class);
154 }
155
156 private void handleAsNumber(Number value) {
157 double c = parse(value);
158 super.setValueInternal(new Insets(c, c, c, c));
159 }
160 }
|