1 2 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 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 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 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 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 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 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 449 private void verifyItem(Item item) 450 { 451 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 item.setOwner(this); 460 } 461 462 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 |