📄 choicegroup.java
字号:
if (choiceType == Choice.POPUP) {
popupList.setFont(itemNum, font);
}
}
public void setSelectedFlags(boolean[] selectedArray)
{
if (selectedArray == null) {
throw new NullPointerException();
}
if (selectedArray.length < numOfItems) {
throw new NullPointerException();
}
if (numOfItems == 0)
return;
if (choiceType == Choice.MULTIPLE) {
for (int i = 0; i < numOfItems; i++) {
setSelectedIndex(i, selectedArray[i]);
}
} else {
int selectedItem = -1;
for (int i = 0; i < numOfItems; i++) {
if (selectedArray[i]) {
setSelectedIndex(i, true);
selectedItem = i;
break;
}
}
if (selectedItem == -1) {
setSelectedIndex(0, true);
}
if (choiceType == Choice.POPUP) {
popupList.setSelectedFlags(selectedArray);
}
}
}
public void setSelectedIndex(int elementNum, boolean selected)
{
if (elementNum < 0 || elementNum >= numOfItems) {
throw new IndexOutOfBoundsException();
}
highlightedItemIndex = elementNum;
if ((choiceType == Choice.EXCLUSIVE ||
choiceType == Choice.POPUP) && selected) {
for (int i = 0; i < numOfItems; i++) {
items[i].setSelectedState(elementNum == i);
}
if (choiceType == Choice.POPUP) {
popupList.setSelectedIndex(elementNum, true);
}
repaint();
} else if (choiceType == Choice.MULTIPLE) {
items[elementNum].setSelectedState(selected);
repaint();
} else if (choiceType == Choice.IMPLICIT) {
if (selected) {
items[elementNum].setSelectedState(selected);
repaint();
}
}
}
public int size()
{
return numOfItems;
}
boolean isFocusable()
{
return true;
}
int getHeight()
{
int height = 0;
if (choiceType == Choice.POPUP) {
if (highlightedItemIndex != -1) {
height += items[highlightedItemIndex].getHeight();
}
} else {
for (int i = 0; i < numOfItems; i++) {
height += items[i].getHeight();
}
}
return super.getHeight() + height;
}
/*
* Get item index from coordinates
*/
int getItemIndexAt(int x, int y)
{
x -= super.getHeight();
int testY = 0;
for (int i = 0; i < numOfItems; i++) {
testY += items[i].getHeight();
if (y < testY) {
return i;
}
}
return -1;
}
int getHeightToItem(int itemIndex)
{
int height = 0;
for (int i = 0; i < itemIndex; i++) {
height += items[i].getHeight();
}
return height;
}
int getItemHeight(int itemIndex)
{
return items[itemIndex].getHeight();
}
int paint(Graphics g)
{
super.paintContent(g);
g.translate(0, super.getHeight());
int translatedY = 0;
if (choiceType == Choice.POPUP) {
int index = getSelectedIndex();
if (index != -1) {
items[index].invertPaint(hasFocus());
items[index].paint(g);
}
g.translate(0, -super.getHeight());
} else {
for (int i = 0; i < numOfItems; i++) {
items[i].invertPaint(i == highlightedItemIndex && hasFocus());
items[i].paint(g);
g.translate(0, items[i].getHeight());
translatedY += items[i].getHeight();
}
g.translate(0, -translatedY);
g.translate(0, -super.getHeight());
}
return getHeight();
}
boolean select()
{
if (numOfItems == 0) {
return false;
}
if (choiceType == Choice.POPUP) {
getOwner().currentDisplay.setCurrent(popupList);
} else {
// XXX What does the following statement do?
// It is correct, in the case of multiple inverts the selected
// state, in exclusive selects the highligthed
// and in implicit it does nothing
// Andres Navarro
setSelectedIndex(highlightedItemIndex, !items[highlightedItemIndex].isSelected());
}
return true;
}
int traverse(int gameKeyCode, int top, int bottom, boolean action)
{
if (this.choiceType == Choice.POPUP) {
// POPUP has a totally different behaviour
if (gameKeyCode == Canvas.UP) {
if (top > 0) {
return -top;
} else {
return Item.OUTOFITEM;
}
} else if (gameKeyCode == Canvas.DOWN) {
if (!action) {
int height = super.getHeight();
int index = getSelectedIndex();
if (index != -1) {
height += items[index].getHeight();
}
if (height > bottom) {
return height - bottom;
} else {
repaint();
}
} else {
return Item.OUTOFITEM;
}
}
} else {
if (gameKeyCode == Canvas.UP) {
if (highlightedItemIndex > 0) {
if (action) {
highlightedItemIndex--;
}
int height = super.getHeight();
for (int i = 0; i < highlightedItemIndex; i++) {
height += items[i].getHeight();
}
if (height < top) {
return height - top;
} else {
repaint();
}
} else {
if (top > 0) {
return -top;
} else {
return Item.OUTOFITEM;
}
}
}
if (gameKeyCode == Canvas.DOWN) {
if ((!action && highlightedItemIndex < numOfItems)
|| (action && highlightedItemIndex < (numOfItems - 1))) {
if (action) {
highlightedItemIndex++;
}
int height = super.getHeight();
for (int i = 0; i <= highlightedItemIndex; i++) {
height += items[i].getHeight();
}
if (height > bottom) {
return height - bottom;
} else {
repaint();
}
} else {
return Item.OUTOFITEM;
}
}
}
return 0;
}
void repaint() {
// the popup list should be repainted
// in the case it is being shown
if (choiceType == Choice.POPUP)
popupList.repaint();
super.repaint();
}
class ChoiceItem extends ImageStringItem
{
private boolean selected;
private Font font;
Image box;
ChoiceItem(String label, Image image, String text)
{
super(label, image, text);
setSelectedState(false);
font = Font.getDefaultFont();
}
Font getFont() {
return font;
}
public void setImage(Image img)
{
this.img = img;
int width = 0;
if (box != null) width+=box.getWidth();
if (this.img != null) width+=img.getWidth();
if (width > 0) width+=2;
stringComponent.setWidthDecreaser(width);
}
int getHeight()
{
int height = 0;
if (box != null) height = box.getHeight();
if (img != null && img.getHeight() > height) {
height = img.getHeight();
}
if (stringComponent.getHeight() > height) {
height = stringComponent.getHeight();
}
return height;
}
int paint(Graphics g)
{
if (stringComponent == null) {
return 0;
}
int widthAddition = 0;
if (box != null) {
g.drawImage(box, 0, 0, Graphics.LEFT | Graphics.TOP);
if (img != null) {
widthAddition = box.getWidth();
g.translate(box.getWidth(), 0);
}
else {
widthAddition = box.getWidth() + 2;
g.translate(box.getWidth() + 2, 0);
}
}
if (img != null) {
widthAddition += img.getWidth() + 2;
g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
g.translate(img.getWidth() + 2, 0);
}
int y = stringComponent.paint(g);
if (widthAddition != 0) {
g.translate(-widthAddition, 0);
}
return y;
}
boolean isSelected()
{
return selected;
}
void setFont(Font f) {
if (f == null)
throw new NullPointerException();
// only allow fonts of the same height
// for now (to simplify the layout)
if (f.getHeight() == font.getHeight())
font = f;
}
void setSelectedState(boolean state)
{
selected = state;
if (choiceType != Choice.IMPLICIT && choiceType != Choice.POPUP) {
box = (Choice.EXCLUSIVE == choiceType ?
(state? imgRadioOn:imgRadioOff) : (state? imgMultiOn:imgMultiOff));
}
}
}
class ImplicitListener implements CommandListener {
public void commandAction(Command c, Displayable d) {
List list = (List) d;
setSelectedIndex(list.getSelectedIndex(), true);
try {
getOwner().currentDisplay.setCurrent(getOwner());
repaint();
} catch (NullPointerException n) {
// this happens if the item becomes an orphan
// (ie not owned by a Form, shouldn't happen
// if correct programming practices are used!!)
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -