1 |
/* Choice.java -- Java choice button widget. |
/* Choice.java -- Java choice button widget. |
2 |
Copyright (C) 1999 Free Software Foundation, Inc. |
Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
63 |
/** |
/** |
64 |
* @serial The index of the selected item in the choice box. |
* @serial The index of the selected item in the choice box. |
65 |
*/ |
*/ |
66 |
private int selectedIndex; |
private int selectedIndex = -1; |
67 |
|
|
68 |
// Listener chain |
// Listener chain |
69 |
private ItemListener item_listeners; |
private ItemListener item_listeners; |
139 |
public synchronized void |
public synchronized void |
140 |
add(String item) |
add(String item) |
141 |
{ |
{ |
142 |
|
if (item == null) |
143 |
|
throw new IllegalArgumentException ("item must be non-null"); |
144 |
|
|
145 |
pItems.addElement(item); |
pItems.addElement(item); |
146 |
|
|
147 |
ChoicePeer cp = (ChoicePeer)getPeer(); |
int i = pItems.size () - 1; |
148 |
if (cp != null) |
if (peer != null) |
149 |
cp.add(item, getItemCount()); |
{ |
150 |
|
ChoicePeer cp = (ChoicePeer) peer; |
151 |
|
cp.add (item, i); |
152 |
|
} |
153 |
|
|
154 |
|
if (i == 0) |
155 |
|
select (0); |
156 |
} |
} |
157 |
|
|
158 |
/*************************************************************************/ |
/*************************************************************************/ |
165 |
public synchronized void |
public synchronized void |
166 |
addItem(String item) |
addItem(String item) |
167 |
{ |
{ |
168 |
pItems.addElement(item); |
add(item); |
|
|
|
|
ChoicePeer cp = (ChoicePeer)getPeer(); |
|
|
if (cp != null) |
|
|
cp.addItem(item, getItemCount()); |
|
169 |
} |
} |
170 |
|
|
171 |
/*************************************************************************/ |
/*************************************************************************/ |
172 |
|
|
173 |
/** |
/** Inserts an item into this Choice. Existing items are shifted |
174 |
* Inserts the specified item to this choice box at the specified index. |
* upwards. If the new item is the only item, then it is selected. |
175 |
* |
* If the currently selected item is shifted, then the first item is |
176 |
* @param item The item to add. |
* selected. If the currently selected item is not shifted, then it |
177 |
* @param index The index at which the item should be inserted. |
* remains selected. |
178 |
*/ |
* |
179 |
|
* @param item The item to add. |
180 |
|
* @param index The index at which the item should be inserted. |
181 |
|
*/ |
182 |
public synchronized void |
public synchronized void |
183 |
insert(String item, int index) |
insert(String item, int index) |
184 |
{ |
{ |
185 |
|
if (index > getItemCount ()) |
186 |
|
index = getItemCount (); |
187 |
|
|
188 |
pItems.insertElementAt(item, index); |
pItems.insertElementAt(item, index); |
189 |
|
|
190 |
ChoicePeer cp = (ChoicePeer)getPeer(); |
if (peer != null) |
191 |
if (cp != null) |
{ |
192 |
cp.addItem(item, getItemCount()); |
ChoicePeer cp = (ChoicePeer) peer; |
193 |
|
cp.add (item, index); |
194 |
|
} |
195 |
|
|
196 |
|
if (getItemCount () == 1 || selectedIndex >= index) |
197 |
|
select (0); |
198 |
} |
} |
199 |
|
|
200 |
/*************************************************************************/ |
/*************************************************************************/ |
210 |
remove(String item) |
remove(String item) |
211 |
{ |
{ |
212 |
int index = pItems.indexOf(item); |
int index = pItems.indexOf(item); |
213 |
|
if (index == -1) |
214 |
|
throw new IllegalArgumentException ("item \"" |
215 |
|
+ item + "\" not found in Choice"); |
216 |
remove(index); |
remove(index); |
217 |
} |
} |
218 |
|
|
230 |
{ |
{ |
231 |
pItems.removeElementAt(index); |
pItems.removeElementAt(index); |
232 |
|
|
233 |
ChoicePeer cp = (ChoicePeer)getPeer(); |
if (peer != null) |
234 |
if (cp != null) |
{ |
235 |
cp.remove(index); |
ChoicePeer cp = (ChoicePeer) peer; |
236 |
|
cp.remove (index); |
237 |
|
} |
238 |
|
|
239 |
|
if (index == selectedIndex) |
240 |
|
select (0); |
241 |
|
else if (selectedIndex > index) |
242 |
|
--selectedIndex; |
243 |
} |
} |
244 |
|
|
245 |
/*************************************************************************/ |
/*************************************************************************/ |
252 |
{ |
{ |
253 |
int count = getItemCount(); |
int count = getItemCount(); |
254 |
|
|
255 |
if (count > 0) |
for (int i = 0; i < count; i++) |
256 |
for (int i = 0; i < count; i++) |
{ |
257 |
remove(i); |
// Always remove 0. |
258 |
|
remove(0); |
259 |
pItems = new Vector(); |
} |
260 |
} |
} |
261 |
|
|
262 |
/*************************************************************************/ |
/*************************************************************************/ |
263 |
|
|
264 |
/** |
/** |
265 |
* Returns the currently selected item. |
* Returns the currently selected item, or null if no item is |
266 |
|
* selected. |
267 |
* |
* |
268 |
* @return The currently selected item. |
* @return The currently selected item. |
269 |
*/ |
*/ |
270 |
public synchronized String |
public synchronized String |
271 |
getSelectedItem() |
getSelectedItem() |
272 |
{ |
{ |
273 |
return((String)pItems.elementAt(selectedIndex)); |
return (selectedIndex == -1 |
274 |
|
? null |
275 |
|
: ((String)pItems.elementAt(selectedIndex))); |
276 |
} |
} |
277 |
|
|
278 |
/*************************************************************************/ |
/*************************************************************************/ |
285 |
public synchronized Object[] |
public synchronized Object[] |
286 |
getSelectedObjects() |
getSelectedObjects() |
287 |
{ |
{ |
288 |
|
if (selectedIndex == -1) |
289 |
|
return null; |
290 |
|
|
291 |
Object[] objs = new Object[1]; |
Object[] objs = new Object[1]; |
292 |
objs[0] = pItems.elementAt(selectedIndex); |
objs[0] = pItems.elementAt(selectedIndex); |
293 |
|
|
323 |
throw new IllegalArgumentException("Bad index: " + index); |
throw new IllegalArgumentException("Bad index: " + index); |
324 |
|
|
325 |
this.selectedIndex = index; |
this.selectedIndex = index; |
326 |
|
if (peer != null) |
327 |
ChoicePeer cp = (ChoicePeer)getPeer(); |
{ |
328 |
if (cp != null) |
ChoicePeer cp = (ChoicePeer) peer; |
329 |
cp.select(index); |
cp.select (index); |
330 |
|
} |
331 |
} |
} |
332 |
|
|
333 |
/*************************************************************************/ |
/*************************************************************************/ |
343 |
select(String item) |
select(String item) |
344 |
{ |
{ |
345 |
int index = pItems.indexOf(item); |
int index = pItems.indexOf(item); |
346 |
select(index); |
if (index >= 0) |
347 |
|
select(index); |
348 |
} |
} |
349 |
|
|
350 |
/*************************************************************************/ |
/*************************************************************************/ |
355 |
public void |
public void |
356 |
addNotify() |
addNotify() |
357 |
{ |
{ |
358 |
if (getPeer() == null) |
if (peer == null) |
359 |
setPeer((ComponentPeer)getToolkit().createChoice(this)); |
peer = getToolkit ().createChoice (this); |
360 |
|
super.addNotify (); |
361 |
} |
} |
362 |
|
|
363 |
/*************************************************************************/ |
/*************************************************************************/ |
430 |
protected String |
protected String |
431 |
paramString() |
paramString() |
432 |
{ |
{ |
433 |
return(getClass().getName() + "(selectedIndex=" + selectedIndex + ")"); |
return ("selectedIndex=" + selectedIndex + "," + super.paramString()); |
434 |
} |
} |
435 |
|
|
436 |
} // class Choice |
} // class Choice |
|
|
|