2011年8月23日星期二

ADF_105:ProgressIndicator组件使用指南之二:彩色进度条

开发环境:JDeveloper 11.1.2.0.0。

1. 页面代码:my_progress_indicator2.jspx
<af:form id="f1">
<af:panelGroupLayout id="pgl1" layout="horizontal">
<af:commandButton text="Start Poll" id="cb1" partialSubmit="true" actionListener="#{viewScope.myBackingBean2.startButton_actionListener}"/>
<af:separator id="s1"/>
<af:commandButton text="Stop Poll" id="cb2" partialSubmit="true" actionListener="#{viewScope.myBackingBean2.stopButton_actionListener}"/>
</af:panelGroupLayout>
<af:poll id="pol1" interval="1000" partialTriggers="cb1 cb2" rendered="true" binding="#{viewScope.myBackingBean2.pollComponent}"/>
<af:progressIndicator id="pi1" partialTriggers="pol1" styleClass="#{viewScope.myBackingBean2.myProgressRangeModel2.styleClass}"
actionListener="#{viewScope.myBackingBean2.progressIndicator_actionListener}" value="#{viewScope.myBackingBean2.myProgressRangeModel2}"/>
</af:form>

2. 为了显示颜色,使用了CSS,创建sample.css。

.red af|progressIndicator::determinate-filled-icon-style {
background-image: url("../../images/redbar.png");
}

.yellow af|progressIndicator::determinate-filled-icon-style {
background-image: url("../../images/yellowbar.png");
}

.green af|progressIndicator::determinate-filled-icon-style {
background-image: url("../../images/greenbar.png");
}

3. 创建trinidad-skins.xml,指向sample.css。
<?xml version="1.0" encoding="UTF-8" ?>
<skins xmlns="http://myfaces.apache.org/trinidad/skin">
<skin>
<id>sample.desktop</id>
<family>sample</family>
<render-kit-id>org.apache.myfaces.trinidad.desktop</render-kit-id>
<extends>fusion.desktop</extends>
<style-sheet-name>skins/sample/sample.css</style-sheet-name>
</skin>
</skins>

4. Managed Bean代码
public void startButton_actionListener(ActionEvent actionEvent) {
startPoll(actionEvent);
}

public void stopButton_actionListener(ActionEvent actionEvent) {
stopPoll(actionEvent);
}

public void progressIndicator_actionListener(ActionEvent actionEvent) {
stopPoll(actionEvent);
}

public void startPoll(ActionEvent actionEvent) {
// pollComponent.setRendered(true);
pollComponent.setInterval(1000);
//AdfFacesContext.getCurrentInstance().addPartialTarget(pollComponent);
myProgressRangeModel2.start(actionEvent);
}

public void stopPoll(ActionEvent actionEvent) {
//pollComponent.setRendered(false);
pollComponent.setInterval(-1);
//AdfFacesContext.getCurrentInstance().addPartialTarget(pollComponent);
myProgressRangeModel2.stop(actionEvent);
}

5. MyProgressRangeModel2.java代码
其最核心的是内部类:ProgressSimulator,该类模拟一个比较耗时业务服务操作。
该类支持多线程,这样就可以运行在另一个线程中,不用等待Managed Bean的方法返回。
这是一个很好的设计:让耗时的业务服务单独启用一个线程,并且使用进度条对用户友好提示。

//Simulate a business service progress
class ProgressSimulator implements Runnable {
public void run() {
try {
//stop fag is true if it is set to true or if value is equals or greater than maximum
stopFlag = stopFlag == true ? true : (getValue() < getMaximum() ? false : true);
//run in loop until stop condition is met. Make sure system doesn't
//fail if values are initially set to the same value
while (!stopFlag && getValue() != getMaximum()) {
Thread.sleep(1000);
setValue(getValue() + stepPace);
//set the color boundaries
if (getValue() >= greenBoundary) {
styleClass = GREEN;
} else if (getValue() >= yellowBoundary) {
styleClass = YELLOW;
} else {
styleClass = RED;
}

if (getValue() == getMaximum()) {
stopFlag = true;
}
}
//stop thread
newProgress.interrupt();
newProgress = null;
} catch (Exception exc) {
exc.printStackTrace();
}
}
}

6. 运行页面






Project下载:MyProgressIndicator2.7z

参考文献:
1. http://www.oracle.com/technetwork/developer-tools/adf/learnmore/42-progressbarcolor-169184.pdf
2. http://forums.oracle.com/forums/thread.jspa?threadID=999826

没有评论: