KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > interactive > ProgressCallback


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.interactive;
17
18 /**
19  * TODO: Add documentation
20  *
21  * @author Fyodor Kupolov
22  * @version 1.0
23  */

24 public class ProgressCallback {
25     protected int size;
26     protected int pos;
27     private int reserved;
28     protected ProgressIndicator progressIndicator;
29
30     public ProgressCallback(int size) {
31         this(size, null);
32     }
33
34     public ProgressCallback(int size, ProgressIndicator indicator) {
35         this.size = size;
36         progressIndicator = indicator;
37     }
38
39     public int step(final int step) {
40         return step(step, null);
41     }
42
43     public int step(final int step, final String JavaDoc message) {
44         if (pos < size) {
45             int s = ((pos + step + reserved) > size) ? (size - pos - reserved)
46                     : step;
47             pos += s;
48             showProgress(message);
49
50             return s;
51         }
52
53         return 0;
54     }
55
56     public boolean isComplete() {
57         return pos >= size;
58     }
59
60     public void complete() {
61         if (pos < size) {
62             step(size - pos);
63         }
64     }
65
66     void freeReserved(final int step, final String JavaDoc message) {
67         if (step > reserved) {
68             int p = reserved;
69             reserved = 0;
70             step(p, message);
71         } else {
72             reserved -= step;
73             step(step, message);
74         }
75     }
76
77     public int getSize() {
78         return size;
79     }
80
81     public int getPos() {
82         return pos;
83     }
84
85     public void setProgressBar(final ProgressIndicator progressIndicator) {
86         this.progressIndicator = progressIndicator;
87     }
88
89     public ProgressCallback fork(final int newSize) {
90         return fork(size - reserved - pos, newSize);
91     }
92
93     public ProgressCallback fork(final int step, final int newSize) {
94         int s = ((pos + reserved + step) > size) ? (size - reserved - pos) : step;
95         reserved += s;
96
97         return new Subprogress(this, newSize, s);
98     }
99
100     private void showProgress(final String JavaDoc message) {
101         if (progressIndicator != null) {
102             progressIndicator.showProgress(((double) pos) / ((double) size),
103                     message);
104         }
105     }
106
107     public int getLeft() {
108         int l = size - pos - reserved;
109
110         return (l < 0) ? 0 : l;
111     }
112
113     protected static class Subprogress extends ProgressCallback {
114         private ProgressCallback parent;
115         private int oldSize;
116         private int oldSizeLeft;
117         private int accum;
118
119         public Subprogress(ProgressCallback parent, int size, int oldSize) {
120             super(size);
121             this.parent = parent;
122             this.oldSize = oldSize;
123             oldSizeLeft = oldSize;
124             accum = 0;
125         }
126
127         public int step(final int step, final String JavaDoc message) {
128             if (pos < size) {
129                 int s = super.step(step, message);
130                 accum += s;
131
132                 final int oldStep = (pos == size) ? oldSizeLeft
133                         : ((accum * oldSize) / size);
134
135                 if (oldStep > 0) {
136                     parent.freeReserved(oldStep, message);
137                     oldSizeLeft -= oldStep;
138                     accum = 0;
139                 }
140
141                 return s;
142             }
143
144             return 0;
145         }
146     }
147 }
148
Popular Tags