choicegrouplfimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 621 行 · 第 1/2 页
JAVA
621 行
/* * * * 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.configurator.Constants;import javax.microedition.lcdui.ChoiceGroup.CGElement;/** * This is the look and 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; if (cg.numOfEls > 0 && cg.choiceType != Choice.MULTIPLE) { selectedIndex = 0; cg.cgElements[selectedIndex].setSelected(true); } } // ******************************************************* // ChoiceGroupLF implementation // ******************************************************** /** * Notifies Look &s; Feel that an element was inserted into the * <code>ChoiceGroup</code> at the 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) { // make sure that there is a default selection if (cg.choiceType != Choice.MULTIPLE) { if (selectedIndex == -1) { selectedIndex = 0; cg.cgElements[selectedIndex].setSelected(true); } else if (elementNum < selectedIndex && nativeId == DisplayableLFImpl.INVALID_NATIVE_ID) { // an element was inserted before selectedIndex and // selectedIndex has to be updated selectedIndex++; } } // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { ImageData imagePartData = null; if (imagePart != null) { imagePartData = imagePart.getImageData(); } insert0(nativeId, elementNum, stringPart, imagePartData, cg.cgElements[elementNum].selected); } lRequestInvalidate(true, true); } /** * Notifies Look &s; Feel 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) { // adjust selected index if (cg.numOfEls == 0) { selectedIndex = -1; } else if (cg.choiceType != ChoiceGroup.MULTIPLE) { if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { if (selectedIndex != -1 && selectedIndex < cg.numOfEls) { cg.cgElements[selectedIndex].setSelected(false); } selectedIndex = getSelectedIndex0(nativeId); } 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); } if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { delete0(nativeId, elementNum, selectedIndex); } lRequestInvalidate(true, true); } /** * Notifies Look &s; Feel that all elements * were deleted in the corresponding ChoiceGroup. */ public void lDeleteAll() { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { deleteAll0(nativeId); } selectedIndex = -1; lRequestInvalidate(true, true); } /** * Notifies Look &s; Fell 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) { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { ImageData imagePartData = null; if (imagePart != null) { imagePartData = imagePart.getImageData(); } // for selected value to be passed correctly to the // newly created element we have to do the sync first // (alternatively we could rely on native to maintain // the selected state correctly) syncSelectedIndex(); syncSelectedFlags(); set0(nativeId, elementNum, stringPart, imagePartData, cg.cgElements[elementNum].selected); } lRequestInvalidate(true, true); } /** * Notifies Look &s; Feel that an element was selected (or * 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) { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { setSelectedIndex0(nativeId, elementNum, selected); } else { if (cg.choiceType == Choice.MULTIPLE) { cg.cgElements[elementNum].setSelected(selected); } else { // selected item cannot be deselected in // EXCLUSIVE, IMPLICIT, POPUP ChoiceGroup if (!selected || (/* choiceType != Choice.IMPLICIT && */ selectedIndex == elementNum)) { return; } cg.cgElements[selectedIndex].setSelected(false); selectedIndex = elementNum; cg.cgElements[selectedIndex].setSelected(true); } } } /** * Notifies Look &s; Feel that selected state was changed on * several elements in the corresponding MULTIPLE ChoiceGroup * (cannot be null). * @param selectedArray an array in which the method collect the * selection status */ public void lSetSelectedFlags(boolean[] selectedArray) { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { setSelectedFlags0(nativeId, selectedArray, selectedArray.length); } } /** * Notifies Look &s; Feel 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) { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { setFitPolicy0(nativeId, fitPolicy); lRequestInvalidate(true, true); } } /** * Notifies Look &s; Feel 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) { // Only update native resource if it exists. if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) { setFont0(nativeId, elementNum, font.getFace(), font.getStyle(), font.getSize()); 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 Theme.curContentFont; } /** * Gets currently selected index * @return currently selected index */ public int lGetSelectedIndex() { if (nativeId == DisplayableLFImpl.INVALID_NATIVE_ID) { return selectedIndex; } else { // sync with native syncSelectedIndex(); 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; if (nativeId == DisplayableLFImpl.INVALID_NATIVE_ID) { for (int i = 0; i < cg.numOfEls; i++) { selectedArray_return[i] = cg.cgElements[i].selected; if (selectedArray_return[i]) { countSelected++; } } } else { countSelected = getSelectedFlags0(nativeId, selectedArray_return, cg.numOfEls); // sync with native for (int i = 0; i < cg.numOfEls; i++) { cg.cgElements[i].setSelected(selectedArray_return[i]); } } return countSelected; } /** * Determines if an element with a passed in index
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?