package NET.worlds.network; import NET.worlds.console.Console; import NET.worlds.console.MultiLineLabel; import NET.worlds.console.PolledDialog; import NET.worlds.core.IniFile; import java.awt.Button; import java.awt.Checkbox; import java.awt.Component; import java.awt.Event; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Panel; import java.awt.ScrollPane; import java.text.MessageFormat; import java.text.NumberFormat; import java.util.Locale; import java.util.Vector; public class UpgradeDialog extends PolledDialog { private static final long serialVersionUID = -6015193016188781866L; private Button yesButton = new Button(Console.message("Yes-now")); private Button noButton = new Button(Console.message("No-later")); private Button moreInfo = new Button(Console.message("Show-me-more")); private Checkbox startupCheck; private boolean forceUpgrades = IniFile.gamma().getIniInt("forceUpgrades", 1) == 1; private static String instructions = Console.message("Please-uncheck") + " \n" + " \n"; private static String forcedInstructions = Console.message("you-can-continue") + " \n" + " \n"; private static String title = Console.message("Upgrade"); private Vector checkboxes; private String info; private Vector list; private Vector bytes; private boolean first; private boolean chooseOne; private String name; private Component getsFocus; private MultiLineLabel msgLabel; public UpgradeDialog(Vector list, Vector bytes, boolean first, String name, String info, boolean chooseOne) { super(Console.getFrame(), null, title, false); this.setAlignment(1); this.list = list; this.info = info; this.name = name; this.first = first; this.bytes = bytes; this.chooseOne = chooseOne; this.readySetGo(); } @Override protected void build() { this.startupCheck = new Checkbox(Console.message("Check-for-up-each"), IniFile.gamma().getIniInt("CheckUpgrades", 1) != 0); int count = this.list.size(); this.checkboxes = new Vector(); Panel p = new Panel(new GridLayout(count, 1)); Checkbox chk = null; for (int i = 0; i < count; i++) { int b = this.bytes.elementAt(i); String kb = NumberFormat.getInstance().format((long)(b / 1000)); Object[] arguments = new Object[]{new String(this.list.elementAt(i).toString()), new String(kb)}; String msg = MessageFormat.format(Console.message("Kbytes"), arguments); chk = new Checkbox(msg, true); if (!this.forceUpgrades) { p.add(chk); } else { p.add(new MultiLineLabel(msg)); } this.checkboxes.addElement(chk); } GridBagLayout gbag = new GridBagLayout(); this.setLayout(gbag); GridBagConstraints c = new GridBagConstraints(); c.anchor = 10; c.fill = 0; c.weightx = 1.0; c.weighty = 1.0; c.gridwidth = 0; c.gridheight = 1; c.insets = new Insets(0, 15, 0, 15); Object[] arguments = new Object[]{new String(this.name)}; String msg = this.first ? MessageFormat.format(Console.message("dont-have"), arguments) : MessageFormat.format(Console.message("update-available"), arguments); this.add(gbag, new MultiLineLabel(msg + "\n ", 5, 5), c); if (count > 3) { ScrollPane sp = new ScrollPane(); GridBagConstraints c2 = new GridBagConstraints(); c2.fill = 1; c2.gridwidth = 0; sp.setSize(280, 100); sp.validate(); this.add(gbag, sp, c2); sp.add(p); } else { this.add(gbag, p, c); } if (!this.forceUpgrades) { this.add(gbag, this.startupCheck, c); } this.msgLabel = new MultiLineLabel(this.calcMsg(), 5, 5); this.add(gbag, this.msgLabel, c); c.gridwidth = 2; this.add(gbag, this.getsFocus = this.yesButton, c); if (!this.forceUpgrades || this.first) { c.gridwidth = 0; this.add(gbag, this.noButton, c); c.insets.top = 5; c.insets.bottom = 15; this.add(gbag, this.moreInfo, c); } } private String calcMsg() { int totalBytes = 0; if (this.bytes != null) { for (int i = 0; i < this.bytes.size(); i++) { if (this.checkboxes.elementAt(i).getState()) { totalBytes += this.bytes.elementAt(i); } } } String msg = ""; String ins = this.forceUpgrades ? forcedInstructions : instructions; if (totalBytes == 0) { msg = Console.message("no-entries"); return "\n" + msg + " \n \n \n \n" + ins; } else { int modemMins = 10 * totalBytes / 288000 + 1; String modemMinutes = modemMins / 10 + "." + modemMins % 10 + " " + (modemMins == 10 ? Console.message("minute") : Console.message("minutes")); int cableMins = 10 * totalBytes / 1000000 + 1; String cableMinutes = cableMins / 10 + "." + cableMins % 10 + " " + (cableMins == 10 ? Console.message("minute") : Console.message("minutes")); NumberFormat nF = NumberFormat.getNumberInstance(Locale.getDefault()); String kb = nF.format((long)(totalBytes / 1000)); Object[] arguments = new Object[]{new String(kb), new String(cableMinutes), new String(modemMinutes)}; msg = MessageFormat.format(Console.message("entries-total"), arguments); return msg + ins; } } @Override public void show() { super.show(); this.getsFocus.requestFocus(); } public synchronized boolean confirmUpgrade() { while (this.isActive()) { try { this.wait(); } catch (InterruptedException var2) { } } return this.getConfirmed(); } public int confirmUpgradeFromList() { if (!this.confirmUpgrade()) { return 0; } else { int i = 0; while (i < this.bytes.size() && this.checkboxes.elementAt(i).getState()) { i++; } return i; } } public Vector rejectedList() { if (!this.confirmUpgrade()) { return this.list; } else { Vector v = new Vector(); for (int i = 0; i < this.bytes.size(); i++) { if (!this.checkboxes.elementAt(i).getState()) { v.addElement(this.list.elementAt(i)); } } return v; } } @Override protected synchronized boolean done(boolean confirmed) { boolean retCode = super.done(confirmed); this.notify(); return retCode; } @Override public boolean action(Event event, Object what) { if (event.target == this.yesButton) { return this.done(true); } else if (event.target == this.noButton) { return this.done(false); } else { if (event.target == this.moreInfo) { NetUpdate.showInfo(this.info); } else if (event.target == this.startupCheck) { IniFile.gamma().setIniInt("CheckUpgrades", this.startupCheck.getState() ? 1 : 0); } else if (event.target instanceof Checkbox) { if (this.chooseOne) { int i = this.checkboxes.indexOf(event.target); if (((Checkbox)event.target).getState()) { while (--i >= 0) { this.checkboxes.elementAt(i).setState(true); } } else { while (++i < this.checkboxes.size()) { this.checkboxes.elementAt(i).setState(false); } } } this.msgLabel.setLabel(this.calcMsg()); return true; } return false; } } @Override public boolean handleEvent(Event event) { return this.forceUpgrades && event.id == 201 ? this.done(true) : super.handleEvent(event); } @Override public boolean keyDown(Event event, int key) { return !this.forceUpgrades && key == 27 ? this.done(false) : super.keyDown(event, key); } }