001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.interactive;
017:
018: /**
019: * TODO: Add documentation
020: *
021: * @author Fyodor Kupolov
022: * @version 1.0
023: */
024: public class ProgressCallback {
025: protected int size;
026: protected int pos;
027: private int reserved;
028: protected ProgressIndicator progressIndicator;
029:
030: public ProgressCallback(int size) {
031: this (size, null);
032: }
033:
034: public ProgressCallback(int size, ProgressIndicator indicator) {
035: this .size = size;
036: progressIndicator = indicator;
037: }
038:
039: public int step(final int step) {
040: return step(step, null);
041: }
042:
043: public int step(final int step, final String message) {
044: if (pos < size) {
045: int s = ((pos + step + reserved) > size) ? (size - pos - reserved)
046: : step;
047: pos += s;
048: showProgress(message);
049:
050: return s;
051: }
052:
053: return 0;
054: }
055:
056: public boolean isComplete() {
057: return pos >= size;
058: }
059:
060: public void complete() {
061: if (pos < size) {
062: step(size - pos);
063: }
064: }
065:
066: void freeReserved(final int step, final String message) {
067: if (step > reserved) {
068: int p = reserved;
069: reserved = 0;
070: step(p, message);
071: } else {
072: reserved -= step;
073: step(step, message);
074: }
075: }
076:
077: public int getSize() {
078: return size;
079: }
080:
081: public int getPos() {
082: return pos;
083: }
084:
085: public void setProgressBar(final ProgressIndicator progressIndicator) {
086: this .progressIndicator = progressIndicator;
087: }
088:
089: public ProgressCallback fork(final int newSize) {
090: return fork(size - reserved - pos, newSize);
091: }
092:
093: public ProgressCallback fork(final int step, final int newSize) {
094: int s = ((pos + reserved + step) > size) ? (size - reserved - pos)
095: : step;
096: reserved += s;
097:
098: return new Subprogress(this , newSize, s);
099: }
100:
101: private void showProgress(final String message) {
102: if (progressIndicator != null) {
103: progressIndicator.showProgress(((double) pos)
104: / ((double) size), message);
105: }
106: }
107:
108: public int getLeft() {
109: int l = size - pos - reserved;
110:
111: return (l < 0) ? 0 : l;
112: }
113:
114: protected static class Subprogress extends ProgressCallback {
115: private ProgressCallback parent;
116: private int oldSize;
117: private int oldSizeLeft;
118: private int accum;
119:
120: public Subprogress(ProgressCallback parent, int size,
121: int oldSize) {
122: super (size);
123: this .parent = parent;
124: this .oldSize = oldSize;
125: oldSizeLeft = oldSize;
126: accum = 0;
127: }
128:
129: public int step(final int step, final String message) {
130: if (pos < size) {
131: int s = super .step(step, message);
132: accum += s;
133:
134: final int oldStep = (pos == size) ? oldSizeLeft
135: : ((accum * oldSize) / size);
136:
137: if (oldStep > 0) {
138: parent.freeReserved(oldStep, message);
139: oldSizeLeft -= oldStep;
140: accum = 0;
141: }
142:
143: return s;
144: }
145:
146: return 0;
147: }
148: }
149: }
|