-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClientGUI.java
More file actions
230 lines (181 loc) · 8.55 KB
/
ClientGUI.java
File metadata and controls
230 lines (181 loc) · 8.55 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.example.websocket_demo.client;
import com.example.websocket_demo.Message;
import jdk.jshell.execution.Util;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
public class ClientGUI extends JFrame implements MessageListener{
private JPanel connectedUsersPanel, messagePanel;
private MyStompClient myStompClient;
private String username;
private JScrollPane messagePanelScrollPane;
public ClientGUI(String username) throws ExecutionException, InterruptedException {
super("User: " + username);
this.username = username;
myStompClient = new MyStompClient(this, username);
setSize(1218, 685);
setLocationRelativeTo(null);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(ClientGUI.this, "Do you really want to leave?",
"Exit", JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION){
myStompClient.disconnectUser(username);
ClientGUI.this.dispose();
}
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
updateMessageSize();
}
});
getContentPane().setBackground(Utilities.PRIMARY_COLOR);
addGuiComponents();
}
private void addGuiComponents(){
addConnectedUsersComponents();
addChatComponents();
}
private void addConnectedUsersComponents(){
connectedUsersPanel = new JPanel();
connectedUsersPanel.setBorder(Utilities.addPadding(10, 10, 10, 10));
connectedUsersPanel.setLayout(new BoxLayout(connectedUsersPanel, BoxLayout.Y_AXIS));
connectedUsersPanel.setBackground(Utilities.SECONDARY_COLOR);
connectedUsersPanel.setPreferredSize(new Dimension(200, getHeight()));
JLabel connectedUsersLabel = new JLabel("Connected Users");
connectedUsersLabel.setFont(new Font("Inter", Font.BOLD, 18));
connectedUsersLabel.setForeground(Utilities.TEXT_COLOR);
connectedUsersPanel.add(connectedUsersLabel);
add(connectedUsersPanel, BorderLayout.WEST);
}
private void addChatComponents(){
JPanel chatPanel = new JPanel();
chatPanel.setLayout(new BorderLayout());
chatPanel.setBackground(Utilities.TRANSPARENT_COLOR);
messagePanel = new JPanel();
messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.Y_AXIS));
messagePanel.setBackground(Utilities.TRANSPARENT_COLOR);
messagePanelScrollPane = new JScrollPane(messagePanel);
messagePanelScrollPane.setBackground(Utilities.TRANSPARENT_COLOR);
messagePanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
messagePanelScrollPane.getVerticalScrollBar().setUnitIncrement(16);
messagePanelScrollPane.getViewport().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
revalidate();
repaint();
}
});
chatPanel.add(messagePanelScrollPane, BorderLayout.CENTER);
JPanel inputPanel = new JPanel();
inputPanel.setBorder(Utilities.addPadding(10, 10, 10, 10));
inputPanel.setLayout(new BorderLayout());
inputPanel.setBackground(Utilities.TRANSPARENT_COLOR);
JTextField inputField = new JTextField();
inputField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_ENTER){
String input = inputField.getText();
// edge case: empty message (prevent empty messages)
if(input.isEmpty()) return;
inputField.setText("");
myStompClient.sendMessage(new Message(username, input));
}
}
});
inputField.setBackground(Utilities.SECONDARY_COLOR);
inputField.setForeground(Utilities.TEXT_COLOR);
inputField.setBorder(Utilities.addPadding(0, 10, 0, 10));
inputField.setFont(new Font("Inter", Font.PLAIN, 16));
inputField.setPreferredSize(new Dimension(inputPanel.getWidth(), 50));
inputPanel.add(inputField, BorderLayout.CENTER);
chatPanel.add(inputPanel, BorderLayout.SOUTH);
add(chatPanel, BorderLayout.CENTER);
}
private JPanel createChatMessageComponent(Message message){
JPanel chatMessage = new JPanel();
chatMessage.setBackground(Utilities.TRANSPARENT_COLOR);
chatMessage.setLayout(new BoxLayout(chatMessage, BoxLayout.Y_AXIS));
chatMessage.setBorder(Utilities.addPadding(20, 20, 10, 20));
JLabel usernameLabel = new JLabel(message.getUser());
usernameLabel.setFont(new Font("Inter", Font.BOLD, 18));
usernameLabel.setForeground(Utilities.TEXT_COLOR);
chatMessage.add(usernameLabel);
JLabel messageLabel = new JLabel();
messageLabel.setText("<html>" +
"<body style='width:" + (0.60 * getWidth()) + "'px>" +
message.getMessage() +
"</body>"+
"</html>");
messageLabel.setFont(new Font("Inter", Font.PLAIN, 18));
messageLabel.setForeground(Utilities.TEXT_COLOR);
chatMessage.add(messageLabel);
System.out.println(messageLabel.getText());
return chatMessage;
}
@Override
public void onMessageRecieve(Message message) {
messagePanel.add(createChatMessageComponent(message));
revalidate();
repaint();
messagePanelScrollPane.getVerticalScrollBar().setValue(Integer.MAX_VALUE);
}
@Override
public void onActiveUsersUpdated(ArrayList<String> users) {
// remove the current user list panel (which should be the second component in the panel)
// the user list panel doesn't get added until after and this is mainly for when the users get updated
if(connectedUsersPanel.getComponents().length >= 2){
connectedUsersPanel.remove(1);
}
JPanel userListPanel = new JPanel();
userListPanel.setBackground(Utilities.TRANSPARENT_COLOR);
userListPanel.setLayout(new BoxLayout(userListPanel, BoxLayout.Y_AXIS));
// code tom ake user list panel scrollable
JScrollPane userListScrollPane = new JScrollPane(userListPanel);
userListScrollPane.setBackground(Utilities.TRANSPARENT_COLOR);
userListScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
userListScrollPane.getVerticalScrollBar().setUnitIncrement(16);
userListScrollPane.getViewport().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
revalidate();
repaint();
}
});
for(String user : users){
JLabel username = new JLabel();
username.setText(user);
username.setForeground(Utilities.TEXT_COLOR);
username.setFont(new Font("Inter", Font.BOLD, 16));
userListPanel.add(username);
}
connectedUsersPanel.add(userListScrollPane);
revalidate();
repaint();
}
private void updateMessageSize(){
for(int i = 0; i < messagePanel.getComponents().length; i++){
Component component = messagePanel.getComponent(i);
if(component instanceof JPanel){
JPanel chatMessage = (JPanel) component;
if(chatMessage.getComponent(1) instanceof JLabel){
JLabel messageLabel = (JLabel) chatMessage.getComponent(1);
messageLabel.setText("<html>" +
"<body style='width:" + (0.60 * getWidth()) + "'px>" +
messageLabel.getText() +
"</body>"+
"</html>");
}
}
}
}
}