📄 inlinetag.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.Command;import gr.fire.browser.util.Form;import gr.fire.browser.util.Page;import gr.fire.browser.util.StyleSheet;import gr.fire.core.FireScreen;import gr.fire.core.Theme;import gr.fire.ui.ImageComponent;import gr.fire.ui.InputComponent;import gr.fire.util.Log;import gr.fire.util.StringUtil;import java.util.Vector;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.TextField;import org.kxml2.io.KXmlParser;/** * @author padeler * */public class InlineTag extends Tag{ public static final String TAG_A = "a"; public static final String TAG_BR = "br"; public static final String TAG_TD = "td"; public static final String TAG_IMG = "img"; public static final String TAG_SPAN = "span"; public static final String TAG_B = "b"; public static final String TAG_STRONG = "strong"; public static final String TAG_I = "i"; public static final String TAG_EM = "em"; public static final String TAG_BIG = "big"; public static final String TAG_SMALL= "small"; public static final String TAG_TT= "tt"; public static final String TAG_U= "u"; public static final String TAG_CENTER= "center"; public static final String TAG_INPUT = "input"; public static final String TAG_LABEL= "label"; public static final String TAG_SELECT = "select"; public static final String TAG_OPTION = "option"; public static final String TAG_BUTTON = "button"; public static final String TAG_TEXTAREA= "textarea"; private String name=TAG_NOTSET; public InlineTag() { } public String getName() { return name; } public void inherit(Tag parent) { if(parent!=null) { inheritStyle(parent); // inherit style information if(parent instanceof BlockTag) this.parentBlockTag = (BlockTag)parent; else this.parentBlockTag = parent.parentBlockTag; } } public void handleTagStart(Browser browser,Page page, KXmlParser parser) { name = parser.getName().toLowerCase(); // the name of the tag Tag parentElement = browser.topTag(); // may be a block element or an inline element inherit(parentElement); // first inherit from parents handleCommonAttributes(parser); // then get extra style from attributes if(TAG_A.equals(name)) { // this is a link. String hrefLocation = parser.getAttributeValue(null,"href"); if(hrefLocation!=null) { Theme th = FireScreen.getTheme(); if(foregroundColor==th.getIntProperty("xhtml.fg.color")) // no change from style attribute, hack until DOM is implemented. foregroundColor = th.getIntProperty("link.fg.color"); Font linkFont = th.getFontProperty("link.font"); try{ font = Font.getFont(font.getFace()|linkFont.getFace(),font.getStyle()|linkFont.getStyle(),font.getSize()); }catch(Exception e)// failed to inherit font attributes. {font = linkFont;} listener = browser.listener; href = new Command("Link",Command.OK,1,hrefLocation); } return; } if(TAG_IMG.equals(name)) { String imgLocation = parser.getAttributeValue(null,"src"); String alt = parser.getAttributeValue(null,"alt"); if(imgLocation!=null && parentBlockTag!=null) { int width= StyleSheet.calculateLenght(parser.getAttributeValue(null,"width"),parentBlockTag.containerWidth); int height=StyleSheet.calculateLenght(parser.getAttributeValue(null,"height"),FireScreen.getScreen().getHeight()); Log.logDebug("Image Element ["+imgLocation+"]/["+alt+"]: "+width+","+height); Image image = page.getCachedImage(imgLocation); ImageComponent primitive= new ImageComponent(image,width,height,this.font,alt); if(image==null && browser.imageLoadingPolicy!=Browser.NO_IMAGES) { if(browser.imageLoadingPolicy==Browser.LOAD_IMAGES_ASYNC && width!=-1 && height!=-1) { page.registerAsyncImageRequest(primitive,imgLocation); } else // LOAD_IMAGES or width,height are not preset. { image = page.loadImage(browser.httpClient,imgLocation); if(image!=null) primitive.setImage(image); } } copyStyle(primitive); parentBlockTag.handleComponent(this, primitive); } return; } if(TAG_BR.equals(name)) { if(parentBlockTag!=null) { parentBlockTag.lineBreak(parentBlockTag.font.getHeight(),true); } return; } if(TAG_SPAN.equals(name) || TAG_TD.equals(name)) { handleCommonAttributes(parser); // then get extra style from attributes handleAlignAttributes(parser); return; } if(TAG_B.equals(name) || TAG_STRONG.equals(name)) { /* * This is a B Inline Element. * As an inline Element i am inside a container. */ font = Font.getFont(font.getFace(),font.getStyle()|Font.STYLE_BOLD,font.getSize()); return; } if(TAG_I.equals(name) || TAG_EM.equals(name)) { /* * This is a I Inline Element. * As an inline Element i am inside a container. */ font = Font.getFont(font.getFace(),font.getStyle()|Font.STYLE_ITALIC,font.getSize()); return; } if(TAG_LABEL.equals(name)) { // nothing to do for labels (ignore "for" attribute until proper DOM is implemented). return; } if(TAG_INPUT.equals(name) || TAG_BUTTON.equals(name)) { Form openForm = page.getOpenForm(); if(openForm==null || parentBlockTag==null) // ignore input elements outside forms { return; } handleInputComponent(openForm,parser); return; } if(TAG_SELECT.equals(name)) { Form openForm = page.getOpenForm(); if(openForm==null || parentBlockTag==null) // ignore input elements outside forms { return; } boolean multiple = parser.getAttributeValue(null,"multiple")!=null; boolean enabled = (parser.getAttributeValue(null,"disabled")==null); String name = parser.getAttributeValue(null,"name"); gr.fire.browser.util.Command menuCommand = new gr.fire.browser.util.Command(name); menuCommand.setEnabled(enabled); menuCommand.setMultiple(multiple); String size = parser.getAttributeValue(null,"size"); if(size!=null) { try{ menuCommand.setSize(Integer.parseInt(size)); }catch(NumberFormatException e) { Log.logWarn("Failed to parse size attribute",e); } } openForm.setMenuCommand(menuCommand); return; } if(TAG_OPTION.equals(name)) { Form openForm = page.getOpenForm(); if(openForm==null || parentBlockTag==null) // ignore input elements outside forms { return; } gr.fire.browser.util.Command menu = openForm.getMenuCommand(); String name=null; if(menu!=null) name = menu.getName(); boolean enabled = (parser.getAttributeValue(null,"disabled")==null); boolean checked = (parser.getAttributeValue(null,"selected")!=null); String value= parser.getAttributeValue(null,"value"); // get the text of the option element int type; String text= ""; try { type = parser.next(); if(type==KXmlParser.TEXT) // text of option element { text = parser.getText(); parser.next(); //progress the parsing once more since TEXT was handled here. } } catch (Exception e) { Log.logWarn("Failed to get text for tag "+name+".",e); return; } InputComponent option = new InputComponent(InputComponent.SWITCH); option.setLayout(FireScreen.VCENTER|FireScreen.CENTER); option.setChecked(checked); option.setEnabled(enabled); option.setName(name); option.setValue(value); option.setInitialValue(checked?"":null); option.setText(text); option.setMaxWidth(parentBlockTag.getContainerWidth()); copyStyle(option); int []minSize = option.getMinSize(); option.setPrefSize(minSize[0],minSize[1]); openForm.addInputComponent(option); return; } if(TAG_TEXTAREA.equals(name)) { Form openForm = page.getOpenForm(); if(openForm==null || parentBlockTag==null) // ignore input elements outside forms { return; } String name = parser.getAttributeValue(null,"name"); boolean enabled = (parser.getAttributeValue(null,"disabled")==null) && (parser.getAttributeValue(null,"readonly")==null); String rowsStr = parser.getAttributeValue(null,"rows"); String colsStr = parser.getAttributeValue(null,"cols"); String sizeStr = parser.getAttributeValue(null,"size"); // get the text of this area int type; String value= ""; try { type = parser.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -