📄 form.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.util;import gr.fire.browser.Browser;import gr.fire.core.BoxLayout;import gr.fire.core.CommandListener;import gr.fire.core.Component;import gr.fire.core.Container;import gr.fire.core.FireScreen;import gr.fire.core.KeyListener;import gr.fire.core.Panel;import gr.fire.ui.InputComponent;import gr.fire.ui.TextArea;import gr.fire.ui.TransitionAnimation;import gr.fire.util.Log;import gr.fire.util.StringUtil;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Hashtable;import java.util.Vector;import javax.microedition.io.HttpConnection;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Displayable;/** * * @author padeler * */public class Form implements CommandListener,KeyListener{ private Vector primitivesVector; private String method="get",action,enctype="application/x-www-form-urlencoded"; private Browser browser; private Command formCommand; private MenuCommand menuCommand =null; private Panel popupMenu=null; public Form(Browser browser, String action, String method,String enctype) { this.browser = browser; this.action=action; this.method=method; this.formCommand = new Command("",Command.OK,1); if(enctype!=null) this.enctype=enctype; primitivesVector = new Vector(); } public void addInputComponent(InputComponent p) { if(p.getType()!=InputComponent.HIDDEN) { // an input primitive intercepts events if it is not HIDDEN if(menuCommand!=null) p.setCommand(menuCommand); else p.setCommand(formCommand); p.setCommandListener(this); } primitivesVector.addElement(p); } public void submit(InputComponent source) { // first generate a list of all the parameters. String params=""; for(int i=0;i<primitivesVector.size();++i) { InputComponent p = ((InputComponent)primitivesVector.elementAt(i)); String name = p.getName(); if(name==null) continue; // ignore null named input fields int type = p.getType(); if((type==InputComponent.RADIO || type==InputComponent.CHECKBOX || type==InputComponent.SWITCH) && p.isChecked()==false) continue; // ignore unchecked radio and checkboxes if((type==InputComponent.SUBMIT || type==InputComponent.RESET || type==InputComponent.MENU) && source!=p) continue;// ignore buttons (reset, submit) except the one that submited the form String value = p.getValue(); if(value==null) value=name; params+= StringUtil.urlEncode(name)+"="+StringUtil.urlEncode(value) + "&"; } byte []data =null; Hashtable reqParams = null; if(params.length()>0) { if(method.equals(HttpConnection.POST)) { data = params.substring(0,params.length()-1).getBytes(); reqParams = new Hashtable(); reqParams.put("content-type",enctype); } else action += "?"+(params.substring(0,params.length()-1)); } Log.logInfo("Submit of Form ["+method+"]: "+action); Log.logInfo("Parameters: "+params); // ok now send the request to the browser. browser.displayPage(action,method,reqParams,data); } public void reset() { for(int i=0;i<primitivesVector.size();++i) { ((InputComponent)primitivesVector.elementAt(i)).reset(); } } private void handleMenuCommand(MenuCommand menu,InputComponent src) { String name = menu.getName(); if(name==null) // ignore... return; if(src.getType()==InputComponent.MENU) { // create a panel with the items of this menu and display it Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS)); int panelWidth=30; int panelHeight=0; for(int i=0;i<primitivesVector.size();++i) { InputComponent p = (InputComponent)primitivesVector.elementAt(i); if(p!=src && name.equals(p.getName())) { // add to panel int[] ms = p.getMinSize(); panelHeight += ms[1]; if(ms[0]>panelWidth) panelWidth=ms[0]; cnt.add(p); } } popupMenu = new Panel(cnt,Panel.VERTICAL_SCROLLBAR,false); cnt.setKeyListener(this); // find the actual position on the screen of the menu int x=0,y=0; Component c = src; while(c!=null) { x += c.getX();y +=c.getY(); c = c.getParent(); } popupMenu.setX(x); popupMenu.setY(y); int tmp = (src.getPrefSize()[1]*menu.getSize()); if(panelHeight>tmp) panelHeight = tmp; popupMenu.setPrefSize(panelWidth,panelHeight,false); FireScreen.getScreen().addComponent(popupMenu); } else if(src.getType()==InputComponent.SWITCH) // event from inside the popup menu { if(menu.isMultiple()) { src.setChecked(!src.isChecked()); src.repaint(); } else // diselect all other elements and close popup { InputComponent menuSwitch =null; for(int i=0;i<primitivesVector.size();++i) { InputComponent p = (InputComponent)primitivesVector.elementAt(i); if(name.equals(p.getName()) && p!=src) { if(p.isChecked()) { p.setChecked(false); } if(p.getType()==InputComponent.MENU) menuSwitch = p; } } src.setChecked(true); if(popupMenu!=null) { FireScreen.getScreen().removeComponent(popupMenu); popupMenu=null; } if(menuSwitch!=null) // update the menu button text. { menuSwitch.setText(src.getText()); menuSwitch.repaint(); } } } } public void commandAction(Command cmd, Component cmp) { InputComponent src = (InputComponent)cmp; if(cmd instanceof MenuCommand) { MenuCommand menu = (MenuCommand)cmd; handleMenuCommand(menu,src); return; } byte type = src.getType(); if(type==InputComponent.TEXT) { TextArea ta = new TextArea(src); FireScreen.getScreen().setCurrent(ta); return; } if(type==InputComponent.SUBMIT) { submit(src); return; } if(type==InputComponent.RESET) { reset(); return; } if(type==InputComponent.CHECKBOX || type==InputComponent.SWITCH) { src.setChecked(!src.isChecked()); src.repaint(); return; } if(type==InputComponent.RADIO) { if(!src.isChecked()) { String name = src.getName(); if(name!=null) { // find all radioboxes in the form, with the same name, and deselect anyone that is selected. for(int i=0;i<primitivesVector.size();++i) { InputComponent prim = (InputComponent)primitivesVector.elementAt(i); if(prim.getType()==InputComponent.RADIO && name.equals(prim.getName()) && prim.isChecked()) { // deselect this one prim.setChecked(false); prim.repaint(); } } } src.setChecked(true); src.repaint(); } return; } } public void commandAction(Command arg0, Displayable arg1) { } public MenuCommand getMenuCommand() { return menuCommand; } public void setMenuCommand(MenuCommand newMenu) { this.menuCommand = newMenu; } public void keyPressed(int code, Component src) { if(popupMenu!=null) { int ga = FireScreen.getScreen().getGameAction(code); if(ga==Canvas.LEFT || ga==Canvas.RIGHT) // close the popup { FireScreen.getScreen().removeComponent(popupMenu); popupMenu=null; } } } public void keyReleased(int code, Component src) { } public void keyRepeated(int code, Component src) { // TODO Auto-generated method stub } public Vector getPrimitivesVector() { return primitivesVector; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -