-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
139 lines (122 loc) · 4.91 KB
/
Copy pathChatClient.java
File metadata and controls
139 lines (122 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
public class ChatClient extends UnicastRemoteObject implements ChatClientInterface {
private ChatServerInterface server;
private String name;
private JTextArea chatArea;
private JTextField messageField;
private JComboBox<String> recipientList; // Moved here for global access
private JButton chatHistoryButton;
private String currentRecipient;
protected ChatClient(String name) throws RemoteException {
super();
this.name = name;
}
public void connectToServer() {
try {
server = (ChatServerInterface) Naming.lookup("rmi://localhost/ChatServer");
server.registerClient(this);
} catch (Exception e) {
System.err.println("Client exception: " + e.getMessage());
e.printStackTrace();
}
}
public void sendMessage(String recipient, String message) {
try {
String formattedMessage = "@" + recipient + ": " + message;
server.sendMessageToClient(name, recipient, formattedMessage);
DatabaseHandler.getInstance().saveMessage(name, recipient, formattedMessage);
} catch (RemoteException e) {
System.err.println("Error sending message: " + e.getMessage());
e.printStackTrace();
}
}
@Override
public void receiveMessage(String message) throws RemoteException {
chatArea.append(message + "\n");
}
@Override
public String getName() throws RemoteException {
return name;
}
@Override
public void updateAvailableUsers(List<String> users) throws RemoteException {
// Update recipient list
if (recipientList != null) { // Ensure recipientList is not null
recipientList.removeAllItems();
for (String user : users) {
recipientList.addItem(user);
}
}
}
public void createAndShowGUI(List<String> availableUsers) {
JFrame frame = new JFrame("Chat Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
chatArea = new JTextArea();
chatArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatArea);
frame.add(scrollPane, BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
messageField = new JTextField();
messageField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String recipient = (String) recipientList.getSelectedItem();
if (recipient != null) {
sendMessage(recipient, messageField.getText());
messageField.setText("");
}
}
});
recipientList = new JComboBox<>(availableUsers.toArray(new String[0])); // Initialize recipientList here
inputPanel.add(recipientList, BorderLayout.WEST);
inputPanel.add(messageField, BorderLayout.CENTER);
chatHistoryButton = new JButton("Chat History");
chatHistoryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String recipient = (String) recipientList.getSelectedItem();
if (recipient != null) {
currentRecipient = recipient;
fetchChatHistory(name, recipient);
}
}
});
inputPanel.add(chatHistoryButton, BorderLayout.EAST);
frame.add(inputPanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
private void fetchChatHistory(String sender, String recipient) {
List<String> chatHistory = DatabaseHandler.getInstance().getChatHistory(sender, recipient);
chatArea.setText(""); // Clear chat area before displaying history
for (String message : chatHistory) {
chatArea.append(message + "\n");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ChatClient client;
try {
String name = JOptionPane.showInputDialog("Enter your name:");
client = new ChatClient(name);
client.connectToServer();
List<String> availableUsers = client.server.getAvailableUsers();
client.createAndShowGUI(availableUsers);
} catch (RemoteException e) {
System.err.println("Client exception: " + e.getMessage());
e.printStackTrace();
}
}
});
}
}