1: package siege.gui.dialogs;
2:
3: import java.awt.BorderLayout;
4: import java.awt.Dimension;
5: import java.awt.Font;
6: import java.awt.event.ActionEvent;
7: import java.awt.event.ActionListener;
8: import javax.swing.BorderFactory;
9: import javax.swing.Box;
10: import javax.swing.BoxLayout;
11: import javax.swing.JButton;
12: import javax.swing.JLabel;
13: import javax.swing.JPanel;
14: import javax.swing.JTextField;
15: import javax.swing.border.EtchedBorder;
16: import siege.core.Game;
17: import siege.core.Player;
18: import siege.util.Log;
19:
20: 21: 22: 23:
24: public class DialogCreateGame extends DialogBasic
25: {
26: 27: 28:
29: public JTextField nameField;
30: public JLabel nameLabel;
31: public JPanel inputPane;
32: public JPanel buttonPane;
33: public JButton submit;
34: public JButton cancel;
35:
36: 37: 38:
39: public DialogCreateGame()
40: {
41: super();
42: setTitle("Create new game");
43:
44: 45: nameField = new JTextField();
46: nameField.setFont(new Font("SansSerif", Font.TRUETYPE_FONT, 14));
47: nameField.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
48:
49: 50: nameLabel = new JLabel("What's your name?");
51: nameLabel.setFont(new Font("SansSerif", Font.BOLD, 14));
52: nameLabel.setLabelFor(nameField);
53:
54: 55: inputPane = new JPanel();
56: inputPane.setLayout(new BoxLayout(inputPane, BoxLayout.Y_AXIS));
57: inputPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
58: inputPane.add(nameLabel);
59: inputPane.add(Box.createRigidArea(new Dimension(0, 10)));
60: inputPane.add(nameField);
61:
62: 63: submit = new JButton("Create new game");
64: cancel = new JButton("Cancel");
65:
66: 67: buttonPane = new JPanel();
68: buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
69: buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
70: buttonPane.add(Box.createHorizontalGlue());
71: buttonPane.add(submit);
72: buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
73: buttonPane.add(cancel);
74:
75: 76: setLayout(new BorderLayout());
77: add(inputPane, BorderLayout.CENTER);
78: add(buttonPane, BorderLayout.PAGE_END);
79:
80:
81: 82: nameField.addActionListener(new ActionListener() {
83: public void actionPerformed(ActionEvent e) {
84: submitDialog();
85: }
86: });
87: submit.addActionListener(new ActionListener() {
88: public void actionPerformed(ActionEvent e) {
89: submitDialog();
90: }
91: });
92: cancel.addActionListener(new ActionListener() {
93: public void actionPerformed(ActionEvent e) {
94: closeDialog();
95: }
96: });
97:
98: 99: finalDialogSettings();
100: }
101:
102: 103: 104:
105: public void submitDialog() {
106: Log.message(this, "submitDialog");
107: if(nameField.getText().isEmpty()) {
108: Log.message(this, "No name written");
109: showAlert("Please write your name");
110: } else {
111: Game game = Game.getInstance();
112: Player localPlayer = game.getLocalPlayer();
113: localPlayer.setName(nameField.getText());
114: game.setChallenger(localPlayer);
115: game.connect();
116: closeDialog();
117: }
118: }
119: }
120: