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

📄 command.java

📁 j2me开发框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @param w
	 */
	void setWidth(int w){
		if( w > this.width )
		    this.bonds[1][0] = this.bonds[0][0] + w;
	}
	/**
	 * 设置菜单项的高度,如果小于字符实际高度,则无效。
	 * @param h
	 */
	void setHeight(int h){
		if( h > this.height )
			this.bonds[1][1] = this.bonds[1][0] + h;
	}	
	
	/**
	 * 是否是用户定义来处理选项的命令
	 * @return
	 */
	public boolean isLevelUser(){
		return this.isLevelUser;
	}
	
	/**
	 * 获取控件的类型名称
	 */
	public String getTypeName() {
		return "Command";
	}
	/**
	 * 增加一个子菜单
	 * @param cmd
	 */
	public void add(Command cmd){
		if(cmd == null){
			return;
		}		
	    if(this.child == null){
	    	this.child = new Vector();
	    }
	    else{
	    	
	    	Command last = (Command)this.child.lastElement();
	    	last.next = cmd;
	    	cmd.pre = last;
	    	if( this.child.size() > 0 ){
	    		Command first = (Command)this.child.firstElement();
	    		cmd.next = first;
	    		first.pre = cmd;
	    	}
	    }
	    this.child.addElement(cmd);
	    cmd.setLevel(this.level + 1);
	    cmd.setLeft(this.isLeft);
	    cmd.parent = this;
	    
	    cmd.calculateSelfFactWidth();
	    
	    if( this.childWidth < cmd.getWidth() ){
	    	this.childWidth = cmd.getWidth();
	    }
	}
	/**
	 * 设置控件在容器内的相对位置
	 * @param x  相对X坐标
	 * @param y  相对Y坐标
	 * @param w  控件占用X方向宽度
	 * @param h  控件占用Y方向高度
	 */
	void setPosition(int x,int y,int w,int h){
		int factHeight = this.height;
		int factWidth  = this.width;
		if( factHeight < h ) factHeight = h;
		if( factWidth < w ) factWidth = w;
		this.bonds[0][0] = x;
		this.bonds[0][1] = y;
		this.bonds[1][0] = x + factWidth;
		this.bonds[1][1] = y + factHeight;
		Logger.debug("X=" + bonds[0][0] + ",Y=" +bonds[0][1] + ",W=" + factWidth + ",H="+ factHeight);
	}
	
	/**
	 * 计算自身实际宽度和高度
	 */
	void calculateSelfFactWidth(){
		Style s = this.getStyle();
		Logger.debug("--------" + this.getLabel()+"---------");
		Font font = s.dfont;
		this.width = 0;
		if( this.commandstr != null &&
	    	!this.commandstr.equals("")){
		    this.width = font.stringWidth(this.commandstr) +
		                 s.paddingLeft +
		                 s.paddingRight;
	    }
	    this.height = font.getHeight() +
	                  s.paddingTop + 
	                  s.paddingBottom;
	    this.setPosition(0, 0, width, height);
	}
	/**
	 * 重新设置子菜单的实际宽度和高度
	 */
	void resetChildFactWidth(){
		calculateSelfFactWidth();
		if(child != null && child.size() > 0 ){
			for(int i=0;i<child.size();i++)
			((Command)child.elementAt(i)).calculateSelfFactWidth();
		}
	}
	
	/**
	 * 重新计算所有子项的宽度和相对坐标
	 */
	void setChildPosition(){
		if( !this.isChildPositionReset )
			return;
		this.isChildPositionReset = false;
		
		if(this.child == null || 
		   this.child.size() == 0 ){
			return;
		}		
		//判断是否有子菜单,如果有,则增加长度
		for(int i=0; i< child.size(); i++){
			Command c = (Command)child.elementAt(i);
			if( c.hasChild() ){
				if(c.width + Config.MENU_ARROW_WIDTH > childWidth )
				childWidth += Config.MENU_ARROW_WIDTH;
			    break;
			}
		}		
		if( this.level == 0 && 
			this.isExpend &&
			!this.isPopCommand ){			
			childWidth = Stage.getCW();			
		}		
		Command curItem;
		int y = 0;
		
		if( !this.isPopCommand ){
			for(int i=0; i<this.child.size(); i++ ){
			  curItem = (Command)this.child.elementAt(i);				
			  curItem.setPosition(0, y, childWidth,curItem.getHeight()); 
			  y += curItem.getHeight();
			}			
		}
		else{		
		    for(int i=0; i<this.child.size(); i++ ){
			  curItem = (Command)this.child.elementAt(i);			
			  if( curItem.isLevelUser() ){
				curItem.setPosition(0, y, childWidth,curItem.getHeight()); 
			    y += curItem.getHeight();
			  }
		    }
		}
		this.childHeight = y;
	}
	
	/**
	 * 关闭子节点
	 */
	void close(){	
		closeChild();
    	Stage.getInstance().notifyRepaintCurrent();
	}
	
	private void closeChild(){
		if( !hasChild() ) return;
		this.isOpen = false;
		for(int i=0; i< this.child.size(); i++ ){
			((Command)this.child.elementAt(i)).closeChild();
		}
	}
	
	/**
	 * 打开子节点
	 */
	void open(){		
		if( !this.hasChild() ) return;		
		this.isOpen = true;
		if( this.isPopCommand ){
			this.setCurrentChildPop();
		}
		else{
			this.setCurrentChildNormal();
		}
		if(this.level > 0 ){
		    Stage.getInstance().notifyRepaint(this);
		}		
	}
	/**
	 * 设置当前子菜单项(正常菜单)
	 */
	void setCurrentChildNormal(){		
		if(this.child == null ||
		   this.child.size() == 0 )
			return;
		
		if(this.currentChild != null &&
		   this.currentChild.enable	){
			this.currentChild.setFocus(true);
			return;
		}		
		for(int i=0;i<child.size();i++){
		    this.currentChild = (Command)this.child.elementAt(i);
		    if(!this.currentChild.enable)
		    	continue;
			 this.currentChild.setFocus(true);
			 break;
		}		
	}
	/**
	 * 设置当前子菜单项(弹出式菜单)
	 */
	void setCurrentChildPop(){		
		if(this.child == null ||
		   this.child.size() == 0 )
			return;		
		if(this.currentChild != null &&
		   this.currentChild.enable &&
		   this.currentChild.isLevelUser() ){
			this.currentChild.setFocus(true);			
			return;
		}		
		for(int i=0;i<child.size();i++){
			if(this.currentChild != null)
			    currentChild.setFocus(false);			
		    this.currentChild = (Command)this.child.elementAt(i);
		    this.currentChild.debug();
		    if(!this.currentChild.enable ||
		       !this.currentChild.isLevelUser() )
		    	continue;
			 this.currentChild.setFocus(true);			 
			 break;
		}		
	}
	/**
	 * 判断子菜单是否处于打开状态
	 * @return
	 */
	boolean isOpened(){
		return this.isOpen;
	}
	/**
	 * 判断是否有子菜单
	 * @return
	 */
	boolean hasChild(){
		if( this.child == null || this.child.size() ==0 )
			return false;
		return true;
	}
	
	void setFocus(boolean focus ){
		if(this.isFocus != focus){
		    this.isFocus = focus;
		    this.isOffImageRepaint = true;
		    Stage.getInstance().notifyRepaint(this);
		}
	}
	
	public boolean hasFocus(){
		return this.isFocus;
	}
	/**
	 * 把焦点移到上一个可以获取焦点的子菜单
	 */
	public void focusPre(){
	    if(this.currentChild == null) 
	    	return;
	    Command curItem = this.currentChild;
	    
	    while(curItem.pre != null ){
	    	curItem = curItem.pre;
	    	if( !curItem.enable ||
	    		(this.isPopCommand && !curItem.isLevelUser()) ) 
	    		continue;
	    	this.currentChild.setFocus(false);
	    	this.currentChild = curItem;
	    	this.currentChild.setFocus(true);
	    	break;
	    }
	}
	/**
	 * 把焦点移到下一个可以获取焦点的子菜单
	 */
	public void focusNext(){
		 if(this.currentChild == null) 
			 return;
		 Command curItem = this.currentChild;		 
		 while(curItem.next != null ){
		     curItem = curItem.next;
		     if( !curItem.enable ||
			    (this.isPopCommand && !curItem.isLevelUser()))
		    	continue;
		     this.currentChild.setFocus(false);
		     this.currentChild = curItem;
		     this.currentChild.setFocus(true);
		     break;
		 }
	}
	/**
	 * 设置子菜单的视图
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public void setViewer(int x,int y,int w,int h){
	    this.vXpos = x;
	    this.vYpos = y;
	    this.vWidth = w;
	    this.vHeight = h;
	    //Logger.info(this.commandstr + " setViewer X=" + x + ",Y=" + y + ",W=" + w + ",H=" + h);
	}
	/**
	 * 设置子菜单的视图
	 */
	private void setChildViewer(){
		if(this.isLeft ){
			setLeftChildViewer();
		}
		else{
			setRightChildViewer();
		}
	}
	
	private void setLeftChildViewer(){		
		if(this.level == 0 ||
		   this.child == null ||
		   this.child.size() == 0 ) return;
		
		if( !this.isChildViewerReset && !this.isPopCommand ) return;
	        this.isChildViewerReset = false;
	        
		//只设置Level > 0 的子菜单的视图
		int screenW = Stage.getCW();
		int screenH = Stage.getCH() -Config.MENU_HEIGHT;
		
		int x = this.getX()+this.getWidth();
		int y = this.getY();
		int vY = y;
		int w = this.childWidth;
		int h = this.childHeight;
		if(x + w > screenW ){
			x = screenW - w;
		}
		if( vY + h > screenH ){
			vY = screenH - h;
		}
		if( y > 40 && vY < 40 ){
			vY = 40;
			h = screenH - 40;
		}		
		this.setViewer(x, vY, w, h);
	}
	
	private void setRightChildViewer(){		
		if(this.level == 0 ||
		   this.child == null ||
		   this.child.size() == 0 ) return;
		if( !this.isChildViewerReset && !this.isPopCommand) return;
            this.isChildViewerReset = false;
	
		//只设置Level > 0 的子菜单的视图
		int screenH = Stage.getCH() -Config.MENU_HEIGHT;
		
		int x = this.getX() - this.childWidth;
		int y = this.getY();
		int vY = y;
		int w = this.childWidth;
		int h = this.childHeight;
		if( x < 0 ){
			x = 0;
		}
		if( vY + h > screenH ){
			vY = screenH - h;
		}
		if( y > 40 && vY < 40 ){
			vY = 40;
			h = screenH - 40;
		}		
		this.setViewer(x, vY, w, h);
	}	
	
	public String toString(){
		return this.commandstr;
	}
	
	public void debug(){
		Logger.info(this.commandstr + ",isLeft=" + this.isLeft + ":Level=" + this.level + ",isExpand=" + this.isExpend + ",focus=" + this.hasFocus() + ",hasChild="+ this.hasChild() + ",isUserLevel=" + this.isLevelUser + ",isPop="+ this.isPopCommand + ",X=" + this.getX() + ",Y=" + this.getY() + ",W=" + this.getWidth() + ",H=" + this.getHeight());
	}

	public void notifyCommand(Command cmd) {
		
	}
	
	/**
	 * 
	 * @param isLeft
	 */
	void setLeft(boolean isLeft){
		if(this.isLeft != isLeft ){
			this.isChildViewerReset = true;
			this.isOffImageRepaint = true;
		}
		this.isLeft = isLeft;
		if( child ==null || child.size()==0) return;
		for(int i=0;i<child.size(); i++){
			((Command)child.elementAt(i)).setLeft(isLeft);
		}
	}
	
	void setPopFlag(boolean pop){
		if(this.isPopCommand == pop )
			return;
		this.isPopCommand = pop;
		this.isChildPositionReset = true;
		this.isOffImageRepaint = true;
		this.isChildViewerReset = true;
		
		if(child != null && child.size() > 0 ){
			for(int i=0; i< child.size(); i++){
				((Command)child.elementAt(i)).setPopFlag(pop);
			}
		}		
	}

	public Style getStyle(){
		if( this.style != null){
			return this.style;
		}
		else if(this.parent != null){
			return parent.getStyle();
		}
		else{
			return Style.getDefaultStyle();
		}
	}
	
	public void setStyle(Style style) {
		this.style = style;		
	}
	
	public void setEnable(boolean able){
		this.enable = able;
	}

	public void setLabel(String lab){
		this.commandstr = lab;
	}
	
	public String getLabel(){
		return this.commandstr;
	}

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

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

⌨️ 快捷键说明

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