📄 widgetinfo.java
字号:
/* * Copyright 2005-2007 * Wolfgang S. Kechel, data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.hecl.midp20.lcdui;import java.util.Hashtable;import java.util.Vector;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Choice;import javax.microedition.lcdui.ChoiceGroup;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.DateField;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Gauge;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.ImageItem;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.List;import javax.microedition.lcdui.Spacer;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.lcdui.Ticker;import org.hecl.HeclException;import org.hecl.IntThing;import org.hecl.ObjectThing;import org.hecl.Properties;import org.hecl.RealThing;import org.hecl.Thing;import org.hecl.misc.HeclUtils;public class WidgetInfo { public static Object asWidget(Thing thing,Class clazz, String clazzname, boolean allownull) throws HeclException { if(allownull && thing.toString().length() == 0) return null; RealThing rt = thing.getVal(); if(rt instanceof ObjectThing) { Object x = ((ObjectThing)rt).get(); if(allownull && x == null) return null; if(clazz.isAssignableFrom(x.getClass())) return x; } if(clazzname != null) { throw HeclException.createInvalidParameter( thing,"parameter",clazzname + " widget required."); } return null; } public static Command asCommand(Thing thing, boolean allownull,boolean throwerror) throws HeclException { return (Command)asWidget(thing, Command.class, throwerror? "Command" :null,allownull); } public static Gauge asGauge(Thing thing, boolean allownull,boolean throwerror) throws HeclException { return (Gauge)asWidget(thing, Gauge.class, throwerror ? "Gauge" : null,allownull); } public static Image asImage(Thing thing, boolean allownull,boolean throwerror) throws HeclException { return (Image)asWidget(thing, Image.class, throwerror ? "Image" : null,allownull); } public static Item asItem(Thing thing, boolean allownull,boolean throwerror) throws HeclException { return (Item)asWidget(thing, Item.class, throwerror ? "Item" : null,allownull); } public static Ticker asTicker(Thing thing, boolean allownull,boolean throwerror) throws HeclException { return (Ticker)asWidget(thing, Ticker.class, throwerror ? "Ticker" : null,allownull); } public static AlertType toAlertType(Thing t) throws HeclException { String s = t.toString().toLowerCase(); int l = alerttypenames.length; for(int i=0; i<l; ++i) if(s.equals(alerttypenames[i])) return alerttypevals[i]; throw new HeclException("Invalid alert type '"+s+"'!"); } public static Thing fromAlertType(AlertType t) throws HeclException { int l = alerttypenames.length; for(int i=0; i<l; ++i) if(t == alerttypevals[i]) return new Thing(alerttypenames[i]); throw new HeclException("Invalid alert type value '"+t+"'!"); } protected static int t2int(Thing t,String nametab[],int valtab[],String emsg) throws HeclException { return s2int(t.toString().toLowerCase(),nametab,valtab,emsg); } protected static int s2int(String s,String nametab[],int valtab[],String emsg) throws HeclException { int l = nametab.length; for(int i=0; i<l; ++i) if(s.equals(nametab[i])) return valtab[i]; throw new HeclException("Invalid " + emsg + " '" + s + "'."); } protected static Thing int2t(int v,String nametab[],int valtab[],String emsg) throws HeclException { return new Thing(int2s(v,nametab,valtab,emsg)); } protected static String int2s(int v,String nametab[],int valtab[],String emsg) throws HeclException { int l = valtab.length; for(int i=0; i<l; ++i) if(v == valtab[i]) return nametab[i]; throw new HeclException("Invalid " + emsg + " value '" + v + "'."); } public static int toColor(Thing t) throws HeclException { String s = t.toString(); try { return s2int(s.toLowerCase(),colornames,colorvals,""); } catch (Exception e) { } return Integer.parseInt(s,16); } public static Thing fromColor(org.awt.Color color) throws HeclException { return fromColor(color.getRGB()); } public static Thing fromColor(int t) throws HeclException { try { return int2t(t,colornames,colorvals,""); } catch (HeclException e) { } return new Thing(Integer.toHexString(t)); } public static int toCanvasAnchor(Thing t) throws HeclException { return t2int(t,canchornames,canchorvals,"anchor"); } public static Thing fromCanvasAnchor(int t) throws HeclException { return int2t(t,canchornames,canchorvals,"anchor"); } public static int toChoiceType(Thing t) throws HeclException { return t2int(t,choicetypenames,choicetypevals,"choice type"); } public static Thing fromChoiceType(int t) throws HeclException { return int2t(t,choicetypenames,choicetypevals,"choice type"); } public static int toListType(Thing t) throws HeclException { int i = toChoiceType(t); if(i == Choice.POPUP) { throw new HeclException("Invalid list type '"+t+"'!"); } return i; } public static Thing fromListType(int t) throws HeclException { try { if(t != Choice.POPUP) { return fromChoiceType(t); } } catch (HeclException e) { } throw new HeclException("Invalid list type value'"+t+"'!"); } public static int toTextType(Thing t) throws HeclException { return t2int(t,texttypenames,texttypevals,"text type"); } public static Thing fromTextType(int t) throws HeclException { return int2t(t & ~TextField.CONSTRAINT_MASK, texttypenames,texttypevals,"text type"); } public static int toWrap(Thing t) throws HeclException { return t2int(t,wrapnames,wrapvals,"wrap type"); } public static Thing fromWrap(int t) throws HeclException { return int2t(t,wrapnames,wrapvals,"wrap type"); } public static String commandLabel(Command c,boolean shortonly) { String l = shortonly ? null : c.getLongLabel(); if(l == null || l.length() == 0) { l = c.getLabel(); } if(l == null && l.length() == 0) {//#ifdef notdef // unfortunately there is no way to detect the command type :-( int t = c.getType(); for(int i=0; i<cmdlabels.length; ++i) { if(t == cmdtypes[i]) { l = cmdlabels[i]; break; } }//#endif l = "???"; } return l; } public static int toCommandType(Thing t) throws HeclException { return t2int(t,cmdtypenames,cmdtypevals,"command type"); } public static Thing fromCommandType(int t) throws HeclException { return int2t(t,cmdtypenames,cmdtypevals,"command type"); } public static int toFontFace(Thing t) throws HeclException { return t2int(t,fontfacenames,fontfacevals,"font face"); } public static int toFontFace(String s) throws HeclException { return s2int(s.toLowerCase(),fontfacenames,fontfacevals,"font face"); } public static Thing fromFontFace(int t) throws HeclException { return int2t(t,fontfacenames,fontfacevals,"font face"); } public static int toFontSize(Thing t) throws HeclException { return t2int(t,fontsizenames,fontsizevals,"font size"); } public static int toFontSize(String s) throws HeclException { return s2int(s.toLowerCase(),fontsizenames,fontsizevals,"font size"); } public static Thing fromFontSize(int t) throws HeclException { return int2t(t,fontsizenames,fontsizevals,"font size"); } public static int toItemAnchor(Thing t) throws HeclException { return t2int(t,anchornames,anchorvals,"anchor"); } public static Thing fromItemAnchor(int t) throws HeclException { return int2t(t &= 0x33,anchornames,anchorvals,"anchor"); } public static int toItemAppearance(Thing t) throws HeclException { return t2int(t,appearancenames,appearancevals,"appearance"); } public static Thing fromItemAppearance(int t) throws HeclException { return int2t(t &= 0x33,appearancenames,appearancevals,"appearance"); } public static int toDateFieldMode(Thing t) throws HeclException { return t2int(t,dfmodenames,dfmodevals,"date field mode"); } public static Thing fromDateFieldMode(int t) throws HeclException { return int2t(t &= 0x33,dfmodenames,dfmodevals,"date field mode"); } public static int toGaugeInitial(Thing t) throws HeclException { if(Character.isDigit(t.toString().charAt(0))) { return HeclUtils.thing2int(t,true,0); } return t2int(t,gaugeinitialnames,gaugeinitialvals,"gauge initval"); } public static Thing fromGaugeInitial(int t) throws HeclException { for(int i=0; i<gaugeinitialvals.length; ++i) { if(i == gaugeinitialvals[i]) return new Thing(gaugeinitialnames[i]); } return IntThing.create(t); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -