001: package snow.utils.gui;
002:
003: import snow.utils.StringUtils;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.*;
007: import javax.swing.border.*;
008: import java.util.*;
009:
010: /** Usage: after creation and before start, call setIndeterminate(time, false)
011: */
012: public class EstimatedProgressDialog extends ProgressModalDialog {
013:
014: final private long startTime = System.currentTimeMillis();
015: final private java.util.Timer timer = new java.util.Timer(
016: "ProgressModalDialog_Timer", true);
017:
018: public EstimatedProgressDialog(JFrame owner, String title,
019: boolean modal) {
020: super (owner, title, modal);
021: setSize(400, 111);
022: } // Constructor
023:
024: public EstimatedProgressDialog(JDialog owner, String title) {
025: super (owner, title);
026: setSize(400, 111);
027: } // Constructor
028:
029: /** simulate a progress of given duration
030: * @param showRemaining if true, create an simulation bar and shows the remaining time
031: * if false, only shows the elapsed time, without progressbar progress
032: */
033: public void setIndeterminate(final long estimatedTime,
034: boolean showRemaining) {
035: if (!showRemaining) {
036: EventQueue.invokeLater(new Runnable() {
037: public void run() {
038: progress.setIndeterminate(true);
039: progress.setStringPainted(true);
040: }
041: });
042:
043: // show the time after 10 s
044: TimerTask tt = new TimerTask() {
045: public void run() {
046: EventQueue.invokeLater(new Runnable() {
047: public void run() {
048: long done = System.currentTimeMillis()
049: - startTime;
050: progress.setString("Duration = "
051: + StringUtils.formatTime(done));
052: }
053: });
054: }
055: };
056: timer.scheduleAtFixedRate(tt, 5000, 1000);
057:
058: } else {
059: EventQueue.invokeLater(new Runnable() {
060: public void run() {
061: progress.setIndeterminate(false);
062: progress.setMaximum(1000);
063: }
064: });
065:
066: TimerTask tt = new TimerTask() {
067: public void run() {
068: // which percent did we achieved ?
069: double p = System.currentTimeMillis() - startTime;
070: p = p / ((double) estimatedTime) * 1000.0;
071:
072: long doneMS = System.currentTimeMillis()
073: - startTime;
074: long remaining = estimatedTime - doneMS;
075:
076: if (p > 1000)
077: p = 990;
078: setProgressValue((int) p, null);
079: // setProgressValue((int) p, "Estimated remaining time = "+StringUtils.formatTime(remaining)+"");
080: }
081: };
082: timer.scheduleAtFixedRate(tt, 1000, 500);
083:
084: }
085: }
086:
087: /** must always be called at the end !
088: */
089: @Override
090: public void closeDialog() {
091: /* if(this.iniFile!=null && !wasCanceled)
092: {
093: long time = System.currentTimeMillis()-this.startTime;
094: this.iniFile.setLong(this.identifier, time);
095: }*/
096: timer.cancel();
097: super .closeDialog();
098: }
099:
100: /* test
101: public static void main(String[] arguments)
102: {
103: JFrame fr = new JFrame();
104: fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
105: final EstimatedProgressDialog pmd = new EstimatedProgressDialog(fr, "Test", false);
106: pmd.setIndeterminate(10000,true);
107: pmd.start();
108: //pmd.addComponentToUI(new JTextArea("Hello\nTotal working time= 12m"));
109: }*/
110: }
|