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

📄 container.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 JAVA
字号:
package org.gggeye.easymf.ui;
import org.gggeye.easymf.log.Logger;

/**
 * Web 视图 1,包括页面的渲染 2,页面的控制逻辑
 * 
 * @author wuhua
 * 
 */
public class Container {
	Panel panel;
	HtmlDocument htmlDocument;
	final static byte VIEW_MOVE_UP = 1;
	final static byte VIEW_MOVE_DOWN = 6;
	
	final static byte VIEW_SCROLL_UP = 2;
	final static byte VIEW_SCROLL_DOWN = 5;
	final static byte VIEW_ENTER_CLICK = 8;

	/**
	 * 事件监听器
	 */
	EventListener eventListener;
	 
	Container(Panel _webBrowser) {
		this.panel = _webBrowser;	 
	}

	protected void doPaintViews(Pen _point) {
		doDrawDocument(_point, this.htmlDocument);
		
	}

	/**
	 * 绘制菜单
	 * 
	 * @param player
	 */
	  void doDrawMenu(Pen p) {
		p.save();
		p.setColor(0xCEF);
		int y = Panel.height - Panel.labelHeight;
		p.graphics.fillRect(0, y, Panel.width,
				Panel.labelHeight);
		y += (Panel.labelHeight - Panel.defaultFont.getHeight()) >> 1;
		String title = "菜单";
		// if(this.htmlDocument != null && htmlDocument.title == null){
		// title = htmlDocument.title;
		// }
		p.setColor(0xFFFFFF);
		p.graphics.drawString(Runtime.getRuntime().freeMemory() / 1024 + "",
				50, y, 20);
		if(this.panel.menu != null && this.panel.menu.menuItems.size() >1){
			p.graphics.drawString(title, 0, y, 20);
		}
		
		p.reset();

	}
	
	/**
	 * doEnterClick
	 */
	private void doEnterClick(int _keyCode) {
		View focus = htmlDocument.getFocusView();
		if(focus == null)
			return;
		if(focus.eventListener == null)
			focus.eventListener = this.eventListener;
		//if(focus != null && eventListener != null){
			//eventListener.htmlElementOnClick(focus);
		//}else{
			focus.doClick(_keyCode);
		//}
	}

	 
	 

	/**
	 * 绘制标题
	 * 
	 * @param player
	 */
	  void doDrawTitle(Pen p) {
		p.save();
		p.setColor(0xCEF);
		p.graphics.fillRect(0, 0, Panel.width,
				Panel.labelHeight);
		int y = (Panel.labelHeight - Panel.defaultFont.getHeight()) >> 1;
		String title = "XWeb";
		if (this.htmlDocument != null && htmlDocument.title != null) {
			title = htmlDocument.title;
		}
		p.setColor(0xFFFFFF);
		p.graphics.drawString(title, 0, y, 20);
		p.reset();

	}

	/**
	 * doDrawDocument
	 * 
	 * @param Pen
	 *            Point
	 * @param doucment
	 *            HtmlDocument
	 */
	private void doDrawDocument(Pen point, HtmlDocument document) {
		if (htmlDocument == null) {
			return;
		}
		try {
			int defaultBackColor = 0xFFFFFF; // doucment.backColor;
			fillBackground(point, defaultBackColor);
			point.setColor(0x0);
			// 绘制页面的时候设置字体
			point.setFont(Panel.defaultFont);
			point.setStrokeStyle(0);

			// int startId = document.tempStartHtmlElementId = dc[0];
			// int endId = document.tempEndHtmlElementId = dc[1];
			point.setOffset(0, document.position + Panel.labelHeight);
			for (int i = 0; i < document.views.size(); i++) {
				View he = (View) document.views
						.elementAt(i);
				// doDrawHtmlElement(Point, he);
				he.doPaint(point, document);

				// 为了加强性能,这里应该判断下,当前元素是否在绘制屏幕以内,如果在的话,才需要绘制
			}
		} catch (Exception e) {
			Logger.debug(e);
		}

	}

	 

 

	/**
	 * 移动页面, 1,查找焦点 2,移动Y轴位置偏移量
	 * 
	 * @param i
	 */
	private void scrollHtmlDocumentByHeight(int _height) {
		// 文档对象的屏幕在整个手机屏幕上的时候直接跳出
		if (htmlDocument.height < Panel.viewHeight)
			return;
		htmlDocument.position += _height;
		if (htmlDocument.position > 0) {
			htmlDocument.position = 0;
			// return;
		}
		if (Math.abs(htmlDocument.position) + Panel.viewHeight > htmlDocument.height) {
			htmlDocument.position = -(htmlDocument.height - Panel.viewHeight);
		}
		// 下面是查找焦点_height < 0 表示像上查找

	}

	/**
	 * 查找焦点, 怎么样查找焦点了。用什么算法了。这很头疼啊 经过调试终于被我成功的解决了这个问题 主要是递归查找方式获取焦点位置。
	 * 如果具有焦点,并且当前焦点在屏幕上则跳出循环
	 * 
	 * @param _findType --
	 *            -1 向上查找, 1向下查找
	 */
	private void findFocusHtmlElement(byte _findType) {
		View htmlElement = this.htmlDocument
				.nextFocusView(_findType);
		// }
		int elements = 0;
		while (htmlElement != null) {
			elements++;
			if (htmlElement.inSreenContent(_findType, Math
					.abs(htmlDocument.position), Math
					.abs(htmlDocument.position)
					+ Panel.viewHeight)) {
				htmlDocument.focusIndex = htmlElement.index;
				return;
			}
			htmlElement = this.htmlDocument.nextFocusView(_findType);
			htmlDocument.focusIndex = htmlElement.index;
			if (elements >= this.htmlDocument.size()) {
				return;
			}		 
		}
	}
	
	final void doClick(int _keyCode){
		switch(_keyCode){
		case Container.VIEW_MOVE_UP: moveFocusElement(Container.VIEW_MOVE_UP);break;
		case Container.VIEW_MOVE_DOWN: moveFocusElement(Container.VIEW_MOVE_DOWN);break;
		case Container.VIEW_SCROLL_UP: moveFocusElement(Container.VIEW_SCROLL_UP);break;
		case Container.VIEW_SCROLL_DOWN: moveFocusElement(Container.VIEW_SCROLL_DOWN); break;
		//case Container.VIEW_ENTER_CLICK: doEnterClick(_keyCode);break;
		default: break;
		}
//		View tView = this.htmlDocument.getFocusView();
//		this.eventListener
		this.doEnterClick(_keyCode);
	}

	/**
	 * 处理屏幕的滚动 1.焦点的滚动 2.滑动滚动
	 * 
	 * @param hd
	 *            HtmlDocument
	 * @param moveType
	 *            byte 0 up, 1 down
	 */
	void moveFocusElement(byte moveType) {
		switch (moveType) {
		case VIEW_MOVE_UP:
			moveDocumentUp();
			break;
		case VIEW_MOVE_DOWN:
			moveDocumentDown();
			break;
		case VIEW_SCROLL_UP:
			scrollHtmlDocumentByHeight(60);
			findFocusHtmlElement(VIEW_MOVE_UP);
			break;
		case VIEW_SCROLL_DOWN:
			scrollHtmlDocumentByHeight(-60);
			findFocusHtmlElement(VIEW_MOVE_DOWN);
			break;
		}

	}

	private void moveDocumentDown() {
		View he = this.htmlDocument.nextFocusView(VIEW_MOVE_DOWN);
		if(he == null && this.htmlDocument.height - Math.abs(this.htmlDocument.position) > Panel.viewHeight){
			this.scrollHtmlDocumentByHeight(-60);
			return;
		}
		
		while (he != null) {
			if ( !he.onSreenContent(VIEW_MOVE_DOWN, Math
					.abs(htmlDocument.position ), Math
					.abs(htmlDocument.position  )
					+ Panel.viewHeight)) {
				this.scrollHtmlDocumentByHeight(-60);

				return;
			}
			if (he.hasFocus) {
				htmlDocument.focusIndex = he.index;
				return;
			}
		}
		
	
		
	}

	private void moveDocumentUp() {
		View he = this.htmlDocument.nextFocusView(VIEW_MOVE_UP);
		if(he == null &&   this.htmlDocument.position  < 0){
			this.scrollHtmlDocumentByHeight(60);
			return;
		}
		while (he != null) {
			if ( !he.onSreenContent(VIEW_MOVE_UP,
							Math.abs(htmlDocument.position
									- Panel.labelHeight), Math
									.abs(htmlDocument.position)
									+ Panel.viewHeight)) {
				this.scrollHtmlDocumentByHeight(60);
				return;
			}

			if (he.hasFocus) {
				htmlDocument.focusIndex = he.index;
				return;
			}
		}
		
		

	}

	final void fillBackground(Pen point, int c) {
		point.save();
		point.setColor(c);
		point.graphics.fillRect(0, 0, Panel.width,
				Panel.height);
		point.reset();
	}
	
    final void setEventListener(EventListener _eventListener){
		this.eventListener = _eventListener;
	}

}

⌨️ 快捷键说明

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