FontPropertyEditor.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.swing.editors;
019 
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 
023 import java.awt.Font;
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(Font.class)
033 public class FontPropertyEditor extends AbstractPropertyEditor {
034     @Override
035     public String getAsText() {
036         if (null == getValue()) { return null}
037         Font font = (FontgetValue();
038         return font.getFamily() "-" + formatStyle(font"-" + font.getSize();
039     }
040 
041     protected String formatStyle(Font font) {
042         if (font.isBold() && font.isItalic()) {
043             return "BOLDITALIC";
044         else if (font.isBold()) {
045             return "BOLD";
046         else if (font.isItalic()) {
047             return "ITALIC";
048         }
049         return "PLAIN";
050     }
051 
052     @Override
053     protected void setValueInternal(Object value) {
054         if (null == value) {
055             super.setValueInternal(null);
056         else if (value instanceof CharSequence) {
057             handleAsString(String.valueOf(value));
058         else if (value instanceof List) {
059             handleAsList((Listvalue);
060         else if (value instanceof Map) {
061             handleAsMap((Mapvalue);
062         else if (value instanceof Font) {
063             super.setValueInternal(value);
064         else {
065             throw illegalValue(value, Font.class);
066         }
067     }
068 
069     protected void handleAsString(String str) {
070         if (isBlank(str)) {
071             super.setValueInternal(null);
072             return;
073         }
074         String[] parts = str.split("-");
075         if (parts.length != 3) {
076             throw illegalValue(str, Font.class);
077         }
078 
079         String family = parts[0];
080         int style = resolveStyle(str, parts[1]);
081         int size = parseSize(str, parts[2]);
082 
083         super.setValueInternal(new Font(family, style, size));
084     }
085 
086     protected void handleAsList(List<?> list) {
087         if (list.isEmpty()) {
088             super.setValueInternal(null);
089             return;
090         }
091 
092         if (list.size() != 3) {
093             throw illegalValue(list, Font.class);
094         }
095 
096         String family = String.valueOf(list.get(0));
097         int style = resolveStyle(list, String.valueOf(list.get(1)));
098         int size = parseSize(list, String.valueOf(list.get(2)));
099 
100         super.setValueInternal(new Font(family, style, size));
101     }
102 
103     protected void handleAsMap(Map<?, ?> map) {
104         if (map.isEmpty()) {
105             super.setValueInternal(null);
106             return;
107         }
108 
109         String family = getMapValue(map, "family""");
110         String style = getMapValue(map, "style""");
111         String size = getMapValue(map, "size""");
112         super.setValueInternal(new Font(family, resolveStyle(map, style), parseSize(map, size)));
113     }
114 
115     protected String getMapValue(Map<?, ?> map, String key, String defaultValue) {
116         Object val = map.get(key);
117         if (null == val) { val = map.get(String.valueOf(key.charAt(0)))}
118         if (null == val) {
119             return defaultValue;
120         else if (val instanceof CharSequence) {
121             return String.valueOf(val).trim();
122         }
123         throw illegalValue(map, Font.class);
124     }
125 
126     protected int parseSize(Object source, String str) {
127         int size;
128         try {
129             size = Integer.parseInt(str);
130         catch (NumberFormatException nfe) {
131             throw illegalValue(source, Font.class);
132         }
133         return size;
134     }
135 
136     protected int resolveStyle(Object source, String str) {
137         if ("PLAIN".equalsIgnoreCase(str)) {
138             return Font.PLAIN;
139         else if ("BOLD".equalsIgnoreCase(str)) {
140             return Font.BOLD;
141         else if ("ITALIC".equalsIgnoreCase(str)) {
142             return Font.ITALIC;
143         else if ("BOLDITALIC".equalsIgnoreCase(str)) {
144             return Font.BOLD | Font.ITALIC;
145         }
146         throw illegalValue(source, Font.class);
147     }
148 }