1 21 22 package org.armedbear.j; 23 24 import javax.swing.SwingUtilities ; 25 26 public class StatusBarProgressNotifier implements Cancellable, ProgressNotifier, Runnable 27 { 28 private Buffer buffer; 29 private Thread updaterThread; 30 private boolean go = true; 31 private long totalBytes; 32 private long fileSize; 33 private String prefix; 34 private boolean cancelled; 35 private String 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 (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 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 progressText) 74 { 75 this.progressText = progressText; 76 } 77 78 public void setText(final String s) 79 { 80 progressText = s; 81 if (s != null) 82 update(); 83 } 84 85 private void update() 86 { 87 Runnable r = new Runnable () { 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 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 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; final long projected = (fileSize * elapsed) / totalBytes; 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 final long rate = (totalBytes * 1000) / elapsed; 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 |