KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > ChoiceGroup


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contributor(s):
21  * Shane Harper
22  */

23  
24 package javax.microedition.lcdui;
25
26
27 public class ChoiceGroup extends Item implements Choice
28 {
29
30     ChoiceItem items[] = new ChoiceItem[4];
31   int numOfItems = 0;
32
33   int choiceType;
34
35   int highlightedItemIndex = -1;
36
37     static byte multiOff[] = {
38         -119, 80, 78, 71, 13, 10, 26, 10, 0, 0,
39       0, 13, 73, 72, 68, 82, 0, 0, 0, 10,
40       0, 0, 0, 11, 2, 3, 0, 0, 0, 59,
41       0, -12, -117, 0, 0, 0, 6, 80, 76, 84,
42       69, -1, -1, -1, -69, -69, -69, -57, 75, -33,
43       -8, 0, 0, 0, 30, 73, 68, 65, 84, 120,
44       -38, 99, 96, 96, 96, 96, 12, 101, -8, -51,
45       -32, -64, 32, -64, -60, -64, -64, -128, 11, 51,
46       -122, 50, -4, 6, 0, 63, 116, 3, 1, 53,
47       -108, 39, -26, 0, 0, 0, 0, 73, 69, 78,
48       68, -82, 66, 96, -126 };
49
50     static byte multiOn[] = {
51         -119, 80, 78, 71, 13, 10, 26, 10, 0, 0,
52       0, 13, 73, 72, 68, 82, 0, 0, 0, 10,
53       0, 0, 0, 11, 2, 3, 0, 0, 0, 59,
54       0, -12, -117, 0, 0, 0, 12, 80, 76, 84,
55       69, -1, -1, -1, -69, -69, -69, 106, 106, 106,
56       2, 2, 2, 106, -103, 14, -47, 0, 0, 0,
57       53, 73, 68, 65, 84, 120, -38, 99, 96, 96,
58       124, -64, -16, -1, -77, 3, -45, 65, -111, 15,
59       76, 12, 108, 12, 76, 12, -4, 12, 76, 12,
60       18, 12, 76, -68, 127, 24, -104, 126, 45, 96,
61       96, -7, -11, -109, -127, -23, -65, 3, 3, -29,
62       127, -122, -113, 0, 5, 37, 12, -34, 1, -99,
63       -83, 100, 0, 0, 0, 0, 73, 69, 78, 68,
64       -82, 66, 96, -126 };
65
66     static byte radioOff[] = {
67         -119, 80, 78, 71, 13, 10, 26, 10, 0, 0,
68       0, 13, 73, 72, 68, 82, 0, 0, 0, 11,
69       0, 0, 0, 11, 2, 3, 0, 0, 0, -44,
70       -62, -97, -75, 0, 0, 0, 9, 80, 76, 84,
71       69, -1, -1, -1, -69, -69, -69, 106, 106, 106,
72       -44, 13, -1, -24, 0, 0, 0, 42, 73, 68,
73       65, 84, 120, -38, 99, 96, 90, -59, -64, 32,
74       -63, 48, -127, 65, -127, 65, -127, 41, -127, -31,
75       5, 19, 3, 3, 3, 50, 102, 80, 96, 80,
76       96, -6, -63, 80, -64, -64, -76, -118, 1, 0,
77       113, 24, 5, 61, 73, -68, -100, 98, 0, 0,
78       0, 0, 73, 69, 78, 68, -82, 66, 96, -126 };
79
80     static byte radioOn[] = {
81       -119, 80, 78, 71, 13, 10, 26, 10, 0, 0,
82       0, 13, 73, 72, 68, 82, 0, 0, 0, 11,
83       0, 0, 0, 11, 2, 3, 0, 0, 0, -44,
84       -62, -97, -75, 0, 0, 0, 12, 80, 76, 84,
85       69, -1, -1, -1, -69, -69, -69, 106, 106, 106,
86       2, 2, 2, 106, -103, 14, -47, 0, 0, 0,
87       50, 73, 68, 65, 84, 120, -38, 5, -63, 65,
88       13, 0, 32, 12, 4, -63, -19, -11, -117, 1,
89       18, 68, -100, 10, 52, 19, 94, 72, 64, 17,
90       101, -122, 44, -44, -29, 98, -52, 89, 77, -102,
91       40, 2, 85, -95, -73, -63, -104, -63, 37, -117,
92       15, -40, 119, 10, 41, 78, 26, -79, 59, 0,
93       0, 0, 0, 73, 69, 78, 68, -82, 66, 96,
94       -126 };
95
96     private static final Image imgMultiOff = Image.createImage(multiOff, 0, multiOff.length);
97     private static final Image imgMultiOn = Image.createImage(multiOn, 0, multiOn.length);
98     private static final Image imgRadioOff = Image.createImage(radioOff, 0, radioOff.length);
99     private static final Image imgRadioOn = Image.createImage(radioOn, 0, radioOn.length);
100
101
102   public ChoiceGroup(String label, int choiceType)
103   {
104         super(label);
105         this.choiceType = choiceType;
106   }
107
108
109   // XXX imageElements is ignored.
110
public ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements)
111     {
112         this(label, choiceType);
113
114         for (int i = 0; i < stringElements.length; i++) {
115             if (imageElements == null) {
116                 append(stringElements[i], null);
117             } else {
118                 append(stringElements[i], imageElements[i]);
119             }
120         }
121     }
122
123
124     public int append(String stringPart, Image imagePart)
125   {
126         insert(numOfItems, stringPart, imagePart);
127
128     return (numOfItems - 1);
129   }
130
131
132   public void delete(int itemNum)
133   {
134         if (itemNum < 0 || itemNum >= numOfItems) {
135             throw new IndexOutOfBoundsException();
136         }
137
138     // Ensure that an item of an EXCLUSIVE list remains selected.
139
if (Choice.EXCLUSIVE == choiceType && items[itemNum].isSelected()) {
140       if (numOfItems > 1) {
141         items[itemNum!=0 ? 0 : 1].setSelectedState(true);
142       }
143     }
144
145     // Delete item.
146
if (itemNum != numOfItems - 1) {
147       System.arraycopy(items, itemNum+1, items, itemNum, numOfItems-itemNum-1);
148     }
149     numOfItems--;
150
151     // Ensure highlighted item remains highlighted (if it wasn't just deleted).
152
if (highlightedItemIndex > itemNum) {
153       --highlightedItemIndex;
154     }
155
156     // Ensure that an item remains highlighted.
157
if (highlightedItemIndex >= numOfItems) {
158       highlightedItemIndex = numOfItems-1;
159     }
160     
161     repaint();
162   }
163
164
165   public Image getImage(int elementNum)
166   {
167         if (elementNum < 0 || elementNum >= numOfItems) {
168             throw new IndexOutOfBoundsException();
169         }
170
171     return null;
172   }
173
174
175   /**
176    * Queries the state of a ChoiceGroup and returns the state of all elements in
177    * the boolean array selectedArray_return. NOTE: this is a result parameter.
178    * It must be at least as long as the size of the ChoiceGroup as returned by
179    * size(). If the array is longer, the extra elements are set to false.
180    *
181    * For ChoiceGroup objects of type MULTIPLE, any number of elements may be
182    * selected and set to true in the result array. For ChoiceGroup objects of
183    * type EXCLUSIVE, exactly one element will be selected, unless there are zero
184    * elements in the ChoiceGroup.
185    */

186   public int getSelectedFlags(boolean[] selectedArray_return)
187   {
188         if (selectedArray_return == null) {
189             throw new NullPointerException();
190         }
191         if (selectedArray_return.length < numOfItems) {
192             throw new IllegalArgumentException();
193         }
194
195     // set selectedArray_return elements and count number of selected items
196
int selectedItemsCount = 0;
197     for (int i = 0; i < selectedArray_return.length; ++i) {
198       selectedArray_return[i] = (i<numOfItems) ? items[i].isSelected() : false;
199       if (selectedArray_return[i]) {
200         ++selectedItemsCount;
201       }
202     }
203
204     return selectedItemsCount;
205   }
206
207   /**
208    * Returns the index number of an element in the ChoiceGroup that is
209    * selected. For ChoiceGroup objects of type EXCLUSIVE there is at most one
210    * element selected, so this method is useful for determining the user's
211    * choice. Returns -1 if there are no elements in the ChoiceGroup.
212    *
213    * For ChoiceGroup objects of type MULTIPLE, this always returns -1 because
214    * no single value can in general represent the state of such a ChoiceGroup.
215    * To get the complete state of a MULTIPLE Choice, see getSelectedFlags.
216    */

217   public int getSelectedIndex()
218   {
219     switch (choiceType) {
220       case Choice.EXCLUSIVE:
221         // XXX It'd be nice if the selected item index was stored, so it isn't
222
// necessary to search for it.
223
for (int i = 0; i < numOfItems; ++i) {
224           if (items[i].isSelected()) return i;
225         }
226         break;
227       case Choice.IMPLICIT:
228         return highlightedItemIndex;
229     }
230     return -1;
231   }
232
233
234   public String getString(int elementNum)
235   {
236         if (elementNum < 0 || elementNum >= numOfItems) {
237             throw new IndexOutOfBoundsException();
238         }
239
240     return items[elementNum].getText();
241   }
242
243
244   public void insert(int elementNum, String stringPart, Image imagePart)
245   {
246         if (elementNum < 0 || elementNum > numOfItems) {
247             throw new IndexOutOfBoundsException();
248         }
249         if (imagePart != null && imagePart.isMutable()) {
250             throw new IllegalArgumentException();
251         }
252         if (stringPart == null) {
253             throw new NullPointerException();
254         }
255
256     if (numOfItems == items.length /*no space left in item array*/) {
257       ChoiceItem newItems[] = new ChoiceItem[numOfItems + 4];
258       System.arraycopy(items, 0, newItems, 0, numOfItems);
259       items = newItems;
260     }
261
262     System.arraycopy(items, elementNum, items, elementNum + 1,
263                      numOfItems - elementNum);
264   
265       items[elementNum] = new ChoiceItem(null, imagePart, stringPart);
266
267     ++numOfItems;
268
269     if (numOfItems == 1) {
270       highlightedItemIndex = 0;
271       if (Choice.EXCLUSIVE == choiceType) {
272         setSelectedIndex(0, true);
273       }
274     }
275     
276     repaint();
277   }
278
279
280   public boolean isSelected(int elementNum)
281   {
282         if (elementNum < 0 || elementNum >= numOfItems) {
283             throw new IndexOutOfBoundsException();
284         }
285
286     return items[elementNum].isSelected();
287   }
288
289
290   public void set(int elementNum, String stringPart, Image imagePart)
291     {
292         if (elementNum < 0 || elementNum >= numOfItems) {
293             throw new IndexOutOfBoundsException();
294         }
295         if (imagePart != null && imagePart.isMutable()) {
296             throw new IllegalArgumentException();
297         }
298         if (stringPart == null) {
299             throw new NullPointerException();
300         }
301
302         items[elementNum].setText(stringPart);
303         items[elementNum].setImage(imagePart);
304         
305         repaint();
306   }
307
308
309   public void setLabel(String label)
310   {
311     super.setLabel(label);
312   }
313
314
315   public void setSelectedFlags(boolean[] selectedArray)
316   {
317         if (selectedArray == null) {
318             throw new NullPointerException();
319         }
320         if (selectedArray.length < numOfItems) {
321             throw new NullPointerException();
322         }
323
324         if (choiceType == Choice.EXCLUSIVE) {
325             boolean performed = false;
326             for (int i = 0; i < numOfItems; i++) {
327                 if (selectedArray[i]) {
328                     setSelectedIndex(i, true);
329                     performed = true;
330                     break;
331                 }
332             }
333             if (!performed) {
334                 setSelectedIndex(0, true);
335             }
336         } else if (choiceType == Choice.MULTIPLE) {
337       for (int i = 0; i < numOfItems; i++) {
338         setSelectedIndex(i, selectedArray[i]);
339       }
340         }
341   }
342
343
344   public void setSelectedIndex(int elementNum, boolean selected)
345   {
346         if (elementNum < 0 || elementNum >= numOfItems) {
347             throw new IndexOutOfBoundsException();
348         }
349
350         if (choiceType == Choice.EXCLUSIVE && selected) {
351             for (int i = 0; i < numOfItems; i++) {
352                 items[i].setSelectedState(elementNum == i);
353             }
354             repaint();
355         } else if (choiceType == Choice.MULTIPLE) {
356       items[elementNum].setSelectedState(selected);
357             repaint();
358         }
359   }
360
361
362   public int size()
363   {
364     return numOfItems;
365   }
366
367
368     boolean isFocusable()
369     {
370         return true;
371     }
372
373
374     int getHeight()
375     {
376         int height = 0;
377         for (int i = 0; i < numOfItems; i++) {
378       height += items[i].getHeight();
379         }
380
381         return super.getHeight() + height;
382     }
383
384
385   int paint(Graphics g)
386   {
387         super.paintContent(g);
388
389         g.translate(0, super.getHeight());
390         int translatedY = 0;
391         for (int i = 0; i < numOfItems; i++) {
392       items[i].invertPaint(i == highlightedItemIndex && hasFocus());
393             items[i].paint(g);
394       g.translate(0, items[i].getHeight());
395             translatedY += items[i].getHeight();
396         }
397         g.translate(0, -translatedY);
398         g.translate(0, -super.getHeight());
399
400         return getHeight();
401   }
402
403
404   boolean select()
405   {
406     if (numOfItems == 0) {
407       return false;
408     }
409
410     // XXX What does the following statement do?
411
setSelectedIndex(highlightedItemIndex, !items[highlightedItemIndex].isSelected());
412
413     return true;
414   }
415
416
417   int traverse(int gameKeyCode, int top, int bottom, boolean action)
418   {
419     if (gameKeyCode == Canvas.UP) {
420       if (highlightedItemIndex > 0) {
421                 if (action) {
422             highlightedItemIndex--;
423                 }
424                 int height = super.getHeight();
425                 for (int i = 0; i < highlightedItemIndex; i++) {
426                     height += items[i].getHeight();
427                 }
428                 if (height < top) {
429                     return height - top;
430                 } else {
431                     repaint();
432                 }
433       } else {
434                 if (top > 0) {
435                     return -top;
436                 } else {
437                     return Item.OUTOFITEM;
438                 }
439             }
440     }
441     if (gameKeyCode == Canvas.DOWN) {
442       if ((!action && highlightedItemIndex < numOfItems)
443             || (action && highlightedItemIndex < (numOfItems - 1))) {
444                 if (action) {
445             highlightedItemIndex++;
446                 }
447                 int height = super.getHeight();
448                 for (int i = 0; i <= highlightedItemIndex; i++) {
449                     height += items[i].getHeight();
450                 }
451                 if (height > bottom) {
452                     return height - bottom;
453                 } else {
454                 repaint();
455                 }
456       } else {
457                 return Item.OUTOFITEM;
458             }
459     }
460
461         return 0;
462   }
463
464
465   class ChoiceItem extends ImageStringItem
466   {
467     private boolean selected;
468
469     ChoiceItem(String label, Image image, String text)
470     {
471       super(label, image, text);
472       setSelectedState(false);
473     }
474
475     boolean isSelected()
476     {
477       return selected;
478     }
479
480     void setSelectedState(boolean state)
481     {
482       selected = state;
483       
484       if (choiceType != Choice.IMPLICIT) {
485                 setImage(Choice.EXCLUSIVE == choiceType ?
486                         (state? imgRadioOn:imgRadioOff) : (state? imgMultiOn:imgMultiOff));
487       }
488     }
489   }
490   
491 }
492
Popular Tags