KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package javax.microedition.lcdui;
25
26 public class Form extends Screen
27 {
28     Item items[] = new Item[4];
29     int numOfItems = 0;
30     int focusItemIndex;
31     ItemStateListener itemStateListener = null;
32
33     
34     public Form(String title)
35     {
36         super(title);
37         focusItemIndex = -2;
38     }
39
40     
41     public Form(String title, Item[] items)
42     {
43         this(title);
44
45         this.items = items;
46         numOfItems = items.length;
47         for (int i = 0; i < numOfItems; i++) {
48             verifyItem(items[i]);
49         }
50     }
51
52     
53     public int append(Item item)
54     {
55         verifyItem(item);
56
57         if (numOfItems + 1 == items.length) {
58             Item newitems[] = new Item[numOfItems + 4];
59             System.arraycopy(items, 0, newitems, 0, numOfItems);
60             items = newitems;
61         }
62         items[numOfItems] = item;
63         numOfItems++;
64
65         return (numOfItems - 1);
66     }
67
68     
69     public int append(Image img)
70     {
71         return append(new ImageItem(null, img, ImageItem.LAYOUT_DEFAULT, null));
72     }
73
74     
75     public int append(String str)
76     {
77         if (str == null) {
78             throw new NullPointerException();
79         }
80
81         return append(new StringItem(null, str));
82     }
83
84     
85     public void delete(int itemNum)
86     {
87         verifyItemNum(itemNum);
88
89         items[itemNum].setOwner(null);
90         System.arraycopy(
91             items,
92             itemNum + 1,
93             items,
94             itemNum,
95             numOfItems - itemNum - 1);
96         numOfItems--;
97     }
98
99     
100     public Item get(int itemNum)
101     {
102         verifyItemNum(itemNum);
103
104         return items[itemNum];
105     }
106
107     
108     public void insert(int itemNum, Item item)
109     {
110         verifyItemNum(itemNum);
111         verifyItem(item);
112
113         if (numOfItems + 1 == items.length) {
114             Item newitems[] = new Item[numOfItems + 4];
115             System.arraycopy(items, 0, newitems, 0, numOfItems);
116             items = newitems;
117         }
118         System.arraycopy(
119             items,
120             itemNum,
121             items,
122             itemNum + 1,
123             numOfItems - itemNum);
124         items[itemNum] = item;
125         items[itemNum].setOwner(this);
126         numOfItems++;
127     }
128
129     
130     public void set(int itemNum, Item item)
131     {
132         verifyItemNum(itemNum);
133         verifyItem(item);
134
135         items[itemNum] = item;
136         items[itemNum].setOwner(this);
137     }
138
139     
140     public void setItemStateListener(ItemStateListener iListener)
141     {
142         itemStateListener = iListener;
143     }
144
145     
146     public int size()
147     {
148         return numOfItems;
149     }
150
151     
152     int paintContent(Graphics g)
153     {
154         int contentHeight = 0;
155         int translateY;
156         for (int i = 0; i < numOfItems; i++) {
157             translateY = items[i].paint(g);
158             g.translate(0, translateY);
159             contentHeight += translateY;
160         }
161         g.translate(0, -contentHeight);
162
163         return contentHeight;
164     }
165
166     
167     int getHeight()
168     {
169         int height = 0;
170
171         for (int i = 0; i < numOfItems; i++) {
172             height += items[i].getHeight();
173         }
174
175         return height;
176     }
177
178     
179     void hideNotify()
180     {
181         super.hideNotify();
182
183         for (int i = 0; i < numOfItems; i++) {
184             if (items[i].isFocusable() && items[i].hasFocus()) {
185                 items[i].setFocus(false);
186                 focusItemIndex = -2;
187                 break;
188             }
189         }
190     }
191
192     
193     void keyPressed(int keyCode)
194     {
195         if (focusItemIndex != -1) {
196             if (Display.getGameAction(keyCode) == Canvas.FIRE) {
197                 items[focusItemIndex].select();
198                 if (itemStateListener != null) {
199                     itemStateListener.itemStateChanged(items[focusItemIndex]);
200                 }
201             } else {
202                 items[focusItemIndex].keyPressed(keyCode);
203             }
204         }
205
206         super.keyPressed(keyCode);
207     }
208
209     
210     void showNotify()
211     {
212         super.showNotify();
213
214         if (focusItemIndex == -2) {
215             focusItemIndex = -1;
216
217             for (int i = 0; i < numOfItems; i++) {
218                 if (items[i].isFocusable()) {
219                     items[i].setFocus(true);
220                     focusItemIndex = i;
221                     break;
222                 }
223             }
224         }
225     }
226
227     
228     int traverse(int gameKeyCode, int top, int bottom)
229     {
230         int height, testItemIndex, traverse, i;
231         int topItemIndex, bottomItemIndex;
232
233         if (numOfItems == 0) {
234             return 0;
235         }
236
237         if (gameKeyCode == Canvas.UP) {
238             topItemIndex = getTopVisibleIndex(top);
239             if (focusItemIndex == -1) {
240                 testItemIndex = topItemIndex;
241                 height = getHeightToItem(testItemIndex);
242                 traverse =
243                     items[testItemIndex].traverse(
244                         gameKeyCode,
245                         top - height,
246                         bottom - height,
247                         false);
248             } else {
249                 testItemIndex = focusItemIndex;
250                 height = getHeightToItem(testItemIndex);
251                 traverse =
252                     items[testItemIndex].traverse(
253                         gameKeyCode,
254                         top - height,
255                         bottom - height,
256                         true);
257             }
258             if (traverse != Item.OUTOFITEM) {
259                 if (focusItemIndex == -1
260                     && items[testItemIndex].isFocusable()) {
261                     items[testItemIndex].setFocus(true);
262                     focusItemIndex = testItemIndex;
263                 }
264                 return traverse;
265             } else {
266                 if (testItemIndex > 0) {
267                     // Czy istnieje obiekt focusable powyzej testItemIndex
268
// widoczny na ekranie
269
// jesli tak to zrob na nim traverse(false) i return
270
// traverse
271
for (i = testItemIndex - 1; i >= topItemIndex; i--) {
272                         if (items[i].isFocusable()) {
273                             if (focusItemIndex != -1) {
274                                 items[focusItemIndex].setFocus(false);
275                             }
276                             items[i].setFocus(true);
277                             focusItemIndex = i;
278                             height = getHeightToItem(i);
279                             traverse =
280                                 items[i].traverse(
281                                     gameKeyCode,
282                                     top - height,
283                                     bottom - height,
284                                     false);
285                             if (traverse == Item.OUTOFITEM) {
286                                 return 0;
287                             } else {
288                                 return traverse;
289                             }
290                         }
291                     }
292                     // Na najnizszym widocznym item zrob traverse(false)
293
height = getHeightToItem(topItemIndex);
294                     traverse =
295                         items[topItemIndex].traverse(
296                             gameKeyCode,
297                             top - height,
298                             bottom - height,
299                             false);
300                     if (traverse == Item.OUTOFITEM) {
301                     } else {
302                         // Sprawdzenie czy znajduje sie powyzej na ekranie
303
// focusable item
304
// jesli tak zrob co trzeba
305
bottomItemIndex = getTopVisibleIndex(bottom + traverse);
306                         if (focusItemIndex != -1
307                             && focusItemIndex > bottomItemIndex) {
308                             items[focusItemIndex].setFocus(false);
309                             focusItemIndex = -1;
310                         }
311                         return traverse;
312                     }
313                 }
314             }
315         }
316         if (gameKeyCode == Canvas.DOWN) {
317             bottomItemIndex = getBottomVisibleIndex(bottom);
318             if (focusItemIndex == -1) {
319                 testItemIndex = bottomItemIndex;
320                 height = getHeightToItem(testItemIndex);
321                 traverse =
322                     items[testItemIndex].traverse(
323                         gameKeyCode,
324                         top - height,
325                         bottom - height,
326                         false);
327             } else {
328                 testItemIndex = focusItemIndex;
329                 height = getHeightToItem(testItemIndex);
330                 traverse =
331                     items[testItemIndex].traverse(
332                         gameKeyCode,
333                         top - height,
334                         bottom - height,
335                         true);
336             }
337             if (traverse != Item.OUTOFITEM) {
338                 if (focusItemIndex == -1
339                     && items[testItemIndex].isFocusable()) {
340                     items[testItemIndex].setFocus(true);
341                     focusItemIndex = testItemIndex;
342                 }
343                 return traverse;
344             } else {
345                 if (testItemIndex < numOfItems - 1) {
346                     // Czy istnieje obiekt focusable ponizej testItemIndex
347
// widoczny na ekranie
348
// jesli tak to zrob na nim traverse(false) i return
349
// traverse
350
for (i = testItemIndex + 1; i <= bottomItemIndex; i++) {
351                         if (items[i].isFocusable()) {
352                             if (focusItemIndex != -1) {
353                                 items[focusItemIndex].setFocus(false);
354                             }
355                             items[i].setFocus(true);
356                             focusItemIndex = i;
357                             height = getHeightToItem(i);
358                             traverse =
359                                 items[i].traverse(
360                                     gameKeyCode,
361                                     top - height,
362                                     bottom - height,
363                                     false);
364                             if (traverse == Item.OUTOFITEM) {
365                                 return 0;
366                             } else {
367                                 return traverse;
368                             }
369                         }
370                     }
371                     // Na najnizszym widocznym item zrob traverse(false)
372
height = getHeightToItem(bottomItemIndex);
373                     traverse =
374                         items[bottomItemIndex].traverse(
375                             gameKeyCode,
376                             top - height,
377                             bottom - height,
378                             false);
379                     if (traverse == Item.OUTOFITEM) {
380                     } else {
381                         // Sprawdzenie czy znajduje sie powyzej na ekranie
382
// focusable item
383
// jesli tak zrob co trzeba
384
topItemIndex = getTopVisibleIndex(top + traverse);
385                         if (focusItemIndex != -1
386                             && focusItemIndex < topItemIndex) {
387                             items[focusItemIndex].setFocus(false);
388                             focusItemIndex = -1;
389                         }
390                         return traverse;
391                     }
392                 }
393             }
394         }
395
396         return 0;
397     }
398
399     
400     int getTopVisibleIndex(int top)
401     {
402         int height = 0;
403
404         for (int i = 0; i < numOfItems; i++) {
405             height += items[i].getHeight();
406             if (height >= top) {
407                 return i;
408             }
409         }
410
411         return numOfItems - 1;
412     }
413
414     
415     int getBottomVisibleIndex(int bottom)
416     {
417         int height = 0;
418
419         for (int i = 0; i < numOfItems; i++) {
420             height += items[i].getHeight();
421             if (height > bottom) {
422                 return i;
423             }
424         }
425
426         return numOfItems - 1;
427     }
428
429     
430     int getHeightToItem(int itemIndex)
431     {
432         int height = 0;
433
434         for (int i = 0; i < itemIndex; i++) {
435             height += items[i].getHeight();
436         }
437
438         return height;
439     }
440
441     /**
442      * Verify that the item is non null and is not owned by this form or anyone
443      * else. If all is ok set the owner to this Form
444      *
445      * @param item the item to be verified
446      * @throws IllegalStateException
447      * @throws NullPointerException
448      */

449     private void verifyItem(Item item)
450     {
451         // Check that we are being passed valid items
452
if (item == null) {
453             throw new NullPointerException("item is null");
454         }
455         if (item.getOwner() != null) {
456             throw new IllegalStateException("item is already owned");
457         }
458         // All is ok make ourselves the owner
459
item.setOwner(this);
460     }
461
462     /**
463      * Verify that the index passed in is valid for this form. ie within the
464      * range 0..size-1
465      *
466      * @param itemNum the number of the item
467      * @throws IndexOutOfBoundsException
468      */

469     private void verifyItemNum(int itemNum)
470     {
471         if (itemNum < 0 || itemNum >= numOfItems) {
472             throw new IndexOutOfBoundsException("item number is outside range of Form");
473         }
474     }
475
476 }
477
Popular Tags