choicegrouppopuplfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,030 行 · 第 1/3 页
JAVA
1,030 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;/* import javax.microedition.lcdui.KeyConverter; */import com.sun.midp.lcdui.EventConstants;import com.sun.midp.lcdui.Text;import com.sun.midp.configurator.Constants;import com.sun.midp.chameleon.skins.ChoiceGroupSkin;import com.sun.midp.chameleon.skins.resources.ChoiceGroupResources;import com.sun.midp.chameleon.skins.ScrollIndSkin;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.layers.ScrollablePopupLayer;import com.sun.midp.chameleon.layers.ScrollIndLayer;import com.sun.midp.chameleon.CGraphicsUtil;import com.sun.midp.chameleon.skins.resources.ScrollIndResourcesConstants;/** * This is the Look &s; Feel implementation for ChoiceGroupPopup. */class ChoiceGroupPopupLFImpl extends ChoiceGroupLFImpl { /** * Creates ChoiceGroupPopupLF for the passed in choiceGroup of * Choice.POPUP type. * @param choiceGroup the ChoiceGroup object associated with this view */ ChoiceGroupPopupLFImpl(ChoiceGroup choiceGroup) { super(choiceGroup); ChoiceGroupResources.load(); viewable = new int[4]; popupLayer = new CGPopupLayer(this); } /** * Sets the content size in the passed in array. * Content is calculated based on the availableWidth. * size[WIDTH] and size[HEIGHT] should be set by this method. * @param size The array that holds Item content size and location * in Item internal bounds coordinate system. * @param width The width available for this Item */ public void lGetContentSize(int size[], int width) { // no label and empty popup => nothing is drawn // no elements => only label is drawn if (cg.numOfEls == 0) { size[WIDTH] = size[HEIGHT] = 0; return; } int w = getAvailableContentWidth(Choice.POPUP, width); int maxContentWidth = getMaxElementWidth(w); viewable[HEIGHT] = calculateHeight(w); int s = (selectedIndex < 0) ? 0 : selectedIndex; size[HEIGHT] = cg.cgElements[s].getFont().getHeight() + (2 * ChoiceGroupSkin.PAD_V); if (maxContentWidth < w) { size[WIDTH] = width - w + maxContentWidth; } else { size[WIDTH] = width; } viewable[WIDTH] = size[WIDTH]; } // ***************************************************** // Package private methods // ***************************************************** /** * Paints the content area of the ChoiceGroup POPUP. * Graphics is translated to contents origin. * @param g The graphics where Item content should be painted * @param width The width available for the Item's content * @param height The height available for the Item's content */ void lPaintContent(Graphics g, int width, int height) { // paint closed state of the popup int textOffset = 0; // if there are no elements, we are done if (cg.numOfEls == 0) { return; } // draw background if (ChoiceGroupSkin.IMAGE_BG != null) { CGraphicsUtil.draw9pcsBackground(g, 0, 0, width, height, ChoiceGroupSkin.IMAGE_BG); } else { // draw widget instead of using images CGraphicsUtil.drawDropShadowBox(g, 0, 0, width, height, ChoiceGroupSkin.COLOR_BORDER, ChoiceGroupSkin.COLOR_BORDER_SHD, ChoiceGroupSkin.COLOR_BG); } // draw icon if (ChoiceGroupSkin.IMAGE_BUTTON_ICON != null) { int w = ChoiceGroupSkin.IMAGE_BUTTON_ICON.getWidth(); int yOffset = height - ChoiceGroupSkin.IMAGE_BUTTON_ICON.getHeight(); if (yOffset > 0) { yOffset = yOffset / 2; } else { yOffset = 0; } width -= (w + 1); if (ScreenSkin.RL_DIRECTION) { textOffset = 0; } else { textOffset = width; } if (ChoiceGroupSkin.IMAGE_BUTTON_BG != null) { CGraphicsUtil.draw9pcsBackground( g, textOffset, 1, w, height - 2, ChoiceGroupSkin.IMAGE_BUTTON_BG); } g.drawImage(ChoiceGroupSkin.IMAGE_BUTTON_ICON, textOffset, yOffset + 1, Graphics.LEFT | Graphics.TOP); width -= ChoiceGroupSkin.PAD_H; } g.translate(ChoiceGroupSkin.PAD_H, ChoiceGroupSkin.PAD_V); int s = selectedIndex < 0 ? 0 : selectedIndex; // paint value if (cg.cgElements[s].imageEl != null) { int iX = g.getClipX(); int iY = g.getClipY(); int iW = g.getClipWidth(); int iH = g.getClipHeight(); if (ScreenSkin.RL_DIRECTION) { textOffset = width; } else { textOffset = 0; } g.clipRect(textOffset, 0, ChoiceGroupSkin.WIDTH_IMAGE, ChoiceGroupSkin.HEIGHT_IMAGE); g.drawImage(cg.cgElements[s].imageEl, textOffset, 0, Graphics.LEFT | Graphics.TOP); g.setClip(iX, iY, iW, iH); if (ScreenSkin.RL_DIRECTION) { textOffset = 0; } else { textOffset = ChoiceGroupSkin.WIDTH_IMAGE + ChoiceGroupSkin.PAD_H; } } else { textOffset = 0; } g.translate(textOffset, 0); Text.drawTruncString(g, cg.cgElements[s].stringEl, cg.cgElements[s].getFont(), (hasFocus) ? ScreenSkin.COLOR_FG_HL : ChoiceGroupSkin.COLOR_FG, width); g.translate(-textOffset, 0); g.translate(-ChoiceGroupSkin.PAD_H, -ChoiceGroupSkin.PAD_V); if (popupLayer.isSizeChanged() && cachedWidth != INVALID_SIZE) { popupLayer.refresh(); popupLayer.setSizeChanged(false); } } /** * Called by the system to indicate traversal has left this Item * This function simply calls lCallTraverseOut() after obtaining LCDUILock. */ void uCallTraverseOut() { super.uCallTraverseOut(); synchronized (Display.LCDUILock) { if (popupLayer.isPopupOpen()) { hilightedIndex = 0; popupLayer.hide(); } } } /** * 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 lCallTraverse(int dir, int viewportWidth, int viewportHeight, int[] visRect) { boolean ret = false; // If we have no elements, or if the user pressed left/right, // don't bother with the visRect and just return false if (cg.numOfEls > 0) { // If we are a closed popup, don't bother with the visRect // and return true on the initial traverse, false on subsequent // traverses if (popupLayer.isPopupOpen()) { ret = super.lCallTraverse(dir, viewportWidth, viewportHeight, visRect); } else { visRect[X] = 0; visRect[Y] = 0; visRect[HEIGHT] = bounds[HEIGHT]; visRect[WIDTH] = bounds[WIDTH]; } } return ret; } /** * Handle traversal in the open popup * @param dir - the direction of traversal (Canvas.UP, Canvas.DOWN) * @param viewportWidth - the width of the viewport * @param viewportHeight - the height of the viewport * @return true if traverse event was handled, false - otherwise */ boolean traverseInPopup(int dir, int viewportWidth, int viewportHeight) { boolean ret = false; if (popupLayer.isPopupOpen()) { if (cg.numOfEls > 1) { int prevIndex = hilightedIndex; int hilightY = 0; switch (dir) { case Canvas.UP: if (hilightedIndex > 0) { hilightedIndex--; } else { hilightedIndex = cg.numOfEls - 1; } break; case Canvas.DOWN: if (hilightedIndex < (cg.numOfEls - 1)) { hilightedIndex++; } else { hilightedIndex = 0; } break; default: break; } if (ret = prevIndex != hilightedIndex) { for (int i = 0; i < hilightedIndex; i++) { hilightY += elHeights[i]; } int y2= hilightY + elHeights[hilightedIndex]; if (hilightY < viewable[Y]) { viewable[Y] = hilightY; } else if (y2 > viewable[Y] + viewportHeight) { viewable[Y] = y2 - viewportHeight; } lRequestPaint(); } } } // popus is opened return ret; } /** * Handle a key press event * * @param keyCode the key which was pressed */ void uCallKeyPressed(int keyCode) { Form form = null; synchronized (Display.LCDUILock) { if (cg.numOfEls == 0) { return; } if (!popupLayer.isPopupOpen()) { if (keyCode != Constants.KEYCODE_SELECT) { return; } // show popup ScreenLFImpl sLF = (ScreenLFImpl)cg.owner.getLF(); int top = getInnerBounds(Y) - sLF.viewable[Y] + contentBounds[Y]; int bottom = sLF.viewport[HEIGHT] - contentBounds[HEIGHT] - top; int x = getInnerBounds(X) - sLF.viewable[X] + contentBounds[X] + getCurrentDisplay().getWindow().getBodyAnchorX(); int y = top + getCurrentDisplay().getWindow().getBodyAnchorY(); hilightedIndex = selectedIndex > 0 ? selectedIndex : 0; popupLayer.show(x, y, contentBounds[WIDTH], contentBounds[HEIGHT], viewable[WIDTH], viewable[HEIGHT], top,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?