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

📄 viewbuilder.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.gggeye.easymf.ui;
import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;

import org.gggeye.easymf.log.Logger;

/**
 * 
 * View 视图排版引擎。
 * 用于组装View
 */
public class ViewBuilder {
	final static int LEFT_SPACE = 2;
	final static int TOP_SPACE = 1;
	// final static int defaultHeight = 20;//这里保存了图片默认的高度
	HtmlDocument htmlDocument;
	/**
	 * 默认颜色
	 */
	int defaultBackColor = 0xFFFFFF;
	int defaultFontColor = 0x0;

	// 只读, 文档的开始位置
	int startX = 2;
	int startY = 0;
	// int L

	int documentWidth = Panel.viewWidth;

	Font currentFont = Panel.defaultFont; // 当前的字体
	int currentBackColor = 0xffffff;
	int currentFontColor = 0x0;
	Font bakFont;
	int bakBackColor;
	int bakFontColor;
	Font defaultFont = Panel.defaultFont; // 默认的字体
	int defaultNextRowHeight = 0; // currentFont.getHeight();
	final int defauletWidth = currentFont.charWidth('图');

	View lastHtmlElement;
	int backLayoutHeight = this.defaultNextRowHeight; // 重新排版记录上一次的高度

	ViewBuilder() {
		htmlDocument = new HtmlDocument();
	}

	ViewBuilder(HtmlDocument _htmlDocument) {
		htmlDocument = _htmlDocument;
	}

	public final Button addButton(String _name, String _action,
			HtmlForm _htmlForm) {
		Button button = new Button(_name, _action, _htmlForm);
		int width = this.defaultFont.stringWidth(button.name) + this.LEFT_SPACE * 4;
		this.resetLeftPostion(width);
		button.left = this.startX;
		button.top = this.startY;
		button.width = width;
		button.height = this.currentFont.getHeight();// + TOP_SPACE*4;
		this.htmlDocument.addView(button);
		this.resetPostion(width, button.height);
		changeDefaultHeight(button.height);

		return button;

	}

	public final void addHtmlForm(HtmlForm _htmlForm, View _htmlElement) {
		if (_htmlForm != null)
			_htmlForm.htmlElements.addElement(_htmlElement);

	}

	public final HtmlForm addHtmlForm(String _name, String _id, String _action,
			String _method, String _charset, String _enctype) {
		HtmlForm htmlForm = new HtmlForm(_name, _id, _action, _method,
				_charset, _enctype, null);
		this.htmlDocument.addHtmlForm(htmlForm);
		return htmlForm;
	}

	public final HtmlSelect addHtmlSelect(String _name, String _value,
			String _iName, String _iValue, int _listType, HtmlForm _htmlForm) {
		HtmlSelect htmlSelect = new HtmlSelect(_name, _value, _iName, _iValue,
				_listType);
		this.htmlDocument.addView(htmlSelect);
		this.addHtmlForm(_htmlForm, htmlSelect);
		return htmlSelect;
	}

	// public final ImageView addImageView(String _href) {
	// return this.addImageView(null, false);
	// }

	/**
	 * 增加一个链接,依赖以后的元素
	 * 
	 * @param url
	 *            String
	 * @param font
	 *            Font
	 * @param selectColor
	 *            int
	 * @param selectedColor
	 *            int
	 * @return HtmlElement
	 */
	public final HyperLink addHyperLink(String _href, HtmlForm _htmlForm) {
		return this.addHyperLink(null, _href, _htmlForm);
	}

	public final HyperLink addHyperLink(String _name, String _href,
			HtmlForm _htmlForm) {
		HyperLink hl = new HyperLink(_href);
		// if(_name != null || _name.length() != 0)
		// hl.values = this.bulidView(_name, currentFont, hl);
		// hl.right = this.startX;
		// hl.buttom = this.startY;
		// this.document.addHtmlElement(hl);
		if (_name != null && _name.length() != 0) {
			this.addText(hl, _name);
		}

		this.addHtmlForm(_htmlForm, hl);
		return hl;
	}

	/**
	 * 构造一个URL的图片
	 * 
	 * @param _hyperLink
	 * @return
	 */
	public final ImageView addImageView(String _src, HyperLink _hyperLink) {
		return this.addImageView(_src, null, _hyperLink);
	}

	public ImageView addImageView(String _src, Image _img, HyperLink _hyperLink) {
		return addImageView(_src, null, _img, null, _hyperLink);
	}
	
	 
	
	public final CheckBox addCheckBox(String _name, String _value, boolean _isCheck, HtmlForm _htmlForm){
		CheckBox tCheckBox = new CheckBox(_name, _value, _isCheck);
		tCheckBox.width = Radio.checked.getWidth() + LEFT_SPACE;
		tCheckBox.height = Radio.checked.getHeight() + TOP_SPACE;
		
		// 重新计算位置,看看是否需要换行
		this.resetLeftPostion(tCheckBox.width);
		tCheckBox.left = this.startX;
		tCheckBox.top = this.startY;
		resetPostion(tCheckBox.width, tCheckBox.height );
		this.htmlDocument.addView(tCheckBox);
		this.addHtmlForm(_htmlForm, tCheckBox);
		return tCheckBox;
		
	}
	
	public final Radio addRadio(String _name, String _id, String _value, boolean _isCheck, HtmlForm _htmlForm){
		Radio tRadio = new Radio(_name, _id, _value, _isCheck);
		tRadio.width = Radio.checked.getWidth() + LEFT_SPACE;
		tRadio.height = Radio.checked.getHeight() + TOP_SPACE ;
		
		// 重新计算位置,看看是否需要换行
		this.resetLeftPostion(tRadio.width);
		tRadio.left = this.startX;
		tRadio.top = this.startY;
		
		changeDefaultHeight(tRadio.height);
		
		resetPostion(tRadio.width, tRadio.height  );
		
		this.htmlDocument.addView(tRadio);
		this.addHtmlForm(_htmlForm, tRadio);
		return tRadio;
		
	}
	
	/**
	 * 插入一个元素
	 * 注意这个Li元素是占据一行的HtmlElement
	 * 这里需要注意了。ICON的图标应该比文字的要高
	 * @param _icon
	 * @param _name
	 * @return
	 */
	public ListItem insertListItem(Image _icon, String _name, int _index){
		
		return null;
	}
	
	/**
	 * 删除指定Index的View控件
	 * 注意排版的问题。
	 * @param _index
	 * @return
	 */
	public View delete(int _index){
		View tView = this.htmlDocument.getView(_index);
		return this.delete(tView);
		
	}
	
	public View delete(View _view){
		this.resetDeleteLayout(_view);
		this.htmlDocument.deleteView(_view);
		return _view;
		
	}
	
	
	
 
	
 

	
	

	/**
	 * 添加一个Image对象排版。
	 * 注意Gif的特殊情况
	 * @param _src
	 * @param _alt
	 * @param _img
	 * @param _action
	 * @param _hyperLink
	 * @return
	 */
	public ImageView addImageView(String _src, String _alt, Image _img,
			String _action, HyperLink _hyperLink) {
		 
			return this.addPNGImageView(_src, _alt, _img, _action, _hyperLink);
			
			
		 
		 
	}
	
	private ImageView addPNGImageView(String _src, String _alt, Image _img,
			String _action, HyperLink _hyperLink){
		ImageView imageView = new ImageView(_src, _alt, _img, _action, _hyperLink);
		addHtmlElement(imageView, _img);
		return imageView;
	}

	private void addHtmlElement(View _htmlElement, Image _image) {
		if (_image == null) {
			_htmlElement.width = this.defauletWidth;
			_htmlElement.height = this.defaultNextRowHeight;
		} else {
			_htmlElement.width = _image.getWidth() + this.LEFT_SPACE;
			_htmlElement.height = _image.getHeight() + this.TOP_SPACE;
		}

	 

		resetImagePostion(_htmlElement);
		changeDefaultHeight(_htmlElement.height);
		this.htmlDocument.addView(_htmlElement);
		lastHtmlElement = _htmlElement;
	}

	public final SelectOption addSeletOption(String _name, String _value) {
		SelectOption so = new SelectOption(_name, _value);
		return so;
	}

	public final void addText(HyperLink _hyperLink, String _text) {
		 
		TextView textView = this.addText(_text);
		textView.addHyperLink(_hyperLink);
	}

	/**
	 * 向文档中添加一个字符串
	 * 
	 * @param parent
	 *            HtmlElement
	 * @param text
	 *            String
	 * @param font
	 *            Font
	 * @param backColor
	 *            int
	 * @param fontColor
	 *            int
	 * @return HtmlElement
	 */
	public final TextView addText(String text) {
		TextView tv = new TextView(text, currentFont, currentBackColor,
				currentFontColor, null);
		tv.values = this.bulidView(text, currentFont, tv);
		this.htmlDocument.addView(tv);
		return tv;
	}

	public final TextArea addTextArea(Panel _panel, String _name, String _value, int _size,
			int _maxSize, int _textType, HtmlForm _htmlForm) {
		return this.addTextArea(_panel,_name, _value, 1, _size, _maxSize, _maxSize,
				_textType, _htmlForm);
	}

	/**
	 * 添加一个TextArea控件,对应的是Input TextArea等控件 1.这里需要注意的问题,因为如果控件超过一行屏幕,需要换行啊。
	 * 
	 * @param _name
	 * @param _value
	 * @param _row
	 * @param _col
	 * @param _size
	 * @param _maxSize
	 * @param _textType
	 * @param _htmlForm
	 * @return
	 */
	public final TextArea addTextArea(Panel _panel, String _name, String _value, int _row,
			int _col, int _size, int _maxSize, int _textType, HtmlForm _htmlForm) {
		_name = _name == null ? "" : _name;
		_value = _value == null ? "" : _value;
		TextArea ta = new TextArea(_panel, _name, _value, _size, _maxSize, _textType);

		_row = _row == 0 ? 1 : _row;
		_col = _col == 0 ? 1 : _col;

		int height = currentFont.getHeight() * _row + TOP_SPACE; // 加上一个TOP间隔

		int width = this.currentFont.charWidth('中') * _col;
		// 重新计算位置,看看是否需要换行
		this.resetLeftPostion(width);


		ta.left = startX;
		ta.top = startY;

		ta.width = width > this.documentWidth ? documentWidth   : width;
		ta.height = height;

		// 如果TextArea只有一行的时候,换行的时候就没必要换了。

		if (_row == 1) {
			// 排版问题解决了,原来是在这里出了点小问题
		    // this.resetPostion(ta.width , this.TOP_SPACE * 2);
			this.startX += ta.width;
			this.startY += TOP_SPACE*2;
			this.changeDefaultHeight(height + TOP_SPACE);
 
		} else {
			this.resetPostion(width, height);
			changeDefaultHeight(height);
		}
		this.htmlDocument.addView(ta);
		this.addHtmlForm(_htmlForm, ta);
		return ta;
	}

	public final void bindSelectOption(SelectOption[] _selectOptions,
			int _selectIndex, HtmlSelect _htmlSelect) {
		if (_selectOptions == null || _selectOptions.length == 0
				|| _htmlSelect == null) {
			throw new java.lang.NullPointerException("Object is Null");
		}

		// SelectOption so = new SelectOption(_names, _values);
		_htmlSelect.selectOption = _selectOptions;
		_htmlSelect.selectIndex = _selectIndex;

		int width = 0; // 用来计算SelectOption的宽度
		for (int j = 0; j < _selectOptions.length; j++) {
			int w = currentFont.stringWidth(_selectOptions[j].name) + 16;
			if (startX + w > this.documentWidth) {
				this.resetPostion(w);
			}
			if (w >= this.documentWidth) {
				width = w;
				break;
			}
			if (w > width) {
				width = w;
			}
		}
		// resetHeight(so);
		_htmlSelect.left = startX;
		_htmlSelect.top = startY;
		_htmlSelect.width = width;
		_htmlSelect.height = this.currentFont.getHeight();
		_htmlSelect.right = startX + width;
		_htmlSelect.buttom = startY + _htmlSelect.height;
		this.resetPostion(width, _htmlSelect.height);
		// this.document.addHtmlElement(so);
		// this.addHtmlForm(_htmlForm, so);
		// return so;

	}

	public final String[] bulidView(String _text, Font _font,
			View _htmlElement) {
		if (_htmlElement == null || _text == null || _text.length() == 0) {
			return null;
		}
		// if(_text != null && _text.startsWith("娱乐")){
		// System.out.print(false);
		// }
		
		Vector vector = new Vector();
		Font font = this.getFont(_font);
		int width = font.stringWidth(_text);
		int height = font.getHeight();
		changeDefaultHeight(height);
		resetHeight(height);
		// resetHeight(_htmlElement);
		int charWidth = _font.charWidth('你');
		//这里需要注意的是第一个字是否需要换行。
		if(charWidth + this.startX > this.documentWidth){
			this.nextLine();
		}
		
		_htmlElement.left = startX;
		_htmlElement.top = startY;
		
		
		// 先要换行。
		// resetPostion(width, height);

		if (width + this.startX < this.documentWidth) {
			_htmlElement.right = width + startX;
			_htmlElement.buttom += startY + height;
			_htmlElement.width = width;
			_htmlElement.height = height;
			vector.addElement(_text);
			resetPostion(width, height);
		} else {
			//int rows = (width) / (this.documentWidth) + 10;

			
			int length = _text.length();
			charWidth = _font.charWidth(_text.charAt(0));
			int lineWidth = 0;
			boolean flag = true;
			int brankWidth = 0;
			 
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < length; i++) {
				lineWidth += charWidth;
				sb.append(_text.charAt(i));
				if (flag) {
					brankWidth = documentWidth - this.startX;
					flag = false;
				}
				if (i == length - 1) {
					charWidth = 0;
				} else {
					charWidth = _font.charWidth(_text.charAt(i + 1));
				}

				if ((lineWidth + charWidth) > brankWidth) {
					 
					if (i != length - 1) {
						resetPostion(lineWidth + charWidth + 6, height);

					}
					vector.addElement(sb.toString());
					sb = null;

⌨️ 快捷键说明

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