summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ProgressBar.java
blob: c4159d7a8d207fdad444b1252bf124d165def6b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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();
   }
}