📄 alert.java
字号:
package com.podome.ui;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.podome.log.Logger;
import com.podome.style.Style;
import com.podome.tool.Config;
import com.podome.tool.EffectHelper;
import com.podome.tool.Key;
/**
* 提示框,如果是确认框,必须加上菜单。并且处理各个菜单的处理结果。
*/
public class Alert extends UIBase implements EventDispatch {
/**
* 提示框的类型(prompt - 提示 confirm - 确认框)
*/
public static final int CONFIRM = -100;
public static final int PROMPT = -200;
/**
* 提示
*/
public static final int INFO = 0;
/**
* 警告
*/
public static final int WARMING = -1;
/**
* 错误
*/
public static final int ERROR = -2;
/**
* 致命错误
*/
public static final int FATAL = -3;
/**
* 在中间显示
*/
public static final int CENTER = 10;
/**
* 在底部显示
*/
public static final int BOTTOM = 11;
/***********************************************************/
private int level = INFO; //显示类别(INFO WARING ERROR FATAL)
private int confirm;
private int xpos;
private int ypos;
//移动偏移值
private int xOffset = 0; //X方向偏移值
// private int xOffMax = 0; //X轴方向的最大值
private int yOffset = 0; //Y方向偏移值
private int yOffMax = 0; //Y轴方向的最大值
//缩放偏移值
// private int wOffset = 0; //宽度偏移值
// private int wOffMax = 0;
// private int hOffset = 0; //高度偏移值
// private int hOffMax = 0;
private int elapsedTime = 0; //累计消耗的时间
private boolean flagStar = false;
private boolean flagStop = false;
private boolean flagEnd = false;
// private int Alpha = 0x11000000;
// private int AlphaCnt = 0;
private static Style style;
/*************************************/
private Menu menu = null;
private Command cmdOK = null;
private Command cmdCancel = null;
private int result = 0;
/**
*
*/
private Alert() {
super();
this.setAutoWrap(true);
this.owner = Stage.currentPage();
}
private Alert(String s){
this();
this.setString(s);
}
public void setString(String s){
super.setString(s);
this.setAutoArea(0, 0, Stage.getCW() - Config.ALERT_DISTANCE*2 - 40 );
//修正最后一行的宽度
bonds[1][0] = Stage.getCW() - Config.ALERT_DISTANCE*2 - 40;
}
/**
*/
private void setAlertLevel(int level) {
this.level = level;
}
public void setDisplayPosition(int position){
this.xpos = Config.ALERT_DISTANCE;
this.ypos = Stage.getCH()- getHeight();
if( Stage.currentPage().menu != null ){
this.ypos -= Menu.getHeight();
}
if( this.confirm == Alert.INFO){
this.yOffMax = - getHeight();
this.ypos += getHeight();
}
}
public int getX(){
return this.xpos + this.xOffset;
}
public int getY(){
return this.ypos + this.yOffset;
}
public int getHeight(){
return bonds[1][1] - bonds[0][1];
}
public int getWidth(){
return bonds[1][0] - bonds[0][0];
}
/**
* 显示在屏幕上
*/
private void show(){
EffectHelper.addToRepaint(this);
flagStar = true;
}
/**
* 从底部慢慢显示提示的消息,然后消失!停留时间默认为2秒。
* @param message
*/
public static void show(String message,int level){
Alert alert = new Alert(message);
alert.setAlertLevel(level);
alert.setDisplayPosition(Alert.BOTTOM);
alert.show();
}
/**
* 从指定位置显示消息,然后消失,停留时间默认为2秒。
* @param message
* @param position -底部 或 中间
*/
public static void show(String message,int level, int position){
Alert alert = new Alert(message);
alert.setAlertLevel(level);
alert.setDisplayPosition(position);
alert.show();
}
public static boolean confirm(String message,String label_ok , String label_cancel,int level){
Alert alert = new Alert(message);
alert.confirm = Alert.CONFIRM;
alert.setAlertLevel(level);
alert.setDisplayPosition(Alert.BOTTOM);
return alert.confirm(label_ok,label_cancel);
}
private boolean confirm(String lableOK,String labelCancel){
result = 0;
if(cmdOK == null)
cmdOK = new Command(lableOK,false);
else
cmdOK.setLabel(lableOK);
if(cmdCancel == null)
cmdCancel = new Command(labelCancel,false);
else
cmdCancel.setLabel(labelCancel);
if( menu == null)
menu = new Menu();
if(Menu.isRightUsage()){
menu.setLeft(cmdOK);
menu.setRight(cmdCancel);
}
else{
menu.setLeft(cmdCancel);
menu.setRight(cmdOK);
}
menu.setCommandDispatcher(this);
if(Stage.currentPage().menu != null){
menu.setStyle( Stage.currentPage().menu.getStyle());
}
Stage.setBlockListener(menu);
EffectHelper.pause();
Stage.setUserEventBlock(true);
Stage.getInstance().notifyRepaint(menu);
Stage.getInstance().notifyRepaint(this);
while( result == 0 ){
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(result == 1)
return true;
else{
return false;
}
}
public void paint(Graphics g) {
Style s = getStyle();
if(this.confirm == Alert.CONFIRM ){
Stage.currentPage().disableScreen(g, Style.disableColor);
paintConfirm(g,s);
}
else{
paintInfo(g,s);
}
}
/**
*
*/
public void paintInfo(Graphics g,Style s) {
int w = getWidth()+ 40;
int h = getHeight();
Image offImage = Image.createImage(w, h);
Graphics imgGraphics = offImage.getGraphics();
imgGraphics.setFont(s.dfont); //设置字体
//画背景
imgGraphics.setColor(s.bColorNormal);
imgGraphics.fillRoundRect(0, 0, w, h, s.rountWidth,s.roundHeight);
//画背景
imgGraphics.setColor(s.sColorNormal);
imgGraphics.drawRoundRect(0, 0, w-1, h-1, s.rountWidth,s.roundHeight);
imgGraphics.setColor(s.fColorNormal);
if(this.level==Alert.INFO && s.bImageLeftTop != null){
imgGraphics.drawImage(s.bImageLeftTop, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
else if(this.level==Alert.WARMING && s.bImageMid != null){
imgGraphics.drawImage(s.bImageMid, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
else if(this.level==Alert.ERROR && s.bImageLeftBottom != null){
imgGraphics.drawImage(s.bImageLeftBottom, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
int offset = 0;
int yoffset = s.paddingTop;
for (int i = 0; i < this.lineNum; i++) {
imgGraphics.drawSubstring(this.getString(), offset, this.lineCharNum[i],
s.paddingLeft + 40, yoffset, Graphics.LEFT | Graphics.TOP);
offset += lineCharNum[i];
yoffset += this.lineHeight;
}
int x = getX();
int y = getY();
g.setClip(x, y, w, -this.yOffset );
g.drawImage(offImage, x, y, Graphics.LEFT | Graphics.TOP);
}
private void paintConfirm(Graphics g,Style s){
int w = getWidth()+ 40;
int h = getHeight();
Image offImage = Image.createImage(w, h);
Graphics imgGraphics = offImage.getGraphics();
imgGraphics.setFont(s.dfont); //设置字体
//画背景
imgGraphics.setColor(s.bColorNormal);
imgGraphics.fillRoundRect(0, 0, w, h, s.rountWidth,s.roundHeight);
//画边框
imgGraphics.setColor(s.sColorNormal);
imgGraphics.drawRoundRect(0, 0, w-1, h-1, s.rountWidth,s.roundHeight);
imgGraphics.setColor(s.fColorNormal);
if(this.level == Alert.INFO && s.bImageLeftTop != null){
imgGraphics.drawImage(s.bImageLeftTop, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
else if(this.level == Alert.WARMING && s.bImageMid != null){
imgGraphics.drawImage(s.bImageMid, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
else if(this.level == Alert.ERROR && s.bImageLeftBottom != null){
imgGraphics.drawImage(s.bImageLeftBottom, s.paddingLeft, (h-s.bImageLeftTop.getHeight())/2, Graphics.LEFT | Graphics.TOP);
}
int offset = 0;
int offsetY = s.paddingTop;
for (int i = 0; i < this.lineNum; i++) {
imgGraphics.drawSubstring(this.getString(), offset, this.lineCharNum[i],
s.paddingLeft + 40, offsetY, Graphics.LEFT | Graphics.TOP);
offset += lineCharNum[i];
offsetY += this.lineHeight;
}
int x = getX();
int y = getY();
g.setClip(x, y, w, h );
g.drawImage(offImage, x, y, Graphics.TOP | Graphics.LEFT);
}
/**
*
*/
public String getTypeName() {
return "Alert";
}
/**
*
*/
public void move(){
if(flagStar){
yOffset = yOffset - ( this.yOffset - this.yOffMax )/5 - Config.MESSAGEBOX_OFFSET;
if(yOffset <= yOffMax){
flagStar = false;
flagStop = true;
}
}
else if(flagStop){
elapsedTime += Config.SCHEMA_INTERVAL;
if( elapsedTime >= Config.MESSAGEBOX_STOPTIME){
flagStop = false;
flagEnd = true;
}
else{
return;
}
}
else if(flagEnd){
yOffset = yOffset + ( this.yOffset - this.yOffMax )/5 + Config.MESSAGEBOX_OFFSET;
Stage.getInstance().notifyRepaintCurrent();
if(yOffset >= 0 ){
flagEnd = false;
yOffset = 0;
EffectHelper.removeRepaint(this);
}
}
Stage.getInstance().notifyRepaint(this);
}
public void keyPressed(int keyCode) {
if(keyCode == Key.KEY_OK ){
this.notifyCommand(cmdOK);
}
}
public void notifyCommand(Command cmd) {
Logger.info("Alert Exec " + cmd.getLabel());
if( cmd == cmdOK ){
result = 1;
Stage.setBlockListener(null);
Stage.setUserEventBlock(false);
EffectHelper.play();
Stage.getInstance().notifyRepaintCurrent();
}
else if( cmd == cmdCancel){
result = -1;
Stage.setBlockListener(null);
Stage.setUserEventBlock(false);
EffectHelper.play();
Stage.getInstance().notifyRepaintCurrent();
}
}
public void setStyle(Style style) {
Alert.style = style;
}
public Style getStyle(){
if(Alert.style == null){
Alert.style = new Style();
Alert.style.paddingTop = 10;
Alert.style.paddingBottom = 10;
Alert.style.bColorNormal = 0x666666;
Alert.style.fColorNormal = 0xFFFFFF;
try {
Alert.style.bImageLeftTop = Image.createImage("/images/icon_info.png");
Alert.style.bImageMid = Image.createImage("/images/icon_waring.png");
Alert.style.bImageLeftBottom = Image.createImage("/images/icon_error.png");
} catch (IOException e) {
e.printStackTrace();
}
}
return Alert.style;
}
public boolean isActive() {
return true;
}
public boolean responseKeyCode(int keyCode) {
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -