📄 choice.java
字号:
package com.podome.ui;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import com.podome.log.Logger;
import com.podome.style.Style;
import com.podome.tool.Key;
/**
* @author leadj
*
*/
public class Choice extends UIBase implements EventDispatch {
private Vector options;
private int currentIndex = 0;
private int selectedIndex = 0;
private boolean isExpand = false; //是否展开
private int yoffset = 0; //子项的偏移值
private int vWidth = 0; //子项的视窗宽度
private int vHeight = 0; //子项的视窗高度
private Menu choiceMenu = null;
private Command selectCmd = null;
private Command cancelCmd = null;
private boolean isRightUsage = true;
public Choice(){
super();
selectCmd = new Command("选择",false);
cancelCmd = new Command("取消",false);
choiceMenu = new Menu();
if(Menu.isRightUsage()){
choiceMenu.setLeft(selectCmd);
choiceMenu.setRight(cancelCmd);
}
else{
isRightUsage = false;
choiceMenu.setLeft(cancelCmd);
choiceMenu.setRight(selectCmd);
}
choiceMenu.setCommandDispatcher(this);
}
public Choice(Vector items){
this();
this.options = items;
}
public void paint(Graphics g) {
g.setClip(0, 0, Stage.getCW(), Stage.getCH());
Style s = this.getStyle();
g.setFont(s.dfont);
this.paintSelf(g, s);
this.paintChilds(g, s);
}
private void paintSelf(Graphics g,Style s){
int x = getX();
int y = getY();
int w = getWidth();
int h = getHeight();
//画背景颜色
g.setColor(this.hasFocus()?s.bColorFocus:s.bColorNormal);
g.fillRoundRect(x, y, w, h, s.rountWidth, s.roundHeight);
//画边框
g.setColor(this.hasFocus()?s.sColorFocus:s.sColorNormal);
g.drawRoundRect(x, y, w, h, s.rountWidth, s.roundHeight);
//画文字
String text = getCurrentItem();
if(text != null){
g.setColor(this.hasFocus()?s.fColorFocus:s.fColorNormal);
g.drawString(text,x+s.paddingLeft, y+s.paddingTop, Graphics.TOP|Graphics.LEFT);
}
//画向下标志
int cx = x + w - 10;
int cy = y + h / 2;
g.setColor(this.hasFocus()?s.bColorNormal:s.bColorFocus);
g.fillTriangle(cx, cy+5, cx+5, cy-3, cx-5, cy-3);
}
/**
* 画子项
* @param g
* @param s
*/
private void paintChilds(Graphics g,Style s){
if( !this.isExpand ) return;
int itemCnt = options.size();
int x = s.paddingLeft + 5;
int y = getY()+this.getHeight();
this.vWidth = Stage.getCW()-s.paddingLeft - s.paddingRight - 10;
this.vHeight = Stage.getCH()-Menu.getHeight()-getY() - getHeight();
int childHeight = this.getHeight()* itemCnt;
int factHeight = Math.min(this.vHeight,childHeight);
g.setClip(x, y+1, this.vWidth, factHeight-1);
//画背景颜色
g.setColor(s.bColorNormal);
g.fillRoundRect(x, y,vWidth, factHeight, s.rountWidth, s.roundHeight);
//画边框
g.setColor(s.sColorNormal);
g.drawRoundRect(x, y,vWidth-1, factHeight-1, s.rountWidth, s.roundHeight);
//画文字
int tX = x + s.paddingLeft;
int tY = y + this.yoffset;
int tScrollWidth = childHeight > vHeight?s.paddingRight + 12:s.paddingRight*2;
int tLineHeight = this.getHeight();
for(int i = 0;i < itemCnt;i++){
if(this.currentIndex == i ){//画当前项背景
g.setColor(s.bColorFocus);
g.fillRoundRect(tX, tY+s.paddingTop, vWidth-tScrollWidth, tLineHeight, s.rountWidth, s.roundHeight);
}
g.setColor(s.fColorNormal);
g.drawString(options.elementAt(i).toString(), tX+s.paddingLeft+10, tY+s.paddingTop, Graphics.TOP|Graphics.LEFT);
tY += tLineHeight;
if(tY + tLineHeight > Stage.getCH()-Menu.getHeight()){
break;
}
}
//画滚动条
if(childHeight > vHeight){
//在下端花三角形
int cX = x + this.vWidth - 8;
int cY = Stage.getCH()-Menu.getHeight()-10;
g.setColor(s.bColorFocus);
if(this.yoffset == 0 ){
g.drawLine(cX-4, cY-1, cX, cY-8);
g.drawLine(cX+4, cY-1, cX, cY-8);
}
else{
g.fillTriangle(cX-5, cY-1, cX, cY-9, cX+5, cY-1);
}
if( this.yoffset <= this.vHeight - childHeight ){
g.drawLine(cX-4, cY+1, cX, cY + 8);
g.drawLine(cX+4, cY+1, cX, cY + 8);
}
else{
g.fillTriangle(cX-5, cY+1, cX, cY+9, cX+5, cY+1);
}
}
}
public void keyPressed(int keyCode) {
if(keyCode == Key.KEY_OK){
if(this.isExpand ){
this.selectedIndex = this.currentIndex;
this.close();
}
else{
this.open();
}
}
else if(keyCode == Key.KEY_DOWN ){
this.keyDownImpl();
}
else if(keyCode == Key.KEY_UP ){
this.keyUpImpl();
}
}
private void keyDownImpl(){
if(this.currentIndex >= options.size() -1) return;
this.currentIndex++;
int cH = ( this.currentIndex + 1 )*getHeight()+this.yoffset;
if( cH > this.vHeight ){
this.yoffset -= getHeight();
}
Stage.getInstance().notifyRepaint(this);
}
private void keyUpImpl(){
if(this.currentIndex <= 0 ) return;
this.currentIndex--;
int cH = ( this.currentIndex ) * getHeight() + this.yoffset;
if( cH < 0 ){
this.yoffset += getHeight();
}
Stage.getInstance().notifyRepaint(this);
}
protected void setAutoArea(int x, int y,int maxWidth){
this.setX(x);
this.setY(y);
Style s = this.getStyle();
Style ps = s;
if(this.owner != null)
ps = owner.getStyle();
int usedWidth = maxWidth + ps.paddingLeft - x;
this.bonds[1][0] = this.bonds[0][0]+ usedWidth;
this.bonds[1][1] = this.bonds[0][1] + s.dfont.getHeight() + s.paddingBottom + s.paddingTop;
}
public void add(String item){
if(item == null) return;
if(this.options == null)
this.options = new Vector();
this.options.addElement(item);
}
public int getCurrentIndex(){
return this.currentIndex;
}
public void setCurrentIndex(int idx){
if(options == null ) return;
if(idx >=0 && idx < options.size())
this.currentIndex = idx;
}
public String getCurrentItem(){
if(options != null)
return options.elementAt(selectedIndex).toString();
else
return null;
}
public void close(){
this.setExpand( false );
}
public void open(){
this.setExpand( true );
}
private void setExpand(boolean f){
this.switchMenuUsage();
if(this.isExpand != f ){
this.isExpand = f;
if(this.owner == null) return;
Menu curPageMenu = owner.menu;
owner.menu = this.choiceMenu;
this.choiceMenu = curPageMenu;
if(this.isExpand){
Stage.getInstance().notifyRepaint(this);
Stage.getInstance().notifyRepaint(owner.menu);
}
else{
Stage.getInstance().notifyRepaintCurrent();
}
}
}
public boolean isActive(){
return this.isExpand;
}
public void setActive(boolean active){
this.setExpand(active);
}
public String getTypeName() {
return "Choice";
}
public int getWidth(){
return this.bonds[1][0]-this.bonds[0][0];
}
public int getHeight(){
return this.bonds[1][1]-this.bonds[0][1];
}
public boolean responseKeyCode(int keyCode) {
if(keyCode == Key.KEY_LEFT ||
keyCode == Key.KEY_RIGHT)
return false;
else
return true;
}
public void notifyCommand(Command cmd) {
if(cmd == this.selectCmd ){
this.keyPressed(Key.KEY_OK);
}
else{
this.setActive(false);
}
}
private void switchMenuUsage(){
if( this.isExpand ) return;
if(this.choiceMenu.style == null){
this.choiceMenu.setStyle(Stage.currentPage().menu.getStyle());
}
if(this.isRightUsage == Menu.isRightUsage()) return;
this.isRightUsage = Menu.isRightUsage();
if(this.isRightUsage){
this.choiceMenu.setLeft(this.selectCmd);
this.choiceMenu.setRight(this.cancelCmd);
}
else{
this.choiceMenu.setLeft(this.cancelCmd);
this.choiceMenu.setRight(this.selectCmd);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -