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 * @(#) ChoiceShadow.java 1.13 - last change made 05/02/97
34 */
35
36package sunsoft.jws.visual.rt.shadow.java.awt;
37
38import sunsoft.jws.visual.rt.base.Global;
39import java.awt.*;
40
41/**
42 * Wraps an AWT widget.  The attributes available for this
43 * class are listed below.  In the type column, type names beginning
44 * with "sunsoft.jws.visual.rt" have been abbreviated to begin with "rt".
45 *
46 * <pre>
47name            type                      default value
48-----------------------------------------------------------------------
49items           [Ljava.lang.String;       item1, item2
50selectedItem    java.lang.String          ""
51*  < /pre>
52*
53* selectedItem: is the item(amoung the the strings in the "items"
54* attribute) that is currently showing in the choice field.  This
55* attribute is not available in the attribute editor, but is instead
56* expected to be used programmatically to change the setting on the
57* choice as the result of a callback or some such.
58*  < p>
59* Check the super class for additional attributes.
60*
61* @see Choice
62* @version 	1.13, 05/02/97
63*/
64public class ChoiceShadow extends ComponentShadow {
65    public ChoiceShadow() {
66        String items[] = { /* NOI18N */"item1", /* NOI18N */"item2"};
67        // changed for bugid 4006105 -kp
68        //   attributes.add("items", "[Ljava.lang.String;", items, CONSTRUCTOR);
69        attributes.add(/* NOI18N */"items",
70		       /* NOI18N */"[Ljava.lang.String;", items, 0);
71        attributes.add(/* NOI18N */"selectedItem",
72		       /* NOI18N */"java.lang.String", /* NOI18N */"", HIDDEN);
73
74        // On WindowsNT, choice menus look bad because they have extra
75        // space on the bottom.  Setting the insets here tries to adjust
76        // for this problem.
77        if (Global.isWindowsNT()) {
78            attributes.add(/* NOI18N */"insets",
79			   /* NOI18N */"java.awt.Insets",
80			new Insets(2, 0, 0, 0));
81        } else if (Global.isMotif()) {
82            // Motif choice menus hang out over their bottom and right edges.
83            // The problem is worse on SGI than Sun.
84            if (Global.isIrix())
85		attributes.add(/* NOI18N */"insets",
86			       /* NOI18N */"java.awt.Insets",
87			    new Insets(0, 0, 4, 12));
88            else
89                attributes.add(/* NOI18N */"insets",
90			       /* NOI18N */"java.awt.Insets",
91			       new Insets(0, 0, 2, 6));
92        }
93    }
94
95    protected Object getOnBody(String key) {
96        if (key.equals(/* NOI18N */"items")) {
97            Choice choice = (Choice)body;
98            int count = choice.countItems();
99            String value[] = new String[count];
100
101            for (int i = 0; i < count; i++)
102                value[i] = choice.getItem(i);
103            return value;
104        } else if (key.equals(/* NOI18N */"selectedItem")) {
105            Choice choice = (Choice)body;
106            return choice.getSelectedItem();
107        }
108        else
109            return (super.getOnBody(key));
110    }
111
112    /**
113     * This makes changes to the Choice body when the user changes
114     * items in it.  it updates the Choice body from the new data.
115     */
116    private void equalizeChoices(Object value) {
117
118        String s[] = (String[])value;
119        Choice choice = (Choice) body;
120        int count = choice.countItems();
121        // remove all the items and add the new list...
122        if (count  > 0)
123            choice.removeAll();
124        // Motif workaround: Need to add at least one item to the choice menu
125        // or else Motif will cause a core dump.
126	if (s == null || s.length == 0)
127            choice.addItem(/* NOI18N */"     ");
128        else {
129            for (int i = 0; i < s.length; i++)
130                if (s[i] != null)
131		    choice.addItem(s[i]);
132        }
133    }
134
135    protected void setOnBody(String key, Object value) {
136        if (key.equals(/* NOI18N */"items")) {
137
138            // added -kp for bugid 4006105
139            equalizeChoices(value);
140            // end of addition -kp
141        } else if (key.equals(/* NOI18N */"selectedItem")) {
142            String str = (String)value;
143            if (str == null)
144                str = /* NOI18N */"";
145
146            Choice choice = (Choice)body;
147            boolean selected = false;
148            int count = choice.countItems();
149
150            for (int i = 0; i < count; i++) {
151                if (choice.getItem(i).equals(str)) {
152                    selected = true;
153                    choice.select(i);
154                    break;
155                }
156            }
157
158            if (!selected && count != 0)
159                choice.select(0);
160        } else
161            super.setOnBody(key, value);
162    }
163
164    public void createBody() {
165        Choice choice = new Choice();
166        body = choice;
167    }
168}
169