ChoiceGrouppublic class ChoiceGroup extends Item implements ChoiceA ChoiceGroup is a group of selectable elements intended to be
placed within a
{@link Form}. The group may be created with a mode that requires a
single choice to be made or that allows multiple choices. The
implementation is responsible for providing the graphical representation of
these modes and must provide visually different graphics for different
modes. For example, it might use "radio buttons" for the
single choice
mode and "check boxes" for the multiple choice mode.
Note: most of the essential methods have been
specified in the {@link Choice Choice} interface. |
Fields Summary |
---|
ChoiceGroupLF | choiceGroupLFThe look&feel associated with this ChoiceGroup.
Set in the constructor. | int | choiceTypeThe type of this ChoiceGroup | int | fitPolicyThe string fit policy for this ChoiceGroup
'0' by default, which is Choice.TEXT_WRAP_DEFAULT | int | numOfElsThe number of elements in this ChoiceGroup | CGElement[] | cgElementsThe array containing the Font of each element (null if no setFont()
method was ever called). If fontEls is non-null, only the elements
which were set by setFont() are non-null. | static final int | GROW_FACTOROptimization for CGElement array size management.
Notice that cgElements.length is not equal to numOfEls.
Use numOfEls only when accessing the array. |
Constructors Summary |
---|
public ChoiceGroup(String label, int choiceType)Creates a new, empty ChoiceGroup , specifying its
title and its type.
The type must be one of EXCLUSIVE ,
MULTIPLE , or POPUP . The
IMPLICIT
choice type is not allowed within a ChoiceGroup .
this(label, choiceType, new String[] {}, null);
| public ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements)Creates a new ChoiceGroup , specifying its title,
the type of the
ChoiceGroup , and an array of Strings
and Images to be used as its
initial contents.
The type must be one of EXCLUSIVE ,
MULTIPLE , or POPUP . The
IMPLICIT
type is not allowed for ChoiceGroup .
The stringElements array must be non-null and
every array element
must also be non-null. The length of the
stringElements array
determines the number of elements in the ChoiceGroup . The
imageElements array
may be null to indicate that the
ChoiceGroup elements have no images.
If the
imageElements array is non-null, it must be the
same length as the
stringElements array. Individual elements of the
imageElements array
may be null in order to indicate the absence of an
image for the
corresponding ChoiceGroup element. Non-null elements
of the
imageElements array may refer to mutable or
immutable images.
this(label, choiceType, stringElements, imageElements, false);
| ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements, boolean implicitAllowed)Special constructor used by List
super(label);
if (!((choiceType == Choice.MULTIPLE) ||
(choiceType == Choice.EXCLUSIVE) ||
((choiceType == Choice.IMPLICIT) && implicitAllowed) ||
(choiceType == Choice.POPUP))) {
throw new IllegalArgumentException();
}
// If stringElements is null NullPointerException will be thrown
// as expected
for (int x = 0; x < stringElements.length; x++) {
if (stringElements[x] == null) {
throw new NullPointerException();
}
}
if (imageElements != null) {
if (stringElements.length != imageElements.length) {
throw new IllegalArgumentException();
}
}
synchronized (Display.LCDUILock) {
this.choiceType = choiceType;
numOfEls = stringElements.length;
cgElements = new CGElement[numOfEls + GROW_FACTOR];
if (imageElements != null) {
for (int i = 0; i < numOfEls; i++) {
cgElements[i] = new CGElement(stringElements[i],
imageElements[i]);
}
} else {
for (int i = 0; i < numOfEls; i++) {
cgElements[i] = new CGElement(stringElements[i],
null /* image */);
}
}
itemLF = choiceGroupLF = LFFactory.getFactory().getChoiceGroupLF(this);
// initialize fonts to default one in all elements;
// this has to be done after ChoiceGroupLF is created
for (int i = 0; i < numOfEls; i++) {
cgElements[i].setFont(null);
}
} // synchronized
|
Methods Summary |
---|
boolean | acceptFocus()Return whether the Item takes user input focus.
return super.acceptFocus() || numOfEls > 0;
| public int | append(java.lang.String stringPart, Image imagePart)Appends an element to the ChoiceGroup .
int elementNum = -1;
synchronized (Display.LCDUILock) {
checkNull(stringPart);
if ((elementNum = insertImpl(numOfEls, stringPart, imagePart))
>= 0) {
choiceGroupLF.lInsert(elementNum, stringPart, imagePart);
}
}
return elementNum;
| private void | checkFlag(boolean[] flag)Check the validity of the selection array
if (flag == null) {
throw new NullPointerException();
}
if (flag.length < numOfEls) {
throw new IllegalArgumentException();
}
| private void | checkIndex(int elementNum)Check the validity of a given element index
if (elementNum < 0 || elementNum >= numOfEls) {
throw new IndexOutOfBoundsException();
}
| private void | checkNull(java.lang.String stringPart)Check the given values for null.
if (stringPart == null) {
throw new NullPointerException();
}
| public void | delete(int elementNum)Deletes the element referenced by elementNum .
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
--numOfEls;
// setup new elements array
if (elementNum != numOfEls) {
System.arraycopy(cgElements, elementNum + 1, cgElements,
elementNum, numOfEls - elementNum);
}
// free some memory... (efficient for very large arrays)
if (cgElements.length > (GROW_FACTOR * 10) &&
cgElements.length / numOfEls >= 2) {
CGElement[] newArray = new CGElement[numOfEls + GROW_FACTOR];
System.arraycopy(cgElements, 0, newArray, 0, numOfEls);
cgElements = newArray;
newArray = null;
}
cgElements[numOfEls] = null;
// notify l&f
choiceGroupLF.lDelete(elementNum);
} // synchronized
| public void | deleteAll()Deletes all elements from this ChoiceGroup .
synchronized (Display.LCDUILock) {
cgElements = new CGElement[GROW_FACTOR]; // initial size
numOfEls = 0;
choiceGroupLF.lDeleteAll();
}
| public int | getFitPolicy()Gets the application's preferred policy for fitting
Choice element
contents to the available screen space. The value returned is the
policy that had been set by the application, even if that value had
been disregarded by the implementation.
// SYNC NOTE: return of atomic value, no locking necessary
return fitPolicy;
| public Font | getFont(int elementNum)Gets the application's preferred font for
rendering the specified element of this Choice . The
value returned is the font that had been set by the application,
even if that value had been disregarded by the implementation.
If no font had been set by the application, or if the application
explicitly set the font to null , the value is the default
font chosen by the implementation.
The elementNum parameter must be within the range
[0..size()-1] , inclusive.
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
return cgElements[elementNum].getFont();
}
| public Image | getImage(int elementNum)Gets the Image part of the element referenced by
elementNum .
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
// return as mutable, if possible
return (cgElements[elementNum].mutableImageEl == null ?
cgElements[elementNum].imageEl :
cgElements[elementNum].mutableImageEl);
}
| Image[] | getImageElements()Helper method, used solely by the native method
call: updatePopupElements()
Image[] ret = new Image[numOfEls];
for (int i = 0; i < numOfEls; i++) {
ret[i] = cgElements[i].imageEl;
}
return ret;
| public int | getSelectedFlags(boolean[] selectedArray_return)Queries the state of a ChoiceGroup and returns the state of
all elements in the
boolean array
selectedArray_return . Note: this
is a result parameter.
It must be at least as long as the size
of the ChoiceGroup as returned by size() .
If the array is longer, the extra
elements are set to false .
For ChoiceGroup objects of type
MULTIPLE , any
number of elements may be selected and set to true in the result
array. For ChoiceGroup objects of type
EXCLUSIVE and POPUP
exactly one element will be selected, unless there are
zero elements in the ChoiceGroup .
checkFlag(selectedArray_return);
synchronized (Display.LCDUILock) {
int numSelected = 0;
if (numOfEls > 0) {
numSelected =
choiceGroupLF.lGetSelectedFlags(selectedArray_return);
}
for (int i = numOfEls; i < selectedArray_return.length; i++) {
selectedArray_return[i] = false;
}
return numSelected;
}
| public int | getSelectedIndex()Returns the index number of an element in the
ChoiceGroup that is
selected. For ChoiceGroup objects of type
EXCLUSIVE and POPUP
there is at most one element selected, so
this method is useful for determining the user's choice.
Returns -1 if
there are no elements in the ChoiceGroup .
For ChoiceGroup objects of type
MULTIPLE , this always
returns -1 because no
single value can in general represent the state of such a
ChoiceGroup .
To get the complete state of a MULTIPLE
Choice , see {@link
#getSelectedFlags getSelectedFlags}.
synchronized (Display.LCDUILock) {
return choiceGroupLF.lGetSelectedIndex();
}
| public java.lang.String | getString(int elementNum)Gets the String part of the element referenced by
elementNum .
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
// return stringEls[elementNum];
return cgElements[elementNum].stringEl;
}
| java.lang.String[] | getStringElements()Helper method, used solely by the native method
call: updatePopupElements()
String[] ret = new String[numOfEls];
for (int i = 0; i < numOfEls; i++) {
ret[i] = cgElements[i].stringEl;
}
return ret;
| public void | insert(int elementNum, java.lang.String stringPart, Image imagePart)Inserts an element into the ChoiceGroup just prior to
the element specified.
synchronized (Display.LCDUILock) {
if (elementNum < 0 || elementNum > numOfEls) {
throw new IndexOutOfBoundsException();
}
checkNull(stringPart);
if (insertImpl(elementNum, stringPart, imagePart) >= 0) {
choiceGroupLF.lInsert(elementNum, stringPart, imagePart);
}
}
| private int | insertImpl(int elementNum, java.lang.String stringPart, Image imagePart)Insert a particular element of this ChoiceGroup
// cgElements is created in the constructor and cannot be null
// full capacity reached
if (numOfEls == cgElements.length) {
CGElement[] newCGEls =
new CGElement[numOfEls + GROW_FACTOR];
System.arraycopy(cgElements, 0, newCGEls, 0, elementNum);
System.arraycopy(cgElements, elementNum,
newCGEls, elementNum + 1, numOfEls - elementNum);
cgElements = newCGEls; // swap them
} else if (elementNum != numOfEls) {
// if we're not appending
System.arraycopy(cgElements, elementNum,
cgElements, elementNum + 1,
numOfEls - elementNum);
}
numOfEls++;
cgElements[elementNum] = new CGElement(stringPart, imagePart);
return elementNum;
| public boolean | isSelected(int elementNum)Gets a boolean value indicating whether this element is selected.
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
return choiceGroupLF.lIsSelected(elementNum);
}
| public void | set(int elementNum, java.lang.String stringPart, Image imagePart)Sets the String and Image parts of the
element referenced by elementNum ,
replacing the previous contents of the element.
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
checkNull(stringPart);
cgElements[elementNum].set(stringPart, imagePart);
choiceGroupLF.lSet(elementNum, stringPart, imagePart);
}
| public void | setFitPolicy(int fitPolicy)Sets the application's preferred policy for fitting
Choice element
contents to the available screen space. The set policy applies for all
elements of the Choice object. Valid values are
{@link #TEXT_WRAP_DEFAULT}, {@link #TEXT_WRAP_ON},
and {@link #TEXT_WRAP_OFF}. Fit policy is a hint, and the
implementation may disregard the application's preferred policy.
if (fitPolicy < TEXT_WRAP_DEFAULT || fitPolicy > TEXT_WRAP_OFF) {
throw new IllegalArgumentException();
}
synchronized (Display.LCDUILock) {
if (this.fitPolicy != fitPolicy) {
this.fitPolicy = fitPolicy;
choiceGroupLF.lSetFitPolicy(fitPolicy);
}
}
| public void | setFont(int elementNum, Font font)Sets the application's preferred font for
rendering the specified element of this Choice .
An element's font is a hint, and the implementation may disregard
the application's preferred font.
The elementNum parameter must be within the range
[0..size()-1] , inclusive.
The font parameter must be a valid Font
object or null . If the font parameter is
null , the implementation must use its default font
to render the element.
synchronized (Display.LCDUILock) {
checkIndex(elementNum);
cgElements[elementNum].setFont(font);
choiceGroupLF.lSetFont(elementNum,
cgElements[elementNum].getFont());
}
| public void | setSelectedFlags(boolean[] selectedArray)Attempts to set the selected state of every element in the
ChoiceGroup . The array
must be at least as long as the size of the
ChoiceGroup . If the array is
longer, the additional values are ignored.
For ChoiceGroup objects of type
MULTIPLE , this sets the selected
state of every
element in the Choice . An arbitrary number of
elements may be selected.
For ChoiceGroup objects of type
EXCLUSIVE and POPUP , exactly one array
element must have the value true . If no element is
true ,
the first element
in the Choice will be selected. If two or more
elements are true , the
implementation will choose the first true element
and select it.
synchronized (Display.LCDUILock) {
checkFlag(selectedArray);
if (numOfEls == 0) {
return;
}
if (choiceType == Choice.MULTIPLE) {
for (int i = 0; i < numOfEls; i++) {
cgElements[i].setSelected(selectedArray[i]);
}
choiceGroupLF.lSetSelectedFlags(selectedArray);
} else {
for (int i = 0; i < numOfEls; i++) {
if (selectedArray[i]) {
choiceGroupLF.lSetSelectedIndex(i, true);
return;
}
}
choiceGroupLF.lSetSelectedIndex(0, true);
}
} // synchronized
| public void | setSelectedIndex(int elementNum, boolean selected)For ChoiceGroup objects of type
MULTIPLE , this simply sets an
individual element's selected state.
For ChoiceGroup objects of type
EXCLUSIVE and POPUP , this can be used only to
select an element. That is, the selected parameter must
be true . When an element is selected, the previously
selected element is deselected. If selected is
false , this call is ignored.
For both list types, the elementNum parameter
must be within
the range
[0..size()-1] , inclusive.
checkIndex(elementNum);
synchronized (Display.LCDUILock) {
choiceGroupLF.lSetSelectedIndex(elementNum, selected);
} // synchronized
| public int | size()Returns the number of elements in the ChoiceGroup .
// SYNC NOTE: return of atomic value, no locking necessary
return numOfEls;
|
|