ajout d'un panel avec la liste des utilisateurs connectés dans la fenêtre principale
This commit is contained in:
parent
eea9f2b769
commit
d3c1e1ac53
3 changed files with 48 additions and 10 deletions
|
@ -19,6 +19,7 @@ public class ClientWindow implements ActionListener {
|
||||||
|
|
||||||
JPanel chatPanel;
|
JPanel chatPanel;
|
||||||
JTextArea chatText;
|
JTextArea chatText;
|
||||||
|
JTextArea knownUsersPanel;
|
||||||
JScrollPane textScroll;
|
JScrollPane textScroll;
|
||||||
|
|
||||||
JPanel sendPanel;
|
JPanel sendPanel;
|
||||||
|
@ -36,13 +37,14 @@ public class ClientWindow implements ActionListener {
|
||||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||||
chatPanel = new JPanel(new BorderLayout(2, 2));
|
chatPanel = new JPanel(new BorderLayout(2, 2));
|
||||||
chatText = new JTextArea(1,1);
|
chatText = new JTextArea(1,1);
|
||||||
|
knownUsersPanel = new JTextArea(1,1);
|
||||||
textScroll = new JScrollPane(chatText);
|
textScroll = new JScrollPane(chatText);
|
||||||
|
|
||||||
sendPanel = new JPanel(new BorderLayout(1, 2));
|
sendPanel = new JPanel(new BorderLayout(1, 2));
|
||||||
messageField = new JTextField();
|
messageField = new JTextField();
|
||||||
sendButton = new JButton("Envoyer");
|
sendButton = new JButton("Envoyer");
|
||||||
|
|
||||||
network = new NetworkClient(chatText);
|
network = new NetworkClient(chatText, knownUsersPanel);
|
||||||
|
|
||||||
while(!connected && username != null)
|
while(!connected && username != null)
|
||||||
{
|
{
|
||||||
|
@ -52,11 +54,11 @@ public class ClientWindow implements ActionListener {
|
||||||
JOptionPane.PLAIN_MESSAGE);
|
JOptionPane.PLAIN_MESSAGE);
|
||||||
|
|
||||||
connected = network.connect(username);
|
connected = network.connect(username);
|
||||||
if(!connected)
|
if(!connected && username != null)
|
||||||
{
|
{
|
||||||
if(username.equals(""))
|
if(username.equals(""))
|
||||||
JOptionPane.showMessageDialog(chatWindow, "Please enter a username", "Error", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(chatWindow, "Please enter a username", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
else if (username != null)
|
else
|
||||||
JOptionPane.showMessageDialog(chatWindow, "This username is already taken", "Error", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(chatWindow, "This username is already taken", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +82,11 @@ public class ClientWindow implements ActionListener {
|
||||||
chatText.setLineWrap(true);
|
chatText.setLineWrap(true);
|
||||||
chatText.setEditable(false);
|
chatText.setEditable(false);
|
||||||
|
|
||||||
|
knownUsersPanel.setLineWrap(false);
|
||||||
|
knownUsersPanel.setEditable(false);
|
||||||
|
|
||||||
chatPanel.add(textScroll);
|
chatPanel.add(textScroll);
|
||||||
|
chatPanel.add(knownUsersPanel, BorderLayout.EAST);
|
||||||
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
||||||
|
|
|
@ -54,12 +54,14 @@ class ReceiveThread extends Thread {
|
||||||
|
|
||||||
int port;
|
int port;
|
||||||
JTextArea displayArea;
|
JTextArea displayArea;
|
||||||
|
JTextArea knownUsersPanel;
|
||||||
List<User> known_users;
|
List<User> known_users;
|
||||||
|
|
||||||
ReceiveThread(int in_port, JTextArea in_displayArea, List<User> in_known_users)
|
ReceiveThread(int in_port, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel)
|
||||||
{
|
{
|
||||||
port = in_port;
|
port = in_port;
|
||||||
displayArea = in_displayArea;
|
displayArea = in_displayArea;
|
||||||
|
knownUsersPanel = in_knownUsersPanel;
|
||||||
known_users = in_known_users;
|
known_users = in_known_users;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
|
@ -88,6 +90,12 @@ class ReceiveThread extends Thread {
|
||||||
if (known_users.get(i).getName().equals(message.getAuthor()))
|
if (known_users.get(i).getName().equals(message.getAuthor()))
|
||||||
known_users.remove(i);
|
known_users.remove(i);
|
||||||
}
|
}
|
||||||
|
knownUsersPanel.setText("");
|
||||||
|
knownUsersPanel.append("Online:\n");
|
||||||
|
for(User a:known_users)
|
||||||
|
{
|
||||||
|
knownUsersPanel.append(a.getName() + "\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||||
link.close();
|
link.close();
|
||||||
|
@ -108,12 +116,14 @@ class ReceiveThread extends Thread {
|
||||||
|
|
||||||
class ConnectionListenerThread extends Thread {
|
class ConnectionListenerThread extends Thread {
|
||||||
JTextArea displayArea;
|
JTextArea displayArea;
|
||||||
|
JTextArea knownUsersPanel;
|
||||||
List<User> known_users;
|
List<User> known_users;
|
||||||
|
|
||||||
ConnectionListenerThread(List<User> in_known_users, JTextArea in_displayArea)
|
ConnectionListenerThread(List<User> in_known_users, JTextArea in_displayArea, JTextArea in_knownUsersPanel)
|
||||||
{
|
{
|
||||||
known_users = in_known_users;
|
known_users = in_known_users;
|
||||||
displayArea = in_displayArea;
|
displayArea = in_displayArea;
|
||||||
|
knownUsersPanel = in_knownUsersPanel;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
|
@ -168,8 +178,17 @@ class ConnectionListenerThread extends Thread {
|
||||||
if(accepted)
|
if(accepted)
|
||||||
{
|
{
|
||||||
known_users.add(new User(username, clientAddress.getHostAddress()));
|
known_users.add(new User(username, clientAddress.getHostAddress()));
|
||||||
|
|
||||||
|
Collections.sort(known_users);
|
||||||
|
|
||||||
displayArea.append(username + " has joined the chat.\n");
|
displayArea.append(username + " has joined the chat.\n");
|
||||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||||
|
knownUsersPanel.setText("");
|
||||||
|
knownUsersPanel.append("Online:\n");
|
||||||
|
for(User a:known_users)
|
||||||
|
{
|
||||||
|
knownUsersPanel.append(a.getName() + "\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -191,6 +210,7 @@ public class NetworkClient {
|
||||||
private User user;
|
private User user;
|
||||||
private List<User> known_users;
|
private List<User> known_users;
|
||||||
private JTextArea chatText;
|
private JTextArea chatText;
|
||||||
|
private JTextArea knownUsersPanel;
|
||||||
|
|
||||||
static String getLocalIP() {
|
static String getLocalIP() {
|
||||||
String ip = "";
|
String ip = "";
|
||||||
|
@ -205,10 +225,11 @@ public class NetworkClient {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkClient(JTextArea in_chatText)
|
NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel)
|
||||||
{
|
{
|
||||||
user = new User("", "");
|
user = new User("", "");
|
||||||
chatText = in_chatText;
|
chatText = in_chatText;
|
||||||
|
knownUsersPanel = in_knownUsersPanel;
|
||||||
known_users = new ArrayList<User>();
|
known_users = new ArrayList<User>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,9 +318,16 @@ public class NetworkClient {
|
||||||
chatText.append(username + " has joined the chat.\n");
|
chatText.append(username + " has joined the chat.\n");
|
||||||
chatText.setCaretPosition(chatText.getDocument().getLength());
|
chatText.setCaretPosition(chatText.getDocument().getLength());
|
||||||
|
|
||||||
ReceiveThread t2 = new ReceiveThread(1237, chatText, known_users);
|
knownUsersPanel.setText("");
|
||||||
|
knownUsersPanel.append(" Online :\n");
|
||||||
|
for(User a:known_users)
|
||||||
|
{
|
||||||
|
knownUsersPanel.append(" " + a.getName() + " \n");
|
||||||
|
}
|
||||||
|
|
||||||
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users, chatText);
|
ReceiveThread t2 = new ReceiveThread(1237, chatText, known_users, knownUsersPanel);
|
||||||
|
|
||||||
|
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users, chatText, knownUsersPanel);
|
||||||
t2.start();
|
t2.start();
|
||||||
t3.start();
|
t3.start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package chat;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class User implements Serializable{
|
public class User implements Comparable<User>, Serializable{
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String address;
|
private String address;
|
||||||
|
@ -37,4 +37,8 @@ public class User implements Serializable{
|
||||||
return (b.getName() == name);
|
return (b.getName() == name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int compareTo (User otherUser)
|
||||||
|
{
|
||||||
|
return name.compareTo(otherUser.getName());
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue