📄 sdabasecontrol.java
字号:
package cn.sda.ui;
import java.util.*;
import javax.microedition.lcdui.*;
import cn.sda.event.*;
/**
* @author not attributable
* @version 1.0
*/
public abstract class SDABaseControl {
protected SDABaseControl parent = null;
protected SDAForm form;
protected Vector ctrlList = new Vector();
protected String name;
protected boolean tabStop = true;
protected boolean down;
protected int left;
protected int top;
protected int width;
protected int height;
private int originLeft, originTop;
protected int foreColor;
protected int backColor;
protected boolean visible;
protected boolean enabled;
public boolean ctl3d;
protected String text;
protected int fontFace, fontStyle, fontSize;
protected boolean parentFont;
protected boolean transparent;
protected int alignment;
//对齐方式
protected int dock = SDAConsts.dsNone;
//event
protected NotifyEvent onClick;
protected KeybordEvent onKeyDown;
protected KeybordEvent onKeyUp;
protected KeybordEvent onKeyPress;
protected PointerEvent onPointerPressed;
protected PointerEvent onPointerReleased;
protected PointerEvent onPointerDragged;
protected NotifyEvent onFocused, onInternalFocused;
protected NotifyEvent onLostFocused, onInternalLostFocused;
//对齐方式的设置的记录列表
private Vector AlignmentList = null;
private class Align {
public SDABaseControl referControl = null;
public SDABaseControl[] arrayControl = null;
public int alignType = -1;
public int paramValue = 0;
}
public SDABaseControl() {
this.name = "";
this.onClick = null;
this.onKeyDown = null;
this.onKeyUp = null;
this.onKeyPress = null;
this.onPointerPressed = null;
this.onPointerReleased = null;
this.onPointerPressed = null;
this.left = 0;
this.top = 0;
this.originLeft = 0;
this.originTop = 0;
this.visible = true;
this.alignment = SDAConsts.alignCenter;
this.ctl3d = false;
this.enabled = true;
this.tabStop = true;
this.backColor = SDAConsts.clBtnFace;
this.foreColor = SDAConsts.clBlack;
this.parentFont = true;
this.transparent = false;
this.text = "";
this.fontFace = Font.getDefaultFont().getFace();
this.fontStyle = Font.getDefaultFont().getStyle();
//this.fontSize = Font.getDefaultFont().getSize();
this.fontSize = Font.SIZE_SMALL;
//对齐管理
AlignmentList = new Vector();
}
protected boolean canLeftTabPrior() {
return true;
}
protected boolean canDownTabNext() {
return true;
}
protected boolean canUpTabPrior() {
return true;
}
protected boolean canRightTabNext() {
return true;
}
public final void SetClip(Graphics g) {
if (parent != null) {
int pleft = parent.originLeft;
int ptop = parent.originTop;
int pright = pleft + parent.width + 1;
int pbottom = ptop + parent.height + 1;
pleft = pleft > this.originLeft ? pleft : this.originLeft;
ptop = ptop > this.originTop ? ptop : this.originTop;
pright = pright < this.originLeft + this.width ? pright : this.originLeft + this.width;
pbottom = pbottom < this.originTop + this.height ? pbottom : this.originTop + this.height;
g.setClip(pleft, ptop, pright - pleft + 1, pbottom - ptop + 1);
} else {
g.setClip(this.originLeft, this.originTop, this.width + 1,
this.height + 1);
}
}
public final void SetClip(Graphics g, int x, int y, int width, int height) {
if (parent != null) {
int pleft = parent.originLeft;
int ptop = parent.originTop;
int pright = pleft + parent.width + 1;
int pbottom = ptop + parent.height + 1;
pleft = pleft > originLeft + x ? pleft : originLeft + x;
ptop = ptop > originTop + y ? ptop : originTop + y;
pright = pright < originLeft + x + width ? pright : originLeft + width + x;
pbottom = pbottom < originTop + y + height ? pbottom : originTop + height + y;
g.setClip(pleft, ptop, pright - pleft, pbottom - ptop);
} else {
g.setClip(this.originLeft + x, this.originTop + y, width, height);
}
}
public void Click() {
if (this.onClick != null) {
this.onClick.Event(this);
}
}
protected final int calcTextHeight() {
return getFont().getHeight();
}
protected final int calcTextWidth() {
if (this.text != null) {
char[] txtarr = this.text.toCharArray();
return getFont().charsWidth(txtarr, 0, txtarr.length);
}
return this.width;
}
protected void DrawText(Graphics g, int LeftSpace, int RightSpace,
int TopSpace, int BottomSpace) {
int dtX = this.originLeft + 1 + LeftSpace;
int dtY = this.originTop + 1;
int clientWidth = width - 2 - LeftSpace - RightSpace;
int clientHeight = height - 2;
String drawText = text;
char[] texts = drawText.toCharArray();
int textWidth = getFont().charsWidth(texts, 0, texts.length);
int textHeight = getFont().getHeight();
if (textWidth < clientWidth) {
if (this.alignment == SDAConsts.alignCenter) {
dtX += (clientWidth - textWidth) / 2;
}
if (this.alignment == SDAConsts.alignRight) {
dtX += (clientWidth - textWidth);
}
} else {
textWidth = clientWidth;
}
if (textHeight < clientHeight) {
dtY += (clientHeight - textHeight) / 2;
}
dtY = dtY + TopSpace - BottomSpace;
g.setClip(this.originLeft + 1 + LeftSpace, this.originTop + 1,
clientWidth,
height - 2);
g.setColor(getForeColor());
g.setFont(getFont());
g.drawString(drawText, dtX, dtY, 0);
}
protected void DrawText(Graphics g, int LeftSpace, int RightSpace) {
DrawText(g, LeftSpace, RightSpace, 0, 0);
}
protected void DrawText(Graphics g) {
DrawText(g, 0, 0);
}
protected void serviceRepaints() {
if (IsCanPaint() && this.form.Application.isDoubleBuffered()) {
form.canvas.repaint();
form.canvas.serviceRepaints();
}
}
protected void repaintControl() {
if (IsCanPaint()) {
if (isTransparent() && parent != null) {
parent.paint();
} else {
paint();
}
serviceRepaints();
}
}
public final Vector FindControls(Class AClass) {
Vector result = new Vector();
for (int i = ctrlList.size() - 1; i >= 0; i--) {
SDABaseControl ctrl = (SDABaseControl) ctrlList.elementAt(i);
if (ctrl.getClass() == AClass) {
result.addElement(ctrl);
}
}
return result;
}
public final Vector FindControls(Class AClass, String CtrlName) {
Vector result = new Vector();
for (int i = ctrlList.size() - 1; i >= 0; i--) {
SDABaseControl ctrl = (SDABaseControl) ctrlList.elementAt(i);
if (ctrl.getClass() == AClass && ctrl.getName().equals(CtrlName)) {
result.addElement(ctrl);
}
}
return result;
}
protected final SDABaseControl FindControl(int x, int y) {
if (!this.visible || !this.enabled) {
return null;
}
SDABaseControl result = this;
for (int i = ctrlList.size() - 1; i >= 0; i--) {
SDABaseControl ctrl = (SDABaseControl) ctrlList.elementAt(i);
if (ctrl.visible && ctrl.enabled) {
if (ctrl.InScreenRect(x, y)) {
result = ctrl.FindControl(x, y);
if (result != null) {
break;
}
}
}
}
/*if (result != null) {
System.out.println(result.getClass().getName() + ".findControl(" +
x + "," + y + ")");
}*/
if (result instanceof SDAForm) {
result = null;
}
return result;
}
protected final boolean IsCanPaint() {
boolean parentVisable = false;
if (this.visible && this.form != null && this.form.canvas != null &&
this.form.graphics != null) {
SDABaseControl ctrl = this;
parentVisable = true;
while (ctrl.parent != null) {
parentVisable = this.parent.visible;
if (!parentVisable) {
break;
}
ctrl = ctrl.parent;
if (ctrl instanceof SDAForm) {
break;
}
}
}
this.originLeft = this.clientXToScreen(this.left);
this.originTop = this.clientYToScreen(this.top);
return parentVisable;
}
protected final void PaintChilds() {
if (!hasChild()) {
return;
}
SDABaseControl ctrl;
setChildsLayout();
for (int i = 0; i < ctrlList.size(); i++) {
ctrl = (SDABaseControl) ctrlList.elementAt(i);
ctrl.ctl3d = this.ctl3d;
ctrl.paint();
}
}
public abstract void paint();
protected void SetForm(SDAForm form) {
this.form = form;
if (ctrlList.isEmpty()) {
return;
}
for (int i = 0; i < ctrlList.size(); i++) {
((SDABaseControl) (ctrlList.elementAt(i))).SetForm(form);
}
}
public void AddControl(SDABaseControl control) {
if (control instanceof SDAMainMenu) {
return;
}
if (!ctrlList.contains(control)) {
ctrlList.addElement(control);
control.parent = this;
if (this.form != null) {
control.SetForm(this.form);
}
}
}
public final void RemoveControl(SDABaseControl control) {
if (ctrlList.contains(control)) {
ctrlList.removeElement(control);
control.setParent(null);
}
}
//查找下一个焦点组件
protected SDABaseControl FindNextFocusedCtrl(SDABaseControl focusedCtrl,
boolean Reverse) {
SDABaseControl result = null;
if (ctrlList.isEmpty()) {
return null;
}
if (focusedCtrl == null) {
int index = Reverse ? ctrlList.size() - 1 : 0;
int endIndex = Reverse ? 0 : ctrlList.size() - 1;
while (Reverse ? (index >= endIndex) : (index <= endIndex)) {
SDABaseControl ctrl = (SDABaseControl) (ctrlList.elementAt(
index));
if (ctrl.CanFocused()) {
result = ctrl;
break;
}
if (ctrl.visible && ctrl.enabled && this.form != null) {
result = ctrl.FindNextFocusedCtrl(null, Reverse);
}
if (result != null) {
break;
}
index += Reverse ? (-1) : (1);
}
} else {
int index = ctrlList.indexOf(focusedCtrl);
index = Reverse ? (--index) : (++index);
int endIndex = Reverse ? 0 : ctrlList.size() - 1;
while (Reverse ? (index >= endIndex) : (index <= endIndex)) {
SDABaseControl ctrl = (SDABaseControl) (ctrlList.elementAt(index));
if (ctrl.CanFocused()) {
result = ctrl;
break;
}
index += Reverse ? (-1) : (1);
}
if (result == null && !(this instanceof SDAForm) && this.parent != null) {
result = this.parent.FindNextFocusedCtrl(focusedCtrl, Reverse);
}
}
return result;
}
//执行所有布局处理
protected void setChildsLayout() {
setChildsDock();
setChildsAlign();
}
//停靠对齐计算
protected void setChildsDock() {
SDABaseControl ctrl = null;
SDABaseControl oldctrl = null;
int dockleft = 0;
int docktop = 0;
int dockbottom = getHeight();
int dockright = getWidth();
//下
for (int i = 0; i < ctrlList.size(); i++) {
ctrl = (SDABaseControl) ctrlList.elementAt(i);
if (!ctrl.visible) {
continue;
}
if (ctrl.dock == SDAConsts.dsBottom) {
ctrl.left = 0;
ctrl.top = dockbottom - ctrl.getHeight();
ctrl.width = getWidth();
dockbottom -= ctrl.getHeight();
if (ctrl instanceof SDASpliter) {
if (oldctrl != null) {
((SDASpliter) ctrl).setChangeControl(oldctrl);
}
}
oldctrl = ctrl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -