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

Class Class, % Method, % Branch, % Line, %
StartPanel 0% (0/1) 0% (0/5) 0% (0/8) 0% (0/56)


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.KeepAliveSocketWrapper; 6 import it.polimi.ingsw.Network.SocketWrapper; 7 import it.polimi.ingsw.Server.Messages.ServerResponses.SupportStructures.StatusCode; 8 import it.polimi.ingsw.Server.Messages.ServerResponses.Welcome; 9  10 import javax.swing.*; 11 import java.awt.*; 12 import java.net.Socket; 13  14 /** 15  * Panel that allows the user to connect to a Server by selecting ip address and port 16  */ 17 public class StartPanel extends JPanel { 18  /** 19  * Create a new StartPanel 20  * 21  * @param ctx Context that will be used by GUI's panels 22  */ 23  public StartPanel(Context ctx) { 24  // labels 25  JLabel title = new JLabel("Eriantys welcomes You!"); 26  title.setFont(new Font("SansSerif", Font.BOLD, 40)); 27  title.setForeground(Color.WHITE); 28  JLabel serverAddressLabel = new JLabel("Server address:", SwingConstants.RIGHT); 29  serverAddressLabel.setFont(new Font("SansSerif", Font.PLAIN, 20)); 30  serverAddressLabel.setForeground(Color.WHITE); 31  JLabel serverPortLabel = new JLabel("Server port:", SwingConstants.RIGHT); 32  serverPortLabel.setFont(new Font("SansSerif", Font.PLAIN, 20)); 33  serverPortLabel.setForeground(Color.WHITE); 34  35  // text fields 36  JTextField address = new JTextField("localhost", 10); 37  JTextField port = new JTextField("8080", 10); 38  39  // buttons 40  JButton connect = new JButton("Connect"); 41  42  // adding all elements to the view 43  this.add(title); 44  this.add(serverAddressLabel); 45  this.add(serverPortLabel); 46  this.add(address); 47  this.add(port); 48  this.add(connect); 49  50  // setting correct focus 51  address.requestFocusInWindow(); 52  53  // actionListeners 54  // enter on address moves focus to port 55  address.addActionListener((actionEvent -> port.requestFocusInWindow())); 56  // enter on port moves focus to connect 57  port.addActionListener((actionEvent -> connect.requestFocusInWindow())); 58  59  connect.addActionListener(actionEvent -> { 60  connect.setEnabled(false); 61  new Thread(() -> { 62  try { 63  SocketWrapper sw = new KeepAliveSocketWrapper(new Socket( 64  address.getText().trim(), 65  Integer.parseInt(port.getText().trim())), 5000, true); 66  if (sw.awaitMessage() instanceof Welcome welcome && welcome.getStatusCode() == StatusCode.Success) { 67  // spawn and change to next view 68  ctx.setSocketWrapper(sw); 69  ctx.getWindow().changeView(new UserCredentialsPanel(ctx)); 70  } else { 71  JOptionPane.showMessageDialog(null, "Server did not welcome us", "Warning", JOptionPane.INFORMATION_MESSAGE); 72  connect.setEnabled(true); 73  } 74  } catch (Exception e) { 75  JOptionPane.showMessageDialog(null, "No valid server was found", "Warning", JOptionPane.INFORMATION_MESSAGE); 76  connect.setEnabled(true); 77  } 78  }).start(); 79  }); 80  81  // layout object declaration and setup 82  SpringLayout layout = new SpringLayout(); 83  84  layout.putConstraint(SpringLayout.VERTICAL_CENTER, title, 340, SpringLayout.NORTH, this); 85  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0, SpringLayout.HORIZONTAL_CENTER, this); 86  87  layout.putConstraint(SpringLayout.VERTICAL_CENTER, serverAddressLabel, 60, SpringLayout.VERTICAL_CENTER, title); 88  layout.putConstraint(SpringLayout.EAST, serverAddressLabel, -10, SpringLayout.HORIZONTAL_CENTER, title); 89  layout.putConstraint(SpringLayout.VERTICAL_CENTER, address, 0, SpringLayout.VERTICAL_CENTER, serverAddressLabel); 90  layout.putConstraint(SpringLayout.WEST, address, 10, SpringLayout.HORIZONTAL_CENTER, title); 91  92  layout.putConstraint(SpringLayout.VERTICAL_CENTER, serverPortLabel, 20, SpringLayout.VERTICAL_CENTER, serverAddressLabel); 93  layout.putConstraint(SpringLayout.EAST, serverPortLabel, -10, SpringLayout.HORIZONTAL_CENTER, title); 94  layout.putConstraint(SpringLayout.VERTICAL_CENTER, port, 0, SpringLayout.VERTICAL_CENTER, serverPortLabel); 95  layout.putConstraint(SpringLayout.WEST, port, 10, SpringLayout.HORIZONTAL_CENTER, title); 96  97  layout.putConstraint(SpringLayout.VERTICAL_CENTER, connect, 40, SpringLayout.VERTICAL_CENTER, serverPortLabel); 98  layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, connect, 0, SpringLayout.HORIZONTAL_CENTER, this); 99  100  // apply layout 101  this.setLayout(layout); 102  } 103  104  @Override 105  protected void paintComponent(Graphics g) { 106  super.paintComponent(g); 107  assert IconLoader.userCredentialBackground != null; 108  g.drawImage(IconLoader.userCredentialBackground.getImage(), 0, 0, null); 109  } 110 }