⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.java

📁 j2me开发框架
💻 JAVA
字号:
package com.podome.ui;


import javax.microedition.lcdui.Graphics;

import com.podome.log.Logger;
import com.podome.style.IStyle;
import com.podome.style.Style;
import com.podome.tool.Config;
import com.podome.tool.EffectHelper;
import com.podome.tool.Key;
/**
 * 该类控制Command类的level 0 级的起始位置,该类画菜单状态条
 * level 0 级只画文字
 * @author leadj
 *
 */
public class Menu implements Paint,EventDispatch,IStyle {
	
	private static boolean isRightUsage = true; //是否右手习惯
	
	private static int height = Config.MENU_HEIGHT;
	
	private boolean isActive = false;
	
	Style style;
	
	Command leftNode;
	Command rightNode;
	
	String labelOK = Config.MENU_LabelConfirm;
	String labelCancel = Config.MENU_LabelCancel;
	
	String cmdOK = new String(Config.MENU_LabelConfirm);
	String cmdCancel = new String(Config.MENU_LabelCancel);
	
	private EventDispatch cmdDispatcher;    // 菜单命令监听器
	
	
	boolean isResetLeftRight = false;
	
	boolean isPopCommand = false;
	
	public  static void setHeight(int h){
		height = h;
	}
	
	public static int getHeight(){
		return height;
	}
	
    /**
     * 事件传递
    */
    public void keyPressed(int keyCode) {
    	if(keyCode == Key.SOFT_LEFT){
    		leftSoftImpl();
    	}
    	else if(keyCode == Key.SOFT_RIGHT ){
    		rightSoftImpl();
    	}
    	else if(leftNode != null && leftNode.isOpened()){
    		leftNode.keyPressed(keyCode);
		}
    	else if(rightNode != null && rightNode.isOpened()){
    		rightNode.keyPressed(keyCode);
    	}
    	else if(cmdDispatcher != null){
    		cmdDispatcher.keyPressed(keyCode);
    	}
	}
    /**
     * 处理左软键被按事件
     */
    void leftSoftImpl(){
    	if(leftNode == null || leftNode.commandstr.equals("")){
    		return;
    	}
    	if(leftNode.commandstr == Config.MENU_LabelConfirm ){ 		
    	    leftNode.keyPressed(Key.KEY_OK);
    	}
    	else if(leftNode.commandstr == Config.MENU_LabelCancel ){
    		switchRightLabel();
    		rightNode.close();
    		this.setActive(false);
    	}
    	else if( leftNode.hasChild() ){   
    		this.setNomalMenu();
    	}
    	else{ //只有单独的一个菜单项
    		leftNode.keyPressed(Key.KEY_OK);
    	}
    }
    
    /**
     * 处理右软键被按事件
     */
    void rightSoftImpl(){
    	if(rightNode == null || rightNode.commandstr.equals("")){
    		return;
    	}
    	if(rightNode.commandstr == Config.MENU_LabelConfirm  ){    		
    		rightNode.keyPressed(Key.KEY_OK);    		
    	}
    	else if(rightNode.commandstr == Config.MENU_LabelCancel ){    		
    		switchLeftLabel();
    		leftNode.close();
    		this.setActive(false);
    	}
    	else if( rightNode.hasChild() ){	
    		this.setNomalMenu();
    	}
    	else{ //只有单独的一个菜单项
    		rightNode.keyPressed(Key.KEY_OK);
    	}    	
    }

	public void paint(Graphics g) {
		//画状态条的背景
		Style s  = this.getStyle();		
		//设置左右菜单的起始坐标
		this.setInitPos();
		
		if(this.isActive && !isPopCommand ){
			Stage.currentPage().disableScreen(g, Style.disableColor);
		}
		
		this.paintBackground(g,s);
		
		this.paintLeftRightLabel(g,s);
		
		if( leftNode != null){
			leftNode.paint(g);
		}
		if( rightNode != null ){
			rightNode.paint(g);
		}
	}	
	
	void setInitPos(){
		if(isResetLeftRight) return;		
		isResetLeftRight = true;
		
		if(leftNode != null){		
			leftNode.setPosition(0,  Stage.getCH() - height, 0, 0);
		}
		
		if(rightNode != null){
			rightNode.setPosition(Stage.getCW() - rightNode.getWidth(), Stage.getCH() - height, 0, 0);
		}
	}
	
	void paintBackground(Graphics g,Style s){
		g.setColor(s.bColorNormal);
		int screenW = Stage.getCW();
		int screenH = Stage.getCH();
		int x = 0;
		int y = screenH -height;
		g.setClip(x, y,screenW, height);
		g.fillRect(x, y, screenW, height);		
		if(s.bImageMid != null){
			g.drawImage(s.bImageMid, screenW/2, y + s.bImageMid.getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);
		}
		if(s.bImageLeftTop != null ){
			g.drawImage(s.bImageLeftTop,0, y, Graphics.LEFT | Graphics.TOP);
		}
		if(s.bImageRightTop != null){
			g.drawImage(s.bImageRightTop,screenW, y, Graphics.RIGHT | Graphics.TOP);
		}
		if(s.bImageLeftBottom != null ){
			g.drawImage(s.bImageLeftBottom,0, screenH, Graphics.LEFT | Graphics.BOTTOM);
		}
		if(s.bImageRightBottom != null){
			g.drawImage(s.bImageRightBottom,screenW, screenH, Graphics.RIGHT | Graphics.BOTTOM);
		}	
	}
	
	void paintLeftRightLabel(Graphics g,Style s){
		g.setColor(s.fColorNormal);
		g.setFont(s.dfont);
		if(leftNode != null && 
		   leftNode.commandstr != null &&
		   !leftNode.commandstr.equals("")){
			g.drawString(leftNode.commandstr, leftNode.getX()+s.paddingLeft, leftNode.getY()+ s.paddingTop, Graphics.TOP|Graphics.LEFT);
		}
		if(rightNode != null && 
		   rightNode.commandstr != null &&
		   !rightNode.commandstr.equals("")){
		   g.drawString(rightNode.commandstr, rightNode.getX()+s.paddingLeft, rightNode.getY()+ s.paddingTop, Graphics.TOP|Graphics.LEFT);
		}
	}
	
	/**
	 * 设置左边的菜单
	 * @param left
	 */
	public void setLeft(Command left){
		this.leftNode = left;
		if(this.leftNode == null) return;
		this.isResetLeftRight = false;
		this.leftNode.setLevel(0);
		this.leftNode.setLeft(true);
		this.leftNode.setCommandDispatcher(this);
		if(leftNode.isOpened() && leftNode.hasChild())
		    this.leftNode.close();
	}
	
	/**
	 * 设置右边的菜单
	 * @param right
	 */
	public void setRight(Command right){
		this.rightNode = right;
		if(this.rightNode == null ) return;
		this.isResetLeftRight = false;
		this.rightNode.setLevel(0);
		this.rightNode.setLeft(false);
		this.rightNode.setCommandDispatcher(this);
		if(rightNode.isOpened() && rightNode.hasChild())
		    this.rightNode.close();
	}
	
	public boolean isActive(){
		return this.isActive;
	}
	
	public void setActive(boolean active){
		if( this.isActive != active ){
		    this.isActive = active;
		    
		    if(this.isActive ){		    	
		    	//停止全部其他动画线程
		    	EffectHelper.pause();
		    	Stage.getInstance().notifyRepaint(this);
		    }
		    else{
		    	//开始其他动画线程
		    	EffectHelper.play();		    	
		    }
		}
	}
	
	public String getTypeName() {
		return "Menu";
	}
	
	/**
	 * 左边展开/关闭,交换提示标签
	 */
	void switchLeftLabel(){
		String s;
		s = this.leftNode.commandstr;
		this.leftNode.commandstr = this.labelOK;
		this.labelOK = s;
		
		//switch right
		s = this.rightNode.commandstr;
		this.rightNode.commandstr = this.labelCancel;
		this.labelCancel = s;
	}
	
	/**
	 * 右边展开/关闭,交换提示标签
	 */
	void switchRightLabel(){
		String s;
		s = this.leftNode.commandstr;
		this.leftNode.commandstr = this.labelCancel;
		this.labelCancel = s;
		
		//switch right
		s = this.rightNode.commandstr;
		this.rightNode.commandstr = this.labelOK;
		this.labelOK = s;
	}
	

	/**
	 * 设置弹出菜单
	 */
	void setPopMenu(UIBase base){
		//计算0级菜单的子项的偏移值
		if( base == null) return;
		Command item  = null;		
		if(leftNode != null && leftNode.hasChild()){			
			switchLeftLabel();		
			item = leftNode;
		}
		else if(rightNode != null && rightNode.hasChild() ){			
			switchRightLabel();	
			item  = rightNode;			
		}
		if( item == null ){			
			return;
		}
		this.setPopFlag(true);
		item.setPopFlag(true);
		item.setChildPosition();	
		int screenW =  Stage.getCW();
		int screenH =  Stage.getCH();
		int x = ( screenW - item.childWidth )/2;
		int y = base.getY();	
		int w = item.childWidth;
		int h = item.childHeight;
		if(y + h > screenH - getHeight()){
			y = screenH - getHeight() - h;
		}
		item.setViewer(x, y, w, h);
		item.open();		
		setActive(true);
	}
	
	/**
	 * 设置为普通菜单
	 */
	void setNomalMenu(){
		Command item  = null;
		this.setPopFlag(false);
		if(leftNode != null && leftNode.hasChild()){
			switchLeftLabel();
			leftNode.setPopFlag(false);
			leftNode.setChildPosition();
			int x = 0;
			int y = leftNode.getY() - leftNode.childHeight;
			int w = leftNode.childWidth;
			int h = leftNode.childHeight;
			if( y < 40 ){
				y = 40;
				h = leftNode.getY() - y;
			}			
			leftNode.setViewer(x, y, w, h);
			item = leftNode;
		}		
		if( rightNode != null && rightNode.hasChild() ){
			switchRightLabel();	
			rightNode.setPopFlag(false);
			rightNode.setChildPosition();
			int x = Stage.getCW() - rightNode.childWidth;
			
			int y = rightNode.getY() - rightNode.childHeight;
			int w = rightNode.childWidth;
			int h = rightNode.childHeight;
			if( y < 40 ){
				y = 40;
				h = rightNode.getY() - y;
			}	
			if(((Command)rightNode.child.firstElement()).isExpend ){
				x = 0;
			}			
			rightNode.setViewer(x, y, w, h);
			item = rightNode;
		}
		
		if(item != null)	item.open();

		this.setActive(true);
	}
	
   	
	/**
	 * 设置菜单命令监听器
	 */
	public void setCommandDispatcher(EventDispatch dispatcher){
		this.cmdDispatcher = dispatcher;
	}

	public void notifyCommand(Command cmd) {
		//判断是左边菜单打开还是右边菜单打开,要交换提示标签并设置菜单不活动
		if(leftNode.isOpened()){			
			switchLeftLabel();
			leftNode.close();
		}
		else if(rightNode.isOpened()){	
			switchRightLabel();
			rightNode.close();
		}
		setActive(false);
		
		//执行用户操作
		Logger.info("Menu Execute " + cmd.toString() );
		if( this.cmdDispatcher != null){
			cmdDispatcher.notifyCommand(cmd);	
		}
	}

	public Style getStyle(){
		if( this.style != null ){
			return this.style;
		}
		else{
			return Style.getDefaultStyle();
		}
	}
	
	public void setStyle(Style style) {
		this.style = style;		
	}
	
	/**
	 * 设置是否左手习惯的菜单
	 * @param isLeft
	 */
	public static void setRightUsage(boolean isRight){
		if( isRightUsage != isRight ){
			isRightUsage = isRight;
		}
	}
	/**
	 * 获取是否左手使用的习惯菜单
	 * @return
	 */
	public static boolean isRightUsage(){
		return isRightUsage;
	}
	
	public void setPopFlag(boolean pop){
		if(this.isPopCommand != pop)
	        this.isPopCommand = pop;
	}

	public boolean responseKeyCode(int keyCode) {
		// TODO Auto-generated method stub
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -