View Javadoc

1   package net.sourceforge.simplegamenet.framework.gui;
2   
3   import java.net.InetAddress;
4   import java.net.UnknownHostException;
5   import javax.swing.*;
6   import net.sourceforge.simplegamenet.specs.to.PlayerSettings;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  class JoinSettingsPanel extends JPanel {
11  
12      private ConnectionWizardDialog connectionWizardDialog;
13  
14      private JLabel nicknameLabel = new JLabel("Nickname:");
15      private JTextField nicknameTextField;
16      private JLabel inetAddressLabel = new JLabel("IP-address:");
17      private JTextField inetAddressTextField;
18      private JLabel portLabel = new JLabel("Port:");
19      private JSpinner portSpinner;
20  
21      private ConnectionConfig connectionConfig;
22  
23      JoinSettingsPanel(ConnectionWizardDialog connectionWizardDialog) {
24          this.connectionWizardDialog = connectionWizardDialog;
25          connectionConfig = connectionWizardDialog.getConnectionConfig();
26  
27          ProportionLayout layout = new ProportionLayout();
28          layout.appendColumn(10);                                 // 0 empty
29          layout.appendColumn(0, ProportionLayout.NO_PROPORTION);  // 1
30          layout.appendColumn(10);                                 // 2 empty
31          layout.appendColumn(0, 1.0);                             // 3
32          layout.appendColumn(20, ProportionLayout.NO_PROPORTION); // 4
33          layout.appendColumn(10);                                 // 5 empty
34          layout.appendRow(0, 1.0);                            // 0 empty
35          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 1
36          layout.appendRow(10);                                // 2 empty
37          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 3
38          layout.appendRow(10);                                // 4 empty
39          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 5
40          layout.appendRow(0, 1.0);                            // 6 empty
41          setLayout(layout);
42  
43          nicknameTextField = new JTextField(connectionConfig.getNickname(),
44                  PlayerSettings.MAXIMUM_NICKNAME_LENGTH);
45          inetAddressTextField = new JTextField(connectionConfig.getHostAddress(), 15);
46          SpinnerNumberModel portSpinnerModel = new SpinnerNumberModel(connectionConfig.getPort(), 0,
47                  (int) Short.MAX_VALUE, 1);
48          portSpinner = new JSpinner(portSpinnerModel);
49          portSpinner.setEditor(new JSpinner.NumberEditor(portSpinner, "#"));
50          add(nicknameLabel, new ProportionConstraints(1, 1));
51          add(nicknameTextField, new ProportionConstraints(3, 1, 2, 1, 3, 1));
52          add(inetAddressLabel, new ProportionConstraints(1, 3));
53          add(inetAddressTextField, new ProportionConstraints(3, 3, 2, 1, 3, 3));
54          add(portLabel, new ProportionConstraints(1, 5));
55          add(portSpinner, new ProportionConstraints(4, 5));
56      }
57  
58      boolean areSettingsAcceptable() {
59          try {
60              connectionConfig.setNickname(nicknameTextField.getText());
61          } catch (IllegalArgumentException e) {
62              JOptionPane.showMessageDialog(connectionWizardDialog, e.getMessage(),
63                      "Invalid settings", JOptionPane.ERROR_MESSAGE);
64              return false;
65          }
66          try {
67              String hostAddress = inetAddressTextField.getText();
68              InetAddress.getByName(hostAddress);
69              connectionConfig.setHostAddress(hostAddress);
70          } catch (UnknownHostException exception) {
71              JOptionPane.showMessageDialog(connectionWizardDialog,
72                      "Please fill in a valid IP-address.",
73                      "Invalid settings",
74                      JOptionPane.ERROR_MESSAGE);
75              return false;
76          }
77          try {
78              connectionConfig.setPort(((Integer) (portSpinner.getValue())).intValue());
79          } catch (IllegalArgumentException e) {
80              JOptionPane.showMessageDialog(connectionWizardDialog, e.getMessage(),
81                      "Invalid settings", JOptionPane.ERROR_MESSAGE);
82              return false;
83          }
84          return true;
85      }
86  
87  }