-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGuiView.java
More file actions
104 lines (91 loc) · 3.3 KB
/
Copy pathClientGuiView.java
File metadata and controls
104 lines (91 loc) · 3.3 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClientGuiView {
private final ClientGuiController controller;
private JFrame frame = new JFrame("chat");
private JTextField textField = new JTextField(50);
private JTextArea messages = new JTextArea(10, 50);
private JTextArea users = new JTextArea(10, 10);
public ClientGuiView(ClientGuiController controller) {
this.controller = controller;
initView();
}
private void initView() {
textField.setEditable(false);
messages.setEditable(false);
users.setEditable(false);
frame.getContentPane().add(textField, BorderLayout.NORTH);
frame.getContentPane().add(new JScrollPane(messages), BorderLayout.WEST);
frame.getContentPane().add(new JScrollPane(users), BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
controller.sendTextMessage(textField.getText());
textField.setText("");
}
});
}
public String getServerAddress() {
return JOptionPane.showInputDialog(
frame,
"write server address:",
"Client config",
JOptionPane.QUESTION_MESSAGE);
}
public int getServerPort() {
while (true) {
String port = JOptionPane.showInputDialog(
frame,
"write server port:",
"Client config",
JOptionPane.QUESTION_MESSAGE);
try {
return Integer.parseInt(port.trim());
} catch (Exception e) {
JOptionPane.showMessageDialog(
frame,
"Server port is incorrect try again.",
"Client config",
JOptionPane.ERROR_MESSAGE);
}
}
}
public String getUserName() {
return JOptionPane.showInputDialog(
frame,
"type your name:",
"Client config",
JOptionPane.QUESTION_MESSAGE);
}
public void notifyConnectionStatusChanged(boolean clientConnected) {
textField.setEditable(clientConnected);
if (clientConnected) {
JOptionPane.showMessageDialog(
frame,
"connection with server established",
"chat",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(
frame,
"client is not connected to a server",
"chat",
JOptionPane.ERROR_MESSAGE);
}
}
public void refreshMessages() {
messages.append(controller.getModel().getNewMessage() + "\n");
}
public void refreshUsers() {
ClientGuiModel model = controller.getModel();
StringBuilder sb = new StringBuilder();
for (String userName : model.getAllUserNames()) {
sb.append(userName).append("\n");
}
users.setText(sb.toString());
}
}