package com.esir.sr.sweetsnake.view; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; import java.util.List; import javax.annotation.PostConstruct; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.esir.sr.sweetsnake.component.ImagePanel; import com.esir.sr.sweetsnake.component.PlayersList; import com.esir.sr.sweetsnake.constants.ClientGuiConstants; import com.esir.sr.sweetsnake.dto.PlayerDTO; /** * * @author Herminaƫl Rougier * @author Damien Jouanno * */ @Component public class PlayersView extends AbstractView { /********************************************************************************************** * [BLOCK] STATIC FIELDS **********************************************************************************************/ /** The serial version UID */ private static final long serialVersionUID = -5820091417435340407L; /** The logger */ private static final Logger log = LoggerFactory.getLogger(PlayersView.class); /********************************************************************************************** * [BLOCK] FIELDS **********************************************************************************************/ /** The view title panel */ private ImagePanel playersListIPL; /** The top panel */ private JPanel topPL; /** The center panel */ private JPanel centerPL; /** The bottom panel */ private JPanel bottomPL; /** The players list */ private PlayersList playersLST; /** The request button */ private JButton requestBTN; /********************************************************************************************** * [BLOCK] CONSTRUCTOR & INIT **********************************************************************************************/ /** * */ protected PlayersView() { super(); } /* * (non-Javadoc) * * @see com.esir.sr.sweetsnake.view.SweetSnakeAbstractView#init() */ @Override @PostConstruct protected void init() { super.init(); log.info("Initializing the Players View"); } /********************************************************************************************** * [BLOCK] PUBLIC METHODS **********************************************************************************************/ /* * (non-Javadoc) * * @see com.esir.sr.sweetsnake.view.SweetSnakeAbstractView#buildImpl() */ @Override public void buildImpl() { setLayout(new BorderLayout()); // top panel initTopPL(); add(topPL, BorderLayout.NORTH); initPlayersListIPL(); topPL.add(playersListIPL); // center panel initCenterPL(); add(centerPL, BorderLayout.CENTER); final List<PlayerDTO> players = new LinkedList<PlayerDTO>(); initPlayersLST(players); centerPL.add(new JScrollPane(playersLST)); // bottom panel initBottomPL(); add(bottomPL, BorderLayout.SOUTH); final GridBagConstraints gbc = new GridBagConstraints(); initRequestBTN(); gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new Insets(5, 0, 0, 0); bottomPL.add(requestBTN, gbc); } /** * * @param playersList */ public void refreshPlayersList(final List<PlayerDTO> playersList) { if (isBuilded) { playersLST.removeAllElements(); final List<PlayerDTO> players = new LinkedList<PlayerDTO>(playersList); if (players.isEmpty()) { playersLST.disableSelection(); playersLST.addElement(new PlayerDTO("No available player for the moment", null, null, 0, 0, false)); } else { playersLST.enableSelection(); for (final PlayerDTO player : players) { playersLST.addElement(player); } } } } /********************************************************************************************** * [BLOCK] PRIVATE METHODS **********************************************************************************************/ /** * */ private void initTopPL() { topPL = new JPanel(); topPL.setOpaque(false); } /** * */ private void initPlayersListIPL() { playersListIPL = new ImagePanel(ClientGuiConstants.PLAYERS_LIST_PATH); } /** * */ private void initCenterPL() { centerPL = new JPanel(); centerPL.setOpaque(false); centerPL.setLayout(new BorderLayout()); } /** * * @param players */ private void initPlayersLST(final List<PlayerDTO> players) { playersLST = new PlayersList(); for (final PlayerDTO player : players) { playersLST.addElement(player); } } /** * */ private void initBottomPL() { bottomPL = new JPanel(); bottomPL.setLayout(new GridBagLayout()); bottomPL.setOpaque(false); } /** * */ private void initRequestBTN() { requestBTN = new JButton("invite"); requestBTN.addActionListener(new RequestGameListener()); } /********************************************************************************************** * [BLOCK] INTERNAL LISTENERS **********************************************************************************************/ /** * * @author Herminaƫl Rougier * @author Damien Jouanno * */ private class RequestGameListener implements ActionListener { /* * (non-Javadoc) * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void actionPerformed(final ActionEvent e) { final List<PlayerDTO> selectedPlayers = playersLST.getSelectedValuesList(); if (selectedPlayers.size() == 0) { gui.displayErrorMessage("Please select an opponent first"); } else { for (final PlayerDTO player : selectedPlayers) { client.sendRequest(player); } } } } }