?? progressgaugeui.java
字號:
/*
* 進(jìn)度條與線程模型
*/
package com.favo.ui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
/**
* @author Favo
* 新版本的pgUI,主要是增加了cancel task的能力,通過回調(diào)CancelableObject的
* cancel方法實(shí)現(xiàn)。
* Preferences - Java - Code Style - Code Templates
*/
public class ProgressGaugeUI implements ProgressObserver, CommandListener {
private static final int GAUGE_MAX = 8;
private static final int GAUGE_LEVELS = 4;
private static ProgressGaugeUI pgUI;
private Form f;
private Gauge gauge;
private Command stopCMD;
boolean stopped;
boolean stoppable;
int current;
Cancelable cancelableObject;
protected ProgressGaugeUI() {
f = new Form("");
gauge = new Gauge("", false, GAUGE_MAX, 0);
stopCMD = new Command("Cancel", Command.STOP, 10);
f.append(gauge);
f.setCommandListener(this);
}
public static ProgressGaugeUI getInstance() {
if (pgUI == null) {
return new ProgressGaugeUI();
}
return pgUI;
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#reset(java.lang.Object)
*/
public void reset() {
current=0;
gauge.setValue(0);
stopped=false;
setStoppable(false);
setTitle("");
setPrompt("");
cancelableObject=null;
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#updateProgress(java.lang.Object)
*/
public void updateProgress(Object param1) {
// TODO Auto-generated method stub
current=(current+1)%GAUGE_LEVELS;
gauge.setValue(current * GAUGE_MAX/GAUGE_LEVELS);
if(param1!=null && param1 instanceof String){
setPrompt((String)param1);
}
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#isStoppable()
*/
public boolean isStoppable() {
return stoppable;
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#setStoppable(boolean)
*/
public void setStoppable(boolean stoppable) {
this.stoppable = stoppable;
if(stoppable){
f.addCommand(stopCMD);
}else{
f.removeCommand(stopCMD);
}
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#isStopped()
*/
public boolean isStopped() {
// TODO Auto-generated method stub
return stopped;
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#setTitle(java.lang.String)
*/
public void setTitle(String title) {
// TODO Auto-generated method stub
f.setTitle(title);
}
/*
* (non-Javadoc)
*
* @see com.favo.ui.ProgressObserver#setPrompt(java.lang.String)
*/
public void setPrompt(String prompt) {
gauge.setLabel(prompt);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1) {
if(arg0==stopCMD){
if(isStoppable())
if(!isStopped()){//保證僅被調(diào)用一次
setStopped(true);
if(cancelableObject!=null)
cancelableObject.cancel();
}
else{
setPrompt("can't stop!");
}
}
}
/* (non-Javadoc)
* @see com.favo.ui.ProgressObserver#show(javax.microedition.lcdui.Display)
*/
public void show(Display display) {
display.setCurrent(f);
}
/* (non-Javadoc)
* @see com.favo.ui.ProgressObserver#exit()
*/
public void exit() {
cancelableObject=null;
}
/* (non-Javadoc)
* @see com.favo.ui.ProgressObserver#setMax()
*/
public void setMax() {
gauge.setValue(GAUGE_MAX);
}
/* (non-Javadoc)
* @see com.favo.ui.ProgressObserver#setStopped(boolean)
*/
public void setStopped(boolean stopped) {
this.stopped=stopped;
}
public void setCancelalbeObject(Cancelable co){
this.cancelableObject=co;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -