KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > StatusBarProgressNotifier


1 /*
2  * StatusBarProgressNotifier.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: StatusBarProgressNotifier.java,v 1.1.1.1 2002/09/24 16:08:45 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.SwingUtilities JavaDoc;
25
26 public class StatusBarProgressNotifier implements Cancellable, ProgressNotifier, Runnable JavaDoc
27 {
28     private Buffer buffer;
29     private Thread JavaDoc updaterThread;
30     private boolean go = true;
31     private long totalBytes;
32     private long fileSize;
33     private String JavaDoc prefix;
34     private boolean cancelled;
35     private String JavaDoc progressText;
36
37     public StatusBarProgressNotifier(Buffer buffer)
38     {
39         this.buffer = buffer;
40     }
41
42     public void cancel()
43     {
44         cancelled = true;
45     }
46
47     public boolean cancelled()
48     {
49         return cancelled;
50     }
51
52     public void progressStart()
53     {
54         if (updaterThread == null) {
55             updaterThread = new Thread JavaDoc(this);
56             updaterThread.setDaemon(true);
57             updaterThread.start();
58         }
59     }
60
61     public void progressStop()
62     {
63         go = false;
64     }
65
66     public void progress(String JavaDoc prefix, long totalBytes, long fileSize)
67     {
68         this.prefix = prefix;
69         this.totalBytes = totalBytes;
70         this.fileSize = fileSize;
71     }
72
73     public void progress(String JavaDoc progressText)
74     {
75         this.progressText = progressText;
76     }
77
78     public void setText(final String JavaDoc s)
79     {
80         progressText = s;
81         if (s != null)
82             update();
83     }
84
85     private void update()
86     {
87         Runnable JavaDoc r = new Runnable JavaDoc() {
88             public void run()
89             {
90                 for (EditorIterator it = new EditorIterator(); it.hasNext();) {
91                     Editor ed = it.nextEditor();
92                     if (ed.getBuffer() == buffer)
93                         ed.status(progressText);
94                 }
95             }
96         };
97         SwingUtilities.invokeLater(r);
98     }
99
100     public void run()
101     {
102         long start = System.currentTimeMillis();
103         while (go) {
104             try {
105                 Thread.sleep(500);
106             }
107             catch (InterruptedException JavaDoc e) {
108                 Log.error(e);
109             }
110             if (go) {
111                 if (prefix != null && totalBytes != 0) {
112                     long elapsed = System.currentTimeMillis() - start;
113                     setText(getProgressText(elapsed));
114                 } else
115                     update();
116             }
117         }
118     }
119
120     private String JavaDoc getProgressText(long elapsed)
121     {
122         if (elapsed == 0)
123             return null;
124         FastStringBuffer sb = new FastStringBuffer(prefix);
125         if (fileSize > 0) {
126             final long percent = (totalBytes * 100) / fileSize;
127             if (percent >= 100)
128                 return null;
129             final long rate = (totalBytes * 1000) / elapsed; // bytes per second
130
final long projected = (fileSize * elapsed) / totalBytes; // milliseconds
131
long seconds = (projected - elapsed) / 1000;
132             long minutes = seconds / 60;
133             if (minutes != 0)
134                 seconds = seconds % 60;
135             final long hours = minutes / 60;
136             if (hours != 0)
137                 minutes = minutes % 60;
138             long K = fileSize / 1000;
139             if (K == 0)
140                 K = 1;
141             sb.append(percent);
142             sb.append("% of ");
143             sb.append(K);
144             sb.append("K (at ");
145             sb.append(rate);
146             sb.append(" bytes/sec, ");
147             if (hours != 0) {
148                 sb.append(hours);
149                 sb.append(':');
150                 if (minutes < 10)
151                     sb.append('0');
152             }
153             sb.append(minutes);
154             sb.append(':');
155             if (seconds < 10)
156                 sb.append('0');
157             sb.append(seconds);
158             sb.append(" remaining)");
159         } else {
160             // We don't know the file size.
161
final long rate = (totalBytes * 1000) / elapsed; // bytes per second
162
sb.append(totalBytes);
163             sb.append(" bytes (at ");
164             sb.append(rate);
165             sb.append(" bytes/sec)");
166         }
167         return sb.toString();
168     }
169 }
170
Popular Tags