View Javadoc

1   package net.sourceforge.simplegamenet.framework.gui;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import javax.swing.*;
6   import net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  public class GameSettingsTabPanel extends JPanel implements ActionListener {
11  
12      private ClientGUIFacade clientGUIFacade = null;
13  
14      private GameSettingsPanel gameSettingsPanel = null;
15      private JButton changeGameSettingsButton = new JButton("Change settings");
16  
17      public GameSettingsTabPanel() {
18          ProportionLayout layout = new ProportionLayout();
19          layout.appendColumn(10);                                // 0 empty
20          layout.appendColumn(0, 1.0);                            // 1
21          layout.appendColumn(10);                                // 2 empty
22          layout.appendRow(10);                                // 0 empty
23          layout.appendRow(0, 1.0);                            // 1
24          layout.appendRow(10);                                // 2 empty
25          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 3
26          layout.appendRow(10);                                // 4 empty
27          setLayout(layout);
28  
29          changeGameSettingsButton.setEnabled(false);
30          changeGameSettingsButton.addActionListener(this);
31          add(changeGameSettingsButton, new ProportionConstraints(1, 3, 1, 1, 1, 3,
32                  ProportionConstraints.CENTER, ProportionConstraints.VERTICAL));
33      }
34  
35      public void gameJoined(ClientGUIFacade serverGUIFacade, GameSettingsPanel gameSettingsPanel,
36                             boolean clientIsHost) {
37          this.clientGUIFacade = serverGUIFacade;
38          this.gameSettingsPanel = gameSettingsPanel;
39          gameSettingsPanel.setEnabled(clientIsHost);
40          add(gameSettingsPanel, new ProportionConstraints(1, 1));
41          changeGameSettingsButton.setEnabled(clientIsHost);
42          validate();
43      }
44  
45      public void joinedGameQuit() {
46          changeGameSettingsButton.setEnabled(false);
47          remove(gameSettingsPanel);
48          clientGUIFacade = null;
49          gameSettingsPanel = null;
50          validate();
51      }
52  
53      public void gameSettingsUpdated(GameSettingsPanel updatedGameSettingsPanel) {
54          remove(gameSettingsPanel);
55          gameSettingsPanel = updatedGameSettingsPanel;
56          add(updatedGameSettingsPanel, new ProportionConstraints(1, 1));
57          validate();
58      }
59  
60      /***
61       * Invoked when an action occurs.
62       */
63      public void actionPerformed(ActionEvent actionEvent) {
64          if (clientGUIFacade == null || !gameSettingsPanel.areSettingsAcceptable()) {
65              return;
66          }
67          try {
68              clientGUIFacade.changeGameSettings(gameSettingsPanel);
69          } catch (IllegalStateException exception) {
70              JOptionPane.showMessageDialog(this,
71                      "Cannot change game settings while game is in progress.",
72                      "Change game settings failed", JOptionPane.ERROR_MESSAGE);
73          } catch (IllegalArgumentException exception) {
74              JOptionPane.showMessageDialog(this,
75                      "Changed game settings are not compatible with current game "
76                      + "state.",
77                      "Change game settings failed", JOptionPane.ERROR_MESSAGE);
78          }
79      }
80  
81  }