xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/shadow/java/awt/ListShadow.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  * @(#) @(#) ListShadow.java 1.32 - last change made 08/12/97
34  */
35 
36 package sunsoft.jws.visual.rt.shadow.java.awt;
37 
38 import sunsoft.jws.visual.rt.awt.GBConstraints;
39 import java.awt.List;
40 import java.awt.SystemColor;
41 import sunsoft.jws.visual.rt.base.Global;
42 
43 /**
44  * Wraps an AWT widget.  The attributes available for this
45  * class are listed below.  In the type column, type names beginning
46  * with "sunsoft.jws.visual.rt" have been abbreviated to begin with "rt".
47  *
48  * <pre>
49 name            type                      default value
50 -----------------------------------------------------------------------
51 allowMultipleSelections java.lang.Boolean false
52 items           [Ljava.lang.String;       item1, item2
53 selectedItem    java.lang.String          null
54 selectedItems   [Ljava.lang.String;       null
55 visibleRows     java.lang.Integer         4
56 *  < /pre>
57 *
58 * selectedItem: is the item(amoung the the strings in the "items"
59 * attribute) that is currently showing in the list.  This attribute is
60 * not available in the attribute editor, but is instead expected to be
61 * used programmatically to change or check the setting.
62 *  < p>
63 * Check the super class for additional attributes.
64 *
65 * @see List
66 * @version 	1.32, 08/12/97
67 */
68 public class ListShadow extends ComponentShadow {
ListShadow()69     public ListShadow() {
70         attributes.add(/* NOI18N */"allowMultipleSelections",
71 		       /* NOI18N */"java.lang.Boolean",
72 		       Boolean.FALSE, 0);
73         String sa[] = { /* NOI18N */"item1", /* NOI18N */"item2"};
74         attributes.add(/* NOI18N */"items",
75 		       /* NOI18N */"[Ljava.lang.String;", sa, 0);
76         attributes.add(/* NOI18N */"selectedItem",
77 		       /* NOI18N */"java.lang.String", null, HIDDEN);
78         attributes.add(/* NOI18N */"selectedItems",
79 		       /* NOI18N */"[Ljava.lang.String;", null, HIDDEN);
80         attributes.add(/* NOI18N */"visibleRows",
81 		       /* NOI18N */"java.lang.Integer", new Integer(4),
82 		       CONSTRUCTOR);
83 
84         GBConstraints c = (GBConstraints)get(/* NOI18N */"GBConstraints");
85         c.fill = GBConstraints.BOTH;
86         attributes.add(/* NOI18N */"GBConstraints",
87 		       /* NOI18N */"sunsoft.jws.visual.rt.awt.GBConstraints",
88 		    c);
89 
90         // This is a work around for JDK color bug. The defaults are
91         // not correctly set
92         if (Global.isWindows())  {
93             attributes.add(/* NOI18N */"background",
94 			   /* NOI18N */"java.awt.Color",
95 			   SystemColor.window, DONTFETCH);
96         }
97         if (Global.isMotif())  {
98             attributes.add(/* NOI18N */"background",
99 			   /* NOI18N */"java.awt.Color",
100 			   SystemColor.text, DONTFETCH);
101             attributes.add(/* NOI18N */"foreground",
102 			   /* NOI18N */"java.awt.Color",
103 			   SystemColor.textText, DONTFETCH);
104         }
105     }
106 
getOnBody(String key)107     protected Object getOnBody(String key) {
108         if (key.equals(/* NOI18N */"visibleRows"))
109 	    return (new Integer(((List) body).getRows()));
110         else if (key.equals(/* NOI18N */"allowMultipleSelections"))
111             return (new Boolean(((List) body).allowsMultipleSelections()));
112         else if (key.equals(/* NOI18N */"items")) {
113             if (((List) body).countItems() == 0)
114 		return null;
115             else {
116                 int index;
117                 String[] listContents = new String[((List)body).countItems()];
118                 for (index = 0; index < listContents.length; index++)
119                     listContents[index] = ((List)body).getItem(index);
120                 return listContents;
121             }
122         } else if (key.equals(/* NOI18N */"selectedItem")) {
123             List list = (List)body;
124             return list.getSelectedItem();
125         } else if (key.equals(/* NOI18N */"selectedItems")) {
126             return ((List)body).getSelectedItems();
127         }
128         else
129             return (super.getOnBody(key));
130     }
131 
132     /**
133      * This efficiently makes changes to the List body when the user changes
134      * items in it.  it updates the List body from the new data.
135      */
equalizeLists(Object value)136     private void equalizeLists(Object value) {
137         String[] newList = ((String[]) (value));
138 
139         int newListIndex = 0, oldListIndex = 0;
140         int dummyIndex;
141 
142         // If the user deleted all of the entries, the newList would be null
143         if (newList == null) {
144             if (((List) body).countItems() > 0) {
145                 ((List) body).delItems(0, ((List)body).countItems()-1);
146             }
147         } else {
148             while (newListIndex < newList.length &&
149 		   oldListIndex < ((List) body).countItems())
150 		{
151 		    String curOldItem = ((List) (body)).getItem(oldListIndex);
152 
153 		    if (newList[newListIndex].equals(curOldItem)) {
154 			newListIndex++;
155 			oldListIndex++;
156 		    } else {
157 			for (dummyIndex = newListIndex;
158 			     dummyIndex < newList.length; dummyIndex++) {
159 			    if (curOldItem.equals(newList[dummyIndex])) {
160 				((List) body).delItem(oldListIndex);
161 				break;
162 			    }
163 			}
164 			((List) body).addItem(newList[newListIndex],
165 					    oldListIndex);
166 			newListIndex++;
167 			oldListIndex++;
168 		    }
169 		}
170 
171             if (oldListIndex < ((List) body).countItems()) {
172                 ((List) body).delItems(oldListIndex,
173 				       ((List)body).countItems()-1);
174             }
175 
176             while (newListIndex < newList.length) {
177                 ((List) body).addItem(newList[newListIndex]);
178                 newListIndex++;
179             }
180         }
181     }
182 
setOnBody(String key, Object value)183     protected void setOnBody(String key, Object value) {
184         if (key.equals(/* NOI18N */"allowMultipleSelections"))
185 		/* JSTYLED */
186 	    ((List) body).setMultipleSelections(((Boolean)value).booleanValue());
187         else if (key.equals(/* NOI18N */"items"))
188             equalizeLists(value);
189         else if (key.equals(/* NOI18N */"visibleRows"))
190             return;	// this must be set in constructor
191         else if (key.equals(/* NOI18N */"selectedItem")) {
192             List list = (List)body;
193             if (list.allowsMultipleSelections())
194 		unselectAll(list);
195             select((List)body, (String)value);
196         } else if (key.equals(/* NOI18N */"selectedItems")) {
197             List list = (List)body;
198             String items[] = (String[])value;
199             if (list.allowsMultipleSelections()) {
200                 unselectAll(list);
201                 if (items != null) {
202                     for (int i = 0; i < items.length; i++)
203                         select(list, items[i]);
204                 }
205             } else {
206                 if (items != null && items.length != 0)
207                     select(list, items[0]);
208                 else
209                     select(list, null);
210             }
211         }
212         else
213             super.setOnBody(key, value);
214     }
215 
select(List list, String s)216     private void select(List list, String s) {
217         if (s == null) {
218             int index = list.getSelectedIndex();
219             if (index != -1)
220                 list.deselect(index);
221             return;
222         }
223 
224         int num = list.countItems();
225         for (int i = 0; i < num; i++) {
226             if (s.equals(list.getItem(i))) {
227                 list.select(i);
228                 break;
229             }
230         }
231     }
232 
unselectAll(List list)233     private void unselectAll(List list) {
234         int indexes[] = list.getSelectedIndexes();
235         if (indexes != null) {
236             for (int i = 0; i < indexes.length; i++)
237                 list.deselect(indexes[i]);
238         }
239     }
240 
createBody()241     public void createBody() {
242 	    /* JSTYLED */
243         body = new List(((Integer) getFromTable(/* NOI18N */"visibleRows")).intValue(),
244 			/* JSTYLED */
245 			((Boolean) getFromTable(/* NOI18N */"allowMultipleSelections")).booleanValue());
246     }
247 }
248