📄 choicegroup.java
字号:
*/ boolean takesFocus() { return numOfEls > 0; } /** * Initialize the highlight of this ChoiceGroup * * @param vpY * @param vpH * @return int Always returns 0 */ int initHilight(int vpY, int vpH) { if (numOfEls > 0) { hilightedIndex = 0; if (choiceType == List.IMPLICIT) { selectedIndex = 0; } } return 0; } /** * Perform a selection on this ChoiceGroup * * @return boolean Flag of wether the selection succeeded or not */ boolean select() { if (numOfEls == 0) { return false; } switch (choiceType) { case Choice.IMPLICIT: return getOwner() instanceof List; case Choice.EXCLUSIVE: if (hilightedIndex == selectedIndex) { return false; } setSelectedIndex(hilightedIndex, true); return getOwner() instanceof Form; case Choice.MULTIPLE: setSelectedIndex(hilightedIndex, !selEls[hilightedIndex]); return getOwner() instanceof Form; default: return false; } } /** * Paint this ChoiceGroup given the supplied Graphics context * * @param g The Graphics object to paint to */ void paint(Graphics g) { int clipY1 = g.getClipY(); int clipY2 = clipY1 + g.getClipHeight(); int translatedY = 0; if (translatedY < clipY2) { layouts[0].paint(g, true, false); int h = layouts[0].getHeight(); g.translate(0, h); translatedY += h; } int imgWidth = 0; if (choiceType == Choice.EXCLUSIVE) { imgWidth = RD_WIDTH; } else if (choiceType == Choice.MULTIPLE) { imgWidth = CKBX_WIDTH; } boolean itemIsInFocus = getOwner() instanceof List ? true : hasFocus(); for (int i = 0; i < numOfEls && translatedY < clipY2; i++) { Layout layout = layouts[i + 1]; int h = layout.getHeight(); if (translatedY + h >= clipY1) { boolean inverted = (i == hilightedIndex && itemIsInFocus); if (choiceType == Choice.IMPLICIT) { layout.paint(g, true, inverted); } else { Image img = null; if (choiceType == Choice.EXCLUSIVE) { img = (i == selectedIndex) ? RD_ON_IMG : RD_OFF_IMG; } else { img = (selEls[i]) ? CKBX_ON_IMG : CKBX_OFF_IMG; } g.setColor(Display.ERASE_COLOR); g.fillRect(0, 0, imgWidth + 2, h); g.drawImage(img, 1, (layout.getLineHeight() + 1) / 2, Graphics.LEFT | Graphics.VCENTER); g.translate(imgWidth + 2, 0); layout.paint(g, true, inverted); g.translate(-imgWidth-2, 0); } } g.translate(0, h); translatedY += h; } g.translate(0, -translatedY); } /** * Get the height of this ChoiceGroup * * @return int The height of this ChoiceGroup */ int getHeight() { return height; } /** * Set the width for this ChoiceGroup to lay out with. * * @param width The allowable width for this ChoiceGroup * @return int The height required by this ChoiceGroup to * display given the allowable width */ int setWidth(int width) { if (width > 0) { height = layouts[0].setWidth(width); if (choiceType == Choice.EXCLUSIVE) { width -= RD_WIDTH + 2; } else if (choiceType == Choice.MULTIPLE) { width -= CKBX_WIDTH + 2; } for (int i = 0; i < numOfEls; i++) { height += layouts[i + 1].setWidth(width); } } return height; } // private /** * Set a particular element of this ChoiceGroup * * @param elementNum The index of the element to establish * @param stringPart The string part of the element to establish * @param imagePart The image part of the element to establish */ private void setImpl(int elementNum, String stringPart, Image imagePart) { // indent 2nd line onward only for IMPLICIT list AND // there is not image boolean offset = choiceType == Choice.IMPLICIT && imagePart == null; if (initLayoutDone()) { int deltaHeight = 0; if (layouts[elementNum + 1] != null) { deltaHeight = -layouts[elementNum + 1].getHeight(); } layouts[elementNum + 1] = new IaSLayout(stringPart, imagePart, Screen.CONTENT_FONT, offset); int w = Display.WIDTH; if (choiceType == Choice.EXCLUSIVE) { w -= RD_WIDTH + 2; } else if (choiceType == Choice.MULTIPLE) { w -= CKBX_WIDTH + 2; } deltaHeight += layouts[elementNum + 1].setWidth(w); height += deltaHeight; contentChanged(0, getYpos(0, layouts[0].getHeight(), elementNum), deltaHeight); } else { layouts[elementNum + 1] = new IaSLayout(stringPart, imagePart, Screen.CONTENT_FONT, offset); } } /** * Insert a particular element of this ChoiceGroup * * @param elementNum The index to insert the element * @param stringPart The string part of the element to insert * @param imagePart The image part of the element to insert * @return int The index of the newly inserted element */ private int insertImpl(int elementNum, String stringPart, Image imagePart) { if (numOfEls + 1 == layouts.length) { Layout[] newLayouts = new Layout[layouts.length + 4]; System.arraycopy(layouts, 0, newLayouts, 0, elementNum + 1); System.arraycopy(layouts, elementNum + 1, newLayouts, elementNum + 2, numOfEls - elementNum); layouts = newLayouts; } else { System.arraycopy(layouts, elementNum + 1, layouts, elementNum + 2, numOfEls - elementNum); } if (choiceType == Choice.MULTIPLE) { if (selEls.length == numOfEls) { boolean newSelEls[] = new boolean[numOfEls + 4]; System.arraycopy(selEls, 0, newSelEls, 0, elementNum); System.arraycopy(selEls, elementNum, newSelEls, elementNum + 1, numOfEls - elementNum); selEls = newSelEls; } else { System.arraycopy(selEls, elementNum, selEls, elementNum + 1, numOfEls - elementNum); } selEls[elementNum] = false; } layouts[elementNum + 1] = null; numOfEls++; if (choiceType != Choice.MULTIPLE && (elementNum < selectedIndex || selectedIndex == -1)) { selectedIndex++; } if (elementNum < hilightedIndex || hilightedIndex == -1) { hilightedIndex++; } setImpl(elementNum, stringPart, imagePart); return elementNum; } /** * Repaint the elements of this ChoiceGroup in the range of * first and second. * * @param first The start index of the elements to repaint * @param second The end index of the elements to repaint */ private void repaintElements(int first, int second) { if (first == - 1 && second == -1) { return; } if ((first > second && second != -1) || first == -1) { int tmp = first; first = second; second = tmp; } int translatedY = layouts[0].getHeight(); Screen owner = getOwner(); int itemY = owner instanceof Form ? ((Form)owner).getItemY(this) : 0; if (first >= 0 && first < numOfEls) { translatedY = getYpos(0, translatedY, first); // Short-cut when two items are next to each other if (first + 1 == second) { owner.repaintContent(0, itemY + translatedY, Display.WIDTH, layouts[first + 1].getHeight() + layouts[second + 1].getHeight()); return; } owner.repaintContent(0, itemY + translatedY, Display.WIDTH, layouts[first + 1].getHeight()); } if (second >= 0 && second < numOfEls) { translatedY = getYpos(first, translatedY, second); owner.repaintContent(0, itemY + translatedY, Display.WIDTH, layouts[second + 1].getHeight()); } } /** * Get the y coordinate of the element at the given index with the * given start index and y coordinate starting position of the element * at that start index * * @param startIndex The index of the starting element * @param yPosOfStart The y coordinate position of the starting element * @param elementNum The index of the element to get the y position for * @return int The y coordinate position of the element at index elementNum */ private int getYpos(int startIndex, int yPosOfStart, int elementNum) { int y = yPosOfStart < 0 ? 0 : yPosOfStart; for (int i = startIndex; i < elementNum; i++) { y += layouts[i + 1].getHeight(); } return y; } /** * Check the validity of a given element index * * @param elementNum The index to check * @throws IndexOutOfBoundsException If no element exists at the * that index */ private void checkIndex(int elementNum) { if (elementNum < 0 || elementNum >= numOfEls) { throw new IndexOutOfBoundsException(); } } /** * Check the given values for null * * @param stringPart The string part of the element * @param imagePart The image part of the element * @throws NullPointerException If the string part is null * @throws IllegalArgumentException If the image part is null * or mutable */ private void checkNull(String stringPart, Image imagePart) { if (stringPart == null) { throw new NullPointerException(); } if (imagePart != null && imagePart.isMutable()) { throw new IllegalArgumentException(); } } /** * Check the validity of the selection array * * @param flag The array of boolean flags representing the * selected state of the elements * @throws NullPointerException If the flag array is null * @throws IllegalArgumentException If the flag array is not * the same size as the element array */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -