📄 blocktag.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.Page;import gr.fire.core.Component;import gr.fire.core.Container;import gr.fire.core.FireScreen;import gr.fire.ui.ImageComponent;import gr.fire.ui.TextComponent;import gr.fire.util.Log;import gr.fire.util.StringUtil;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import org.kxml2.io.KXmlParser;/** * @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_TR = "tr"; 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; protected int containerWidth; // the width of this container. It is either inherited by the parent or set by attributes public BlockTag() { elementContainer = new Container(); font = FireScreen.getTheme().getFontProperty("font"); } 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,Page page, KXmlParser 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 if(parentBlockTag!=null) parentBlockTag.lineBreak(parentBlockTag.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); } else if(TAG_FORM.equals(name)) { inherit(parentElement,true); // first inherit from parents handleCommonAttributes(parser); // then get extra style from attributes lineBreak(font.getHeight(),false); String action = parser.getAttributeValue(null,"action"); String method = parser.getAttributeValue(null,"method"); String enctype = parser.getAttributeValue(null,"enctype"); if(method==null || method.toLowerCase().toLowerCase().equals("get")) method = HttpConnection.GET; else method = HttpConnection.POST; Form newForm = new Form(browser,action,method,enctype); if(browser.listener!=null) newForm.setFormListener(browser.listener); // set the new form to the parent. page.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(),false); // elementContainer.setLayoutManager(new BoxLayout(BoxLayout.Y_AXIS)); } else if(TAG_TR.equals(name)) { inherit(parentElement,true); // first inherit from parents handleCommonAttributes(parser); // then get extra style from attributes handleAlignAttributes(parser); lineBreak(font.getHeight(),true); // elementContainer.setLayoutManager(new BoxLayout(BoxLayout.X_AXIS)); } 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 if(parentBlockTag!=null) parentBlockTag.lineBreak(parentBlockTag.font.getHeight(),false); lineBreak(font.getHeight()/2,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(),false); copyStyle(elementContainer); elementContainer.setId(tagId); //elementContainer.setBackgroundColor(Component.); // overide default transparent color. (should change when default CSS is implemented) page.setPageContainer(elementContainer); return; } else if(TAG_SCRIPT.equals(name) || TAG_STYLE.equals(name)) { // consume Log.logDebug("<"+name+">"); //Log.logDebug(parser.getText()); Log.logDebug(".....Skipped....."); Log.logDebug("</"+name+">"); return; } else if(TAG_META.equals(name)) { page.parseMetaTag(parser); return; } else if(TAG_TITLE.equals(name)) { try{ int type = parser.next(); if(type==KXmlParser.TEXT) { page.setPageTitle(parser.getText().trim()); parser.next(); // progress the parsing once more since TEXT was handled here. } }catch(Exception e){ Log.logWarn("Failed to parse page title.",e); } return; } copyStyle(elementContainer); elementContainer.setId(tagId); } public void handleTagEnd(Browser browser,Page page, KXmlParser parser) { String name = getName(); if(TAG_P.equals(name)) { int baseLine =getBaseLine(); /* * This the end of a P Element.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -