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