1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2010 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.client.gui;
14 
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 
18 import javax.swing.BorderFactory;
19 import javax.swing.Box;
20 import javax.swing.BoxLayout;
21 import javax.swing.JDialog;
22 import javax.swing.JLabel;
23 import javax.swing.JPanel;
24 import javax.swing.JProgressBar;
25 import javax.swing.Timer;
26 
27 public class ProgressBar extends JDialog {
28 	private static final long serialVersionUID = 6241161656154797719L;
29 	/** Default delay between updating */
30 	private static final int SLEEP_TIME = 200;
31 	/** Maximum value for the progress bar */
32 	private static final int MAX_VALUE = 100;
33 	/** Default step size */
34 	private static final int STEP_SIZE = MAX_VALUE / 50;
35 
36 	private JProgressBar progressBar;
37 
38 	/** Speed factor for updating the bar */
39 	private int stepSizeMultiplier = 1;
40 	/**
41 	 * Keeps track of how many times it has looped with a multiplier greater
42 	 * than 0
43 	 */
44 	private int stepCounter;
45 
46 	private final Timer timer = new Timer(SLEEP_TIME, new Updater());
47 
48 	/**
49 	 * Create a new ProgressBar.
50 	 *
51 	 * @param w parent dialog
52 	 */
ProgressBar(final JDialog w)53 	public ProgressBar(final JDialog w) {
54 		super(w, "Connecting...", true);
55 		initializeComponents();
56 		this.pack();
57 		setLocationRelativeTo(w);
58 	}
59 
initializeComponents()60 	private void initializeComponents() {
61 		JPanel contentPane = (JPanel) this.getContentPane();
62 
63 		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
64 		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
65 
66 		contentPane.add(new JLabel("Connecting..."));
67 		contentPane.add(Box.createVerticalStrut(5));
68 
69 		progressBar = new JProgressBar(0, MAX_VALUE);
70 		progressBar.setStringPainted(false);
71 		contentPane.add(progressBar);
72 	}
73 
74 	/**
75 	 * Timer task that updates the progress bar.
76 	 */
77 	private class Updater implements ActionListener {
78 		private int counter = 0;
79 
80 		@Override
actionPerformed(ActionEvent arg0)81 		public void actionPerformed(ActionEvent arg0) {
82 			counter += STEP_SIZE * stepSizeMultiplier;
83 			progressBar.setValue(counter);
84 			if (stepCounter >= 0) {
85 				if (stepCounter == 0) {
86 					stepSizeMultiplier = 1;
87 				}
88 				stepCounter--;
89 			}
90 			if (counter > 100) {
91 				cancel();
92 			}
93 		}
94 	}
95 
96 	/** Start updating the progress bar */
start()97 	public void start() {
98 		timer.start();
99 		setVisible(true);
100 	}
101 
102 	/**
103 	 * Temporarily speeds up the bar.
104 	 */
step()105 	public void step() {
106 		stepCounter = 3;
107 		stepSizeMultiplier = 3;
108 	}
109 
110 	/**
111 	 *  Speeds up to quickly finish.
112 	 */
finish()113 	public void finish() {
114 		stepCounter = 20;
115 		stepSizeMultiplier = 3;
116 		timer.setDelay(15);
117 	}
118 
119 	/**
120 	 * Exits quickly.
121 	 */
cancel()122 	public void cancel() {
123 		timer.stop();
124 		// workaround near failures in AWT at openjdk (tested on openjdk-1.6.0.0)
125 		try {
126 		    this.dispose();
127 		} catch(NullPointerException npe) {
128 			return;
129 		}
130 	}
131 }
132