Debian Codegraph @

src/com/jidesoft/converter/FontConverter.java

raw
1 /*
2 * @(#) FontConverter.java
3 *
4 * Copyright 2002 - 2003 JIDE Software. All rights reserved.
5 */
6 package com.jidesoft.converter;
7
8 import java.awt.*;
9 import java.util.Locale;
10 import java.util.ResourceBundle;
11
12 /**
13 * Converter which converts Font to String and converts it back.
14 */
15 public class FontConverter implements ObjectConverter {
16 public String toString(Object object, ConverterContext context) {
17 if (object instanceof Font) {
18 Font font = (Font) object;
19 return font.getName() + ", " + getResourceString(font.getStyle()) + ", " + font.getSize();
20 }
21 else {
22 return null;
23 }
24 }
25
26 protected String getResourceString(int style) {
27 final ResourceBundle resourceBundle = Resource.getResourceBundle(Locale.getDefault());
28 switch (style) {
29 case Font.PLAIN:
30 return resourceBundle.getString("Font.plain");
31 case Font.BOLD:
32 return resourceBundle.getString("Font.bold");
33 case Font.ITALIC:
34 return resourceBundle.getString("Font.italic");
35 case Font.BOLD | Font.ITALIC:
36 return resourceBundle.getString("Font.boldItalic");
37 default:
38 return "";
39 }
40 }
41
42 protected int getStyleValue(String style) {
43 final ResourceBundle resourceBundle = Resource.getResourceBundle(Locale.getDefault());
44 if (resourceBundle.getString("Font.italic").equalsIgnoreCase(style)) {
45 return Font.ITALIC;
46 }
47 else if (resourceBundle.getString("Font.bold").equalsIgnoreCase(style)) {
48 return Font.BOLD;
49 }
50 else if (resourceBundle.getString("Font.boldItalic").equalsIgnoreCase(style)) {
51 return Font.BOLD | Font.ITALIC;
52 }
53 else /*if("PLAIN".equals(style))*/ {
54 return Font.PLAIN;
55 }
56 }
57
58 public boolean supportToString(Object object, ConverterContext context) {
59 return true;
60 }
61
62 public Object fromString(String string, ConverterContext context) {
63 if (string == null || string.length() == 0) {
64 return null;
65 }
66 else {
67 String fontFace = null;
68 int style = Font.PLAIN;
69 int size = 10;
70
71 String[] strings = string.split(",");
72 if (strings.length > 0) {
73 fontFace = strings[0].trim();
74 }
75 if (strings.length > 1) {
76 style = getStyleValue(strings[1].trim());
77 }
78 if (strings.length > 2) {
79 try {
80 double s = Double.parseDouble(strings[2].trim());
81 size = (int) s;
82 }
83 catch (NumberFormatException e) {
84 // ignore
85 }
86 }
87
88 if (fontFace != null) {
89 return new Font(fontFace, style, size);
90 }
91 else {
92 return null;
93 }
94 }
95 }
96
97 public boolean supportFromString(String string, ConverterContext context) {
98 return true;
99 }
100 }