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

📄 uicontainer.java

📁 j2me开发框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				this.currentItem.hasFocus = false;
				if( cidx != items.size()-1 ){
				    this.currentItem = (UIBase)items.elementAt(cidx+1);
				}
			}
			Stage.getInstance().notifyRepaint(this);		
		}    	
	    
	    if(  preItem == null && this.yOffset < 0 ){
	    	this.yOffset += this.vHeight /2;
	    	this.offsetImageRepaint = true;
			if( this.yOffset > 0 ){
				this.yOffset =  0;
			}			
			if( !this.currentItem.isInViewer() ){
				this.currentItem.hasFocus = false;
				if( cidx != items.size()-1 ){
				    this.currentItem = (UIBase)items.elementAt(cidx+1);
				}
			}
			Stage.getInstance().notifyRepaint(this);
	    }
	}	
	
	private UIBase searchNextY(){
		int cidx = this.items.indexOf(this.currentItem);	    
	    UIBase next = null;
	    UIBase last = null;
	    while( ++cidx < items.size() ){
	    	next = (UIBase)items.elementAt( cidx );
	    	if( !next.enable ||
	    		next.bonds[0][1] == currentItem.bonds[0][1]) continue;
	    	if(last == null){
	    	    last = next;
	    	}
	    	else{
	    		if(next.bonds[0][1] != last.bonds[0][1] ) break;
	    		if( Math.abs(this.currentItem.getX()-next.getX()) < 
	    			Math.abs(this.currentItem.getX()-last.getX())){
	    			last = next;
	    		}
	    	}	    	
	    }
	    return last;	    	
	}
	
    /**
     * 向下方向键的处理
     */	
	private void focusToNextY(){
		if( this.currentItem == null){
			this.currentItem = this.firstItem;
			if( this.currentItem.enable ){
				this.currentItem.setFocus(true);
				return;
			}
		}
		int lastPos = this.currentItem.getY() + this.currentItem.bonds[1][1]-this.currentItem.bonds[0][1];
	    if(lastPos > this.getHeight() ){
	    	this.yOffset -= this.currentItem.getLineHeight()* 4;
	    	this.offsetImageRepaint = true;
	    	if( this.yOffset < this.vHeight - this.height ){
				this.yOffset =  this.vHeight - this.height;
			}
	    	Stage.getInstance().notifyRepaint(this);
	    	return;
	    }
	    
		int cidx = this.items.indexOf(this.currentItem);
		
	    UIBase nextItem = this.searchNextY();
	    
		if( nextItem != null && nextItem.isInViewer()){    			
			this.currentItem.setFocus(false);
			nextItem.setFocus(true);
			this.currentItem = nextItem;
		}
		else if(nextItem != null ){
			this.yOffset -= this.vHeight /2;
			this.offsetImageRepaint = true;
			if( this.yOffset < this.vHeight - this.height ){
				this.yOffset =  this.vHeight - this.height;
			}			
			if( nextItem.isInViewer()){
    			this.currentItem.hasFocus = false;
    			nextItem.hasFocus = true;
    			this.currentItem = nextItem;
    		}
			else if( !this.currentItem.isInViewer()){
				this.currentItem.hasFocus = false;
				if( cidx > 0 ){
				    this.currentItem = (UIBase)items.elementAt(cidx-1);	
				}
			}
			Stage.getInstance().notifyRepaint(this);
			
		}    		
	    
	    if( nextItem == null && this.yOffset > this.vHeight - this.height ){
	    	this.yOffset -= this.vHeight /2;
	    	this.offsetImageRepaint = true;
			if( this.yOffset < this.vHeight - this.height ){
				this.yOffset =  this.vHeight - this.height;
			}			
			if( !this.currentItem.isInViewer() ){
				this.currentItem.hasFocus = false;
				if( cidx > 0 ){
				    this.currentItem = (UIBase)items.elementAt(cidx-1);	
				}	
			}
			Stage.getInstance().notifyRepaint(this);
	    }
		
	}
	
	private void focusToPreX(){
	    if(this.currentItem == null ||
	       this.currentItem == this.firstItem ) return;		
	    int cidx = this.items.indexOf(this.currentItem);	    
		UIBase pre;
		while( cidx > 0 ){
			pre = (UIBase)items.elementAt(--cidx);
			if( currentItem.bonds[0][1] != pre.bonds[0][1]) break;
			if( ! pre.enable ) continue;
			this.currentItem.setFocus(false);
			pre.setFocus(true);
			this.currentItem = pre;
			break;
		}
	}
	
	private void focusToNextX(){
		//处理第一个元素
		if(this.currentItem == null ||
	       this.currentItem == this.lastItem ) return;	    
		int cidx = this.items.indexOf(this.currentItem);	
		UIBase next;
		while( cidx < items.size() ){
			next = (UIBase)items.elementAt(++cidx);
			if( currentItem.bonds[0][1] != next.bonds[0][1]) break;
			if( ! next.enable ) continue;
			this.currentItem.setFocus(false);
			next.setFocus(true);
			this.currentItem = next;
			break;
		}
	}
	
	/**
	 * 往容器中添加一个控件,如果容器的宽度没有设定,则不对加入
	 * 的控件做任何布局操作,如果宽度已经设定或发生改变,则需要
	 * 重新布局内部的控件。
	 * @param obj
	 */
	public void add(UIBase obj){
		obj.owner = this;
		if(this.firstItem == null){
	    	this.firstItem = obj;	    	
	    }	    
	    if(this.width > 0 ){
	    	setPosition(lastItem, obj);
	    }
	    this.items.addElement(obj);	    
	    this.lastItem = obj;
	}
	
	/**
	 * 根据第一个元素设置第二个元素的坐标和占用区域
	 * @param first
	 * @param next
	 */
	private void setPosition(UIBase first,UIBase next){
		if( !isItemLayout ) 
			isItemLayout = true;
		if(next == null ) return;
		int x;
		int y;
		int w = this.getWidth() - this.getStyle().paddingLeft - this.getStyle().paddingRight;
		if(first == null){
			x = this.getStyle().paddingLeft;
			y = this.getStyle().paddingTop;		
		}
		else{			
	    	if( next.isNewLine ) {
	    	    x = getStyle().paddingLeft;
	    	    y = first.bonds[1][1] + next.getStyle().marginTop;
	    	}
	    	else{
	    		x = first.bonds[1][0] + first.getStyle().marginRight + next.getStyle().marginLeft;
		    	y = first.bonds[1][1] - next.getLineHeight();
	    	}
		}
    	next.setAutoArea(x, y, w );
	}
	
	/**
	 * 接受容器中控件尺寸变化的通知
	 * @param obj
	 */
	public void notifySizeChange(UIBase obj){
		resetLayout(obj);
	}
	
	/**
	 * 重新布局元素(空间),该方法将对元素的可见区重新计算
	 * @param obj 起始元素
	 */
	protected void resetLayout(UIBase obj){
		if( this.getWidth() <= 0 ){
		    this.vWidth = Stage.getCW();
		    this.vHeight = Stage.getCH();
		    if(this.menu != null){
		    	this.vHeight -= Menu.getHeight();
		    }
		    this.width = this.vWidth - 5;
		}
		
	    int i;	    
	    UIBase first,next;
		if( obj == this.firstItem ){
	    	this.setPosition(null,this.firstItem );
	    	first = this.firstItem;
	    	i = 1;
	    }
		else{
		    i = items.indexOf(obj);
		    first = (UIBase)items.elementAt(i-1);
		}
	    if( i< 0 ) return;
		for(;i<items.size(); i++){
	    	 next = (UIBase)items.elementAt(i);
	    	 this.setPosition(first,next );
	    	 first = next;
	    }
		this.height = this.lastItem.bonds[1][1];		
	}
	/**
	 * 画滚动条
	 */
	private void paintScroller(Graphics g){
		if( this.vHeight >= this.height ) return;
		int scope = 0;
		int sHeight = this.vHeight * this.vHeight / this.height;		
		scope = this.vHeight - sHeight;		
		int sx = this.vWidth - 5;
		int sy = scope * this.yOffset / (this.vHeight - this.height);				
			// 画状态条背景
		int iColor = 0xCCCCCC;
		g.setColor(iColor);
		g.setClip(0, 0, this.vWidth, this.vHeight);		
		g.fillRect( sx, this.getY(), 5, this.vHeight );
		// 画状态条滑块
		iColor = 0x666666;
		g.setColor(iColor);
		g.fillRect(sx+1, sy, 4, sHeight);
	}
	
	void disableScreen(Graphics g, int color){
		int x = this.getX();
		int y = this.getY();
		int w = this.vWidth;
		int h = this.vHeight;
		g.setClip(x, y, w, h);
		int[] rgbArr = new int[w*5];
		for(int i=rgbArr.length-1; i > 0; i-- ){
			//rgbArr[i] = 0x55FFFFFF;
			rgbArr[i] = color;	
		}
		for(int i= h-5;i > -5; i-=5){
			g.drawRGB(rgbArr, 0, w, x, i, w, 5, true);
		}
	}
	
	/**
	 * 增加可以显示的的菜单
	 * @param m
	 */
	public void addMenu(Menu m,Command cmd){
		this.menu = m;
		this.defaultCmd = cmd;
        this.menu.setCommandDispatcher(this);
	}
	
	/**
	 * 设置在选项上按了OK键后默认的菜单执行命令
	 * @param cmd
	 */
	public void setDefaultCommand(Command cmd){
	    this.defaultCmd = cmd;   	
	}
	/**
	 * 设置系统菜单命令监听器
	 * @param lstr
	 */
	public void setCommandListener(CommandListener lstr){
		this.commandListener = lstr;
	}
	/**
	 * 设置选项命令监听器
	 */
    public void setItemCommandListener(ItemCommandListener itemLstr){
    	this.itemCommandListener = itemLstr;
    }

    
	public void notifyCommand(Command cmd) {
		Logger.info("UIContainer Execute " + cmd.toString());
		if( !cmd.isLevelUser() && commandListener != null){			
			this.commandListener.commandAction(cmd);
		}
		else if(cmd.isLevelUser() && itemCommandListener != null){	
			if( this.currentItem != null && currentItem.enable ){
				itemCommandListener.commandAction(cmd, currentItem);
			}
		}		
	}

	public boolean isActive() {
		return true;
	}

	public boolean responseKeyCode(int keyCode) {
		return true;
	}	
}

⌨️ 快捷键说明

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