package Client; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class UserInterface extends JFrame implements ActionListener, KeyListener { private JTextField message; private JTextArea output; private ChatClient client; private JTextField host = new JTextField("localhost", 15); private JTextField port = new JTextField("6667", 4); private JButton connect; private JButton send; public void outputWriter (String line) { output.append(line + "\n"); } /** Graphic interface recipient the client*/ public UserInterface(ChatClient client) { this.client = client; this.setIconImage(getIconImage()); this.setSize(800, 400); output = new JTextArea(10, 20); output.setBackground(Color.gray); JPanel msgPanel = new JPanel(); JPanel conPanel = new JPanel(); JPanel vertPanel = new JPanel(); JScrollPane scrollText = new JScrollPane(output); message = new JTextField(40); message.addKeyListener(this); JButton stop = new JButton("Stop"); stop.setActionCommand("stop"); stop.addActionListener(this); stop.setToolTipText("Click this button to exit."); this.send = new JButton("Send"); send.setActionCommand("send"); send.addActionListener(this); send.setToolTipText("Click this button to send message."); this.connect = new JButton("Connect"); connect.setActionCommand("connect"); connect.addActionListener(this); connect.setToolTipText("Click this button to connect the server."); this.send.setEnabled(false); vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS)); msgPanel.add(connect); msgPanel.add(send); msgPanel.add(new JLabel("Message:")); msgPanel.add(message); msgPanel.add(stop); conPanel.add(new JLabel("host:")); conPanel.add(host); conPanel.add(new JLabel("port:")); conPanel.add(port); vertPanel.add(scrollText); vertPanel.add(conPanel); vertPanel.add(msgPanel); add(vertPanel); setVisible(true); } int counterClick = 0; /** Syncing the the action that the button send to the perform * @param name of the action */ public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); //FIXME: delete. String command = e.getActionCommand(); if (command == "stop") { client.shutdown(); System.exit(0); } if (command == "send" ) { output.append(message.getText() + "\n"); client.PRIVMSG(message.getText()); message.setText(""); } if (command == "connect" && counterClick == 0 ) { if (client.connect(host.getText(),Integer.parseInt(port.getText()))) { output.append("You've connect to the server succesufully" + "\n"); counterClick++; this.send.setEnabled(true); this.connect.setEnabled(false); } } } @Override //FIXME: Do not send user send text with enter before connect. public void keyPressed(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) { output.append(message.getText() + "\n"); client.PRIVMSG(message.getText()); message.setText(""); } if (e.getKeyChar() == KeyEvent.VK_ESCAPE) { client.shutdown(); System.exit(0); } if ((e.getModifiers() & KeyEvent.ALT_DOWN_MASK) == 0 && e.getKeyChar() == KeyEvent.VK_C && counterClick == 0) { output.append("You've connect to the server succesufully" + "\n"); counterClick++; this.send.setEnabled(true); this.connect.setEnabled(false); } } public void keyReleased(KeyEvent arg0) {} public void keyTyped(KeyEvent arg0) {} }