xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/type/BaseEnum.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  * @(#) BaseEnum.java 1.17 - 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.util.Enumeration;
41 
42 /* BEGIN JSTYLED */
43 /**
44  * Base class for types that implement enumerations
45  * (in the C style) where
46  * an integer signifies an important answer to a
47  * multiple-choice question.
48  * Sub-classers are supposed to supply the list of strings
49  * that describe the
50  * enumerations values available (and their corresponding integer values)
51  * in their static initializers.  Sub-classes should set up their own
52  * instance of the BaseEnumHelper class to hold their
53  * enumeration definition.
54  * <p>
55  * When you sub-class off of BaseEnum, Visual Java will automatically
56  * recognize your type as an enumeration, and offer a choice menu in
57  * the attribute editor of Visual Java for selecting the value of an
58  * attribute of that type.
59  * <p>
60  * The AlignmentEnum, shown below, is used for setting whether the
61  * text in a Label will appear on the left, center, or right.
62  * <p>
63  * To use the example below to create a new enumerated type, copy
64  * everything and then modify the imports and static constructor to use
65  * the constants and names for the choices you'd like to offer in your
66  * enumeration.
67  * <p>
68  * The addConverter call will register your type with Visual Java's
69  * type conversion interface and make it understood that this type is
70  * an enumeration.  Change the
71  * "sunsoft.jws.visual.rt.type.AlignmentEnum" argument to reflect the
72  * package name and class of your new enumerated type.
73  *
74  * <pre>
75  * package sunsoft.jws.visual.rt.type;
76  *
77  * import sunsoft.jws.visual.rt.type.BaseEnum;
78  * import sunsoft.jws.visual.rt.type.BaseEnumHelper;
79  * import sunsoft.jws.visual.rt.type.Converter;
80  * import java.awt.Label;
81  *
82  * public class AlignmentEnum extends BaseEnum {
83  *   private static BaseEnumHelper helper = new BaseEnumHelper();
84  *
85  *   static {
86  *     helper.add(Label.LEFT, "left");
87  *     helper.add(Label.CENTER, "center");
88  *     helper.add(Label.RIGHT, "right");
89  *     helper.setDefaultChoice(Label.LEFT);
90  *     Converter.addConverter("sunsoft.jws.visual.rt.type.AlignmentEnum",
91  *          "sunsoft.jws.visual.rt.type.BaseEnumConverter");
92  *   }
93  *
94  *   public AlignmentEnum() {
95  *     super();
96  *   }
97  *
98  *   public AlignmentEnum(int choice) {
99  *     super(choice);
100  *   }
101  *
102  *   public AlignmentEnum(String choice) {
103  *     super(choice);
104  *   }
105  *
106  *   protected BaseEnumHelper getHelper() {
107  *     return(helper);
108  *   }
109  * }
110  * </pre>
111  *
112  * @see BaseEnumHelper
113  * @see AlignmentEnum
114  * @see java.awt.Label
115  * @version 1.17, 07/25/97
116 */
117 
118 /* END JSTYLED */
119 public abstract class BaseEnum implements Cloneable {
120     /**
121      * The currently selected value from the enum.
122      */
123     protected Integer currentChoice;
124 
125     /**
126      * Constructor, sets the choice to the default.
127      */
BaseEnum()128     protected BaseEnum() {
129         set(getHelper().getDefaultChoice());
130     }
131 
132     /**
133      * Constructor to use when integer value is available.
134      *
135      * @param choice enumerated value
136      */
BaseEnum(int choice)137     protected BaseEnum(int choice) {
138         set(choice);
139     }
140 
141     /**
142      * Constructor to use when string of choice is available.
143      *
144      * @param choice enumerated value
145      */
BaseEnum(String choice)146     protected BaseEnum(String choice) {
147         set(choice);
148     }
149 
150     /**
151      * Gets the helper class that stores the enum definition.
152      *
153      * Each sub-classer should override this so that a different
154      * class-wide instance of BaseEnumHelper is returned.  If there
155      * weren't a mechanism like this, then all sub-classers would be
156      * sharing a single helper, and that would be bad.
157      *
158      * @return a helper use by all enumeration instances of
159      * a single class
160     */
getHelper()161     protected abstract BaseEnumHelper getHelper();
162 
163     /**
164      * Sets the enumeration to the given integer value.
165      * Checks validity
166      * of the choice.
167      *
168      * @param choice enumerated value
169      * @exception Error when an invalid choice is given
170      */
set(int choice)171     public void set(int choice) {
172         if (getHelper().isValid(choice))
173             currentChoice = new Integer(choice);
174         else
175             throw new ParseException(Global.fmtMsg(
176 			   "sunsoft.jws.visual.rt.type.BaseEnum.FMT.30",
177 	   Global.getMsg(
178 		 "sunsoft.jws.visual.rt.type.BaseEnum.invalid__int__choice__"),
179 						   new Integer(choice),
180 						   /* BEGIN JSTYLED */
181 						   Global.getMsg("sunsoft.jws.visual.rt.type.BaseEnum.__given__to__Enum__class.14")));
182 	/* END JSTYLED */
183     }
184 
185     /**
186      * Sets the enumeration to the given string value.  Checks validity
187      * of the choice.
188      *
189      * @param choice string version of the enumerated value
190      * @exception Error when an invalid choice is given
191      */
set(String choice)192     public void set(String choice) {
193         if (getHelper().isValid(choice))
194             currentChoice = getHelper().getInteger(choice);
195         else
196             throw new ParseException(Global.fmtMsg(
197 			   "sunsoft.jws.visual.rt.type.BaseEnum.FMT.30",
198 						   /* BEGIN JSTYLED */
199 						   Global.getMsg("sunsoft.jws.visual.rt.type.BaseEnum.invalid__string__choic.15"),
200 						   choice,
201 						   Global.getMsg("sunsoft.jws.visual.rt.type.BaseEnum.__given__to__Enum__class.16")));
202 	/* END JSTYLED */
203     }
204 
205     /**
206      * Returns the int value of the current selection
207      * from the enumeration.
208     */
intValue()209     public int intValue() {
210         return (currentChoice.intValue());
211     }
212 
213     /**
214      * Returns the String description of the current selection from the
215      * enumeration.
216      */
toString()217     public String toString() {
218         return (getHelper().getString(currentChoice));
219     }
220 
221     /**
222      * Returns a java.util.Enumeration of the String descriptions
223      * available in this enum.
224      */
elements()225     public Enumeration elements() {
226         return (getHelper().elements());
227     }
228 
229     /**
230      * Returns an array containing all of the String descriptions
231      * available in this enum.
232      */
descriptions()233     public String[] descriptions() {
234         return (getHelper().descriptions());
235     }
236 
237     /**
238      * Returns a copy of this enumeration instance.
239      */
clone()240     public Object clone() {
241         try {
242             return super.clone();
243         } catch (CloneNotSupportedException e) {
244             // this shouldn't happen, since we are Cloneable
245             throw new InternalError();
246         }
247     }
248 
249     /**
250      * Returns true if this enumeration instance and the one given have
251      * the same selection made.
252      */
equals(Object obj)253     public boolean equals(Object obj) {
254         if (obj instanceof BaseEnum) {
255             if (getClass() == obj.getClass()) {
256                 Integer otherChoice = ((BaseEnum)obj).currentChoice;
257                 if (currentChoice != null)
258                     return (currentChoice.equals(otherChoice));
259                 else
260                     return (otherChoice == null);
261             }
262         }
263         return false;
264     }
265 }
266