📄 panel.java
字号:
package com.cuit.lui;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
public class Panel extends Component {
private PopUp leftPopup, rightPopup;
protected int titleBGColor = LUIConfig.DEFAULT_TITLEBG_COLOR;
protected int scrollBarBGColor = LUIConfig.DEFAULT_SCROLLBARBG_COLOR;
protected int taskBarBGColor = LUIConfig.DEFAULT_TASKBARBG_COLOR;
protected int scrollStep = LUIConfig.DEFAULT_SCROLLSTEP;
protected Vector rows = new Vector();
protected Vector others = new Vector();
protected int panelWidth, panelHeight;
protected int titleBarHeight = 15;
protected int taskBarHeight = 15;
protected int scrollBarPosx = 0;
protected int scrollBarPosy = 0;
protected int scrollBarWidth = 5;
protected int scrollBarHeight = 0;
protected int usedHeight = 0;
protected int internalHeight = 0;
protected int rowStarty = 0;
protected boolean isScrolled = false;
protected int rowPointer = -1;
protected String title;
protected Command leftCommand, rightCommand;
public Panel() {
initialPanel(null, null);
}
public Panel(PopUp l, PopUp r) {
initialPanel(l, r);
}
private void initialPanel(PopUp l, PopUp r) {
leftPopup = l;
rightPopup = r;
width = panelWidth = LScreen.screenWidth;
height = panelHeight = LScreen.screenHeight;
adjustLayout();
LScreen.console("panel initialized");
}
public void adjustLayout() {
scrollBarPosx = posx + getWidth() - scrollBarWidth;
scrollBarPosy = posy + titleBarHeight + 1;
internalHeight = getHeight() - titleBarHeight - taskBarHeight;
rowStarty = posy + titleBarHeight;
if (rows.size() > 0) {
Object[] tmprows = new Object[rows.size()];
rows.copyInto(tmprows);
removeAll();
for (int i = 0; i < tmprows.length; i++) {
add((Component) tmprows[i]);
}
}
}
public void addLeftPopUp(PopUp l) {
if (leftPopup != null) {
removeLeftPopUp();
}
leftPopup = l;
leftPopup.setPosx(posx + 1);
leftPopup.setPosy(posy + panelHeight - leftPopup.getHeight()
- taskBarHeight);
leftPopup.adjustLayout();
}
public void addRightPopUp(PopUp r) {
if (rightPopup != null) {
removeRightPopUp();
}
rightPopup = r;
}
public void addPopUp(PopUp l, PopUp r) {
addLeftPopUp(l);
addRightPopUp(r);
}
public void add(Component p) {
rows.addElement(p);
p.setPosx(posx);
p.setPosy(rowStarty + usedHeight + 1);
p.adjustLayout();
usedHeight += p.getHeight();
if (usedHeight > getInternalHeight()) {
isScrolled = true;
scrollBarHeight = (int) ((float) getInternalHeight() / usedHeight * getInternalHeight());
}
if (p.getClass().getName().equals("com.cuit.lui.ChoiceGroup")) {
ChoiceGroup cg = (ChoiceGroup) p;
cg.setWidth(panelWidth - scrollBarWidth - 1);
cg.adjustLayout();
}
if (p.getClass().getName().equals("com.cuit.lui.ChoicePopup")) {
others.addElement(new Integer(rows.size() - 1));
}
}
public void insert(int nb,Component c){
if(nb<0||nb>rows.size()){
LScreen.error("insert: nb invalid!!!");
}else{
int originalPointer=rowPointer;
if(nb<rowPointer){
originalPointer=++rowPointer;
}
rows.insertElementAt(c, nb);
adjustLayout();
rowPointer=originalPointer;
}
}
public void remove(int nb) {
if(nb<0||nb>rows.size()-1){
LScreen.warning("remove index can't be smaller than 0 or larger than size-1");
return;
}
int h = ((Component) rows.elementAt(nb)).getHeight();
usedHeight -= h;
if(usedHeight<0){
usedHeight=0;
}
if (usedHeight <= getInternalHeight()) {
isScrolled = false;
}
rows.removeElementAt(nb);
for (int start = nb; start < rows.size(); start++) {
Row r = (Row) rows.elementAt(start);
r.setPosy(r.getPosy() - h);
}
rowPointer--;
if(rowPointer<0)
rowPointer=0;
}
public void setString(int nb,String s){
if(nb<0||nb>rows.size()-1){
LScreen.warning("setString:nb invalid!!!");
return;
}else{
((Row)rows.elementAt(nb)).setText(s);
}
}
public void removeAll() {
for (int i = rows.size() - 1; i >= 0; i--) {
remove(i);
}
reset();
}
private void removeLeftPopUp() {
if (leftPopup == null)
return;
leftPopup = null;
LScreen.console("leftpopup removed");
}
private void removeRightPopUp() {
if (rightPopup == null)
return;
rightPopup = null;
LScreen.console("rightpopup removed");
}
public void removePopUp() {
removeLeftPopUp();
removeRightPopUp();
}
public void paint(Graphics g) {
if (g == null) {
LScreen.error("panel graphic is null");
return;
} else {
LScreen.console("panel paint...");
g.setClip(posx, posy, panelWidth, panelHeight);
g.setColor(0xffffff);
g.fillRect(posx, posy, panelWidth, panelHeight);
if (isBorder) {
g.setColor(0x000000);
g.drawRect(posx, posy, panelWidth, panelHeight);
}
g.setColor(titleBGColor);
g.fillRect(posx, posy, panelWidth, titleBarHeight);
if (title != null) {
g.setColor(0x000000);
g.drawString(title, posx, posy, 0);
}
if (isScrolled) {
LScreen.console("!!!scrolled!!!!!!!!");
g.setColor(scrollBarBGColor);
g.fillRoundRect(scrollBarPosx, scrollBarPosy, scrollBarWidth,
scrollBarHeight, 10, 10);
}
int tasky = panelHeight - taskBarHeight;
g.setColor(taskBarBGColor);
g.fillRect(posx, tasky, panelWidth, taskBarHeight);
g.setColor(0x000000);
if (leftPopup != null) {
g.drawString(leftPopup.getLabel(), posx, tasky, 0);
}
if (rightPopup != null) {
String ls = rightPopup.getLabel();
int labelstrposx = panelWidth
- LScreen.wrapTextFont.charsWidth(ls.toCharArray(), 0,
ls.length());
g.drawString(ls, labelstrposx, tasky, 0);
}
if (leftCommand != null) {
g.drawString(leftCommand.getLabel(), posx, tasky, 0);
}
if (rightCommand != null) {
String ls = rightCommand.getLabel();
int lsposx = panelWidth
- LScreen.wrapTextFont.charsWidth(ls.toCharArray(), 0,
ls.length());
g.drawString(ls, lsposx, tasky, 0);
}
g.setClip(posx, posy + titleBarHeight, panelWidth, internalHeight);
for (int i = 0; i < rows.size(); i++) {
Component c = (Component) rows.elementAt(i);
if (c.getClass().getName() != "com.cuit.lui.ChoicePopup") {
if (c.needRepaint)
c.paint(g);
}
}
for (int i = 0; i < others.size(); i++) {
int nb = ((Integer) others.elementAt(i)).intValue();
Component c = (Component) rows.elementAt(nb);
if (c.needRepaint)
c.paint(g);
}
if (leftPopup != null && leftPopup.isShow()) {
if (leftPopup.needRepaint)
leftPopup.paint(g);
}
if (rightPopup != null && rightPopup.isShow()) {
if (rightPopup.needRepaint)
rightPopup.paint(g);
}
}
}
public boolean keyEvent(int keyCode) {
needRepaint = false;
if (leftPopup == null && rightPopup == null && leftCommand == null
&& rightCommand == null) {
if (rows == null) {
LScreen.warning("rows are null!");
return needRepaint;
}
needRepaint = traverseRows(keyCode);
return needRepaint;
} else {
if (leftCommand != null && keyCode == LUIConfig.LEFT_SOFTKEY) {
System.out.println("leftcommand");
setCommand(leftCommand);
generateEvent();
return needRepaint;
}
if (rightCommand != null && keyCode == LUIConfig.RIGHT_SOFTKEY) {
System.out.println("rightcommand");
setCommand(rightCommand);
generateEvent();
return needRepaint;
}
if (leftPopup != null && keyCode == LUIConfig.LEFT_SOFTKEY) {
if (leftPopup.isShow()) {
closePopup(LUIConfig.LEFT);
} else {
openPopup(LUIConfig.LEFT);
}
needRepaint = true;
return needRepaint;
}
if (rightPopup != null && keyCode == LUIConfig.RIGHT_SOFTKEY) {
if (rightPopup.isShow()) {
closePopup(LUIConfig.RIGHT);
} else {
openPopup(LUIConfig.RIGHT);
}
needRepaint = true;
return needRepaint;
}
if (leftPopup != null && leftPopup.getOnFocus()) {
needRepaint = leftPopup.keyEvent(keyCode);
LScreen.console("leftPopup key processed");
return needRepaint;
} else if (rightPopup != null && rightPopup.getOnFocus()) {
needRepaint = rightPopup.keyEvent(keyCode);
LScreen.console("rightPopup key processed");
return needRepaint;
} else {
needRepaint = traverseRows(keyCode);
return needRepaint;
}
}
}
private boolean traverseRows(int keyCode) {
boolean changed = true;
int k = keyCode;
if (k == LUIConfig.UP) {
LScreen.console("up pressed");
Component pre = getPrevious();
System.out.println("rowPointer " + rowPointer);
Component c = (Component) rows.elementAt(rowPointer);
// System.out.println(c.getClass().getName());
if (c.getClass().getName().equals("com.cuit.lui.ChoiceGroup")
&& c.isOnFocus) {
changed = c.keyEvent(k);
// return changed;
}
if (c.getClass().getName().equals("com.cuit.lui.ChoicePopup")
&& c.isOnFocus && ((ChoicePopup) c).choiceBoxShow) {
changed = c.keyEvent(k);
return changed; // ///////must not omit this!!!!
}
if (!checkNeedScroll(pre, k)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -