001: /*
002: * Koala Bean Markup Language - Copyright (C) 1999 Dyade
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a
005: * copy of this software and associated documentation files
006: * (the "Software"), to deal in the Software without restriction, including
007: * without limitation the rights to use, copy, modify, merge, publish,
008: * distribute, sublicense, and/or sell copies of the Software, and to permit
009: * persons to whom the Software is furnished to do so, subject to the
010: * following conditions:
011: * The above copyright notice and this permission notice shall be included
012: * in all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
015: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
017: * IN NO EVENT SHALL Dyade BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: * Except as contained in this notice, the name of Dyade shall not be
023: * used in advertising or otherwise to promote the sale, use or other
024: * dealings in this Software without prior written authorization from
025: * Dyade.
026: *
027: * $Id: FontEditor.java,v 1.1.1.1 1999/11/03 15:34:30 phk Exp $
028: * Author: Thierry.Kormann@sophia.inria.fr
029: */
030:
031: package fr.dyade.koala.xml.kbml.editors;
032:
033: import java.beans.*;
034: import java.awt.Font;
035:
036: /**
037: * A simple property editor for the <code>java.awt.Font</code> class.
038: *
039: * @author Thierry.Kormann@sophia.inria.fr
040: */
041: public class FontEditor extends PropertyEditorSupport {
042:
043: public String getJavaInitializationString() {
044: Font font = (Font) getValue();
045: return "new java.awt.Font(\"" + font.getFamily()
046: + "\", java.awt.Font." + getFontStyle(font.getStyle())
047: + ", " + font.getSize() + ")";
048: }
049:
050: public void setAsText(String s) throws IllegalArgumentException {
051: int c1 = s.indexOf(',');
052: int c2 = s.indexOf(',', c1 + 1);
053: if (c1 < 0 || c2 < 0) {
054: // Invalid string.
055: throw new IllegalArgumentException(s);
056: }
057: try {
058: String family = s.substring(0, c1);
059: int style = getFontStyle(s.substring(c1 + 1, c2));
060: int size = Integer.parseInt(s.substring(c2 + 1));
061: setValue(new Font(family, style, size));
062: } catch (Exception ex) {
063: throw new IllegalArgumentException(s);
064: }
065: }
066:
067: public String getAsText() {
068: Font font = (Font) getValue();
069: return font.getFamily() + "," + getFontStyle(font.getStyle())
070: + "," + font.getSize();
071: }
072:
073: static int getFontStyle(String style) {
074: style = style.toUpperCase();
075: for (int i = 0; i < fontStyleDefs.length; ++i) {
076: if (style.equals(fontStyleDefs[i].name)) {
077: return fontStyleDefs[i].val;
078: }
079: }
080: throw new IllegalArgumentException(style);
081: }
082:
083: static String getFontStyle(int style) {
084: for (int i = 0; i < fontStyleDefs.length; ++i) {
085: if (style == fontStyleDefs[i].val) {
086: return fontStyleDefs[i].name;
087: }
088: }
089: throw new IllegalArgumentException("" + style);
090: }
091:
092: // font style definition
093: static FontStyleDef[] fontStyleDefs = {
094: new FontStyleDef("BOLD", Font.BOLD),
095: new FontStyleDef("ITALIC", Font.ITALIC),
096: new FontStyleDef("PLAIN", Font.PLAIN) };
097:
098: /**
099: * A simple class to code/decode abstract font style to string.
100: */
101: public static class FontStyleDef {
102:
103: public int val;
104: public String name;
105:
106: public FontStyleDef(String name, int val) {
107: this.name = name;
108: this.val = val;
109: }
110: }
111: }
|