1: package
2: {
3: import mx.core.UIComponent;
4:
5: public final class UIUtilities
6: {
7:
8: /**
9: * Executes a for loop that pauses once in a while to let the UI update itself. The parameters of this method
10: * are derived from a normal
11: * for(var i :int =0; i <10;i++) {
12: * }
13: * loop.
14: * @param fromInclusive The 0 in the loop above.
15: * @param toExclusive The 10 in the loop above.
16: * @param loopBodyFunction The loop body, everything between {} in the loop above. This function must accept an int,
17: * which represents the current iteration.
18: * @param updateProgressFunction The method that needs to be called to update the UI, for example a progressbar.
19: * this method must accept two ints, The first is the number of iterations processed, the other is the total number of
20: * of iterations that need to be processed.
21: * @param componentInDisplayList Any component that is connected to the displaylist. This method makes use
22: * of the callLater() method which is available on any UIComponent. The root Application is an easy choice.
23: * @param numberOfPauses The number of times this method pauses to let the UI update itself.
24: * The correct amount is hardware dependent, 8 pauses doesn't mean you'll see 8 UI updates. Experiment
25: * to find the number that suits you best. A higher number means less performance, but more ui updates and
26: * visual feedback.
27: **/
28: public static function pausingFor(fromInclusive:int, toExclusive :int,loopBodyFunction : Function,updateProgressFunction : Function,componentInDisplayList:UIComponent,
29: numberOfPauses : int = 8) : void {
30: executeLoop(fromInclusive,toExclusive, toExclusive / numberOfPauses, loopBodyFunction,updateProgressFunction, componentInDisplayList)
31: }
32:
33:
34: private static function executeLoop(fromInclusive:int, toExclusive :int,numberOfIterationsBeforePause : int, loopBodyFunction : Function,
35: updateProgressFunction : Function,componentInDisplayList : UIComponent) : void {
36: var i : int = fromInclusive;
37: for(i; i < toExclusive;i++) {
38: //determine the rest of the number of iterations processed and the numberOfIterationsBeforePause
39: //This is needed to determine whether a pause should occur.
40: var rest : Number = i % numberOfIterationsBeforePause;
41:
42: //If the rest is 0 and i not is 0, a pause must occur to let the ui update itself
43: if(rest == 0 && i != 0) {
44:
45: //use callLater to pause and let the UI update.....
46: componentInDisplayList.callLater(
47: //Supply anonymous function to the callLater method, which can be called after the pause...
48: function(index:int) : void {
49: //after pausing, resume work...
50: loopBodyFunction(index);
51: //We need to continue with the executeLoop() method. The current index has already
52: //been processed so continue this method with the next index
53: executeLoop(index + 1,toExclusive,numberOfIterationsBeforePause,loopBodyFunction,updateProgressFunction,componentInDisplayList);
54: },
);
55: //When using callLater to let the UI update, my own code must be finished. So break out of the loop
56: break;
57: } else {
58: //No time for a pause
59: loopBodyFunction(i);
60: //Just before a pause occurs, report progress so that a user can set progress values
61: if(rest == numberOfIterationsBeforePause - 1) {
62: updateProgressFunction(i + 1, toExclusive);
63: }
64: }
65: }
66: //Final progress update
67: updateProgressFunction(i + 1, toExclusive);
68: }
69:
70: }
71: }