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