Coverage Summary for Class: LobbySelectionPanel (it.polimi.ingsw.Client.GUI.Panels)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| LobbySelectionPanel | 0% (0/1) | 0% (0/6) | 0% (0/12) | 0% (0/147) |
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.Network.SocketWrapper; 6 import it.polimi.ingsw.Server.Messages.Events.Requests.ConnectLobbyRequest; 7 import it.polimi.ingsw.Server.Messages.Events.Requests.CreateLobbyRequest; 8 import it.polimi.ingsw.Server.Messages.Message; 9 import it.polimi.ingsw.Server.Messages.ServerResponses.LobbyConnected; 10 import it.polimi.ingsw.Server.Messages.ServerResponses.SupportStructures.LobbyInfo; 11 import it.polimi.ingsw.Server.Messages.ServerResponses.SupportStructures.StatusCode; 12 13 import javax.swing.*; 14 import java.awt.*; 15 import java.io.IOException; 16 import java.util.List; 17 import java.util.UUID; 18 19 /** 20 * JTabbedPane that allows the user to create or connect to a lobby 21 */ 22 public class LobbySelectionPanel extends JTabbedPane { 23 /** 24 * Create a new LobbySelectionPanel 25 * 26 * @param ctx context that will be used by GUI's game's panels 27 * @param publicLobbies list of publicLobbies available at that moment 28 */ 29 public LobbySelectionPanel(Context ctx, List<LobbyInfo> publicLobbies) { 30 // unwrapping context into useful variables 31 Window window = ctx.getWindow(); 32 SocketWrapper sw = ctx.getSocketWrapper(); 33 34 // tabbed pane tabs 35 JPanel connectPanel = new JPanel(); 36 JPanel createPanel = new JPanel(); 37 38 // adding tabs to the pane 39 this.add("Connect", connectPanel); 40 this.add("Create", createPanel); 41 42 // connect tab setup 43 { 44 // labels 45 JLabel title = new JLabel("Select a lobby from the list, or input a lobby id to connect to."); 46 JLabel publicLobbiesLabel = new JLabel("These are all the public lobbies you can connect to:"); 47 JLabel lobbyIDLabel = new JLabel("Connecting to:", SwingConstants.RIGHT); 48 49 // text fields 50 JTextField lobbyID = new JTextField(30); 51 52 // buttons 53 JButton connect = new JButton("Connect"); 54 55 // list cell renderer 56 ListCellRenderer<LobbyInfo> cellRenderer = (list, info, index, isSelected, _ignored) -> { 57 JLabel displayedText = new JLabel(); 58 displayedText.setText( 59 "ID: " + info.getID() + 60 " || Admin: " + info.getAdmin() + 61 " || Size: " + (info.getPlayers().size()) + "/" + info.getMaxPlayers()); 62 if (isSelected) { 63 displayedText.setBackground(list.getSelectionBackground()); 64 displayedText.setForeground(list.getSelectionForeground()); 65 } else { 66 displayedText.setBackground(list.getBackground()); 67 displayedText.setForeground(list.getForeground()); 68 } 69 displayedText.setOpaque(true); 70 return displayedText; 71 }; 72 73 // list of public lobbies 74 JList<LobbyInfo> publicLobbiesList = new JList<>(publicLobbies.toArray(LobbyInfo[]::new)); 75 publicLobbiesList.setLayoutOrientation(JList.VERTICAL); 76 publicLobbiesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 77 publicLobbiesList.setCellRenderer(cellRenderer); 78 79 // wrapping the list of lobbies in a scrollable panel 80 JScrollPane scrollablePublicLobbiesList = new JScrollPane(); 81 scrollablePublicLobbiesList.setViewportView(publicLobbiesList); 82 scrollablePublicLobbiesList.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 83 scrollablePublicLobbiesList.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 84 scrollablePublicLobbiesList.setPreferredSize(new Dimension(720, 100)); 85 86 // adding all elements to the view 87 connectPanel.add(title); 88 connectPanel.add(publicLobbiesLabel); 89 connectPanel.add(lobbyIDLabel); 90 connectPanel.add(lobbyID); 91 connectPanel.add(connect); 92 connectPanel.add(scrollablePublicLobbiesList); 93 94 // setting correct focus 95 scrollablePublicLobbiesList.requestFocusInWindow(); 96 97 // actionListeners 98 publicLobbiesList.addListSelectionListener(listSelectionEvent -> { 99 int selectedIndex = publicLobbiesList.getSelectedIndex(); 100 lobbyID.setText(publicLobbies.get(selectedIndex).getID().toString()); 101 }); 102 lobbyID.addActionListener(actionEvent -> { 103 lobbyID.setText(lobbyID.getText().trim()); 104 connect.requestFocusInWindow(); 105 }); 106 connect.addActionListener(actionEvent -> { 107 connect.setEnabled(false); 108 // normalize id 109 String idString = lobbyID.getText().trim(); 110 lobbyID.setText(idString); 111 UUID id = UUID.fromString(idString); 112 try { 113 sw.sendMessage(new ConnectLobbyRequest(id)); 114 Message response = sw.awaitMessage(); 115 if (response instanceof LobbyConnected lobbyConnected) { 116 if (lobbyConnected.getStatusCode() == StatusCode.Success) { 117 //Switch to a new LobbySelectionPanel if user has been accepted by Server 118 window.changeView(new GameStartingPanel(ctx, false, lobbyConnected.getLobbyID())); 119 } else { 120 JOptionPane.showMessageDialog(null, "Try again.", "Error", JOptionPane.INFORMATION_MESSAGE); 121 connect.setEnabled(true); 122 } 123 } else { 124 throw new IllegalStateException("Unexpected value: " + response); 125 } 126 } catch (Exception e) { 127 JOptionPane.showMessageDialog(null, "Error in the connection with the server", 128 "Error", JOptionPane.INFORMATION_MESSAGE); 129 try { 130 sw.close(); 131 } catch (IOException ex) { 132 throw new RuntimeException(ex); 133 } 134 window.changeView(new StartPanel(ctx)); 135 } 136 }); 137 138 // layout object decleration and setup 139 SpringLayout layout = new SpringLayout(); 140 141 layout.putConstraint(SpringLayout.VERTICAL_CENTER, title, 20, SpringLayout.NORTH, connectPanel); 142 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0, SpringLayout.HORIZONTAL_CENTER, connectPanel); 143 144 layout.putConstraint(SpringLayout.VERTICAL_CENTER, publicLobbiesLabel, 20, SpringLayout.SOUTH, title); 145 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, publicLobbiesLabel, 0, SpringLayout.HORIZONTAL_CENTER, connectPanel); 146 layout.putConstraint(SpringLayout.NORTH, scrollablePublicLobbiesList, 20, SpringLayout.VERTICAL_CENTER, publicLobbiesLabel); 147 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, scrollablePublicLobbiesList, 0, SpringLayout.HORIZONTAL_CENTER, connectPanel); 148 149 layout.putConstraint(SpringLayout.VERTICAL_CENTER, lobbyIDLabel, 20, SpringLayout.SOUTH, scrollablePublicLobbiesList); 150 layout.putConstraint(SpringLayout.EAST, lobbyIDLabel, -10, SpringLayout.HORIZONTAL_CENTER, connectPanel); 151 layout.putConstraint(SpringLayout.VERTICAL_CENTER, lobbyID, 0, SpringLayout.VERTICAL_CENTER, lobbyIDLabel); 152 layout.putConstraint(SpringLayout.WEST, lobbyID, 10, SpringLayout.HORIZONTAL_CENTER, connectPanel); 153 154 layout.putConstraint(SpringLayout.VERTICAL_CENTER, connect, 40, SpringLayout.VERTICAL_CENTER, lobbyIDLabel); 155 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, connect, 0, SpringLayout.HORIZONTAL_CENTER, connectPanel); 156 157 // apply layout 158 connectPanel.setLayout(layout); 159 } 160 161 // create tab setup 162 { 163 // labels 164 JLabel title = new JLabel("Create a lobby"); 165 JLabel openLobbyLabel = new JLabel("Public lobby:", SwingConstants.RIGHT); 166 JLabel maxPlayersLabel = new JLabel("Max Players:", SwingConstants.RIGHT); 167 168 // buttons 169 JButton create = new JButton("Create"); 170 JCheckBox openLobby = new JCheckBox(); 171 JRadioButton maxPlayers_2 = new JRadioButton("2"); 172 maxPlayers_2.setActionCommand("2"); 173 JRadioButton maxPlayers_3 = new JRadioButton("3"); 174 maxPlayers_3.setActionCommand("3"); 175 JRadioButton maxPlayers_4 = new JRadioButton("4"); 176 maxPlayers_4.setActionCommand("4"); 177 178 // radio buttons get grouped up 179 ButtonGroup maxPlayers = new ButtonGroup(); 180 maxPlayers_2.setSelected(true); 181 maxPlayers.add(maxPlayers_2); 182 maxPlayers.add(maxPlayers_3); 183 maxPlayers.add(maxPlayers_4); 184 185 // adding all elements to the view 186 createPanel.add(title); 187 createPanel.add(openLobbyLabel); 188 createPanel.add(maxPlayersLabel); 189 createPanel.add(create); 190 createPanel.add(openLobby); 191 createPanel.add(maxPlayers_2); 192 createPanel.add(maxPlayers_3); 193 createPanel.add(maxPlayers_4); 194 195 create.addActionListener(actionEvent -> { 196 create.setEnabled(false); 197 // normalize id 198 try { 199 sw.sendMessage(new CreateLobbyRequest( 200 openLobby.isSelected(), 201 Integer.parseInt(maxPlayers.getSelection().getActionCommand()) 202 )); 203 boolean again = true; 204 do { 205 Message response = sw.awaitMessage(); 206 if (response instanceof LobbyConnected lobbyConnected) { 207 if (lobbyConnected.getStatusCode() == StatusCode.Success) { 208 //Switch to a new LobbySelectionPanel if user has been accepted by Server 209 window.changeView(new GameStartingPanel(ctx, true, lobbyConnected.getLobbyID())); 210 again = false; 211 } else { 212 JOptionPane.showMessageDialog(null, "Try again.", "Error", JOptionPane.INFORMATION_MESSAGE); 213 create.setEnabled(true); 214 } 215 } else { 216 throw new IllegalStateException("Unexpected value: " + response); 217 } 218 } while (again); 219 } catch (Exception e) { 220 JOptionPane.showMessageDialog(null, "Error in the connection with the server", 221 "Error", JOptionPane.INFORMATION_MESSAGE); 222 try { 223 sw.close(); 224 } catch (IOException ex) { 225 throw new RuntimeException(ex); 226 } 227 window.changeView(new StartPanel(ctx)); 228 } 229 }); 230 231 // layout object decleration and setup 232 SpringLayout layout = new SpringLayout(); 233 234 layout.putConstraint(SpringLayout.VERTICAL_CENTER, title, 20, SpringLayout.NORTH, createPanel); 235 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0, SpringLayout.HORIZONTAL_CENTER, createPanel); 236 237 layout.putConstraint(SpringLayout.VERTICAL_CENTER, openLobbyLabel, 20, SpringLayout.SOUTH, title); 238 layout.putConstraint(SpringLayout.EAST, openLobbyLabel, -10, SpringLayout.HORIZONTAL_CENTER, createPanel); 239 layout.putConstraint(SpringLayout.VERTICAL_CENTER, openLobby, 0, SpringLayout.VERTICAL_CENTER, openLobbyLabel); 240 layout.putConstraint(SpringLayout.WEST, openLobby, 10, SpringLayout.HORIZONTAL_CENTER, createPanel); 241 242 layout.putConstraint(SpringLayout.VERTICAL_CENTER, maxPlayersLabel, 20, SpringLayout.SOUTH, openLobbyLabel); 243 layout.putConstraint(SpringLayout.EAST, maxPlayersLabel, -10, SpringLayout.HORIZONTAL_CENTER, createPanel); 244 layout.putConstraint(SpringLayout.VERTICAL_CENTER, maxPlayers_2, 0, SpringLayout.VERTICAL_CENTER, maxPlayersLabel); 245 layout.putConstraint(SpringLayout.WEST, maxPlayers_2, 10, SpringLayout.HORIZONTAL_CENTER, createPanel); 246 layout.putConstraint(SpringLayout.VERTICAL_CENTER, maxPlayers_3, 0, SpringLayout.VERTICAL_CENTER, maxPlayersLabel); 247 layout.putConstraint(SpringLayout.WEST, maxPlayers_3, 10, SpringLayout.EAST, maxPlayers_2); 248 layout.putConstraint(SpringLayout.VERTICAL_CENTER, maxPlayers_4, 0, SpringLayout.VERTICAL_CENTER, maxPlayersLabel); 249 layout.putConstraint(SpringLayout.WEST, maxPlayers_4, 10, SpringLayout.EAST, maxPlayers_3); 250 251 layout.putConstraint(SpringLayout.VERTICAL_CENTER, create, 40, SpringLayout.VERTICAL_CENTER, maxPlayersLabel); 252 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, create, 0, SpringLayout.HORIZONTAL_CENTER, createPanel); 253 254 // apply layout 255 createPanel.setLayout(layout); 256 } 257 } 258 }