📄 selector.java
字号:
package com.ismyway.anyview;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
/**
* <p>Title: AnyView</p>
*
* <p>Description: E680(I) Reader</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: www.ismyway.com</p>
*
* @author ZhangJian
* @version 1.0
*/
public class Selector extends AbstractWindow {
public final static int ANCHOR = Graphics.LEFT | Graphics.TOP;
final static int ButtonHeight = 20; //滚动条需要空间的高
protected String title; //标题
protected byte[][] byteItems; //选项的字节数组形式
//protected Image[] itemImage; //图标,暂留
protected Image windowImage; //保存当前图体的Image对象
protected int itemIndex = 0;
private int topIndex = 0; //当前总页数
boolean showScrollBar = false; //是否需要滚动条
private int earseScrollBarHeight; //可重绘部分的高(即滚动条占的高)
private int cubeWidth = 0; //选择框的宽
private boolean hasItems = true; //当前的选项集是否为空
protected int itemPages = 0;
protected Image cube;
private boolean pageup = false, pagedown = false; //记录用户点击触摸屏时的动作
final static int HorPad = 10;
final static int LinePad = 5;
protected int ShowItems = 10;
protected int VecPad = 25;
public Selector(String title, String[] items) {
this(title, items, 0);
}
public Selector(String title, String[] items, int defaultIndex) {
WINDOW_STATE = OPENING;
this.title = title;
if (null == items || items.length < 1) {
this.byteItems = new byte[1][];
byteItems[0] = AnyView.cf.str2bytes("(EMPTY!)");
hasItems = false;
} else {
this.byteItems = new byte[items.length][];
for (int i = 0; i < items.length; i++) {
byteItems[i] = AnyView.cf.str2bytes(items[i]);
}
}
itemIndex = defaultIndex;
topIndex = 0;
while (topIndex + ShowItems <= itemIndex) {
topIndex += ShowItems;
}
if (defaultIndex < 0 || defaultIndex > byteItems.length - 1) {
itemIndex = 0;
topIndex = 0;
}
defineWindow();
refreshList();
}
public Selector(String title, byte[][] items, int defaultIndex) {
WINDOW_STATE = OPENING;
this.title = title;
if (null == items || items.length < 1) {
this.byteItems = new byte[1][];
byteItems[0] = AnyView.cf.str2bytes("(EMPTY!)");
hasItems = false;
} else {
this.byteItems = items;
}
itemIndex = defaultIndex;
topIndex = defaultIndex;
if (defaultIndex < 0 || defaultIndex > items.length - 1) {
itemIndex = 0;
topIndex = 0;
}
defineWindow();
refreshList();
}
void defineWindow() {
//确定标题栏的高
VecPad = AnyView.cf.FontHeight + 10;
//确定当前窗口显示的条目
if (AnyView.readType == 1) { //横屏
if ((AnyView.cf.FontHeight + LinePad) * ShowItems + VecPad +
LinePad > AnyView.ScreenWidth) { //需要翻页
ShowItems = (AnyView.ScreenWidth - LinePad - VecPad -
ButtonHeight) / (AnyView.cf.FontHeight + LinePad);
}
} else {
if ((AnyView.cf.FontHeight + LinePad) * ShowItems + VecPad +
LinePad > AnyView.ScreenHeight) { //需要翻页
ShowItems = (AnyView.ScreenHeight - LinePad - VecPad -
ButtonHeight) / (AnyView.cf.FontHeight + LinePad);
}
}
//确定当前窗口的宽
int max = AnyView.cf.str2bytes(title).length;
for (int i = 0; i < byteItems.length; i++) {
int c = byteItems[i].length;
if (c >= max) {
max = c;
}
}
this.width = max * AnyView.cf.AsciiWidth + (HorPad << 1);
//确定窗口的高
if (byteItems.length <= ShowItems) { //当前可以显示完成
this.height = byteItems.length * (AnyView.cf.FontHeight + LinePad) +
LinePad + VecPad;
ShowItems = byteItems.length;
showScrollBar = false;
} else {
this.height = ShowItems * (AnyView.cf.FontHeight + LinePad) +
LinePad + VecPad + ButtonHeight; //6是翻页按钮的高
showScrollBar = true;
}
if (AnyView.readType == 1) { //横屏
this.LEFT = (AnyView.ScreenWidth - this.height) >> 1;
this.TOP = (AnyView.ScreenHeight + this.width) >> 1;
} else {
this.LEFT = (AnyView.ScreenWidth - this.width) >> 1;
this.TOP = (AnyView.ScreenHeight - this.height) >> 1;
}
this.LEFT = this.LEFT < 0 ? 0 : this.LEFT;
this.TOP = this.TOP < 0 ? 0 : this.TOP;
//是否需要滚动条
cubeWidth = width - 4;
if (showScrollBar) {
earseScrollBarHeight = height - VecPad - ButtonHeight; //确定需要擦除滚动条背景的高
cubeWidth -= 4;
} else {
earseScrollBarHeight = height - VecPad - 2;
}
//创建选择时的背景条
int[] c = new int[cubeWidth * (AnyView.cf.FontHeight + 2)];
for (int i = 0; i < c.length; i++) {
c[i] = 0x7FFFFFFF;
}
cube = Image.createRGBImage(c, cubeWidth,
CustomFont.FontHeight + 2, true);
c = null;
}
void refreshList() {
windowImage = Image.createImage(width, height);
Graphics g = windowImage.getGraphics();
g.setColor(AnyView.background);
g.fillRect(0, 0, width, height);
g.setColor(AnyView.bordorcolor);
g.drawRect(0, 0, width - 1, height - 1);
g.drawLine(0, VecPad, width, VecPad);
//绘制标题
byte[] t = AnyView.cf.str2bytes(title);
AnyView.cf.setColor(AnyView.titlecolor);
Image img = AnyView.cf.buildImage(t);
g.drawImage(img, 5, 5, ANCHOR);
img = null;
//绘制选项
int top = VecPad + 5;
AnyView.cf.setColor(AnyView.fontcolor);
for (int i = 0; i < ShowItems; i++) {
img = AnyView.cf.buildImage(byteItems[i + topIndex]);
g.drawImage(img, HorPad, top, ANCHOR);
top += AnyView.cf.FontHeight + LinePad;
}
//绘制滚动条背景
g.setColor(AnyView.background);
g.fillRect(width - HorPad, VecPad + 1, HorPad, earseScrollBarHeight);
g.setColor(AnyView.bordorcolor);
g.drawRect(0, 0, width - 1, height - 1);
//绘制滚动条
if (showScrollBar) {
g.setColor(AnyView.bordorcolor);
g.drawLine(0, height - ButtonHeight, width, height - ButtonHeight);
g.drawLine(width >> 1, height - ButtonHeight, width >> 1, height);
g.drawLine(width - 5, VecPad + 1, width - 5, height - ButtonHeight);
int x = ((width >> 1) - 11) >> 1;
int y = height - ButtonHeight + 7;
g.drawImage(AnyView.UpArrow, x, y, ANCHOR);
x += width >> 1;
g.drawImage(AnyView.DownArrow, x, y, ANCHOR);
int h = height - VecPad - ButtonHeight - 4;
x = topIndex * h / byteItems.length;
y = (topIndex + ShowItems) * h / byteItems.length;
g.drawLine(width - 3, VecPad + 2 + x, width - 3, VecPad + 2 + y);
}
}
/*public Selector(String title, String[] items, int width,
int height) {
this.title = title;
this.items = items;
this.width = width;
this.height = height;
int[] c = new int[(width - 4) * (AnyView.cf.FontHeight + 2)];
for (int i = 0; i < c.length; i++) {
c[i] = 0x7FFFFFFF;
}
cube = Image.createRGBImage(c, (width - 4), CustomFont.FontHeight + 2, true);
prepare();
}*/
/*protected void setItems(String[] items) {
this.items = items;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -