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

📄 blocktag.java

📁 wap浏览器 日程安排 Rss 棋牌游戏
💻 JAVA
字号:
/* * Fire (Flexible Interface Rendering Engine) is a set of graphics widgets for creating GUIs for j2me applications.  * Copyright (C) 2006-2008 Bluevibe (www.bluevibe.net) * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA *  *//** *  */package gr.fire.browser;import gr.fire.browser.util.Form;import gr.fire.browser.util.PageMetaData;import gr.fire.browser.util.XmlPullParser;import gr.fire.core.Component;import gr.fire.core.Container;import gr.fire.core.FireScreen;import gr.fire.core.Theme;import gr.fire.ui.ImageComponent;import gr.fire.ui.TextComponent;import gr.fire.util.Log;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** * @author padeler * */public class BlockTag extends Tag{	public static final String TAG_HTML = "html";	public static final String TAG_HEAD = "head";	public static final String TAG_TITLE = "title";	public static final String TAG_META = "meta";	public static final String TAG_BODY = "body";		public static final String TAG_STYLE = "style";	public static final String TAG_P = "p";	public static final String TAG_DIV = "div";	public static final String TAG_HR = "hr";	public static final String TAG_TABLE = "table";	public static final String TAG_H1 = "h1";	public static final String TAG_H2 = "h2";	public static final String TAG_H3 = "h3";	public static final String TAG_H4 = "h4";	public static final String TAG_H5 = "h5";	public static final String TAG_H6 = "h6";		public static final String TAG_SCRIPT = "script";	public static final String TAG_FORM = "form";			protected Container elementContainer = new Container();	protected int containerWidth; // the width of this container. It is either inherited by the parent or set by attributes			public BlockTag()	{		Theme th = FireScreen.getTheme();		font = th.defaultFont;		backgroundColor = th.componentBackgroundColor;		foregroundColor = th.componentForegroundColor;	}		public void inherit(Tag parent, boolean softLineBreak)	{		if(parent!=null)		{			inheritStyle(parent); // inherit style information			if(parent instanceof BlockTag) this.parentBlockTag = (BlockTag)parent;			else this.parentBlockTag = parent.parentBlockTag;		}				if(parentBlockTag!=null)		{			parentBlockTag.elementContainer.add(elementContainer);			if(softLineBreak)				parentBlockTag.lineBreak(parentBlockTag.font.getHeight(),false);			elementContainer.setX(parentBlockTag.getPointerX());			elementContainer.setY(parentBlockTag.getBaseLine()-parentBlockTag.getLineHeight());			containerWidth = parentBlockTag.getContainerWidth()-parentBlockTag.getPointerX();		}	}		public void handleTagStart(Browser browser, XmlPullParser parser)	{		String name = parser.getName().toLowerCase(); // the name of the tag		setName(name);				Tag parentElement = browser.topTag(); // may be a block element or an inline element				if(TAG_P.equals(name))		{			/*			 * This is a P Element. 			 * As a container i will have a new line before and a new line after my content			 */			inherit(parentElement,true); // first inherit from parents			handleCommonAttributes(parser); // then get extra style from attributes			lineBreak(font.getHeight(),true); // paragraph starts with line break.		}		else if(TAG_DIV.equals(name))		{			inherit(parentElement,true); // first inherit from parents			handleCommonAttributes(parser); // then get extra style from attributes			handleAlignAttributes(parser);						lineBreak(font.getHeight(),true);					}		else if(TAG_FORM.equals(name))		{			inherit(parentElement,true); // first inherit from parents			handleCommonAttributes(parser); // then get extra style from attributes			lineBreak(font.getHeight(),true);						String action = parser.getAttributeValue("action");			String method = parser.getAttributeValue("method");			String enctype = parser.getAttributeValue("enctype");			if(method==null || method.toLowerCase().toLowerCase().equals("get")) method = HttpConnection.GET;			else method = HttpConnection.POST;						Form newForm = new Form(browser,action,method,enctype);			// set the new form to the parent.			browser.setOpenForm(newForm);					}		else if(TAG_TABLE.equals(name))		{			inherit(parentElement,true); // first inherit from parents			handleCommonAttributes(parser); // then get extra style from attributes			handleAlignAttributes(parser);						lineBreak(font.getHeight(),true);					}		else if(TAG_H1.equals(name))		{			handleHeaderTagStart(parentElement,parser,Font.getFont(font.getFace(),Font.STYLE_BOLD,Font.SIZE_LARGE));		}		else if(TAG_H2.equals(name))		{			handleHeaderTagStart(parentElement,parser,Font.getFont(font.getFace(),Font.STYLE_BOLD|Font.STYLE_ITALIC,Font.SIZE_LARGE));		}				else if(TAG_H3.equals(name))		{			handleHeaderTagStart(parentElement,parser,Font.getFont(font.getFace(),Font.STYLE_BOLD,Font.SIZE_MEDIUM));		}		else if(TAG_H4.equals(name))		{			handleHeaderTagStart(parentElement,parser,Font.getFont(font.getFace(),Font.STYLE_BOLD,Font.SIZE_SMALL));		}		else if(TAG_H5.equals(name) || TAG_H6.equals(name))		{			handleHeaderTagStart(parentElement,parser,Font.getFont(font.getFace(),Font.STYLE_PLAIN,Font.SIZE_MEDIUM));		}		else if(TAG_HR.equals(name))		{			/*			 * This is a HR (Horizontal ruler) Element. 			 * It will have a new line before and a new line after it.			 */			inherit(parentElement,true); // first inherit from parents			handleCommonAttributes(parser); // then get extra style from attributes			lineBreak(font.getHeight(),true); // insert a line break.		}		else if(TAG_BODY.equals(name))		{			// body starts at the top left corner of the page.			handleCommonAttributes(parser); // then get extra style from attributes			handleColorAttributes(parser);						containerWidth = browser.getViewportWidth();			lineBreak(font.getHeight(),true);			browser.setPageContainer(elementContainer);		}		else if(TAG_SCRIPT.equals(name) || TAG_STYLE.equals(name))		{ // consume 			int type= parser.next();			Log.logInfo("<"+name+">");			//Log.logInfo(parser.getText());			Log.logInfo(".....Skipped.....");			Log.logInfo("</"+name+">");			return;		}		else if(TAG_META.equals(name))		{			PageMetaData meta = browser.getPageMetaData();			meta.parseMetaTag(parser);			return;		}		else if(TAG_TITLE.equals(name))		{			parser.next();			PageMetaData meta = browser.getPageMetaData();			meta.setPageTitle(parser.getText());			return;		}				copyStyle(elementContainer);		elementContainer.setId(tagId);	}	public void handleTagEnd(Browser browser, XmlPullParser parser)	{		String name = getName();		if(TAG_P.equals(name))		{			int baseLine = getBaseLine();			/*			 * This the end of a P Element. 			 * As a container i will have a new line before and a new line after my content			 */			if(parentBlockTag!=null)			{// update pointer position of parent				parentBlockTag.lineBreak(baseLine,true);			}						elementContainer.setPrefSize(containerWidth,baseLine,false);						return;		}				if(TAG_DIV.equals(name) || TAG_TABLE.equals(name) || TAG_H1.equals(name) || TAG_H2.equals(name) || TAG_H3.equals(name) || TAG_H4.equals(name) || TAG_H5.equals(name) || TAG_H6.equals(name))		{						if(parentBlockTag!=null)			{// update pointer position of parent				parentBlockTag.lineBreak(getBaseLine(),true);			}			elementContainer.setPrefSize(containerWidth,getBaseLine(),false);			return;		}				if(TAG_FORM.equals(name))		{						if(parentBlockTag!=null)			{// update pointer position of parent				parentBlockTag.lineBreak(getBaseLine(),true);			}			elementContainer.setPrefSize(containerWidth,getBaseLine(),false);			browser.setOpenForm(null);			return;		}				if(TAG_HR.equals(name))		{			// prepare the hr image			Image hrImage = Image.createImage(containerWidth,font.getHeight());			Graphics g = hrImage.getGraphics();			g.setColor(foregroundColor);			g.drawLine(0,font.getHeight()/2,containerWidth,font.getHeight()/2);			ImageComponent imCmp = new ImageComponent(hrImage,-1,-1,this.font,"");			copyStyle(imCmp);			handleComponent(this,imCmp); // add the hr image in the container.						int baseLine = getBaseLine();						if(parentBlockTag!=null)			{// update pointer position of parent				parentBlockTag.lineBreak(baseLine,true);			}						elementContainer.setPrefSize(containerWidth,baseLine,false);						return;		}				if(TAG_BODY.equals(name))		{			elementContainer.setPrefSize(containerWidth,getBaseLine(),false);			return;		}	}			private void handleHeaderTagStart(Tag parentElement,XmlPullParser parser, Font font)	{		inherit(parentElement,true); // first inherit from parents		handleCommonAttributes(parser); // then get extra style from attributes		handleAlignAttributes(parser);		this.font = font;		lineBreak(font.getHeight(),true);	}		public void handleText(Tag topLevelTag, String txt)	{		if(this.getName().equals(TAG_P)==false && txt.trim().length()==0) // ignore empty strings outside a paragraph.			return;				if(getBaseLine()==0) // first line of text		{			lineBreak(topLevelTag.getLineHeight(),true);		}		// add a text element 		int vw = containerWidth;				TextComponent el = new TextComponent(txt,vw,pointerX);		topLevelTag.copyStyle(el);		el.validate();		int fontHeight = topLevelTag.font.getHeight();				// update other primitives on the same line to have the correct height.		updatePrimitivesInLineForPrimitiveHeight(fontHeight);				el.setX(0); // start from the left side of the block element		el.setY(getBaseLine()-fontHeight);		int lastLineWidth = el.getLastLineWidth();		int contentHeight = el.getContentHeight();		el.setPrefSize(vw,contentHeight,false);		elementContainer.add(el);		int lineCount = el.getFormatedText().size();//		Log.logInfo("("+getName()+")->("+topLevelTag.getName()+")==["+lastLineWidth+"]====["+txt+"]====["+baseLine+"]====["+lineCount+"]=====>"+pointerX);		if(lineCount==1)		{			// baseline is the same.			pointerX += lastLineWidth;			if(pointerX>=vw)			{// line break				lineBreak(topLevelTag.getLineHeight(),false);			}		}		else if(lineCount>1)		{			lineBreak((contentHeight-el.getLastLineHeight()),true);			pointerX = lastLineWidth;		}				el.setId(topLevelTag.tagId);				return;	}		public void handleComponent(Tag topLevelTag, Component primitive)	{		if(getBaseLine()==0) // first line of text		{ 			lineBreak(topLevelTag.font.getHeight(),true);		}		// add a text element 		int vw = containerWidth;		Component el = primitive;		el.validate();		int d[] = el.getPrefSize();		int w = d[0];		int h = d[1];				if(pointerX>0 && pointerX+w>vw)		{			lineBreak(topLevelTag.font.getHeight(),false);		}				// update other primitives on the same line to have the correct height.		updatePrimitivesInLineForPrimitiveHeight(h);		el.setX(pointerX); 		el.setY(getBaseLine()-h);		elementContainer.add(el);		// update the pointetX,pointerY.		pointerX += w;		if(pointerX>=vw)		{ // line break			lineBreak(topLevelTag.font.getHeight(),false);		}					el.setId(topLevelTag.tagId);				return;	}		private void updatePrimitivesInLineForPrimitiveHeight(int primitiveHeight)	{		int baseLine = getBaseLine();		int lineHeight = getLineHeight();		if(primitiveHeight>lineHeight) // there may be a component on my left that need rearangement		{ // the new lineHeight will be primitiveHeight and the base line will move lower.			for(int i=elementContainer.countComponents()-1;i>=0;--i) // yes there is at least one.			{				Component primitive = (Component)elementContainer.getComponent(i);								if((primitive.getY()+primitive.getContentHeight())!=baseLine) 				{ // done, this primitive ends above current location.					break;				}								if(primitive instanceof TextComponent)				{					TextComponent tp = (TextComponent)primitive;					int lastLineExtraHeight = tp.getLastLineExtraHeight() + (primitiveHeight-lineHeight);					tp.setLastLineExtraHeight(lastLineExtraHeight);					int []d = tp.getPrefSize();					tp.setPrefSize(d[0],d[1]+lastLineExtraHeight,false);				}				else if(primitive instanceof ImageComponent)				{					ImageComponent ip = (ImageComponent)primitive;					ip.setY(ip.getY()+ (primitiveHeight-lineHeight));				}			}			setBaseLine(baseLine +(primitiveHeight-lineHeight));			setLineHeight(primitiveHeight);		}			}		public int getContainerWidth()	{		return containerWidth;	}}

⌨️ 快捷键说明

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