xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/type/FontConverter.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /*
30  *        Copyright (C) 1996  Active Software, Inc.
31  *                  All rights reserved.
32  *
33  * @(#) FontConverter.java 1.14 - last change made 07/25/97
34  */
35 
36 package sunsoft.jws.visual.rt.type;
37 
38 import sunsoft.jws.visual.rt.base.Global;
39 
40 import java.awt.Font;
41 import java.util.Hashtable;
42 import java.util.StringTokenizer;
43 
44 /**
45  * Converts Font objects to strings and back again.  An example of the
46  * string representation: "name=Helvetica;style=bold;size=14".  The
47  * styles that can currently be used are "plain", "bold", and
48  * "italic".
49  *
50  * @version 1.14, 07/25/97
51  */
52 public class FontConverter extends Converter {
53 
54     private static final int styleDefault = Font.PLAIN;
55     private static final int sizeDefault = 12;
56 
57     /**
58      * Converts a Font instance to a string.
59      */
convertToString(Object obj)60     public String convertToString(Object obj) {
61         if (obj == null)
62             return /* NOI18N */"";
63 
64         Font font = (Font) obj;
65         String style;
66 
67         switch (font.getStyle()) {
68 	case Font.PLAIN:
69             style = /* NOI18N */"plain";
70             break;
71 	case Font.BOLD:
72             style = /* NOI18N */"bold";
73             break;
74 	case Font.ITALIC:
75             style = /* NOI18N */"italic";
76             break;
77 	default:
78             /* JSTYLED */
79 	    System.out.println(Global.getMsg("sunsoft.jws.visual.rt.type.FontConverter.Warning-co-__unknown__fon.29") + font.getStyle());
80             style = /* NOI18N */"plain";
81             break;
82         }
83 
84         return (/* NOI18N */"name=" + font.getName()
85 		+ /* NOI18N */";style=" + style
86 		+ /* NOI18N */";size=" + font.getSize());
87     }
88 
89     /**
90      * Converts a string to a new instance of Font.
91      *
92      * @exception ParseException when there is a format
93      * problem with the string
94     */
convertFromString(String s)95     public Object convertFromString(String s) {
96         if (s == null || s.length() == 0)
97             return null;
98 
99         SubFieldTokenizer sft = new SubFieldTokenizer(s);
100         Hashtable table = sft.getHashtable();
101 
102         String name = (String) table.get(/* NOI18N */"name");
103         if (name == null || name.length() <= 0)
104 	    /* BEGIN JSTYLED */
105 	    throw new ParseException(Global.getMsg("sunsoft.jws.visual.rt.type.FontConverter.Missing__font__name-co-__") + s);
106 	/* END JSTYLED */
107 	int style;
108         String styleString = (String) table.get(/* NOI18N */"style");
109         if (styleString != null) {
110             if (styleString.equals(/* NOI18N */"italic"))
111                 style = Font.ITALIC;
112             else if (styleString.equals(/* NOI18N */"bold"))
113                 style = Font.BOLD;
114             else if (styleString.equals(/* NOI18N */"plain"))
115                 style = Font.PLAIN;
116             else {
117 		/* BEGIN JSTYLED */
118 		throw new ParseException(Global.getMsg("sunsoft.jws.visual.rt.type.FontConverter.Invalid__font__style-co-__.30") + s);
119 		/* END JSTYLED */
120             }
121         } else {
122             style = styleDefault;
123         }
124 
125         int size;
126         String sizeString = (String) table.get(/* NOI18N */"size");
127         if (sizeString != null) {
128             try {
129                 size = Integer.valueOf(sizeString).intValue();
130             } catch (NumberFormatException e) {
131 		/* BEGIN JSTYLED */
132 		throw new ParseException(Global.getMsg("sunsoft.jws.visual.rt.type.FontConverter.Invalid__font__size-co-__") + s);
133 	    }
134 	    if (size <= 0) {
135 		throw new ParseException(Global.getMsg("sunsoft.jws.visual.rt.type.FontConverter.Negative__font__size-co-__.31") + s);
136 		/* END JSTYLED */
137             }
138         } else {
139             size = sizeDefault;
140         }
141 
142         return (new Font(name, style, size));
143     }
144 }
145