📄 sdabaseedit.java
字号:
package cn.sda.ui;import cn.sda.event.BaseEditTextChangeEvent;import cn.sda.event.KeybordEvent;import cn.sda.event.NotifyEvent;import cn.sda.event.PointerEvent;import cn.sda.pinyin.InputInterface;import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Font;/** * @author not attributable * @version 1.0 */public class SDABaseEdit extends SDABaseControl implements InputInterface { //边框 private int borderStyle = SDAConsts.bsNone; //是不是显示焦点框 private boolean showFocusRect = false; //隐藏选定内容 private boolean hideSelection = true; //最大长度 private int maxLength = 32767; //是否显示多行 private boolean multiLine = false; //密码 private char passwordChar = 0; //只读 protected boolean readOnly = false; //滚动边框(多行) private int scrollBars = SDAConsts.srNone; //文字对齐方式 private int textAlign = SDAConsts.alignLeft; //是否自动换行(多行) private boolean wordWrap = true; private int barwidth = 10; //边框线颜色 private int borderColor = SDAConsts.clBlack; //滚动条颜色 private int scrollBarColor = SDAConsts.clBtnFace; //内部变量 //行数,行最大长度 private int lineNum = 0; private int maxLineLenght = 0; //垂直开始显示行 private int startLine = 0; //水平开始显示位置 private int startLeft = 0; //变动前位置 private int oldStartLine = 0; private int oldStartLeft = 0; //行数 private int lineNo = 0; //水平滚条位置 private int HSLeft = 0; private int HSTop = 0; private int HSWidth = 0; private int HSHeight = 0; //垂直滚动条位置 private int VSLeft = 0; private int VSTop = 0; private int VSWidth = 0; private int VSHeight = 0; //点击的滚动条的位置 private int oldScrollPointx = 0; private int oldScrollPointy = 0; //按下滚动条 private boolean isscrollbarpointdown = false; //记录按下的滚动条0:水平,1:垂直的 private byte scrollbardownHV = 0; //当前光标位置 private int cursorPos = 0; //光标行列 private int cursorRow = 0; protected int cursorCol = 0; //光标绝对位置 private int cursorX = 0; private int cursorY = 0; //选择行的起始位置和内容 private int cursorLineStartPos = 0; private String cursorLineText = ""; //前一行长度 private int priorLineLen = 0; //临时buffer StringBuffer strBuffer = new StringBuffer(); //对齐方式 private int oldAlignType = SDAConsts.alignLeft; //输入法 private int imeType = SDAInputPanel.imNone; //文字变动 private BaseEditTextChangeEvent onTextChange = null; // Graphics gg = null; protected SDABaseEdit() { super(); internalSDAEdit(); } private void internalSDAEdit() { setVisible(true); setLeft(0); setTop(0); setBorderStyle(SDAConsts.bsFixedSingle); super.setWidth(60); //高度根据字体高度来 setHeight(getFont().getHeight() + 4); super.setBackColor(SDAConsts.clWhite); onPointerPressed = new PointerEvent() { public void Event(SDABaseControl ctrl, int x, int y) { doPointerPressed(x, y); } }; onPointerReleased = new PointerEvent() { public void Event(SDABaseControl ctrl, int x, int y) { doPointerReleased(x, y); } }; onKeyUp = new KeybordEvent() { public void Event(SDABaseControl ctrl, int keyCode) { doKeyUp(keyCode); } }; onInternalLostFocused = new NotifyEvent() { public void Event(SDABaseControl ctrl) { doLostFocus(); } }; onInternalFocused = new NotifyEvent() { public void Event(SDABaseControl ctrl) { doGetFocus(); } }; //设置系统菜单 /* systemMenu=new SDAPopMenu(); systemMenu.AddItem("back", "退格"); systemMenu.AddItem("delete", "删除"); systemMenu.AddItem("enter", "插入回车"); systemMenu.AddItem("", "-"); systemMenu.AddItem("ABC", "大写字母").setRadioItem(true); systemMenu.AddItem("abc", "小写字母").setRadioItem(true); systemMenu.AddItem("123", "数字").setRadioItem(true); systemMenu.AddItem("拼音", "中文拼音").setRadioItem(true); systemMenu.AddItem("标点", "插入标点").setRadioItem(true); systemMenu.AddItem("符号", "插入符号").setRadioItem(true); systemMenu.AddItem("", "-"); systemMenu.AddItem("全", "全角"); systemMenu.setOnClick(new PopMenuOnClickEvent() { public void Event(SDAMenuItem item, String itemName, String itemCaption) { //处理 if(itemName.equals("back")){ char ch=8; inputFuncChar(ch, false); } if(itemName.equals("delete")){ char ch=46; inputFuncChar(ch, false); } if(itemName.equals("enter")){ char ch=13; inputFuncChar(ch, false); } if (itemName.equals("ABC")) { form.Application.inputPanel.setImeType(SDAInputPanel.imUpperCase); } if (itemName.equals("abc")) { form.Application.inputPanel.setImeType(SDAInputPanel.imLowerCase); } if (itemName.equals("123")) { form.Application.inputPanel.setImeType(SDAInputPanel.imDigit); } if (itemName.equals("拼音")) { form.Application.inputPanel.setImeType(SDAInputPanel.imPinYin); } if (itemName.equals("标点")) { form.Application.inputPanel.setImeType(SDAInputPanel.imPoint); SDASelectPointForm.getInstance().open(getSelf()); } if (itemName.equals("符号")) { form.Application.inputPanel.setImeType(SDAInputPanel.imSymbol); SDASelectSymbolForm.getInstance().open(getSelf()); } if (itemName.equals("全")) { form.Application.inputPanel.setSBCCase(!form.Application.inputPanel.isSBCCase()); item.setChecked(form.Application.inputPanel.isSBCCase()); } else { item.setChecked(true); } } }); */ } public void paint() { internalPaint(); } private void internalPaint() { if (!IsCanPaint()) { return; } Graphics g = form.getGraphics(); gg = g; //设置字体 g.setFont(getFont()); gg.setFont(getFont()); //可视下才画 if (isVisible()) { //初始化引用变量 int thisWidth = width; int thisHeight = height; int HBarWidth = getHBarWidth(); int VBarHeight = getVBarHeight(); int fontHeight = getFont().getHeight(); //对齐方式处理后,画组件 //外框 SetClip(g); //透明 if (!isTransparent()) { g.setColor(backColor); fillRect(g, 0, 0, thisWidth, thisHeight); } if (borderStyle == SDAConsts.bsFixed3D) { g.setColor(SDAConsts.clGray); drawLine(g, 0, 0, thisWidth, 0); drawLine(g, 0, 0, 0, thisHeight); g.setColor(SDAConsts.clBlack); drawLine(g, 1, 1, thisWidth - 1, 1); drawLine(g, 1, 1, 1, thisHeight - 1); g.setColor(SDAConsts.clBtnFace); drawLine(g, 2, thisHeight - 1, thisWidth - 2, thisHeight - 1); drawLine(g, thisWidth - 1, 2, thisWidth - 1, thisHeight - 1); g.setColor(SDAConsts.clWhite); drawLine(g, 1, thisHeight, thisWidth, thisHeight); drawLine(g, thisWidth, 1, thisWidth, thisHeight); if (isFoucsed() && showFocusRect) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } } if (borderStyle == SDAConsts.bsFixedSingle) { //边框 g.setColor(getBorderColor()); drawRect(g, 0, 0, thisWidth, thisHeight); if (isFoucsed() && showFocusRect) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } } //打字 g.setColor(getForeColor()); //文字对齐方式 if (!isMultiLine()) { if (textAlign == SDAConsts.alignLeft) { paintString(g, getText(), 2, (height - fontHeight) / 2); } if (textAlign == SDAConsts.alignCenter) { //文字长度 int Len = getFont().stringWidth(getText()); int sleft = (thisWidth - Len) / 2; paintString(g, getText(), sleft, (height - fontHeight) / 2); } if (textAlign == SDAConsts.alignRight) { //文字长度 int Len = getFont().stringWidth(getText()); int sleft = (thisWidth - Len) - 1; paintString(g, getText(), sleft, (height - fontHeight) / 2); } } else { paintString(g, getText(), 2, (height - fontHeight) / 2); } //多行滚动条 if (borderStyle == SDAConsts.bsFixed3D) { SetClip(g, 2, 2, thisWidth - 2, height - 2); } else { SetClip(g, 1, 1, thisWidth - 1, height - 1); } if (isMultiLine()) { if ((scrollBars == SDAConsts.srHorizontal) || (scrollBars == SDAConsts.srBoth)) { //水平 g.setColor(getScrollBarColor()); fillRect(g, 0, height - barwidth, barwidth, barwidth); fillRect(g, HBarWidth - barwidth, height - barwidth, barwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, 0, thisHeight - barwidth, barwidth, barwidth); drawRect(g, HBarWidth - barwidth, height - barwidth, barwidth, barwidth); //滚动块 g.setColor(backColor); fillRect(g, barwidth, height - barwidth, HBarWidth - 2 * barwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, barwidth, thisHeight - barwidth, HBarWidth - 2 * barwidth, barwidth); //块大小 int pwidth = HBarWidth - 2 * barwidth; if (!isWordWrap()) { if (maxLineLenght > HBarWidth) { pwidth = (HBarWidth * (HBarWidth - 2 * barwidth)) / maxLineLenght; } } //计算块位置 int ppos = (startLeft * (HBarWidth - 2 * barwidth)) / maxLineLenght; //修正位置 if (ppos + pwidth > HBarWidth - 2 * barwidth) { ppos = HBarWidth - 2 * barwidth - pwidth; } //画块 g.setColor(getScrollBarColor()); fillRect(g, barwidth + ppos, thisHeight - barwidth, pwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, barwidth + ppos, thisHeight - barwidth, pwidth, barwidth); //记录大小 HSLeft = barwidth + ppos; HSTop = thisHeight - barwidth; HSWidth = pwidth; HSHeight = barwidth; //画块上的线 int tpos = HSWidth / 2 + HSLeft; drawRect(g, tpos, HSTop + 2, 0, HSHeight - 4); if (tpos - 2 > HSLeft) { drawRect(g, tpos - 2, HSTop + 2, 0, HSHeight - 4); } if (tpos + 2 < HSLeft + HSWidth) { drawRect(g, tpos + 2, HSTop + 2, 0, HSHeight - 4); } //三角 g.setColor(getBorderColor()); fillTriangle(g, barwidth / 2 - 2, thisHeight - barwidth / 2, barwidth / 2 + 2, thisHeight - barwidth / 2 - 4, barwidth / 2 + 2, thisHeight - barwidth / 2 + 4); fillTriangle(g, HBarWidth - barwidth + barwidth / 2 + 2, thisHeight - barwidth / 2, HBarWidth - barwidth + barwidth / 2 - 2, thisHeight - barwidth / 2 - 4, HBarWidth - barwidth + barwidth / 2 - 2, thisHeight - barwidth / 2 + 4); } if ((scrollBars == SDAConsts.srVertical) || (scrollBars == SDAConsts.srBoth)) { //垂直 g.setColor(getScrollBarColor()); fillRect(g, thisWidth - barwidth, 0, barwidth, barwidth); fillRect(g, thisWidth - barwidth, VBarHeight - barwidth, barwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, thisWidth - barwidth, 0, barwidth, barwidth); drawRect(g, thisWidth - barwidth, VBarHeight - barwidth, barwidth, barwidth); //滚动块 g.setColor(backColor); fillRect(g, thisWidth - barwidth, barwidth, barwidth, VBarHeight - 2 * barwidth); g.setColor(borderColor); drawRect(g, thisWidth - barwidth, barwidth, barwidth, VBarHeight - 2 * barwidth); //块大小 int pheight = VBarHeight - 2 * barwidth; if (lineNum * getFont().getHeight() > VBarHeight) { pheight = (VBarHeight * (VBarHeight - 2 * barwidth) / (lineNum * getFont().getHeight())); } //计算块位置 int ppos = barwidth; if (lineNum > 0) { ppos = (startLine * getFont().getHeight() * (VBarHeight - 2 * barwidth)) / (lineNum * getFont().getHeight()); } //修正位置 if (ppos + pheight > VBarHeight - 2 * barwidth) { ppos = VBarHeight - 2 * barwidth - pheight; } //画块 g.setColor(getScrollBarColor());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -