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 * @(#) BaseEnumConverter.java 1.16 - last change made 06/18/97
34 */
35
36package sunsoft.jws.visual.rt.type;
37
38/**
39 * Converts enum types to and from strings and to code.  This converter
40 * can handle any sub-class of BaseEnum.
41 *
42 * @see BaseEnum
43 * @version 	1.16, 06/18/97
44 */
45public class BaseEnumConverter extends Converter {
46    private Class enumClass = null;
47
48    public BaseEnumConverter() {
49    }
50
51    /**
52     * Constructs an instance of this converter that can be used for the
53     * given subclass of BaseEnum.
54     *
55     * @param type the fully qualified class name of the subclass
56     */
57    public BaseEnumConverter(String type) {
58        setConverterType(type);
59    }
60
61    private BaseEnum makeEnum(String s) {
62        if (enumClass == null) {
63            try {
64                enumClass = Class.forName(getConverterType());
65            }
66            catch (ClassNotFoundException e) {
67                throw new Error(e.getMessage());
68            }
69        }
70
71        try {
72            BaseEnum retval = (BaseEnum) enumClass.newInstance();
73            retval.set(s);
74            return (retval);
75        }
76        catch (Exception e) {
77            throw new Error(e.getMessage());
78        }
79    }
80
81    /**
82     * Returns the string representation of the enumeration
83     * selected in the
84     * given BaseEnum object.
85     *
86     * @param obj an instance of BaseEnum or one of its subclasses
87     */
88    public String convertToString(Object obj) {
89        return (((BaseEnum) obj).toString());
90    }
91
92    /**
93     * Returns a new instance of BaseEnum (or subclass) for enumeration
94     * value given.
95     *
96     * @param s string version of the enumeration choice
97     */
98    public Object convertFromString(String s) {
99        return (makeEnum(s));
100    }
101
102    /**
103     * Returns a block of code that creates a new BaseEnum (or subclass)
104     * like the one given.
105     *
106     * @param the BaseEnum instance to emulate
107     */
108    public String convertToCode(Object obj) {
109        StringBuffer buf = new StringBuffer();
110
111        buf.append(/* NOI18N */"new ");
112        buf.append(obj.getClass().getName());
113        buf.append(/* NOI18N */"(");
114
115        ListParser.quote(obj.toString(), buf, true);
116
117        buf.append(/* NOI18N */")");
118
119        return buf.toString();
120    }
121
122    /**
123     * Helps signify that in an attribute editor (like the
124     * one in the Desginer), an instance of BaseEnum
125     * is not viewable in a
126     * textfield.  It will be displayed using a choice menu instead.
127     *
128     * @return false
129     */
130    public boolean viewableAsString() {
131        return false;
132    }
133}
134