KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > EstimatedProgressDialog


1 package snow.utils.gui;
2
3 import snow.utils.StringUtils;
4 import snow.Language.Language;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.border.*;
9 import java.util.*;
10
11 /** Usage: after creation and before start, call setIndeterminate(time, false)
12 */

13 public final class EstimatedProgressDialog extends ProgressModalDialog
14 {
15
16   final private long startTime = System.currentTimeMillis();
17   final private java.util.Timer JavaDoc timer = new java.util.Timer JavaDoc("ProgressModalDialog_Timer", true);
18
19   public EstimatedProgressDialog( JFrame owner, String JavaDoc title, boolean modal)
20   {
21     super(owner,title,true,modal);
22     setSize(400,111);
23   } // Constructor
24

25
26   public EstimatedProgressDialog( JDialog owner, String JavaDoc title)
27   {
28     super(owner,title,true);
29     setSize(400,111);
30   } // Constructor
31

32
33   /** simulate a progress of given duration
34   */

35   public void setIndeterminate(final long estimatedTime, boolean showRemaining)
36   {
37     if(!showRemaining)
38     {
39        EventQueue.invokeLater(new Runnable JavaDoc()
40        {
41          public void run()
42          {
43            progress.setIndeterminate(true);
44            // show the time after 10 s
45
TimerTask tt = new TimerTask()
46            {
47               public void run()
48               {
49                  EventQueue.invokeLater(new Runnable JavaDoc()
50                  {
51                    public void run()
52                    {
53                      long done = System.currentTimeMillis()-startTime;
54                      progress.setString(
55                        Language.translate("Duration = %",StringUtils.formatTime(done))+"");
56                    }
57                  });
58               }
59            };
60            timer.scheduleAtFixedRate(tt, 10000, 1000);
61          }
62        });
63     }
64     else
65     {
66        EventQueue.invokeLater(new Runnable JavaDoc()
67        {
68          public void run()
69          {
70            progress.setIndeterminate(false);
71            progress.setMaximum(1000);
72          }
73        });
74
75        TimerTask tt = new TimerTask()
76        {
77           public void run()
78           {
79              // which percent did we achieved ?
80
double p = System.currentTimeMillis()-startTime;
81              p = p/((double) estimatedTime)*1000.0;
82
83              long doneMS = System.currentTimeMillis()-startTime;
84              long remaining = estimatedTime-doneMS;
85
86              if(p>1000) p=990;
87              setProgressValue((int) p, "Estimated remaining time = "+StringUtils.formatTime(remaining)+"");
88           }
89        };
90        timer.scheduleAtFixedRate(tt, 1000, 500);
91
92     }
93   }
94
95
96   /** must always be called at the end !
97   */

98   public void closeDialog()
99   {
100 /* if(this.iniFile!=null && !wasCanceled)
101       {
102         long time = System.currentTimeMillis()-this.startTime;
103         this.iniFile.setLong(this.identifier, time);
104       }*/

105       timer.cancel();
106       super.closeDialog();
107   }
108
109
110
111 } // EstimatedProgressDialog
Popular Tags