📄 choicegroup.java
字号:
// *********************************************************** /** * 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) { stringEls[elementNum] = stringPart; // Create Image storage, if needed if ((imagePart != null) && (imageEls == null)) { imageEls = new Image[stringEls.length]; mutableImageEls = new Image[stringEls.length]; } if (imageEls != null) { if ((imagePart != null) && imagePart.isMutable()) { // Adding a mutable Image; save it and create a // snapshot for display purposes mutableImageEls[elementNum] = imagePart; imageEls[elementNum] = Image.createImage(imagePart); } else { // We get here if imagePart is 'null' or if imagePart // is not mutable. In either case, the result is the // same; set the displayable image to imagePart // (whether an Image or 'null', it is the same) and // set the mutableImage to 'null' mutableImageEls[elementNum] = null; imageEls[elementNum] = imagePart; } } invalidate(); } /** * 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 == stringEls.length) { String[] newStrings = new String[stringEls.length + 4]; System.arraycopy(stringEls, 0, newStrings, 0, elementNum); System.arraycopy(stringEls, elementNum, newStrings, elementNum + 1, numOfEls - elementNum); stringEls = newStrings; if (imageEls != null) { Image[] newImages = new Image[imageEls.length + 4]; Image[] newMutableImages = new Image[imageEls.length + 4]; System.arraycopy(imageEls, 0, newImages, 0, elementNum); System.arraycopy(imageEls, elementNum, newImages, elementNum + 1, numOfEls - elementNum); System.arraycopy(mutableImageEls, 0, newMutableImages, 0, elementNum); System.arraycopy(mutableImageEls, elementNum, newMutableImages, elementNum + 1, numOfEls - elementNum); imageEls = newImages; mutableImageEls = newMutableImages; } if (fontEls != null) { Font[] newFonts = new Font[fontEls.length + 4]; System.arraycopy(fontEls, 0, newFonts, 0, elementNum); System.arraycopy(fontEls, elementNum, newFonts, elementNum + 1, numOfEls - elementNum); } } else { System.arraycopy(stringEls, elementNum, stringEls, elementNum + 1, numOfEls - elementNum); if (imageEls != null) { System.arraycopy(imageEls, elementNum, imageEls, elementNum + 1, numOfEls - elementNum); System.arraycopy(mutableImageEls, elementNum, mutableImageEls, elementNum + 1, numOfEls - elementNum); } if (fontEls != null) { System.arraycopy(fontEls, elementNum, fontEls, elementNum + 1, 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; } stringEls[elementNum] = null; if (imageEls != null) { imageEls[elementNum] = null; mutableImageEls[elementNum] = null; } if (fontEls != null) { fontEls[elementNum] = null; } numOfEls++; if (choiceType != Choice.MULTIPLE && (elementNum < selectedIndex || selectedIndex == -1)) { selectedIndex++; hilightedIndex = selectedIndex; } else if (elementNum < hilightedIndex || hilightedIndex == -1) { hilightedIndex++; } setImpl(elementNum, stringPart, imagePart); return elementNum; } /** * 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 */ private void checkNull(String stringPart, Image imagePart) { if (stringPart == null) { throw new NullPointerException(); } } /** * 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 */ private void checkFlag(boolean[] flag) { if (flag == null) { throw new NullPointerException(); } if (flag.length < numOfEls) { throw new IllegalArgumentException(); } } /** * Paint this ChoiceGroup * * @param g the Graphics to paint to * @param w the width to paint */ private void paintElements(Graphics g, int w) { int elWidth = w; int textOffset; Image img; Font fnt; int translatedX = 0; int translatedY = 0; if (choiceType == Choice.EXCLUSIVE) { translatedX = RD_WIDTH + LABEL_PAD; elWidth -= translatedX; } else if (choiceType == Choice.MULTIPLE) { translatedX = CKBX_WIDTH + LABEL_PAD; elWidth -= translatedX; } if ((elWidth != cachedWidth) || (elHeights.length != numOfEls)) { calculateElementHeight(elWidth); } // start for for (int i = 0; i < numOfEls; i++) { if (choiceType == Choice.EXCLUSIVE) { img = (i == selectedIndex) ? RD_ON_IMG : RD_OFF_IMG; } else if (choiceType == Choice.MULTIPLE) { img = (selEls[i]) ? CKBX_ON_IMG : CKBX_OFF_IMG; } else { img = null; } if (img != null) { g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP); // elWidth = w - translatedX; g.translate(translatedX, 0); } textOffset = 0; if (imageEls != null && imageEls[i] != null) { int iX = g.getClipX(); int iY = g.getClipY(); int iW = g.getClipWidth(); int iH = g.getClipHeight(); g.clipRect(0, 0, PREFERRED_IMG_W, PREFERRED_IMG_H); g.drawImage(imageEls[i], 0, 0, Graphics.LEFT | Graphics.TOP); g.setClip(iX, iY, iW, iH); textOffset = PREFERRED_IMG_W + LABEL_PAD; } if (fontEls != null && fontEls[i] != null) { fnt = fontEls[i]; } else { fnt = Screen.CONTENT_FONT; } if (i == hilightedIndex && hasFocus) { g.fillRect(textOffset, 0, g.getClipWidth() - translatedX - textOffset, elHeights[i]); // If there was an offset, we need to fill in the // hilight box under the element's image if (textOffset != 0 && elHeights[i] > textOffset) { g.fillRect(0, textOffset, textOffset, elHeights[i] - textOffset); } Text.paint(stringEls[i], fnt, g, elWidth, elHeights[i], textOffset, ((fitPolicy == Choice.TEXT_WRAP_OFF) ? (Text.INVERT | Text.TRUNCATE) : Text.INVERT), null); } else { Text.paint(stringEls[i], fnt, g, elWidth, elHeights[i], textOffset, ((fitPolicy == Choice.TEXT_WRAP_OFF) ? (Text.NORMAL | Text.TRUNCATE) : Text.NORMAL), null); } if (img != null) { g.translate(-translatedX, 0); } g.translate(0, elHeights[i]); translatedY += elHeights[i]; } // end for g.translate(0, -translatedY); } /** * Get the total element height of this CGroup * * @param width the desired width for this CG * @return the total element height */ private int calculateElementHeight(int width) { // we cache the width we calculated the heights for cachedWidth = width; int eHeight = 0; if (elHeights == null || elHeights.length < numOfEls) { elHeights = new int[numOfEls]; } int textOffset = 0; Font fnt; for (int x = 0; x < numOfEls; x++) { if (imageEls == null || imageEls[x] == null) { textOffset = 0; } else { textOffset = PREFERRED_IMG_W + LABEL_PAD; } if (fontEls == null || fontEls[x] == null) { fnt = Screen.CONTENT_FONT; } else { fnt = fontEls[x]; } if (fitPolicy == TEXT_WRAP_OFF || choiceType == Choice.POPUP) { elHeights[x] = fnt.getHeight(); } else { elHeights[x] = Text.getHeightForWidth(stringEls[x], fnt, width, textOffset); } eHeight += elHeights[x]; } return eHeight; } /** * Updates the native data structures used to draw * a Choice.POPUP type choice group menu. * * @param strings array of string elements for the choice group * @param images array of image elements for the choice group * (may be null if no choice elements contain images) * @param numElements number of elements in the choice group * @param selectedElem initial element to draw in selected (hilighted)state * @param xPos x coordinate of where the open choice group should attempt * to position itself. (the upper right corner of the closed choice * group arrow) * @param yPos y coordinate of where the open choice group should attempt to * position itself. * @param vWidth width of the viewport * @param vHeight height of the viewport * @param maxWidth width of widest choice group element * @param tickerFlag true if owner has a ticker showing * @param titleFlag true if owner has a title showing */ private native void updatePopupElements(String[] strings, Image[] images, int numElements, int selectedElem, int xPos, int yPos, int vWidth, int vHeight, int maxWidth, boolean tickerFlag, boolean titleFlag); /** * When an open popup choice group closes, this method asks the native * data structure for the newly selected choice element. * * @return index of selected choice element, or <code>-1</code> in * if the popup choice menu was canceled out of */ private native int getPopupSelection(); /** * The default height for the popup window for a popup choice, * 130 pixels by default */ private static final int PU_WIN_HEIGHT = 130; /** * Image to represent an unselected checkbox */ private static final Image CKBX_OFF_IMG; /** * Image to represent a selected checkbox */ private static final Image CKBX_ON_IMG; /** * Width of a checkbox image (both selected and unselected) */ private static final int CKBX_WIDTH = 10; /** * Height of a checkbox image (both selected and unselected) */ private static final int CKBX_HEIGHT = 11; /** * Image to represent an unselected radio button */ private static final Image RD_OFF_IMG; /** * Image to represent a selected radio button */ private static final Image RD_ON_IMG; /** * Width of a radio button image (both selected and unselected) */ private static final int RD_WIDTH = 11; /** * Height of a radio button image (both selected and unselected) */ private static final int RD_HEIGHT = 11; /** * Image to represent a popup arrow */ private static final Image POPUP_ARROW_IMG; /** * Width of the popup arrow image */ private static final int POPUP_AR_WIDTH = 11; /** * Height of popup arrow image */ private static final int POPUP_AR_HEIGHT = 11; /** * The preferred image width for an image as part of an element of * a choice (12 pixels). */ static final int PREFERRED_IMG_W = 12; /** * The preferred image height for an image as part of an element of * a choice (12 pixels). */ static final int PREFERRED_IMG_H = 12; static { /* * Initialize the icons necessary for the various modes. */ CKBX_OFF_IMG = ImmutableImage.createIcon("checkbox_off.png"); CKBX_ON_IMG = ImmutableImage.createIcon("checkbox_on.png"); RD_OFF_IMG = ImmutableImage.createIcon("radio_off.png"); RD_ON_IMG = ImmutableImage.createIcon("radio_on.png"); POPUP_ARROW_IMG = ImmutableImage.createIcon("popup_arrow.png"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -