星期日, 十一月 26, 2006

Oracle ADF poll(计时器)和progressIndicator(进度条)的使用

<af:poll interval="1000" id="pollid"/>

interval的设置代表毫秒, 也就是说, 在这个例子中, 每1秒中刷新一次页面, poll常常和progressIndicator一起使用

<af:progressIndicator id="progressid"
action="progressEnd" value="#{progress.progressModel}" partialTriggers="pollid" >
</af:progressIndicator>

在faces-config.xml文件中

<af:progressIndicator id="progressid"
action="progressEnd" value="#{progress.progressModel}" partialTriggers="pollid" >
<af:outputFormatted styleUsage="instruction"
value="#{progress.message}"/>
</af:progressIndicator>

在progressbean文件

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.view.faces.model.BoundedRangeModel;
import oracle.adf.view.faces.model.DefaultBoundedRangeModel;

/**
* Bean for progress component demos.
*
*/
public class ProgressBean implements Serializable {

public BoundedRangeModel getProgressModel() {
if (null == model) {
prepare();
}
return model;
}

public void cancelProcess(ActionEvent event) {
endProcess();
}

protected void prepare() {
model = new DefaultBoundedRangeModel(-1,4);
//pu: simulate asynchronous model updates on a different thread
processThread = new ProcessThread(contextMappingList,projectID);
processThread.start();
}

protected void endProcess() {
message="cancelled";
processThread = null;
model = null;
}

public void setMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

protected class ProcessThread extends Thread implements Serializable {
/**
* @param updateIntervalFactor - controls the speed of the thread
* @param updateValueFactor - The value by which the 'value' from the
* model should be incremented for every cycle. Randomizes the increment
* if updateValueFactor supplied is '0'.
*/
ProcessThread(List contextMappingList, Integer projectID) {
_contextMappingList=contextMappingList;
this.projectID=projectID;
}
public void run(){
try {
model.setValue(0);
message="0";

this.sleep(1000);
model.setValue(1);
message="1";

this.sleep(1000);
model.setValue(2);
message="2";

this.sleep(1000);
model.setValue(3);
message="3";

this.sleep(1000);
model.setValue(4);
message="4";

this.sleep(1000);
model.setValue(5);
message="5";

this.sleep(1000);
message="Successfully!";

} catch (Exception e) {
message="Error: " + e.getMessage();
model.setValue(11);
try {
this.sleep(1000);
}
catch (InterruptedException e1) {
//ignore
}
model = null;
}
model = null;
}
private List _contextMappingList;
private Integer projectID;

}

protected volatile DefaultBoundedRangeModel model;
protected volatile ProcessThread processThread;
private String message = "Starting...";

private static final Logger _LOG =
Logger.getLogger(ProgressBean.class.getName());

}

在这个文件中ProgressBean 必须有BoundedRangeModel,

new DefaultBoundedRangeModel(-1,4);第一个参数是value的初始值, 第二个是最大值

在run()中model.setValue(5); 可以动态设置当前值, 当setvalue的值大于最大值时, poll刷新页面时progressIndicator会调用progress检查value, 如果大于maxvalue, 就会执行progressIndicator的action.

没有评论: