Coverage Summary for Class: GameStartingPanel (it.polimi.ingsw.Client.GUI.Panels)

Class Class, % Method, % Branch, % Line, %
GameStartingPanel 0% (0/1) 0% (0/5) 0% (0/11) 0% (0/104)


1 package it.polimi.ingsw.Client.GUI.Panels; 2  3 import it.polimi.ingsw.Client.GUI.Context; 4 import it.polimi.ingsw.Client.GUI.Window; 5 import it.polimi.ingsw.Model.Enums.GameMode; 6 import it.polimi.ingsw.Network.SocketWrapper; 7 import it.polimi.ingsw.Server.Messages.Events.Requests.StartGameRequest; 8 import it.polimi.ingsw.Server.Messages.Message; 9 import it.polimi.ingsw.Server.Messages.ServerResponses.*; 10 import it.polimi.ingsw.Server.Messages.ServerResponses.SupportStructures.StatusCode; 11  12 import javax.swing.*; 13 import java.awt.*; 14 import java.io.IOException; 15 import java.util.UUID; 16  17 /** 18  * Panel that allows lobby's admin to start the game (is also possible to select gameMode) after it has been filled 19  */ 20 public class GameStartingPanel extends JPanel { 21  /** 22  * Create a new GameStartingPanel 23  * 24  * @param ctx context that will be used by GUI's game's panels 25  * @param isAdmin GameStartingPanel has been created by lobby's admin 26  * @param lobbyID Lobby's UUID 27  */ 28  public GameStartingPanel(Context ctx, boolean isAdmin, UUID lobbyID) { 29  // unwrapping context into useful variables 30  Window window = ctx.getWindow(); 31  SocketWrapper sw = ctx.getSocketWrapper(); 32  33  // labels 34  JLabel title = new JLabel("Connected to the lobby"); 35  JLabel lobbyIDlabel = new JLabel("Connected to the lobby"); 36  JLabel connectedPlayersLablel = new JLabel("Players in lobby:"); 37  JLabel gameModeLabel = new JLabel("Advanced mode:"); 38  39  // text boxes 40  JTextField lobbyIDField = new JTextField(); 41  lobbyIDField.setText(lobbyID.toString()); 42  lobbyIDField.setEditable(false); 43  44  // buttons 45  JCheckBox gameMode = new JCheckBox(); 46  gameMode.setEnabled(isAdmin); 47  gameMode.setToolTipText("Only the lobby admin can select the game mode"); 48  JButton disconnect = new JButton("Disconnect from the lobby"); 49  JButton start = new JButton("Start the game"); 50  start.setToolTipText("Only the lobby admin can start the lobby"); 51  start.setEnabled(isAdmin); // start button is enabled only for admin of the lobby 52  53  // list cell renderer 54  ListCellRenderer<String> cellRenderer = (list, nick, index, isSelected, _ignored) -> { 55  JLabel displayedText = new JLabel(); 56  displayedText.setText(nick); 57  //enable this code to let selections be shown 58  if (isSelected) { 59  displayedText.setBackground(list.getSelectionBackground()); 60  displayedText.setForeground(list.getSelectionForeground()); 61  } else { 62  displayedText.setBackground(list.getBackground()); 63  displayedText.setForeground(list.getForeground()); 64  } 65  displayedText.setOpaque(true); 66  return displayedText; 67  }; 68  69  // list of connected players 70  JList<String> connectedPlayersList = new JList<>(new String[]{}); 71  connectedPlayersList.setLayoutOrientation(JList.VERTICAL); 72  connectedPlayersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 73  connectedPlayersList.setCellRenderer(cellRenderer); 74  connectedPlayersList.setPreferredSize(new Dimension(720, 100)); 75  76  // adding all elements to the view 77  this.add(title); 78  this.add(disconnect); 79  this.add(start); 80  this.add(connectedPlayersLablel); 81  this.add(connectedPlayersList); 82  this.add(lobbyIDlabel); 83  this.add(lobbyIDField); 84  this.add(gameModeLabel); 85  this.add(gameMode); 86  87  disconnect.addActionListener(actionEvent -> { 88  try { 89  sw.close(); 90  } catch (IOException e) { 91  throw new RuntimeException(e); 92  } 93  }); 94  95  start.addActionListener(actionEvent -> { 96  try { 97  sw.sendMessage(new StartGameRequest(gameMode.isSelected() ? GameMode.ADVANCED : GameMode.SIMPLE)); 98  } catch (IOException e) { 99  throw new RuntimeException(e); 100  } 101  }); 102  103  // layout object declaration and setup 104  SpringLayout layout = new SpringLayout(); 105  106  layout.putConstraint(SpringLayout.VERTICAL_CENTER, title, 20, SpringLayout.NORTH, this); 107  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0, SpringLayout.HORIZONTAL_CENTER, this); 108  109  layout.putConstraint(SpringLayout.NORTH, lobbyIDlabel, 20, SpringLayout.SOUTH, title); 110  layout.putConstraint(SpringLayout.EAST, lobbyIDlabel, -10, SpringLayout.HORIZONTAL_CENTER, this); 111  layout.putConstraint(SpringLayout.VERTICAL_CENTER, lobbyIDField, 0, SpringLayout.VERTICAL_CENTER, lobbyIDlabel); 112  layout.putConstraint(SpringLayout.WEST, lobbyIDField, 10, SpringLayout.HORIZONTAL_CENTER, this); 113  114  layout.putConstraint(SpringLayout.NORTH, connectedPlayersLablel, 20, SpringLayout.SOUTH, lobbyIDField); 115  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, connectedPlayersLablel, 0, SpringLayout.HORIZONTAL_CENTER, this); 116  layout.putConstraint(SpringLayout.NORTH, connectedPlayersList, 20, SpringLayout.SOUTH, connectedPlayersLablel); 117  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, connectedPlayersList, 0, SpringLayout.HORIZONTAL_CENTER, this); 118  119  layout.putConstraint(SpringLayout.NORTH, gameModeLabel, 20, SpringLayout.SOUTH, connectedPlayersList); 120  layout.putConstraint(SpringLayout.EAST, gameModeLabel, -10, SpringLayout.HORIZONTAL_CENTER, this); 121  layout.putConstraint(SpringLayout.VERTICAL_CENTER, gameMode, 0, SpringLayout.VERTICAL_CENTER, gameModeLabel); 122  layout.putConstraint(SpringLayout.WEST, gameMode, 10, SpringLayout.HORIZONTAL_CENTER, this); 123  layout.putConstraint(SpringLayout.NORTH, start, 20, SpringLayout.SOUTH, gameMode); 124  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, start, 0, SpringLayout.HORIZONTAL_CENTER, this); 125  layout.putConstraint(SpringLayout.NORTH, disconnect, 20, SpringLayout.SOUTH, start); 126  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, disconnect, 0, SpringLayout.HORIZONTAL_CENTER, this); 127  128  // apply layout 129  this.setLayout(layout); 130  131  // start socket listener task 132  new Thread(() -> { 133  while (true) { 134  try { 135  Message input = sw.awaitMessage(); 136  switch (input) { 137  case LobbyClosed ignored -> { 138  JOptionPane.showMessageDialog(null, "Lobby was closed by the server.\n" + 139  "Client is disconnecting from the server.", "Lobby closed", JOptionPane.INFORMATION_MESSAGE); 140  sw.close(); 141  window.changeView(new StartPanel(ctx)); 142  } 143  case ClientConnected clientConnected -> { 144  synchronized (connectedPlayersList) { 145  connectedPlayersList.setListData(clientConnected.getPlayers().toArray(String[]::new)); 146  } 147  } 148  case ClientDisconnected clientDisconnected -> { 149  synchronized (connectedPlayersList) { 150  connectedPlayersList.setListData(clientDisconnected.getPlayers().toArray(String[]::new)); 151  } 152  } 153  case GameInit gameInit -> { 154  if (gameInit.getStatusCode() == StatusCode.Fail) { 155  JOptionPane.showMessageDialog(null, gameInit.getErrorMessage(), "Error", JOptionPane.INFORMATION_MESSAGE); 156  } 157  } 158  case GameStarted ignored -> { 159  window.changeView(new GameInProgressPanel(ctx)); 160  return; 161  } 162  default -> throw new IllegalStateException("Unexpected value: " + input.getClass()); 163  } 164  } catch (Exception e) { 165  JOptionPane.showMessageDialog(null, "Disconnected from server", "Error", JOptionPane.INFORMATION_MESSAGE); 166  try { 167  sw.close(); 168  } catch (IOException ex) { 169  throw new RuntimeException(ex); 170  } 171  window.changeView(new StartPanel(ctx)); 172  return; 173  } 174  } 175  }).start(); 176  } 177 }