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