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

📄 viewbuilder.java

📁 开发框架。 一.说明: 此框架的意图是解决手机软件开发中常遇到
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					sb = new StringBuffer();
					lineWidth = 0;
					flag = true;
				}
				if (i == length - 1) {
					resetPostion(lineWidth + charWidth + 6, height);
					vector.addElement(sb.toString());
					sb = null;
					sb = new StringBuffer();
					break;
				}
			}
		}
		// 设置文档的right, buttom
		_htmlElement.right = this.startX;
		_htmlElement.buttom = this.startY;
		_htmlElement.width = width;
		_htmlElement.height = height;
		String[] result = new String[vector.size()];
		vector.copyInto(result);
		vector = null;
		return result;
	}

	/**
	 * 改变高度,用来换行
	 */
	private void changeDefaultHeight(int height) {
		if (this.defaultNextRowHeight < height) {
			defaultNextRowHeight = height;
		}

	}

	/**
	 * 调用了startSytle必须已endStyle结束
	 * 
	 * @see startSytle;
	 */
	public final void endStyle() {
		resetStyle();
	}

	final Font getFont(Font _font) {
		currentFont = _font == null ? currentFont : _font;
		return currentFont;
	}

	void nextLine() {
		// defalutHeight = defalutHeight<
		// this.currentFont.getHeight()?currentFont.getHeight():defalutHeight;
		this.nextLine(this.defaultNextRowHeight);
	}
	
	 
	void nextLine(int height) {
		startX = LEFT_SPACE;
		startY += height; // + this.TOP_SPACE;
	 
		//this.htmlDocument.height += height;
		this.resetDefaultHeight();
	}

	void nextLine(int w, int h) {
		if (startX + w > this.documentWidth) {
			this.nextLine(h);
		} else {
			startX += w;
			startY += h;
		}
	}

	private void resetDefaultHeight() {
		this.defaultNextRowHeight = this.currentFont.getHeight();

	}

	/**
	 * 用来换行用的。很复杂啊
	 * 
	 * @param _h
	 */
	final void resetHeight(int _h) {
		if (lastHtmlElement != null && this.lastHtmlElement.height > _h) {
			startY += lastHtmlElement.height - _h + this.TOP_SPACE;
			// this.lastHtmlElement = null;
			this.lastHtmlElement = null;
		} else {

		}
	}

	/**
	 * 处理图片的偏移问题
	 * 
	 * @param _he
	 */
	final void resetImagePostion(View _he) {
		// 图片换行
		if (startX + _he.width > this.documentWidth) {
//			if (lastHtmlElement != null) {
//				startY += lastHtmlElement.height + this.TOP_SPACE;
//				// lastHtmlElement = null;
//			}
			nextLine(defaultNextRowHeight + TOP_SPACE);

			// startY += this.defalutHeight;
		} 
		else {
			// 如果图片的高度大于默认换行的感度,才需要重新设置startY的位置
			// if(_he.height > this.defalutHeight)
			resetHeight(_he.height);

		}

		_he.left = startX;
		_he.top = startY;
		_he.right = _he.left + _he.width;
		_he.buttom = _he.top + _he.height;
		startX = _he.right;
		//this.htmlDocument.height = _he.buttom;
		// startY = _he.buttom;
	}
	
	 /**
	  * 删除元素的时候需要重新排版
	  * @param _view
	  */
	final void resetDeleteLayout(View _view){
		try {
			if (_view == null)
				return;
			// 从指定的元素开始重新计算位置
			// 先偏移相近的第一个元素
			// 如果发现,需要偏移的Y轴小于默认的换行高度,则不需要重新排版了
			 
			backLayoutHeight = _view.height > this.backLayoutHeight ? _view.height
					: backLayoutHeight;
			int topOffset =   - _view.height;
			int leftOffset = this.defauletWidth -_view.width + LEFT_SPACE;
		
			View nextElement = htmlDocument
					.getViewHtmlElement(_view.index + 1);
			
			if(nextElement == null){
				resetViewBuilder(topOffset);
				return;
			}

		 
			if (_view.top == nextElement.top
					&& (_view.left + _view.width + nextElement.width) > this.documentWidth) {
				topOffset += this.defaultNextRowHeight;
				nextElement.left = LEFT_SPACE;
				// nextElement.top += topOffset;
				nextElement.setTopPostion(topOffset);
			}
			// 在同一条水平线才需要偏移X轴
			else if (_view.top == nextElement.top) {
				// htmlElement.top += topOffset;
				nextElement.left += leftOffset;
			}
			 
			else {
				topOffset = -_view.height;
				
				
						//- nextElement.top;
				 
				// 只有小于的时候才需要重新计算位置, 位置不需要重新计算
				if (nextElement.top < _view.top + _view.height) {
					return;
				}

				nextElement.setTopPostion(topOffset);
			}
			for (int j = nextElement.index + 1; j < this.htmlDocument.size(); j++) {
				View htmlElement = htmlDocument.getView(j);
				// 如果当前的元素的宽度+上个元素的宽度超过了文档宽度,则换行
				if(htmlElement instanceof HtmlHiddenField){
					continue;
				}
				if (_view.top == htmlElement.top) {
					// htmlElement.top += topOffset;
					htmlElement.left += leftOffset;
				}else {
					htmlElement.setTopPostion(topOffset);
				}

			}
			// 这里需要处理高度的问题,因为重新排过版所以需要处理高度的问题
			resetViewBuilder(topOffset);
			//this.startX
		} catch (Exception e) {
			Logger.debug(e);
		}
	}

	private void resetViewBuilder(int topOffset) {
		this.htmlDocument.height += topOffset;
		this.startY += topOffset;
	}
	

	/**
	 * 重新排版 布局需要考虑的问题。<br/> 
	 * 1 X 轴的偏移量<br/>
	 *  2 Y 轴的偏移量 考虑那个Y轴比较高,比较低的处理 相同图片高度不一致的处理<br/>
	 * 这个重新排版有点问题。需要记录 比如不能处理上几次的高度,导致了排版的时候会出现问题。<br/>
	 * 
	 * @param _htmlDocument
	 *  
	 */
	final void resetLayout(View _htmlElement) {
		try {
			if (_htmlElement == null)
				return;
			// 从指定的元素开始重新计算位置
			// 先偏移相近的第一个元素
			// 如果发现,需要偏移的Y轴小于默认的换行高度,则不需要重新排版了
			if (_htmlElement.height < this.defaultNextRowHeight) {
				return;
			}
			backLayoutHeight = _htmlElement.height > this.backLayoutHeight ? _htmlElement.height
					: backLayoutHeight;
			int topOffset = _htmlElement.height - this.defaultNextRowHeight;
			int leftOffset = _htmlElement.width - this.defauletWidth
					+ LEFT_SPACE;
		
			View nextElement = htmlDocument
					.getViewHtmlElement(_htmlElement.index + 1);
			if(nextElement == null)
				return;

		 
			if (_htmlElement.top == nextElement.top
					&& (_htmlElement.left + _htmlElement.width + nextElement.width) > this.documentWidth) {
				topOffset += this.defaultNextRowHeight;
				nextElement.left = LEFT_SPACE;
				// nextElement.top += topOffset;
				nextElement.setTopPostion(topOffset);
			}
			// 在同一条水平线才需要偏移X轴
			else if (_htmlElement.top == nextElement.top) {
				// htmlElement.top += topOffset;
				nextElement.left += leftOffset;
			}
			 
			else {
				topOffset = (_htmlElement.top + _htmlElement.height)
						- nextElement.top;
				 
				// 只有小于的时候才需要重新计算位置, 位置不需要重新计算
				if (nextElement.top > _htmlElement.top + _htmlElement.height) {
					return;
				}

				nextElement.setTopPostion(topOffset);
			}
			for (int j = nextElement.index + 1; j < this.htmlDocument.size(); j++) {
				View htmlElement = htmlDocument.getView(j);
				// 如果当前的元素的宽度+上个元素的宽度超过了文档宽度,则换行
				if(htmlElement instanceof HtmlHiddenField){
					continue;
				}
				if (_htmlElement.top == htmlElement.top) {
					// htmlElement.top += topOffset;
					htmlElement.left += leftOffset;
				}else {
					htmlElement.setTopPostion(topOffset);
				}

			}
			// 这里需要处理高度的问题,因为重新排过版所以需要处理高度的问题
			this.htmlDocument.height += topOffset;
		} catch (Exception e) {
			Logger.debug(e);
		}

	}

	/**
	 * 重新计算 startX的坐标
	 * 
	 * @param _width
	 */
	void resetLeftPostion(int _width) {
		// 这个是排版元素开始时候判断元素的位置
		if (startX != 2 && startX + _width > this.documentWidth) {
			this.nextLine(this.defaultNextRowHeight);
		}
	}

	/**
	 * resetPostion
	 * 
	 * @param left
	 *            int
	 * @param top
	 *            int
	 */
	private void resetPostion(View _he, int _width, int _height,
			int fontHeight) {

	}

	/**
	 * resetLeftAndTop
	 * 
	 * @param width
	 *            int
	 */
	private void resetPostion(int _width) {
		resetPostion(_width, this.defaultNextRowHeight);
		// resetPotion(_width, 0, currentFont.getHeight());
	}

	/**
	 * 排版元素结束后,需要重新计算过X, Y的位置。
	 * 
	 * @param _width
	 * @param _height
	 */
	private void resetPostion(int _width, int _height) {
		if (startX + _width > this.documentWidth) {
			nextLine(_height);
			// if (lastHtmlElement != null &&
			// lastHtmlElement.buttom > this.startY) {
			// this.startY += _height;
			// this.startX = lastHtmlElement.right;
			// }else{
			// // if(lastHtmlElement != null)
			// lastHtmlElement = null;
			// }
		} else {
			startX += _width + LEFT_SPACE;
		}

	}

	/**
	 * 恢复上一样式
	 */
	public final void resetStyle() {
		this.currentFont = bakFont;
		this.currentBackColor = bakBackColor;
		this.currentFontColor = bakFontColor;

	}

	/**
	 * 保存上一样式
	 */
	public final void saveStyle() {
		this.bakFont = this.currentFont;
		this.bakBackColor = this.currentBackColor;
		this.bakFontColor = this.currentFontColor;
	}

	/**
	 * 给排版控件增加样式
	 * 
	 * @param _font
	 *            Font
	 * @param _backColor
	 *            int
	 * @param _fontColor
	 *            int
	 */
	public final void startSytle(Font _font, int _backColor, int _fontColor) {
		this.saveStyle();
		this.currentFont = _font == null ? this.defaultFont : _font;
		this.currentBackColor = _backColor;
		this.currentFontColor = _fontColor;
	}

	/**
	 * 添加属性,如果,HtmlForm 是空的话,则是整个页面的属性,否则 比如 <input name="usrname" value=""></input>
	 * <postfield name="ok" value=$username></postfield>
	 * 上面的情况,应该是,应该是把生成的username绑定到postfield中,
	 * postfield应该绑定到htmlForm中,让他们建立关联关系。
	 * 
	 * @param _name
	 * @param _value
	 * @param _htmlForm
	 */

	public void addHiddenHtmlElement(String _name, String _value,
			HtmlForm _htmlForm) {
// HtmlElement htmlElement = this.htmlDocument
// .findHtmlElementByName(_name);
		// 如果htmlElement查找不到元素,则添加新的元素
		HtmlHiddenField htmlHiddenField = new HtmlHiddenField();
		htmlHiddenField.name = _name;
		htmlHiddenField.value = _value;
		// 不存在form,则把隐含参数直接追加给HtmlDocument
// if (_htmlForm != null) {
// // if (htmlElement != null) {
// // this.addHtmlForm(_htmlForm, htmlElement);
// // } else {
// this.htmlDocument.addHtmlElement(htmlHiddenField);
//				
// // }
// }else{
// //this.add
//			
// }
		this.htmlDocument.addView(htmlHiddenField);
		this.addHtmlForm(_htmlForm, htmlHiddenField);
		

	}
	
	/**
	 * 单纯加一个固定高,固定宽度的组件
	 * @param _view
	 * @return
	 */
	public View addView(View _view){
		this.resetLeftPostion(_view.width);
		_view.top = this.startY;
		_view.left = this.startX;
		this.nextLine(_view.height);
		//this.resetHeight(_view.height);
		this.htmlDocument.addView(_view);
		return _view;
	}

	/**
	 * 换行支持
	 * 1, 支持一个字体高度的换行工作
	 * 2, 支持指定高度的换行工作。
	 */
	public final void addBr() {
		this.nextLine();
		
	}

	/**
	 * 恢复初始化布局
	 */
	public void reset() {
		this.startX = 2;
		this.startY = 0;
		this.defaultFont = Panel.defaultFont;
		this.htmlDocument.reset();
	}
	
//	/**
//	 * 保证换行到最后一行
//	 */
//	public void endHtmlDocument(){
//		this.nextLine();
//	}
//	
//	/**
//	 * 开始页面构造
//	 */
//	public void startHtmlDocument(){
//		this.startX = 2;
//		this.startY = 0;
//	}
	
	
}

⌨️ 快捷键说明

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