summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/ConnectPanel.java
blob: 5c74a66c547ab503932ca76a2398b87ab27fe7af (plain)
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
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.view.IConnectPanel;
import jrummikub.view.LoginError;

/**
 * Panel shown during network connecting process and showing errors
 */
public class ConnectPanel extends JPanel implements IConnectPanel {
	private static final long serialVersionUID = 1L;
	private JLabel messageLabel;
	private JProgressBar foobar;
	private JButton cancelButton;

	private Event cancelEvent = new Event();

	/**
	 * Creates new connection panel
	 */
	ConnectPanel() {
		setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 1;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;

		messageLabel = new JLabel();
		messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(messageLabel, c);

		c.gridy = 1;
		c.insets = new Insets(5, 5, 5, 5);

		foobar = new JProgressBar();
		foobar.setIndeterminate(true);
		add(foobar, c);

		c.gridy = 2;

		cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelEvent.emit();
			}
		});
		add(cancelButton, c);

		setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
				new EmptyBorder(10, 10, 10, 10)));
	}

	@Override
	public void setMode(LoginError error) {
		if (error == null) {
			foobar.setVisible(true);
			messageLabel.setText("Verbindung wird hergestellt");
			cancelButton.setText("Abbrechen");
		} else {
			foobar.setVisible(false);
			String text = "";
			switch (error) {
			case UNKNOWN_ERROR:
				text = "Ein unbekannter Fehler ist aufgetreten";
				break;
			case AUTHENTICATION_FAILED:
				text = "Die Authentifizierung ist fehlgeschlagen";
				break;
			case CONNECTION_REFUSED:
				text = "Die Verbindung wurde abgelehnt";
				break;
			case TIMEOUT:
				text = "Der Server reagiert nicht";
				break;
			case UNKNOWN_CHANNEL:
				text = "Der Channel konnte nicht gefunden werden";
				break;
			case UNKNOWN_HOST:
				text = "Der Server konnte nicht gefunden werden";
				break;
			}
			messageLabel.setText(text);
		}
	}

	@Override
	public IEvent getCancelEvent() {
		return cancelEvent;
	}

}