-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathChatRoomGUI.java
212 lines (185 loc) · 6.07 KB
/
ChatRoomGUI.java
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
package Client;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
public class ChatRoomGUI {
protected Shell shell;
private Group group;
private Button btnSend;
private Text message;
private Text txtChat;
private String text = "";
private List userList;
private String selectedRCVRString = "";
private static Client client;
private Calendar cal;
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public ChatRoomGUI(Client c){
setClient(c);
}
/**
* Launch the application.
* @param args
*/
/**
* Open the window.
* @throws IOException
*/
/**
* @wbp.parser.entryPoint
*/
public void open() throws IOException {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
* @throws IOException
*/
protected void createContents() throws IOException {
shell = new Shell();
shell.addShellListener(new ShellAdapter() {
@Override
public void shellClosed(ShellEvent e) {
client.sendEXIT();
System.exit(0);
}
});
shell.setSize(450, 300);
shell.setText("Username " + "[" + client.getUsername() + "]" + " | Host " + "[" + client.getHost() + "]" + " | Port " + "[" + client.getPortNumber() + "]");
shell.setLayout(new BorderLayout(0, 0));
userList = new List(shell, SWT.BORDER);
userList.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int[] selectedItems = userList.getSelectionIndices();
for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++){
selectedRCVRString = userList.getItem(selectedItems[loopIndex]);
}
System.out.println("Selected Items: " + selectedRCVRString);
}
});
userList.setItems(new String[] {});
userList.setLayoutData(BorderLayout.EAST);
group = new Group(shell, SWT.NONE);
group.setLayoutData(BorderLayout.SOUTH);
group.setLayout(new BorderLayout(0, 0));
message = new Text(group, SWT.BORDER);
message.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
client.sendMessage(selectedRCVRString,message.getText());
Calendar cal = Calendar.getInstance();
if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){
text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n";
}else{
text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n";
}
txtChat.setText(text);
message.setText("");
}
}
});
message.setLayoutData(BorderLayout.CENTER);
btnSend = new Button(group, SWT.NONE);
btnSend.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
client.sendMessage(selectedRCVRString,message.getText());
Calendar cal = Calendar.getInstance();
if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){
text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n";
}else{
text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n";
}
txtChat.setText(text);
message.setText("");
}
});
btnSend.setLayoutData(BorderLayout.EAST);
btnSend.setText("Send");
txtChat = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
txtChat.addListener(SWT.Modify, new Listener(){
public void handleEvent(Event e){
txtChat.setTopIndex(txtChat.getLineCount() - 1);
}
});
txtChat.setText(text);
txtChat.setEditable(false);
txtChat.setLayoutData(BorderLayout.CENTER);
}
public void addUserList(final ArrayList<String> users){
new Thread(new Runnable() {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try{
userList.add("");
removeAllUsernames();
for (int i = 0; i < users.size(); i++) {
userList.add(users.get(i));
}
shell.layout();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}).start();
}
public void updateChat(final String serverMSG){
new Thread(new Runnable() {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try{
text += serverMSG +" \n";
txtChat.setText(text);
shell.layout();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}).start();
}
private void removeAllUsernames(){
userList.removeAll();
userList.add("GLOBAL");
}
public static Client getClient() {
return client;
}
public static void setClient(Client client) {
ChatRoomGUI.client = client;
}
}