Coverage Summary for Class: CloudPanel (it.polimi.ingsw.Client.GUI.Panels)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| CloudPanel | 0% (0/1) | 0% (0/3) | 0% (0/16) | 0% (0/44) |
1 package it.polimi.ingsw.Client.GUI.Panels; 2 3 import it.polimi.ingsw.Client.GUI.Components.CloudComponent; 4 import it.polimi.ingsw.Client.GUI.Listeners.GUISocketListener; 5 import it.polimi.ingsw.Controller.Actions.ChooseCloudTile; 6 import it.polimi.ingsw.Controller.Actions.EndTurnOfActionPhase; 7 import it.polimi.ingsw.Controller.Actions.MoveMotherNature; 8 import it.polimi.ingsw.Model.Cloud; 9 import it.polimi.ingsw.Model.PlayerBoard; 10 import it.polimi.ingsw.Network.SocketWrapper; 11 import it.polimi.ingsw.Server.Messages.Events.Requests.PlayerActionRequest; 12 13 import javax.swing.*; 14 import java.awt.*; 15 import java.io.IOException; 16 import java.util.ArrayList; 17 import java.util.List; 18 19 import static it.polimi.ingsw.Client.GUI.IconLoader.cloudIcon; 20 import static it.polimi.ingsw.Client.GUI.IconLoader.sky; 21 22 /** 23 * Class used for printing all game's clouds and their students. It also permits to end user's turn 24 */ 25 public class CloudPanel extends JPanel { 26 27 /** 28 * Contains GuiReader's information necessary to record user's requests during his turn 29 */ 30 private final GUISocketListener guiSocketListener; 31 32 /** 33 * Create a new JPanel and draw all clouds (and their students) 34 * 35 * @param clouds clouds from model that needs to be drawn 36 * @param currentPlayer current Player's playerBoard 37 * @param guiSocketListener guiReader necessary for checking and saving actions requested by user 38 * @param sw socketWrapper to send messages to Server 39 */ 40 public CloudPanel(List<Cloud> clouds, PlayerBoard currentPlayer, GUISocketListener guiSocketListener, SocketWrapper sw) { 41 this.guiSocketListener = guiSocketListener; 42 //create the label that contains all others components 43 JLabel backGroundLabel = new JLabel(sky); 44 backGroundLabel.setLayout(null); 45 backGroundLabel.setBounds(0, 0, 1080, 720); 46 this.add(backGroundLabel); 47 //list containing clouds' buttons 48 ArrayList<CloudComponent> cloudButtons = new ArrayList<>(clouds.size()); 49 //create endTurn's button and set its layout 50 JButton endTurnButton = new JButton("END YOUR TURN"); 51 endTurnButton.setBackground(new Color(255, 153, 51)); 52 endTurnButton.setForeground(Color.BLACK); 53 endTurnButton.setFocusPainted(false); 54 //set visible only whether the player has played a chooseCloudTile action 55 endTurnButton.setVisible(guiSocketListener.getSuccessfulRequestsByType(ChooseCloudTile.class) == 1); 56 //add on-click action listener to endTurnButton 57 endTurnButton.addActionListener(e -> { 58 // skip execution of the action if a previous action still hasn't been processed by the server 59 if (guiSocketListener.awaitingPlayerActionFeedback()) { 60 JOptionPane.showMessageDialog(null, "Please wait for the server to process your previous" + 61 "request before making a new one"); 62 return; 63 } 64 if (guiSocketListener.getSuccessfulRequestsByType(ChooseCloudTile.class) == 1) { 65 66 //create endTurn action and its playerActionRequest 67 EndTurnOfActionPhase endTurnOfActionPhase = new EndTurnOfActionPhase(currentPlayer.getId()); 68 PlayerActionRequest playerActionRequest = new PlayerActionRequest(endTurnOfActionPhase); 69 //save action inside guiReader 70 guiSocketListener.savePlayerActionRequest(endTurnOfActionPhase); 71 try { 72 sw.sendMessage(playerActionRequest); 73 } catch (IOException ex) { 74 throw new RuntimeException(ex); 75 } 76 } 77 }); 78 //for every cloud: 79 for (int i = 0; i < clouds.size(); i++) { 80 //create a new CloudComponent and add it on cloudButtons list 81 cloudButtons.add(new CloudComponent(cloudIcon, clouds.get(i))); 82 int finalI = i; 83 //add on-click action listener to cloudComponent 84 cloudButtons.get(cloudButtons.size() - 1).addActionListener(e -> { 85 // skip execution of the action if a previous action still hasn't been processed by the server 86 if (guiSocketListener.awaitingPlayerActionFeedback()) { 87 JOptionPane.showMessageDialog(null, "Please wait for the server to process your previous" + 88 "request before making a new one"); 89 return; 90 } 91 if (guiSocketListener.getSuccessfulRequestsByType(MoveMotherNature.class) == 1) { 92 //create chooseCloudTile action and its playerActionRequest 93 ChooseCloudTile chooseCloudTile = new ChooseCloudTile(currentPlayer.getId(), finalI); 94 PlayerActionRequest playerActionRequest = new PlayerActionRequest(chooseCloudTile); 95 //save action inside guiReader 96 this.guiSocketListener.savePlayerActionRequest(chooseCloudTile); 97 try { 98 sw.sendMessage(playerActionRequest); 99 } catch (IOException ex) { 100 throw new RuntimeException(ex); 101 } 102 } 103 }); 104 105 } 106 //--ABSOLUTE POSITIONING-- 107 cloudButtons.get(0).setBounds(300, 125, 200, 200); 108 cloudButtons.get(1).setBounds(700, 125, 200, 200); 109 if (cloudButtons.size() == 3) cloudButtons.get(2).setBounds(300, 350, 200, 200); 110 if (cloudButtons.size() == 4) cloudButtons.get(3).setBounds(700, 350, 200, 200); 111 endTurnButton.setBounds(500, 550, 150, 75); 112 //add cloudButtons and endTurnButton to backgroundLabel 113 cloudButtons.forEach(backGroundLabel::add); 114 backGroundLabel.add(endTurnButton); 115 } 116 }