123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package chat;
-
- import java.net.*;
-
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.*;
-
- import java.io.*;
- import java.util.*;
-
- import chat.User;
-
- public class ClientWindow implements ActionListener {
- JFrame chatWindow;
-
- JPanel chatPanel;
- JTextArea chatText;
- JTextArea knownUsersPanel;
- JScrollPane textScroll;
-
- JPanel sendPanel;
- JTextField messageField;
- JButton sendButton;
-
- NetworkClient network;
-
-
- ClientWindow()
- {
- Boolean connected = false;
- Boolean outdoor = false;
- Boolean exit = false;
- String username = "";
- String destinationIP = "";
-
-
- chatWindow = new JFrame("Système de clavardage 2.0.1");
- chatPanel = new JPanel(new BorderLayout(2, 2));
- chatText = new JTextArea(1,1);
- knownUsersPanel = new JTextArea(1,1);
- textScroll = new JScrollPane(chatText);
-
- sendPanel = new JPanel(new BorderLayout(1, 2));
- messageField = new JTextField();
- sendButton = new JButton("Envoyer");
-
- network = new NetworkClient(chatText, knownUsersPanel);
-
- while(!connected && !exit)
- {
- Object[] options = {"Connect as indoor user", "Connect as outdoor user"};
- int n = JOptionPane.showOptionDialog(chatWindow, "", "Choose Login Method", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
-
- if(n == JOptionPane.YES_OPTION)
- {
- outdoor = false;
- destinationIP = "192.168.1.255";
- }
- else if (n == JOptionPane.NO_OPTION)
- {
- outdoor = true;
- destinationIP = JOptionPane.showInputDialog(chatWindow,
- "Enter the server's IP address",
- "Enter Server Address",
- JOptionPane.PLAIN_MESSAGE);
- }
- else
- {
- exit = true;
- }
-
-
- if((!outdoor || (destinationIP != null)) && !exit)
- {
- username = JOptionPane.showInputDialog(chatWindow,
- "Enter a username",
- "Choose Username",
- JOptionPane.PLAIN_MESSAGE);
-
- connected = network.connect(username, outdoor, destinationIP);
- if(!connected && username != null)
- {
- if(username.equals(""))
- JOptionPane.showMessageDialog(chatWindow, "Please enter a username", "Error", JOptionPane.ERROR_MESSAGE);
- else
- JOptionPane.showMessageDialog(chatWindow, "This username is already taken", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
-
- if(username != null && !exit)
- {
- sendButton.addActionListener(this);
- messageField.addActionListener(this);
- chatWindow.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- network.disconnect();
- }
- });
-
-
- sendPanel.add(messageField);
- sendPanel.add(sendButton, BorderLayout.EAST);
-
- chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- chatText.setLineWrap(true);
- chatText.setEditable(false);
-
- knownUsersPanel.setLineWrap(false);
- knownUsersPanel.setEditable(false);
-
- chatPanel.add(textScroll);
- chatPanel.add(knownUsersPanel, BorderLayout.EAST);
- chatPanel.add(sendPanel, BorderLayout.SOUTH);
-
- chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
-
- //Display the window.
- chatWindow.pack();
- chatWindow.setVisible(true);
- chatWindow.setSize(new Dimension(1024, 768));
- }
-
- }
- public static void main (String [] args)
- {
- JFrame.setDefaultLookAndFeelDecorated(true);
-
- ClientWindow window = new ClientWindow();
- }
-
- public void actionPerformed(ActionEvent event) {
- String message = messageField.getText();
- if("".compareTo(message) != 0)
- {
- messageField.setText("");
- network.send(new Message(network.getUser(), message, false));
- }
- }
- }
|