ProgressManager.java
[java]
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
-
@author oxplay
- Radosław Kwolek
*/
public class ProgressManager extends AbstractAppState {
private AppStateManager stateManager;
private static ArrayList<ProgressCode> progressCodeList = new ArrayList<ProgressCode>();
public ProgressManager(AppStateManager stateManager) {
this.stateManager = stateManager;
}
public void init() {
stateManager.attach(this);
}
public static void addProgress(ProgressCode progress) {
progressCodeList.add(progress);
}
@Override
public void update(float tpf) {
Iterator<ProgressCode> i = progressCodeList.iterator();
while (i.hasNext()) {
ProgressCode element = i.next();
if (!element.isDone()) {
element.update(tpf);
} else {
i.remove();
}
}
}
}
[/java]
CodeFragment.java
[java]/**
*
-
@author oxplay
- Radosław Kwolek
*/
public abstract class CodeFragment {
private int priority = 0;
public void setPriority(int priority) {
this.priority = priority;
}
public int getPriority() {
return this.priority;
}
public abstract void execute() throws Exception;
}
[/java]
ProgressCode.java
[java]import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
/**
*
-
@author oxplay
- Radosław Kwolek
*/
public abstract class ProgressCode {
private boolean done = false;
private boolean executing = false;
private int progressStep = 0;
private int progressStepMax = 0;
private ArrayList<CodeFragment> progressFragmentList = new ArrayList<CodeFragment>();
public boolean isDone(){
return done;
}
public void sortByPriority() {
class CompareCodes implements Comparator<CodeFragment> {
@Override
public int compare(CodeFragment obj1, CodeFragment obj2) {
CodeFragment emp1 = (CodeFragment) obj1;
CodeFragment emp2 = (CodeFragment) obj2;
return (emp1.getPriority() > emp2.getPriority() ? -1 : (emp1.getPriority() == emp2.getPriority() ? 0 : 1));
}
}
CompareCodes compareCodes = new CompareCodes();
Collections.sort(progressFragmentList, compareCodes);
}
public void startExecute() throws Exception {
if (!executing) {
executing = true;
progressStep = 0;
progressStepMax = progressFragmentList.size();
sortByPriority();
onExecutionProgress(0f);
ProgressManager.addProgress(this);
} else {
throw new Exception("Cannot startExecute while it is executing");
}
}
private void onSuccess() {
done = true;
onExecutionSuccess();
}
private void onFailure(Exception error) {
done = true;
onExecutionFailure(error);
}
public void update(float tpf) {
try {
float progressValue = (float) progressStep / (float) progressStepMax;
Iterator<CodeFragment> it = progressFragmentList.iterator();
if (it.hasNext()) {
CodeFragment processExecution = it.next();
processExecution.execute();
onExecutionProgress(progressValue);
it.remove();
} else {
progressValue = 1f;
onExecutionProgress(progressValue);
onSuccess();
}
progressStep++;
} catch (Exception error) {
onFailure(error);
}
}
public CodeFragment addCode(CodeFragment code) throws Exception {
if (!executing) {
progressFragmentList.add(code);
return code;
} else {
throw new Exception("Cannot add Task to executing Progress");
}
}
public abstract void onExecutionProgress(float progress);
public abstract void onExecutionSuccess();
public abstract void onExecutionFailure(Exception error);
}
[/java]
initialize ProgressManager
[java]
ProgressManager progressManager = new ProgressManager(stateManager);
progressManager.init();
[/java]
so now we can easly do a progressbar like:
[java]
ProgressCode progressCode = new ProgressCode() {
@Override
public void onExecutionProgress(float progress) {
System.out.println("progress: " + progress); // Increase your progressbar
}
@Override
public void onExecutionSuccess() {
System.out.println("progress Success!"); // What to happend on success
}
@Override
public void onExecutionFailure(Exception error) {
System.out.println("progress Failure!"); // Exception Failure
}
};
try {
progressCode.addCode(new CodeFragment() {
@Override
public void execute() throws Exception {
System.out.println("Execution 1");
}
}).setPriority(2);
progressCode.addCode(new CodeFragment() {
@Override
public void execute() throws Exception {
System.out.println("Execution 2");
}
}).setPriority(1);
progressCode.addCode(new CodeFragment() {
@Override
public void execute() throws Exception {
System.out.println("Execution 3");
}
}).setPriority(0);
progressCode.startExecute();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} // Exception catch needed if in some situation you will try to add at actually executing Process
[/java]