Coverage Summary for Class: UserCredentialsPanel (it.polimi.ingsw.Client.GUI.Panels)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| UserCredentialsPanel | 0% (0/1) | 0% (0/6) | 0% (0/10) | 0% (0/60) |
1 package it.polimi.ingsw.Client.GUI.Panels; 2 3 import it.polimi.ingsw.Client.GUI.Context; 4 import it.polimi.ingsw.Client.GUI.IconLoader; 5 import it.polimi.ingsw.Network.SocketWrapper; 6 import it.polimi.ingsw.Server.Messages.Events.Requests.DeclarePlayerRequest; 7 import it.polimi.ingsw.Server.Messages.Message; 8 import it.polimi.ingsw.Server.Messages.ServerResponses.LobbyServerAccept; 9 import it.polimi.ingsw.Server.Messages.ServerResponses.SupportStructures.StatusCode; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.io.IOException; 14 15 /** 16 * Panel that allows user to choose his username 17 */ 18 public class UserCredentialsPanel extends JPanel { 19 /** 20 * Create a new UserCredentialsPanel 21 * 22 * @param ctx context received form StartPanel 23 */ 24 public UserCredentialsPanel(Context ctx) { 25 // unwrapping context into useful variables 26 SocketWrapper sw = ctx.getSocketWrapper(); 27 28 // labels 29 JLabel title = new JLabel("Login"); 30 title.setFont(new Font("SansSerif", Font.BOLD, 50)); 31 title.setForeground(Color.WHITE); 32 JLabel usernameLabel = new JLabel("Username:", SwingConstants.RIGHT); 33 usernameLabel.setFont(new Font("SansSerif", Font.PLAIN, 20)); 34 usernameLabel.setForeground(Color.WHITE); 35 36 // text fields 37 JTextField username = new JTextField("guest" + (int) (Math.random() * 1000), 10); 38 39 // buttons 40 JButton login = new JButton("Login"); 41 42 // adding all elements to the view 43 this.add(title); 44 this.add(usernameLabel); 45 this.add(username); 46 this.add(login); 47 48 // setting correct focus 49 username.requestFocusInWindow(); 50 51 // actionListeners 52 username.addActionListener((actionEvent -> { 53 username.setText(username.getText().trim()); 54 login.requestFocusInWindow(); 55 })); 56 login.addActionListener(actionEvent -> { 57 login.setEnabled(false); 58 // normalize username 59 ctx.setNickname(username.getText().trim()); 60 username.setText(ctx.getNickname()); 61 try { 62 sw.sendMessage(new DeclarePlayerRequest(ctx.getNickname())); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 new Thread(() -> { 67 try { 68 boolean again = true; 69 do { 70 Message response = sw.awaitMessage(); 71 if (response instanceof LobbyServerAccept lobbyAccept) { 72 if (lobbyAccept.getStatusCode() == StatusCode.Success) { 73 //Switch to a new LobbySelectionPanel if user has been accepted by Server 74 ctx.getWindow().changeView(new LobbySelectionPanel(ctx, lobbyAccept.getPublicLobbies())); 75 again = false; 76 } else { 77 JOptionPane.showMessageDialog(null, "Server denied your login", 78 "Warning", JOptionPane.INFORMATION_MESSAGE); 79 login.setEnabled(true); 80 } 81 } else { 82 throw new IllegalStateException("Unexpected value: " + response); 83 } 84 } while (again); 85 } catch (Exception e) { 86 JOptionPane.showMessageDialog(null, "Error in the connection with the server", 87 "Warning", JOptionPane.INFORMATION_MESSAGE); 88 try { 89 sw.close(); 90 } catch (IOException ex) { 91 throw new RuntimeException(ex); 92 } 93 ctx.getWindow().changeView(new StartPanel(ctx)); 94 } 95 }).start(); 96 }); 97 98 // layout object declaration and setup 99 SpringLayout layout = new SpringLayout(); 100 101 layout.putConstraint(SpringLayout.VERTICAL_CENTER, title, 340, SpringLayout.NORTH, this); 102 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0, SpringLayout.HORIZONTAL_CENTER, this); 103 104 layout.putConstraint(SpringLayout.VERTICAL_CENTER, usernameLabel, 65, SpringLayout.VERTICAL_CENTER, title); 105 layout.putConstraint(SpringLayout.EAST, usernameLabel, -10, SpringLayout.HORIZONTAL_CENTER, title); 106 layout.putConstraint(SpringLayout.VERTICAL_CENTER, username, 0, SpringLayout.VERTICAL_CENTER, usernameLabel); 107 layout.putConstraint(SpringLayout.WEST, username, 10, SpringLayout.HORIZONTAL_CENTER, title); 108 109 layout.putConstraint(SpringLayout.VERTICAL_CENTER, login, 40, SpringLayout.VERTICAL_CENTER, username); 110 layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, login, 0, SpringLayout.HORIZONTAL_CENTER, this); 111 112 // apply layout 113 this.setLayout(layout); 114 } 115 116 @Override 117 protected void paintComponent(Graphics g) { 118 super.paintComponent(g); 119 assert IconLoader.userCredentialBackground != null; 120 g.drawImage(IconLoader.userCredentialBackground.getImage(), 0, 0, null); 121 } 122 }