📄 choicegroup.java
字号:
int maxElWidth = 0; Font fnt; int grWidth = 0; if (choiceType == Choice.EXCLUSIVE) { grWidth = RD_WIDTH + LABEL_PAD; } else if (choiceType == Choice.MULTIPLE) { grWidth = CKBX_WIDTH + LABEL_PAD; } for (int i = 0; i < numOfEls; i++) { w = (imageEls == null || imageEls[i] == null) ? 0 : PREFERRED_IMG_W + LABEL_PAD; if (stringEls[i] != null && stringEls[i].length() > 0) { textOffset = w; if (fontEls != null && fontEls[i] != null) { fnt = fontEls[i]; } else { fnt = Screen.CONTENT_FONT; } if (choiceType != Choice.POPUP) { w = Text.getWidestLineWidth(stringEls[i].toCharArray(), textOffset, DEFAULT_WIDTH - grWidth, fnt); } else { w = Text.getWidestLineWidth(stringEls[i].toCharArray(), textOffset, DEFAULT_WIDTH * 2, fnt); } if (w > maxElWidth) { maxElWidth = w; } } } w = lW; if (choiceType == Choice.POPUP) { if (w > 0) { w += LABEL_PAD; } w += POPUP_AR_WIDTH + LABEL_PAD + maxElWidth; maxPopupWidth = maxElWidth; return (w > Item.DEFAULT_WIDTH ? Item.DEFAULT_WIDTH : w); } if (w < grWidth + maxElWidth) { // minimum required width is the maximum of the label width and // of the widest element width w = grWidth + maxElWidth; } return (w < DEFAULT_WIDTH ? w : DEFAULT_WIDTH); } /** * Get the preferred width of this Item * * @param h the desired height for this CG * @return the preferred width */ int callPreferredWidth(int h) { // If we're a List, we want to be as wide as possible if (isList) { return 500; } return callMinimumWidth(); } /** * Get the minimum height of this Item * * @return the minimum height */ int callMinimumHeight() { return callPreferredHeight(callMinimumWidth()); } /** * Get the preferred height of this Item * * @param width the desired width for this CG * @return the preferred height */ int callPreferredHeight(int width) { if (width == -1) { width = callPreferredWidth(-1); } // empty ChoiceGroup is not shown if (width == 0) { return 0; } int lH = getLabelHeight(width); if (choiceType == Choice.EXCLUSIVE) { width -= (RD_WIDTH + LABEL_PAD); } else if (choiceType == Choice.MULTIPLE) { width -= (CKBX_WIDTH + LABEL_PAD); } int eHeight = calculateElementHeight(width); if (lH == 0) { // empty ChoiceGroup is not shown if (eHeight == 0) { return 0; } } else { lH += LABEL_PAD; } if (choiceType == Choice.POPUP) { if (eHeight > 0) { eHeight = elHeights[selectedIndex == -1 ? 0 : selectedIndex]; } if (lH <= LABEL_HEIGHT) { // single line label return (eHeight > LABEL_HEIGHT - LABEL_PAD ? eHeight : LABEL_HEIGHT); } } return lH + eHeight; } /** * Paint this ChoiceGroup * * @param g the Graphics to paint to * @param w the width to paint * @param h the height to paint */ void callPaint(Graphics g, int w, int h) { int labelHeight = super.paintLabel(g, w); // If its an empty ChoiceGroup, just return if (numOfEls == 0 && choiceType != Choice.POPUP) { return; } int translatedX = 0; int translatedY = 0; if (choiceType == Choice.POPUP) { // paint closed state of the popup if (labelHeight > LABEL_HEIGHT) { // translatedX = 0; translatedY = labelHeight; } else { translatedX = getLabelWidth(); translatedX = (translatedX > 0) ? translatedX + LABEL_PAD : 0; // translatedY = 0; } g.drawImage(POPUP_ARROW_IMG, translatedX, translatedY, Graphics.LEFT | Graphics.TOP); if (numOfEls == 0) { return; } translatedX += (POPUP_AR_WIDTH + LABEL_PAD); if (imageEls != null && imageEls[selectedIndex] != null) { int iX = g.getClipX(); int iY = g.getClipY(); int iW = g.getClipWidth(); int iH = g.getClipHeight(); g.clipRect(translatedX, translatedY, PREFERRED_IMG_W, PREFERRED_IMG_H); g.drawImage(imageEls[selectedIndex], translatedX, translatedY, Graphics.LEFT | Graphics.TOP); g.setClip(iX, iY, iW, iH); } int elWidth = w; if (elWidth != cachedWidth) { calculateElementHeight(elWidth); } elWidth -= POPUP_AR_WIDTH + LABEL_PAD; Font fnt; if (fontEls != null && fontEls[selectedIndex] != null) { fnt = fontEls[selectedIndex]; } else { fnt = Screen.CONTENT_FONT; } g.translate(translatedX, translatedY); int textOffset = 0; if (imageEls != null && imageEls[selectedIndex] != null) { textOffset = PREFERRED_IMG_W + LABEL_PAD; } if (hasFocus) { // draw the hilight after drawing the label g.fillRect(textOffset, 0, g.getClipWidth() - textOffset, elHeights[selectedIndex]); // If there was an offset, we need to fill in the // hilight box under the element's image if (textOffset != 0 && elHeights[selectedIndex] > textOffset) { g.fillRect(0, textOffset, textOffset, elHeights[selectedIndex] - textOffset); } Text.paint(stringEls[selectedIndex], fnt, g, elWidth, elHeights[selectedIndex], textOffset, (Text.INVERT | Text.TRUNCATE), null); } else { Text.paint(stringEls[selectedIndex], fnt, g, elWidth, elHeights[selectedIndex], textOffset, (Text.NORMAL | Text.TRUNCATE), null); } g.translate(-translatedX, -translatedY); } else { translatedY = labelHeight; if (labelHeight > 0) { translatedY += LABEL_PAD; } g.translate(0, translatedY); paintElements(g, w); g.translate(0, -translatedY); } } /** * Handle traversal within this ChoiceGroup * * @param dir the direction of traversal * @param viewportWidth the width of the viewport * @param viewportHeight the height of the viewport * @param visRect the in/out rectangle for the internal traversal location * @return True if traversal occurred within this ChoiceGroup */ boolean callTraverse(int dir, int viewportWidth, int viewportHeight, int[] visRect) { super.callTraverse(dir, viewportWidth, viewportHeight, visRect); // If we have no elements, or if the user pressed left/right, // don't bother with the visRect and just return false if (numOfEls == 0) { return false; } // If we are a closed popup, don't bother with the visRect // and return true on the initial traverse, false on subsequent // traverses if ((choiceType == Choice.POPUP) && !popUpOpen) { if (!traversedIn) { traversedIn = true; return true; } else { return false; } } int lh = getLabelHeight(visRect[WIDTH]); visRect[Y] = (lh > 0) ? lh + LABEL_PAD : 0; for (int i = 0; i < hilightedIndex; i++) { visRect[Y] += elHeights[i]; } visRect[HEIGHT] = elHeights[hilightedIndex]; if (!traversedIn) { traversedIn = true; } else { if (dir == Canvas.UP) { if (hilightedIndex > 0) { hilightedIndex--; visRect[Y] -= elHeights[hilightedIndex]; } else { return popUpOpen; } } else if (dir == Canvas.DOWN) { if (hilightedIndex < (numOfEls - 1)) { visRect[Y] += elHeights[hilightedIndex]; hilightedIndex++; } else { return popUpOpen; } } else { return popUpOpen; } } visRect[HEIGHT] = elHeights[hilightedIndex]; if (choiceType == Choice.IMPLICIT) { selectedIndex = hilightedIndex; // FIX ME: notify listener } repaint(); return true; } /** * Traverse out of this ChoiceGroup */ void callTraverseOut() { super.callTraverseOut(); traversedIn = false; } /** * Handle a key press event * * @param keyCode the key which was pressed */ void callKeyPressed(int keyCode) { if (keyCode != Display.KEYCODE_SELECT || numOfEls == 0) { return; } switch (choiceType) { case Choice.POPUP: if (!popUpOpen) { displayManager.suspendPainting(); int lW = getLabelWidth(); boolean tickerflag = false; boolean titleflag = false; if (owner.getTicker() != null) { tickerflag = true; } if (owner.getTitle() != null) { titleflag = true; } updatePopupElements(stringEls, imageEls, numOfEls, selectedIndex, bounds[X] + owner.viewport[X] - owner.view[X] + lW, bounds[Y] + owner.viewport[Y] - owner.view[Y], owner.viewport[WIDTH], owner.viewport[HEIGHT], maxPopupWidth, tickerflag, titleflag); popUpOpen = !popUpOpen; } else { displayManager.resumePainting(); popUpOpen = !popUpOpen; int selected = getPopupSelection(); if (selected >= 0) { hilightedIndex = selected; setSelectedIndexImpl(hilightedIndex, true); owner.itemStateChanged(this); } invalidate(); } break; case Choice.EXCLUSIVE: if (hilightedIndex == selectedIndex) { return; } setSelectedIndexImpl(hilightedIndex, true); owner.itemStateChanged(this); break; case Choice.MULTIPLE: setSelectedIndexImpl(hilightedIndex, !selEls[hilightedIndex]); owner.itemStateChanged(this); break; default: break; } } /** * Get the type of this ChoiceGroup * * @return The type of this ChoiceGroup, ie IMPLICIT, EXPLICIT, etc. */ int getType() { return choiceType; } /** * Set the selected state of the given element index * * @param elementNum the index of the element to select * @param selected true if the element should be selected */ void setSelectedIndexImpl(int elementNum, boolean selected) { checkIndex(elementNum); switch (choiceType) { case Choice.IMPLICIT: // hilight changes as a result of selection only // if it is an implicit choice if (!selected) { return; } selectedIndex = elementNum; hilightedIndex = elementNum; break; case Choice.POPUP: /* fall through */ case Choice.EXCLUSIVE: // selected item cannot be deselected if (selectedIndex == elementNum || !selected) { return; } selectedIndex = elementNum; break; case Choice.MULTIPLE: selEls[elementNum] = selected; break; } repaint(); } /** * Determine if Form should not traverse to this ChoiceGroup * * @return true if Form should not traverse to this ChoiceGroup */ boolean shouldSkipTraverse() { if ((label == null || label.equals("")) && (numOfEls == 0)) { return true; } return false; }// ***********************************************************// private
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -