📄 uicontainer.java
字号:
package com.podome.ui;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.podome.log.Logger;
import com.podome.style.IStyle;
import com.podome.style.Style;
import com.podome.tool.Key;
public class UIContainer implements Paint,EventDispatch,IStyle {
int xpos = 0; //容器的X轴坐标
int ypos = 0; //容器的Y轴坐标
int width = 0; //容器的宽度
int height = 0; //容器的高度
int xOffset = 0; //容器的内部控件的X方向偏移值
int yOffset = 0; //容器的内部控件的Y方向偏移值
int vWidth = 0; //容器的视窗宽度(一般等于屏幕宽度)
int vHeight = 0; //容器的视窗高度(一般等于屏幕高度减去菜单状态条高度)
boolean isItemLayout = false;
Vector items;
UIBase currentItem;
UIBase firstItem;
UIBase lastItem;
private Style style;
Menu menu; // 菜单
Command defaultCmd; // 默认的菜单命令
ItemCommandListener itemCommandListener; // 选项监听器
CommandListener commandListener; // 菜单命令监听器
Image offsetImage;
boolean offsetImageRepaint = true;
public UIContainer(){
items = new Vector();
}
public Style getStyle(){
if( this.style != null)
return this.style;
else
return Style.getDefaultStyle();
}
public void setStyle(Style s){
this.style = s;
}
public void setX(int x){
this.xpos = x;
}
public int getX(){
return this.xpos;
}
public void setY(int y){
this.ypos = y;
}
public int getY(){
return this.ypos;
}
/**
* 宽度改变将影响到加入内部的控件的布局
* @param w
*/
public void setWidth(int w){
if(this.width == w) return;
this.width = w;
//重新设置内部控件的布局
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
/**
* 高度改变将影响到加入内部的控件的布局
* @param h
*/
public void setHeight(int h){
this.height = h;
}
public void paint(Graphics g) {
if( !isItemLayout ){//如果没有设置过内部控件的位置,在花之前设置
if(this.firstItem == null) return;
this.resetLayout(this.firstItem);
}
Style s = this.getStyle();
if(this.offsetImage == null){
this.offsetImageRepaint = true;
this.offsetImage = Image.createImage(Stage.getCW(), Stage.getCH());
}
//关闭当前焦点的元素
boolean resetFocus = false;
if( this.currentItem != null ){
if( !this.currentItem.overflow ||
!this.currentItem.editAble ){
this.currentItem.hasFocus = false;
resetFocus = true;
}
}
if(this.offsetImageRepaint){
this.offsetImageRepaint = false;
Graphics offGraphics = offsetImage.getGraphics();
this.paintBackground(offGraphics,s);
this.paintChild(offGraphics, s);
this.paintScroller(offGraphics);
}
g.setClip(0, 0, Stage.getCW(), Stage.getCH());
g.drawImage(offsetImage, 0, 0, Graphics.LEFT|Graphics.TOP);
if( resetFocus ){
this.currentItem.hasFocus = true;
this.currentItem.paint(g);
}
//画菜单
if(this.menu != null){
this.menu.paint(g);
}
}
void paintChild(Graphics g,Style s){
//如果当前元素为空,则从头开始画
UIBase base = this.currentItem;
if(this.currentItem == null){
base = this.firstItem;
}
if( base == null ) return;
boolean startPaint = false;
int spipCont = 0;
//以当前元素为基点,向前画.
int iBase = items.indexOf(base);
for( int i= iBase; i >= 0; i--){
base = (UIBase)items.elementAt(i);
if( base.isInViewer()){
base.setVisble(true);
base.paint(g);
startPaint = true;
}
else if( startPaint ){
break;
}
else if(spipCont<10){
spipCont++;
}
else{
break;
}
}
//以当前元素为基点,向后画。
startPaint = false;
spipCont = 0;
for(int i=iBase+1; i < items.size(); i++){
base = (UIBase)items.elementAt(i);
if( base.isInViewer()){
base.setVisble(true);
base.paint(g);
startPaint = true;
}
else if( startPaint ){
break;
}
else if(spipCont<10){
spipCont++;
}
else{
break;
}
}
}
void paintBackground(Graphics g,Style s){
g.setColor(s.bColorNormal );
int x = getX();
int y = getY();
g.setClip(x, y, this.vWidth, this.vHeight);
g.fillRect(x, y, this.vWidth, this.vHeight);
//画背景图片
if(s.bImageMid != null)
g.drawImage(s.bImageMid, this.vWidth/2, this.vHeight/2, Graphics.VCENTER | Graphics.HCENTER);
if(s.bImageLeftTop != null)
g.drawImage(s.bImageLeftTop, x, y, Graphics.TOP | Graphics.LEFT);
if(s.bImageRightTop != null)
g.drawImage(s.bImageRightTop,x+ this.vWidth, y,Graphics.TOP|Graphics.RIGHT);
if(s.bImageLeftBottom != null)
g.drawImage(s.bImageLeftBottom, x, y+this.vHeight, Graphics.BOTTOM | Graphics.LEFT);
if(s.bImageRightBottom != null)
g.drawImage(s.bImageRightBottom,x+ this.vWidth, y+this.vHeight,Graphics.BOTTOM |Graphics.RIGHT);
}
/**
* 按键事件处理
*/
public void keyPressed(int keyCode) {
if( this.menu != null ){
if( this.menu.isActive() || keyCode == Key.SOFT_LEFT
|| keyCode == Key.SOFT_RIGHT ){
this.menu.keyPressed(keyCode);
return;
}
}
keyEventImpl(keyCode);
}
/**
* 按键控制
* @param keyCode
*/
private void keyEventImpl(int keyCode){
// 先判断当前节点是否输入节点
if(currentItem != null &&
Stage.eventDispatchCls.isInstance(currentItem)){
EventDispatch cur = (EventDispatch)currentItem;
if( (cur.isActive() ||keyCode == Key.KEY_OK ) &&
cur.responseKeyCode(keyCode)){
cur.keyPressed(keyCode);
return;
}
}
if (keyCode == Key.KEY_OK) {
confirm(); // 确认操作
} else if (keyCode == Key.KEY_UP) {
focusToPreY(); // 移到上一个兄弟
} else if (keyCode == Key.KEY_DOWN) {
focusToNextY();
} else if (keyCode == Key.KEY_RIGHT) {
focusToNextX(); // 移到水平方向的下一个
} else if (keyCode == Key.KEY_LEFT) {
focusToPreX(); // 移到水平方向的上一个
}
}
/**
* 确认键的处理
*/
private void confirm(){
if( !this.currentItem.enable ) return;
if( this.defaultCmd != null &&
this.defaultCmd.isLevelUser() &&
this.itemCommandListener != null){
itemCommandListener.commandAction(this.defaultCmd, this.currentItem);
return;
}
else if(this.menu != null &&
this.itemCommandListener != null){
//弹出菜单
menu.setPopMenu(this.currentItem);
}
}
private UIBase searchPreY(){
int cidx = this.items.indexOf(this.currentItem);
UIBase pre = null;
UIBase last = null;
while( --cidx >= 0 ){
pre = (UIBase)items.elementAt( cidx );
if( !pre.enable ||
pre.bonds[0][1] == currentItem.bonds[0][1]) continue;
if(last == null){
last = pre;
}
else{
if(pre.bonds[0][1] != last.bonds[0][1] ) break;
if( Math.abs(this.currentItem.getX()-pre.getX()) <
Math.abs(this.currentItem.getX()-last.getX())){
last = pre;
}
}
}
return last;
}
/**
* 向上方向键的处理
*/
private void focusToPreY(){
if( this.currentItem == null) return;
//如果当前获得焦点的元素太大,每次向下移动4行,直到可以看到全部元素
if(this.currentItem.getY() < 0 ){
this.yOffset += this.currentItem.getLineHeight()*4;
this.offsetImageRepaint = true;
if( this.yOffset > 0 ){
this.yOffset = 0;
}
Stage.getInstance().notifyRepaint(this);
return;
}
int cidx = this.items.indexOf(this.currentItem);
UIBase preItem = this.searchPreY();
if( preItem != null && preItem.isInViewer()){
this.currentItem.setFocus(false);
preItem.setFocus(true);
this.currentItem = preItem;
}
else if(preItem != null ){
this.yOffset += this.vHeight /2;
this.offsetImageRepaint = true;
if( this.yOffset > 0 ){
this.yOffset = 0;
}
if(preItem.isInViewer()){
this.currentItem.hasFocus = false;
preItem.hasFocus = true;
this.currentItem = preItem;
}
else if( !this.currentItem.isInViewer() ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -