choicegrouplfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,108 行 · 第 1/3 页
JAVA
1,108 行
/* * * * 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 com.sun.midp.lcdui.Text;import com.sun.midp.configurator.Constants;import javax.microedition.lcdui.ChoiceGroup.CGElement;import com.sun.midp.chameleon.skins.ChoiceGroupSkin;import com.sun.midp.chameleon.skins.resources.ChoiceGroupResources;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.layers.ScrollIndLayer;/** * This is the look &s; feel implementation for ChoiceGroup. */class ChoiceGroupLFImpl extends ItemLFImpl implements ChoiceGroupLF { /** * Creates ChoiceLF for the passed in ChoiceGroup. * @param choiceGroup - the ChoiceGroup object associated with this view */ ChoiceGroupLFImpl(ChoiceGroup choiceGroup) { super(choiceGroup); cg = choiceGroup; ChoiceGroupResources.load(); if (cg.numOfEls > 0) { if (cg.choiceType != Choice.MULTIPLE) { selectedIndex = 0; cg.cgElements[selectedIndex].setSelected(true); } hilightedIndex = -1; } contentX = getContentX(cg.choiceType); elHeights = new int[cg.numOfEls + ChoiceGroup.GROW_FACTOR]; } // ******************************************************* // ChoiceGroupLF implementation // ******************************************************** /** * 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) { // Allow lists to be as big at least as the view port if ((cg.owner != null) && (cg.owner instanceof List)) { size[WIDTH] = cg.owner.getLF().lGetWidth(); int eHeight = calculateHeight( getAvailableContentWidth(cg.choiceType, size[WIDTH])); size[HEIGHT] = (cg.owner.getLF().lGetHeight() > eHeight ? cg.owner.getLF().lGetHeight() : eHeight); } else { if (cg.numOfEls == 0) { size[WIDTH] = size[HEIGHT] = 0; } int availableWidth = getAvailableContentWidth(cg.choiceType, width); int eHeight = calculateHeight(availableWidth); int maxContentWidth = getMaxElementWidth(availableWidth); if (maxContentWidth <= availableWidth) { // note that width - availableWidth equals // choice image area all horizontal padding; // thus width - availableWidth + maxContentWidth is // the width of the widest element in ChoiceGroup with padding size[WIDTH] = width - availableWidth + maxContentWidth; } else { size[WIDTH] = width; } size[HEIGHT] = eHeight; } } /** * Notifies L&F that an element was inserted into the * <code>ChoiceGroup</code> at the elementNum specified. * * @param elementNum the index of the element where insertion occurred * @param stringPart the string part of the element to be inserted * @param imagePart the image part of the element to be inserted, * or <code>null</code> if there is no image part */ public void lInsert(int elementNum, String stringPart, Image imagePart) { // Implicit, popup and exclusive // (in those case there is always a selection) if (cg.choiceType != Choice.MULTIPLE) { if (selectedIndex == -1) { selectedIndex = 0; cg.cgElements[selectedIndex].setSelected(true); } else if (elementNum <= selectedIndex) { selectedIndex++; } } // set hilighted index (it always exists) if (hasFocus && (elementNum <= hilightedIndex || hilightedIndex == -1)) { hilightedIndex++; } // Note that cg.numOfEls is already + 1 // elHeights is created in the constructor and cannot be null if (cg.numOfEls - 1 == elHeights.length) { // full capacity reached int[] newArray = new int[cg.numOfEls + ChoiceGroup.GROW_FACTOR]; System.arraycopy(elHeights, 0, newArray, 0, elementNum); System.arraycopy(elHeights, elementNum, newArray, elementNum + 1, cg.numOfEls - elementNum - 1); elHeights = newArray; // swap them } else if (elementNum != cg.numOfEls - 1) { // if we're not appending System.arraycopy(elHeights, elementNum, elHeights, elementNum + 1, cg.numOfEls - elementNum - 1); } lRequestInvalidate(true, true); } /** * Notifies L&F that an element referenced by <code>elementNum</code> * was deleted in the corresponding ChoiceGroup. * * @param elementNum the index of the deleted element */ public void lDelete(int elementNum) { if (cg.numOfEls == 0) { selectedIndex = -1; hilightedIndex = -1; } else { // adjust hilighted index if (elementNum < hilightedIndex) { hilightedIndex--; } else if (elementNum == hilightedIndex && hilightedIndex == cg.numOfEls) { hilightedIndex = cg.numOfEls - 1; } if (cg.choiceType != ChoiceGroup.MULTIPLE) { if (elementNum < selectedIndex) { selectedIndex--; } else if (elementNum == selectedIndex && selectedIndex == cg.numOfEls) { // last element is selected and deleted - // new last should be selected selectedIndex = cg.numOfEls - 1; } cg.cgElements[selectedIndex].setSelected(true); } } // setup new elements array (note that numOfEls is already -1) if (elementNum != cg.numOfEls) { System.arraycopy(elHeights, elementNum + 1, elHeights, elementNum, cg.numOfEls - elementNum); } // free some memory... (efficient for very large arrays) if (elHeights.length > (ChoiceGroup.GROW_FACTOR * 10) && elHeights.length / cg.numOfEls >= 2) { int[] newArray = new int[cg.numOfEls + ChoiceGroup.GROW_FACTOR]; System.arraycopy(elHeights, 0, newArray, 0, cg.numOfEls); elHeights = newArray; newArray = null; } lRequestInvalidate(true, true); } /** * Notifies L&F that all elements * were deleted in the corresponding ChoiceGroup. */ public void lDeleteAll() { selectedIndex = hilightedIndex = -1; elHeights = new int[ChoiceGroup.GROW_FACTOR]; // initial size lRequestInvalidate(true, true); } /** * Notifies L&F that the <code>String</code> and * <code>Image</code> parts of the * element referenced by <code>elementNum</code> were set in * the corresponding ChoiceGroup, * replacing the previous contents of the element. * * @param elementNum the index of the element set * @param stringPart the string part of the new element * @param imagePart the image part of the element, or <code>null</code> * if there is no image part */ public void lSet(int elementNum, String stringPart, Image imagePart) { lRequestInvalidate(true, true); } /** * Notify this itemLF that its owner screen has changed. * Clear internal state if its new owner is null. * * @param oldOwner old owner screen before this change. New owner * can be found in Item model. */ public void lSetOwner(Screen oldOwner) { super.lSetOwner(oldOwner); if (item.owner != null && item.owner instanceof List) { drawsTraversalIndicator = false; } } /** * Notifies L&F that an element was selected/deselected in the * corresponding ChoiceGroup. * * @param elementNum the number of the element. Indexing of the * elements is zero-based * @param selected the new state of the element <code>true=selected</code>, * <code>false=not</code> selected */ public void lSetSelectedIndex(int elementNum, boolean selected) { setSelectedIndex(elementNum, selected); lRequestPaint(); } /** * Notifies L&F that selected state was changed on several elements * in the corresponding MULTIPLE ChoiceGroup. * @param selectedArray an array in which the method collect the * selection status */ public void lSetSelectedFlags(boolean[] selectedArray) { lRequestPaint(); } /** * Notifies L&F that a new text fit policy was set in the corresponding * ChoiceGroup. * @param fitPolicy preferred content fit policy for choice elements */ public void lSetFitPolicy(int fitPolicy) { lRequestInvalidate(true, true); } /** * Notifies L&F that a new font was set for an element with the * specified elementNum in the corresponding ChoiceGroup. * @param elementNum the index of the element, starting from zero * @param font the preferred font to use to render the element */ public void lSetFont(int elementNum, Font font) { lRequestInvalidate(true, true); } /** * Gets default font to render ChoiceGroup element if it was not * set by the application * @return - the font to render ChoiceGroup element if it was not * set by the app */ public Font getDefaultFont() { return getTextFont(cg.choiceType, false); } /** * Gets currently selected index * @return currently selected index */ public int lGetSelectedIndex() { return selectedIndex; } /** * Gets selected flags (only elements corresponding to the * elements are expected to be filled). ChoiceGroup sets the rest to * false * @param selectedArray_return to contain the results * @return the number of selected elements */ public int lGetSelectedFlags(boolean[] selectedArray_return) { int countSelected = 0; for (int i = 0; i < cg.numOfEls; i++) { selectedArray_return[i] = cg.cgElements[i].selected; if (selectedArray_return[i]) { countSelected++; } } return countSelected; } /** * Determines if an element with a passed in index * is selected or not. * @param elementNum the index of an element in question * @return true if the element is selected, false - otherwise */ public boolean lIsSelected(int elementNum) { return cg.cgElements[elementNum].selected; } // ***************************************************** // Package private methods // ***************************************************** /** * Determine if this Item should have a newline after it * * @return true if it should have a newline after */ boolean equateNLA() { if (super.equateNLA()) { return true; } return ((cg.layout & Item.LAYOUT_2) != Item.LAYOUT_2); } /** * Determine if this Item should have a newline before it * * @return true if it should have a newline before */ boolean equateNLB() { if (super.equateNLB()) { return true; } return ((cg.layout & Item.LAYOUT_2) != Item.LAYOUT_2); } /** * Handle traversal within this ChoiceGroup
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?