package NET.worlds.network; import NET.worlds.console.Console; import NET.worlds.console.PolledDialog; import NET.worlds.core.Std; import java.awt.Event; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Label; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.Locale; import java.util.Vector; public class ProgressDialog extends PolledDialog { private static final long serialVersionUID = 7533646170494183110L; private static final String bytesMsg = Console.message("Bytes-remaining"); private int bytesTotal; private int bytesLoaded; private ProgressBar progressBar; private Label progressBytes; private Label progressTime; private int startTime = Std.getFastTime(); private CacheFile cf; private boolean cancelled; private int wholeFileBytes = 0; public ProgressDialog(int totalSize) { super(Console.getFrame(), null, Console.message("Download-Progress"), false); this.bytesTotal = totalSize; this.progressBar = new ProgressBar(240); NumberFormat nF = NumberFormat.getNumberInstance(Locale.getDefault()); String strSize = nF.format((long)totalSize); this.progressBytes = new Label(bytesMsg + strSize); this.progressTime = new Label(""); this.setAlignment(1); this.readySetGo(); } public boolean loadFiles(Vector names, Vector urls) throws IOException { try { int count = names.size(); int i = 0; while (true) { if (i >= count) { return true; } synchronized (this) { if (this.cancelled) { NetUpdate.warnUser("Upgrade cancelled."); break; } if (this.cf != null) { this.wholeFileBytes = this.wholeFileBytes + this.cf.bytesLoaded(); } this.cf = Cache.getFile(urls.elementAt(i)); } this.cf.waitUntilLoaded(); if (!this.cf.isActive()) { this.cf.markTemporary(); NetUpdate.warnUser("Upgrade cancelled."); break; } if (this.cf.error()) { this.cf.markTemporary(); NetUpdate.warnUser("Error getting upgrade info, try again later"); break; } copyFile(this.cf.getLocalName(), names.elementAt(i)); this.cf.markTemporary(); this.cf.close(); i++; } } finally { this.done(true); } return false; } public static boolean copyFile(String from, String to) { int lastSlash = to.lastIndexOf(47); int lastBackslash = to.lastIndexOf(92); if (lastBackslash > lastSlash) { lastSlash = lastBackslash; } if (lastSlash >= 0) { new File(to.substring(0, lastSlash)).mkdirs(); } FileInputStream s = null; FileOutputStream d = null; try { try { s = new FileInputStream(from); d = new FileOutputStream(to); byte[] buffer = new byte[1024]; while (true) { int got = s.read(buffer); if (got == -1) { return true; } d.write(buffer, 0, got); } } finally { if (d != null) { d.close(); } if (s != null) { s.close(); } } } catch (IOException var12) { return false; } } @Override protected void build() { GridBagLayout gbag = new GridBagLayout(); this.setLayout(gbag); GridBagConstraints c = new GridBagConstraints(); c.anchor = 10; c.gridwidth = 0; c.weightx = 1.0; c.weighty = 1.0; this.add(gbag, new Label(Console.message("Downloading-update")), c); c.insets = new Insets(2, 2, 2, 2); this.add(gbag, this.progressBar, c); c.insets = new Insets(0, 2, 0, 2); c.anchor = 17; this.add(gbag, this.progressBytes, c); c.fill = 2; this.add(gbag, this.progressTime, c); } @Override protected synchronized void activeCallback() { int n; synchronized (this) { n = this.wholeFileBytes; if (this.cf != null) { n += this.cf.bytesLoaded(); } } if (n != this.bytesLoaded) { this.bytesLoaded = n; this.progressBar.setProgress((double)this.bytesLoaded / this.bytesTotal); int bytesRemaining = this.bytesTotal - this.bytesLoaded; NumberFormat nF = NumberFormat.getNumberInstance(Locale.getDefault()); String strRemain = nF.format((long)bytesRemaining); this.progressBytes.setText(bytesMsg + strRemain); int elapsed = (Std.getFastTime() - this.startTime) / 1000; if (elapsed > 0) { double bytesPerSec = (double)this.bytesLoaded / elapsed; if (bytesPerSec > 0.0) { String bps = formatTime((long)(bytesRemaining / bytesPerSec)); Object[] arguments = new Object[]{new String(bps)}; this.progressTime.setText(MessageFormat.format(Console.message("Time-remaining"), arguments)); } } } this.notify(); } @Override protected boolean done(boolean confirmed) { synchronized (this) { this.cancelled = true; if (this.cf != null) { this.cf.close(); } } return super.done(confirmed); } private static String fmtTwoDigit(long val) { return val < 10L ? "0" + val : "" + val; } private static String formatTime(long secs) { String result = ""; long hrs = secs / 3600L; secs -= hrs * 3600L; long mins = secs / 60L; secs -= mins * 60L; if (hrs > 0L) { result = result + hrs + ":"; } return result + fmtTwoDigit(mins) + ":" + fmtTwoDigit(secs); } @Override public boolean handleEvent(Event event) { if (event.id == 1004) { Console.getFrame().requestFocus(); return true; } else { return super.handleEvent(event); } } }