📄 form.java
字号:
} if (itemNum < 0 || itemNum > numOfItems) { throw new IndexOutOfBoundsException(); } insertImpl(itemNum, item); } } /** * Deletes the <code>Item</code> referenced by * <code>itemNum</code>. The size of the <code>Form</code> * shrinks by one. It is legal to delete all items from a * <code>Form</code>. * The <code>itemNum</code> parameter must be * within the range <code>[0..size()-1]</code>, inclusive. * * @param itemNum the index of the item to be deleted * @throws IndexOutOfBoundsException if <code>itemNum</code> is invalid */ public void delete(int itemNum) { synchronized (Display.LCDUILock) { if (itemNum < 0 || itemNum >= numOfItems) { throw new IndexOutOfBoundsException(); } Item deletedItem = items[itemNum]; deletedItem.setOwner(null); numOfItems--; if (traverseIndex == itemNum) { formMode = FORM_TRAVERSE; } if (traverseIndex > itemNum || traverseIndex == numOfItems) { traverseIndex--; } if (itemNum < numOfItems) { System.arraycopy(items, itemNum + 1, items, itemNum, numOfItems - itemNum); } // Delete reference to the last item // that was left after array copy items[numOfItems] = null; // The Form is clear; reset its state if (numOfItems == 0 && items.length > GROW_SIZE) { items = new Item[GROW_SIZE]; // start fresh } invalidate(null); } // synchronized } /** * Deletes all the items from this <code>Form</code>, leaving * it with zero items. * This method does nothing if the <code>Form</code> is already empty. * * @since MIDP 2.0 */ public void deleteAll() { synchronized (Display.LCDUILock) { if (numOfItems == 0) { return; } for (int x = 0; x < numOfItems; x++) { items[x].setOwner(null); items[x] = null; } if (items.length > GROW_SIZE) { items = new Item[GROW_SIZE]; // start fresh } // Reset form state numOfItems = 0; formMode = FORM_TRAVERSE; traverseIndex = -1; invalidate(null); } } /** * Sets the item referenced by <code>itemNum</code> to the * specified item, * replacing the previous item. The previous item is removed * from this <code>Form</code>. * The <code>itemNum</code> parameter must be * within the range <code>[0..size()-1]</code>, inclusive. * * <p>The end result is equal to * <code>insert(n, item); delete(n+1);</code><br> * although the implementation may optimize the repainting * and usage of the array that stores the items. <P> * * @param itemNum the index of the item to be replaced * @param item the new item to be placed in the <code>Form</code> * * @throws IndexOutOfBoundsException if <code>itemNum</code> is invalid * @throws IllegalStateException if the item is already owned by * a container * @throws NullPointerException if <code>item</code> is * <code>null</code> */ public void set(int itemNum, Item item) { synchronized (Display.LCDUILock) { // NullPointerException will be thrown // by item.getOwner() if item == null if (item.getOwner() != null) { throw new IllegalStateException(); } if (itemNum < 0 || itemNum >= numOfItems) { throw new IndexOutOfBoundsException(); } setImpl(itemNum, item); } } /** * Gets the item at given position. The contents of the * <code>Form</code> are left * unchanged. * The <code>itemNum</code> parameter must be * within the range <code>[0..size()-1]</code>, inclusive. * * @param itemNum the index of item * * @return the item at the given position * * @throws IndexOutOfBoundsException if <code>itemNum</code> is invalid */ public Item get(int itemNum) { synchronized (Display.LCDUILock) { if (itemNum < 0 || itemNum >= numOfItems) { throw new IndexOutOfBoundsException(); } return items[itemNum]; } } /** * Sets the <code>ItemStateListener</code> for the * <code>Form</code>, replacing any previous * <code>ItemStateListener</code>. If * <code>iListener</code> is <code>null</code>, simply * removes the previous <code>ItemStateListener</code>. * @param iListener the new listener, or <code>null</code> to remove it */ public void setItemStateListener(ItemStateListener iListener) { synchronized (Display.LCDUILock) { itemStateListener = iListener; } } /** * Gets the number of items in the <code>Form</code>. * @return the number of items */ public int size() { // SYNC NOTE: return of atomic value, no locking necessary return numOfItems; } /** * Returns the width in pixels of the displayable area available for items. * The value may depend on how the device uses the screen and may be * affected by the presence or absence of the ticker, title, or commands. * The <code>Items</code> of the <code>Form</code> are * laid out to fit within this width. * @return the width of the <code>Form</code> in pixels * @since MIDP 2.0 */ public int getWidth() { return viewport[WIDTH] - CELL_SPACING - CELL_SPACING; } /** * Returns the height in pixels of the displayable area available * for items. * This value is the height of the form that can be displayed without * scrolling. * The value may depend on how the device uses the screen and may be * affected by the presence or absence of the ticker, title, or commands. * @return the height of the displayable area of the * <code>Form</code> in pixels * @since MIDP 2.0 */ public int getHeight() { return viewport[HEIGHT] - CELL_SPACING - CELL_SPACING; }// ************************************************************// protected methods// ************************************************************// ************************************************************// package private methods// ************************************************************ /** * Called to commit any pending user interaction for the current item. * Override the no-op in Displayable. */ void commitPendingInteraction() { Item curItem = getCurrentItem(); if (curItem != null) { curItem.commitPendingInteraction(); } } /** * Set the current traversal location to the given Item. * This call has no effect if the given Item is the * current traversal item, or if the given Item is not * part of this Form. * * @param i the Item to make the current traversal item */ void setCurrentItem(Item i) { // SYNC NOTE: This method is called from Display which holds // LCDUILock around the call if (i == null || i.owner != this) { return; } if (traverseIndex != -1 && items[traverseIndex] == i) { return; } for (int x = 0; x < numOfItems; x++) { if (items[x] == i) { setTraverseIndex(CustomItem.NONE, traverseIndex, x); ((Screen)paintDelegate).resetToTop = false; return; } } } /** * notify this Form it is being shown on the given Display * * @param d the Display showing this Form */ void callShowNotify(Display d) { super.callShowNotify(d); synchronized (Display.LCDUILock) { // Whenever this Form is shown, update the layout layout(); // In most cases, we reset the form to the top // The cast is safe because paintDelegate is always // either a List, TextBox, Alert or this Form itself. if (((Screen)paintDelegate).resetToTop) { traverseIndex = -1; view[Y] = 0; view[X] = 0; } else { ((Screen)paintDelegate).resetToTop = true; } } // We issue a default traverse when a Form is initially // shown to traverse to the first item in the Form traverse(CustomItem.NONE); } /** * notify this Form it is being hidden on the given Display * * @param d the Display hiding this Form */ void callHideNotify(Display d) { super.callHideNotify(d); // SYNC NOTE: Rather than make a complete copy of the set // of items on this form, we'll simply catch any exception // that occurs and move on. The only problem that could occur // would be items being deleted from the Form, which would // mean the application was removing items from the Form // while it was technically still visible. if (traverseIndex != -1) { try { items[traverseIndex].callTraverseOut(); } catch (Throwable t) { // Simply swallow the error and move on } } // We need to loop through our Items and call hideNotify // on those that were visible for (int x = 0; x < numOfItems; x++) { try { if (items[x].visible) { items[x].callHideNotify(); } } catch (Throwable t) { // Simply swallow the error and move on } } } /** * Handle a key press * * @param keyCode the key code of the key which was pressed */ void callKeyPressed(int keyCode) { Item i = null; synchronized (Display.LCDUILock) { if (numOfItems == 0 || traverseIndex < 0) { return; } i = items[traverseIndex]; } // synchronized // SYNC NOTE: formMode can only change as a result of a // traversal, which can only occur serially on the event // thread, so its safe to use it outside of the lock if (keyCode == Display.KEYCODE_UP || keyCode == Display.KEYCODE_DOWN || keyCode == Display.KEYCODE_LEFT || keyCode == Display.KEYCODE_RIGHT) { traverse(Display.getGameAction(keyCode)); return; } // SYNC NOTE: callKeyPressed may result in a call to the // application, so we make sure we do this outside of the // LCDUILock if (i != null) { // pass the keypress onto the current item i.callKeyPressed(keyCode); } } /** * Handle a key release event * * @param keyCode the key which was released */ void callKeyReleased(int keyCode) { Item i = null; synchronized (Display.LCDUILock) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -