01: /*
02: * @(#) FontNameConverter.java
03: *
04: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.awt.*;
09:
10: /**
11: * Converter which converts Font Name String to String and converts it back.
12: * It's almost the same as StringConverter except if user types in a string
13: * which cannot find in font list on your computer, it will return null in fromString and return "" in toString.
14: */
15: public class FontNameConverter implements ObjectConverter {
16: /**
17: * ConverterContext for a font name.
18: */
19: public static ConverterContext CONTEXT = new ConverterContext(
20: "FontName");
21:
22: public String toString(Object object, ConverterContext context) {
23: if (object == null) {
24: return "";
25: } else {
26: String[] fontNames = GraphicsEnvironment
27: .getLocalGraphicsEnvironment()
28: .getAvailableFontFamilyNames();
29: for (int i = 0; i < fontNames.length; i++) { // check font if it is available
30: String fontName = fontNames[i];
31: if (fontName.equals(object)) {
32: return fontName;
33: }
34: }
35: return "";
36: }
37: }
38:
39: public boolean supportToString(Object object,
40: ConverterContext context) {
41: return true;
42: }
43:
44: public Object fromString(String string, ConverterContext context) {
45: if (string.length() == 0) {
46: return null;
47: } else {
48: String[] fontNames = GraphicsEnvironment
49: .getLocalGraphicsEnvironment()
50: .getAvailableFontFamilyNames();
51: for (int i = 0; i < fontNames.length; i++) { // check font if it is available
52: String fontName = fontNames[i];
53: if (fontName.equals(string)) {
54: return string;
55: }
56: }
57: return null;
58: }
59: }
60:
61: public boolean supportFromString(String string,
62: ConverterContext context) {
63: return true;
64: }
65: }
|