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 * @(#) GBConstraintsConverter.java 1.21 - last change made 07/25/97
34 */
35
36package sunsoft.jws.visual.rt.type;
37
38import sunsoft.jws.visual.rt.base.Global;
39import sunsoft.jws.visual.rt.base.Shadow;
40import sunsoft.jws.visual.rt.awt.GBConstraints;
41import java.util.Hashtable;
42
43/**
44 * Converts instances of GBConstraints to strings and back again.
45 *
46 * @version 1.21, 07/25/97
47 */
48public class GBConstraintsConverter extends Converter {
49
50    private static final GBConstraints constraintsDefault =
51	new GBConstraints();
52
53    private String constantToString(int c) {
54        if (c == GBConstraints.RELATIVE)
55            return (/* NOI18N */"relative");
56        else if (c == GBConstraints.REMAINDER)
57            return (/* NOI18N */"remainder");
58        else if (c == GBConstraints.NONE)
59            return (/* NOI18N */"none");
60        else if (c == GBConstraints.BOTH)
61            return (/* NOI18N */"both");
62        else if (c == GBConstraints.HORIZONTAL)
63            return (/* NOI18N */"horizontal");
64        else if (c == GBConstraints.VERTICAL)
65            return (/* NOI18N */"vertical");
66        else if (c == GBConstraints.CENTER)
67            return (/* NOI18N */"center");
68        else if (c == GBConstraints.NORTH)
69            return (/* NOI18N */"north");
70        else if (c == GBConstraints.NORTHEAST)
71            return (/* NOI18N */"northeast");
72        else if (c == GBConstraints.EAST)
73            return (/* NOI18N */"east");
74        else if (c == GBConstraints.SOUTHEAST)
75            return (/* NOI18N */"southeast");
76        else if (c == GBConstraints.SOUTH)
77            return (/* NOI18N */"south");
78        else if (c == GBConstraints.SOUTHWEST)
79            return (/* NOI18N */"southwest");
80        else if (c == GBConstraints.WEST)
81            return (/* NOI18N */"west");
82        else if (c == GBConstraints.NORTHWEST)
83            return (/* NOI18N */"northwest");
84        else
85	    /* BEGIN JSTYLED */
86	    throw new Error(Global.fmtMsg(
87					  "sunsoft.jws.visual.rt.type.GBConstraintsConverter.FMT.29",
88					  Global.getMsg("sunsoft.jws.visual.rt.type.GBConstraintsConverter.unknown__constant"),
89					  new Integer(c)));
90	/* END JSTYLED */
91    }
92
93    private int stringToConstant(String s) {
94        if (s.equals(/* NOI18N */"relative"))
95            return GBConstraints.RELATIVE;
96        else if (s.equals(/* NOI18N */"remainder"))
97            return GBConstraints.REMAINDER;
98        else if (s.equals(/* NOI18N */"none"))
99            return GBConstraints.NONE;
100        else if (s.equals(/* NOI18N */"both"))
101            return GBConstraints.BOTH;
102        else if (s.equals(/* NOI18N */"horizontal"))
103            return GBConstraints.HORIZONTAL;
104        else if (s.equals(/* NOI18N */"vertical"))
105            return GBConstraints.VERTICAL;
106        else if (s.equals(/* NOI18N */"center"))
107            return GBConstraints.CENTER;
108        else if (s.equals(/* NOI18N */"north"))
109            return GBConstraints.NORTH;
110        else if (s.equals(/* NOI18N */"northeast"))
111            return GBConstraints.NORTHEAST;
112        else if (s.equals(/* NOI18N */"east"))
113            return GBConstraints.EAST;
114        else if (s.equals(/* NOI18N */"southeast"))
115            return GBConstraints.SOUTHEAST;
116        else if (s.equals(/* NOI18N */"south"))
117            return GBConstraints.SOUTH;
118        else if (s.equals(/* NOI18N */"southwest"))
119            return GBConstraints.SOUTHWEST;
120        else if (s.equals(/* NOI18N */"west"))
121            return GBConstraints.WEST;
122        else if (s.equals(/* NOI18N */"northwest"))
123            return GBConstraints.NORTHWEST;
124        else
125	    /* BEGIN JSTYLED */
126	    throw new Error(Global.fmtMsg(
127					  "sunsoft.jws.visual.rt.type.GBConstraintsConverter.FMT.29",
128					  Global.getMsg("sunsoft.jws.visual.rt.type.GBConstraintsConverter.unknown__constant"),
129					  s));
130	/* END JSTYLED */
131    }
132
133    /**
134     * Converts an instance of GBConstraints to a string representation.
135     */
136    public String convertToString(Object obj) {
137        if (obj != null) {
138            GBConstraints c = (GBConstraints) obj;
139            String retval = /* NOI18N */"";
140
141            if (c.gridx != constraintsDefault.gridx) {
142                retval = retval + /* NOI18N */"x=" + c.gridx
143		    + /* NOI18N */";";
144            }
145            if (c.gridy != constraintsDefault.gridy) {
146                retval = retval + /* NOI18N */"y=" + c.gridy
147		    + /* NOI18N */";";
148            }
149            if (c.gridwidth != constraintsDefault.gridwidth) {
150                retval = retval + /* NOI18N */"width=" + c.gridwidth
151		    + /* NOI18N */";";
152            }
153            if (c.gridheight != constraintsDefault.gridheight) {
154                retval = retval + /* NOI18N */"height="
155		    + c.gridheight + /* NOI18N */";";
156            }
157            if (c.weightx != constraintsDefault.weightx) {
158                retval = retval + /* NOI18N */"weightx=" + c.weightx
159		    + /* NOI18N */";";
160            }
161            if (c.weighty != constraintsDefault.weighty) {
162                retval = retval + /* NOI18N */"weighty=" + c.weighty
163		    + /* NOI18N */";";
164            }
165            if (c.fill != constraintsDefault.fill) {
166                retval = retval + /* NOI18N */"fill="
167		    + constantToString(c.fill) + /* NOI18N */";";
168            }
169            if (c.ipadx != constraintsDefault.ipadx) {
170                retval = /* NOI18N */(retval + "ipadx="
171				      + c.ipadx + ";");
172            }
173            if (c.ipady != constraintsDefault.ipady) {
174                retval = /* NOI18N */(retval + "ipady="
175				      + c.ipady + ";");
176            }
177            if (c.shrinkx != constraintsDefault.shrinkx) {
178                retval = /* NOI18N */(retval + "shrinkx="
179				      + c.shrinkx + ";");
180            }
181            if (c.shrinky != constraintsDefault.shrinky) {
182                retval = /* NOI18N */(retval + "shrinky="
183				      + c.shrinky + ";");
184            }
185
186            if (retval.length() > 0 &&
187		retval.charAt(retval.length() - 1) == /* NOI18N */ ';')
188		return (retval.substring(0, retval.length() - 1));
189            else
190                return (retval);
191        } else {
192            return (null);
193        }
194    }
195
196    /**
197     * Returns a code for creating a GBConstraints instance
198     * like the one given.
199     *
200     * @param obj an instance of GBConstraints
201     */
202    public String convertToCode(Object obj) {
203        return (/* NOI18N */("new GBConstraints(\""
204			     + convertToString(obj) + "\")"));
205    }
206
207    private int getIntegerFromTable(Hashtable table, String key) {
208        String value = (String) table.get(key);
209        if (value != null)
210            return (Integer.valueOf(value).intValue());
211        else
212            return (0);
213    }
214
215    private boolean getBooleanFromTable(Hashtable table, String key) {
216        String value = (String) table.get(key);
217        if (value != null)
218            return (Boolean.valueOf(value).booleanValue());
219        else
220            return (false);
221    }
222
223    private double getDoubleFromTable(Hashtable table, String key) {
224        String value = (String) table.get(key);
225        if (value != null)
226            return (Double.valueOf(value).doubleValue());
227        else
228            return (0.0);
229    }
230
231    private int getConstantFromTable(Hashtable table, String key) {
232        String value = (String) table.get(key);
233        if (value != null)
234            return (stringToConstant(value));
235        else
236            return (0);
237    }
238
239    /**
240     * Converts a string into a new instance of GBConstraints.
241     *
242     * @exception Error when there is a problem with the string
243     */
244    public Object convertFromString(String s) {
245        if (s != null && s.length() > 0) {
246            SubFieldTokenizer sft = new SubFieldTokenizer(s);
247            Hashtable table = sft.getHashtable();
248            GBConstraints retval = new GBConstraints();
249
250            if (table.containsKey(/* NOI18N */"x")) {
251                retval.gridx = getIntegerFromTable(table, /* NOI18N */"x");
252            }
253            if (table.containsKey(/* NOI18N */"y")) {
254                retval.gridy = getIntegerFromTable(table, /* NOI18N */"y");
255            }
256            if (table.containsKey(/* NOI18N */"width")) {
257                retval.gridwidth = getIntegerFromTable(
258					       table, /* NOI18N */"width");
259            }
260            if (table.containsKey(/* NOI18N */"height")) {
261                retval.gridheight = getIntegerFromTable(
262						table, /* NOI18N */"height");
263            }
264            if (table.containsKey(/* NOI18N */"weightx")) {
265                retval.weightx = getDoubleFromTable(
266					    table, /* NOI18N */"weightx");
267            }
268            if (table.containsKey(/* NOI18N */"weighty")) {
269                retval.weighty = getDoubleFromTable(
270					    table, /* NOI18N */"weighty");
271            }
272            if (table.containsKey(/* NOI18N */"fill")) {
273                retval.fill = getConstantFromTable(
274						   table, /* NOI18N */"fill");
275            }
276            if (table.containsKey(/* NOI18N */"ipadx")) {
277                retval.ipadx = getIntegerFromTable(
278						   table, /* NOI18N */"ipadx");
279            }
280            if (table.containsKey(/* NOI18N */"ipady")) {
281                retval.ipady = getIntegerFromTable(
282						   table, /* NOI18N */"ipady");
283            }
284            if (table.containsKey(/* NOI18N */"shrinkx")) {
285                retval.shrinkx = getBooleanFromTable(
286					     table, /* NOI18N */"shrinkx");
287            }
288            if (table.containsKey(/* NOI18N */"shrinky")) {
289                retval.shrinky = getBooleanFromTable(
290					     table, /* NOI18N */"shrinky");
291            }
292
293            return (retval);
294        } else {
295            return (null);
296        }
297    }
298}
299