001: /*
002: * Copyright (c) 2001 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: * @Author: Silvere Martin-Michiellot
032: *
033: */
034:
035: package com.db.utils.task;
036:
037: /** Uses a SwingWorker to perform a time-consuming (and utterly fake) task. */
038:
039: public class LongTask {
040:
041: private int lengthOfTask;
042: private int current = 0;
043: private String statMessage;
044:
045: LongTask() {
046:
047: //Compute length of task...
048: //In a real program, this would figure out
049: //the number of bytes to read or whatever.
050: lengthOfTask = 1000;
051:
052: }
053:
054: /**
055: * Called from ProgressBarDemo to start the task.
056: */
057: void go() {
058:
059: current = 0;
060: final SwingWorker worker = new SwingWorker() {
061: public Object construct() {
062: return new ActualTask();
063: }
064: };
065: worker.start();
066:
067: }
068:
069: /**
070: * Called from ProgressBarDemo to find out how much work needs
071: * to be done.
072: */
073: int getLengthOfTask() {
074:
075: return lengthOfTask;
076:
077: }
078:
079: /**
080: * Called from ProgressBarDemo to find out how much has been done.
081: */
082: int getCurrent() {
083:
084: return current;
085:
086: }
087:
088: void stop() {
089:
090: current = lengthOfTask;
091:
092: }
093:
094: /**
095: * Called from ProgressBarDemo to find out if the task has completed.
096: */
097: boolean done() {
098:
099: if (current >= lengthOfTask)
100: return true;
101: else
102: return false;
103:
104: }
105:
106: String getMessage() {
107:
108: return statMessage;
109:
110: }
111:
112: /**
113: * The actual long running task. This runs in a SwingWorker thread.
114: */
115: class ActualTask {
116:
117: ActualTask() {
118: //Fake a long task,
119: //making a random amount of progress every second.
120: while (current < lengthOfTask) {
121: try {
122: Thread.sleep(1000); //sleep for a second
123: current += Math.random() * 100; //make some progress
124: if (current > lengthOfTask) {
125: current = lengthOfTask;
126: }
127: statMessage = "Completed " + current + " out of "
128: + lengthOfTask + ".";
129: } catch (InterruptedException e) {
130: }
131: }
132: }
133:
134: }
135:
136: }
|