001: /*
002: * @(#) FontConverter.java
003: *
004: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005: */
006: package com.jidesoft.converter;
007:
008: import java.awt.*;
009: import java.util.Locale;
010: import java.util.ResourceBundle;
011:
012: /**
013: * Converter which converts Font to String and converts it back.
014: */
015: class FontConverter implements ObjectConverter {
016: public String toString(Object object, ConverterContext context) {
017: if (object instanceof Font) {
018: Font font = (Font) object;
019: return font.getName() + ", "
020: + getResourceString(font.getStyle()) + ", "
021: + font.getSize();
022: } else {
023: return null;
024: }
025: }
026:
027: protected String getResourceString(int style) {
028: final ResourceBundle resourceBundle = Resource
029: .getResourceBundle(Locale.getDefault());
030: switch (style) {
031: case Font.PLAIN:
032: return resourceBundle.getString("Font.plain");
033: case Font.BOLD:
034: return resourceBundle.getString("Font.bold");
035: case Font.ITALIC:
036: return resourceBundle.getString("Font.italic");
037: case Font.BOLD | Font.ITALIC:
038: return resourceBundle.getString("Font.boldItalic");
039: default:
040: return "";
041: }
042: }
043:
044: protected int getStyleValue(String style) {
045: final ResourceBundle resourceBundle = Resource
046: .getResourceBundle(Locale.getDefault());
047: if (resourceBundle.getString("Font.italic").equalsIgnoreCase(
048: style)) {
049: return Font.ITALIC;
050: } else if (resourceBundle.getString("Font.bold")
051: .equalsIgnoreCase(style)) {
052: return Font.BOLD;
053: } else if (resourceBundle.getString("Font.boldItalic")
054: .equalsIgnoreCase(style)) {
055: return Font.BOLD | Font.ITALIC;
056: } else /*if("PLAIN".equals(style))*/{
057: return Font.PLAIN;
058: }
059: }
060:
061: public boolean supportToString(Object object,
062: ConverterContext context) {
063: return true;
064: }
065:
066: public Object fromString(String string, ConverterContext context) {
067: if (string == null || string.length() == 0) {
068: return null;
069: } else {
070: String fontFace = null;
071: int style = Font.PLAIN;
072: int size = 10;
073:
074: String[] strings = string.split(",");
075: if (strings.length > 0) {
076: fontFace = strings[0].trim();
077: }
078: if (strings.length > 1) {
079: style = getStyleValue(strings[1].trim());
080: }
081: if (strings.length > 2) {
082: try {
083: size = Integer.parseInt(strings[2].trim());
084: } catch (NumberFormatException e) {
085: }
086: }
087:
088: if (fontFace != null) {
089: return new Font(fontFace, style, size);
090: } else {
091: return null;
092: }
093: }
094: }
095:
096: public boolean supportFromString(String string,
097: ConverterContext context) {
098: return true;
099: }
100: }
|