package ch.epfl.lca.sc250.gui; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * @author Christophe Trefois * */ public class CnFrameP2P extends CnFrame implements ActionListener { /** * Generated serialID by Eclipse for Serializble Objects */ private static final long serialVersionUID = -3251522255074162116L; /** * The TextArea used to display the messages */ private JTextArea textArea; /** * TextField of the IP */ private JTextField ipAdressTextField; /** * TextField of the Amount of Money */ private JTextField amountMoneyTextField; /** * TextField of the position of the letter */ private JTextField letterPosition; /** * Button used to send the request */ private JButton sendMessage; /** * Instance of the main Program which is of type ITCPSender */ private ITCPSender tcpSender = null; /** * Constructs a CnFrameP2P Instance with title windowTitle * * @param windowTitle */ public CnFrameP2P(String windowTitle) { super(windowTitle); setSize(600, 300); JPanel centerPanel = new JPanel(); textArea = new JTextArea(); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); centerPanel.setLayout(new GridLayout(1, 1)); centerPanel.add(scrollPane); // put the centerPanel to the Center getContentPane().add(centerPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout()); topPanel.add(new JLabel("IP Address: ")); ipAdressTextField = new JTextField(); topPanel.add(ipAdressTextField); ipAdressTextField.setPreferredSize(new java.awt.Dimension(78, 20)); topPanel.add(new JLabel("Money: ")); amountMoneyTextField = new JTextField(); topPanel.add(amountMoneyTextField); amountMoneyTextField.setPreferredSize(new java.awt.Dimension(49, 20)); topPanel.add(new JLabel("Position: ")); letterPosition = new JTextField(); topPanel.add(letterPosition); letterPosition.setPreferredSize(new java.awt.Dimension(56, 20)); sendMessage = new JButton(); topPanel.add(sendMessage); sendMessage.setText("Send Request"); sendMessage.setActionCommand("Send"); sendMessage.addActionListener(this); getContentPane().add(topPanel, BorderLayout.NORTH); // Make this frame visible setVisible(true); } /** * Append Text to the textArea * * @param textToAppend */ public void appendText(String textToAppend) { textArea.append(super.getCurrentTime() + " : " + textToAppend); textArea.setCaretPosition(textArea.getText().length()); } /** * Clear the TextArea. */ public void clearTextArea() { textArea.replaceRange("", 0, textArea.getText().length()); } /** * */ public void actionPerformed(ActionEvent arg0) { String command = arg0.getActionCommand(); if (command.equalsIgnoreCase("Send")) { if (tcpSender == null) return; appendText("IP: " + ipAdressTextField.getText() + " - Buy For: " + amountMoneyTextField.getText() + " - Position: " + letterPosition.getText() + "\n"); ipAdressTextField.setEnabled(false); amountMoneyTextField.setEnabled(false); letterPosition.setEnabled(false); sendMessage.setText("Waiting ..."); sendMessage.removeActionListener(this); sendMessage.setEnabled(false); tcpSender.sendMessage(ipAdressTextField.getText(), amountMoneyTextField.getText(), letterPosition.getText()); } } /** * This method is called when an Answer is received from the other Player. * <br> * It then is displayed in the GUI, and the GUI is reset. * * @param communication */ public void receivedMessage(String communication) { appendText(communication + "\n"); ipAdressTextField.setEnabled(true); amountMoneyTextField.setEnabled(true); letterPosition.setEnabled(true); sendMessage.setText("Send Request"); sendMessage.addActionListener(this); sendMessage.setEnabled(true); ipAdressTextField.setText(""); amountMoneyTextField.setText(""); letterPosition.setText(""); appendText("----- Communication Completed --------\n"); } /** * Used to work with the Interface * * @param tcpSender */ public void setTcpSender(ITCPSender tcpSender) { this.tcpSender = tcpSender; } /** * When an offer is received, display a Confirm Dialog where the user can * choose Yes or No * * @param IP * The IP we got the offer from * @param amount * The amount the other player gives us for a letter * @param position * The position of the letter he wants to buy * @return True or False */ public boolean getOfferResult(String IP, String amount, String position) { boolean chosenValue = false; String msgToDisplay = "Buyer from IP: " + IP + "\nWants to buy Letter at Position " + position + " for " + amount + "$"; int returnValue = JOptionPane.showConfirmDialog(this, msgToDisplay, "You got an Offer !", JOptionPane.YES_NO_OPTION); if (returnValue == JOptionPane.OK_OPTION) { chosenValue = true; } else { chosenValue = false; } return chosenValue; } }