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 * @(#) @(#) PointConverter.java 1.8 - last change made 07/25/97
34 */
35
36package sunsoft.jws.visual.rt.type;
37
38import java.awt.Point;
39import java.util.Hashtable;
40import java.util.Enumeration;
41import sunsoft.jws.visual.rt.base.Global;
42
43
44/**
45 * Converts Point objects to strings and back again.
46 * An example of the string representation: "x=45;y=24".
47 *
48 * @version 	1.8, 07/25/97
49 */
50public class PointConverter extends Converter {
51    public String convertToString(Object obj) {
52        if (obj == null)
53            return /* NOI18N */"";
54
55        Point p = (Point)obj;
56        return (/* NOI18N */"x=" + p.x + /* NOI18N */";y=" + p.y);
57    }
58
59    /**
60     * Converts a string representation to a new instance of Point.
61     *
62     * @exception ParseException when there is a format
63     * problem with the string
64    */
65    public Object convertFromString(String s) {
66        if (s == null || s.length() == 0)
67            return null;
68
69        SubFieldTokenizer sft = new SubFieldTokenizer(s);
70        Hashtable table = sft.getHashtable();
71        Point p = new Point(0, 0);
72
73        Enumeration e = table.keys();
74        while (e.hasMoreElements()) {
75            String key = (String)e.nextElement();
76            if (!key.equals(/* NOI18N */"x") &&
77		!key.equals(/* NOI18N */"y")) {
78                throw new ParseException(Global.fmtMsg(
79	        "sunsoft.jws.visual.rt.type.PointConverter.IllegalPoint",
80						       key));
81            }
82        }
83
84        if (table.containsKey(/* NOI18N */"x"))
85            p.x = getIntegerFromTable(table, /* NOI18N */"x");
86        if (table.containsKey(/* NOI18N */"y"))
87            p.y = getIntegerFromTable(table, /* NOI18N */"y");
88
89        return p;
90    }
91
92    private int getIntegerFromTable(Hashtable table, String key) {
93        String value = (String) table.get(key);
94        if (value != null) {
95            try {
96                return Integer.valueOf(value).intValue();
97            }
98            catch (NumberFormatException ex) {
99                throw new ParseException(Global.fmtMsg(
100   	        "sunsoft.jws.visual.rt.type.PointConverter.BadFormattedValue",
101						       value));
102            }
103        } else {
104            return (0);
105        }
106    }
107
108    public String convertToCode(Object obj) {
109        if (obj == null) {
110            return /* NOI18N */"new java.awt.Point(0, 0)";
111        } else {
112            Point p = (Point)obj;
113            return (/* NOI18N */"new java.awt.Point(" + p.x
114		    + /* NOI18N */", " + p.y + /* NOI18N */")");
115        }
116    }
117}
118