package NET.worlds.console; import NET.worlds.network.CacheEntry; import java.awt.Button; import java.awt.Canvas; import java.awt.Color; import java.awt.Event; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.Label; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ProgressBar extends Frame implements ActionListener { private static final long serialVersionUID = -5057984821909220829L; private int _total; private int _current = 0; private boolean _offline = false; private Canvas _barCanvas; private Label _header; private TextField _message; private Button _offlineButton; private static final int BAR_WIDTH = 250; private static final int BAR_HEIGHT = 20; private static final int BORDER = 10; public ProgressBar(String header, int total) { this._header = new Label(header); this._total = total; if (this._total < 1) { this._total = 1; } this.build(); this.setTitle("Loading Progress"); } public void advance() { this._current++; if (this._current > this._total) { this._current = this._total; } this.paintBar(); } public int current() { return this._current; } public int total() { return this._total; } public int percentComplete() { return this._current * 100 / this._total; } public boolean offline() { return this._offline; } public void setMessage(String m) { this._message.setText(m); this.update(this.getGraphics()); } protected int pixelsComplete() { return this._current * 250 / this._total; } protected void build() { this.setLayout(new GridLayout(3, 1, 10, 0)); this._barCanvas = new Canvas(); this._barCanvas.setSize(270, 40); this.add(this._barCanvas); this._message = new TextField("Working..."); this._message.setEditable(false); this.add(this._message); Panel buttonPanel = new Panel(); this._offlineButton = new Button("Go Offline"); this._offlineButton.addActionListener(this); buttonPanel.add(this._offlineButton); this.add(buttonPanel); this.pack(); } @Override public void paint(Graphics g) { this.paintBar(); } @Override public void actionPerformed(ActionEvent ae) { if (ae.getSource() == this._offlineButton) { this._offline = true; CacheEntry.setOffline(); this._offlineButton.setEnabled(false); } } @Override public boolean handleEvent(Event e) { if (e.id == 201) { this.setVisible(false); this.dispose(); Main.end(); throw new Error("User cancelled startup."); } else { return super.handleEvent(e); } } protected void paintBar() { Graphics g = this._barCanvas.getGraphics(); Image off = this.createImage(270, 40); Graphics offG = off.getGraphics(); offG.setColor(this.getBackground()); offG.fillRect(0, 0, 270, 40); offG.setColor(Color.gray); offG.fillRect(10, 10, this.pixelsComplete(), 20); offG.setColor(Color.darkGray); offG.draw3DRect(11, 11, this.pixelsComplete() - 1, 18, true); offG.setColor(Color.black); offG.drawRect(10, 10, 250, 20); String perString = Integer.toString(this.percentComplete()) + "% Complete"; g.setFont(new Font("Times Roman", 0, 12)); FontMetrics fm = g.getFontMetrics(); int halfHeight = fm.getHeight() / 2; int halfWidth = fm.stringWidth(perString) / 2; offG.drawString(perString, 135 - halfWidth, 20 + halfHeight); g.drawImage(off, 0, 0, null); offG.dispose(); off.flush(); } }