-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
735 lines (633 loc) · 28.7 KB
/
Copy pathClient.java
File metadata and controls
735 lines (633 loc) · 28.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/* WEBSERVER Client.java
* EE422C Project 7 submission by
* Anthony Bauer
* amb6869
* 16480
* Grant Uy
* gau84
* 16480
* Slip days used: <1>
* Fall 2016
*/
package assignment7;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class Client extends Application {
// IO streams
BufferedWriter toServer = null;
BufferedReader fromServer = null;
Thread recieve;
ServerRecieve sa;
int clientId;
Label currentChat;
Contact currentContact;
ListView<Label> chatMessages;
HashMap<Integer,Contact> groups;
ContactList<Contact> groupsView;
HashMap<String,Contact> onlineFriends;
ContactList<Contact> onlineFriendsView;
HashMap<String,Contact> offlineFriends;
ContactList<Contact> offlineFriendsView;
HashMap<String,Contact> onlineStrangers;
ContactList<Contact> onlineStrangersView;
@Override // Override the start method in the Application class
public void start(Stage loginStage) {
BorderPane loginPane = new BorderPane();
loginPane.setPadding(new Insets(5));
VBox loginBox = new VBox();
loginBox.setSpacing(10);
loginBox.setAlignment(Pos.CENTER);
HBox serverBox = new HBox();
serverBox.setSpacing(10);
serverBox.setAlignment(Pos.CENTER);
Label serverLabel = new Label("Server Hostname: ");
TextField ipField = new TextField("localhost"); //default
serverBox.getChildren().addAll(serverLabel,ipField);
loginBox.getChildren().add(serverBox);
HBox usernameBox = new HBox();
usernameBox.setSpacing(10);
usernameBox.setAlignment(Pos.CENTER);
Label usernameLabel = new Label("Username: ");
TextField usernameField = new TextField("demo"); //TODO REMOVE
addTextLimiter(usernameField,15);
usernameBox.getChildren().addAll(usernameLabel,usernameField);
loginBox.getChildren().add(usernameBox);
HBox passwordBox = new HBox();
passwordBox.setSpacing(10);
passwordBox.setAlignment(Pos.CENTER);
Label passwordLabel = new Label("Password: ");
PasswordField passwordField = new PasswordField();
addTextLimiter(passwordField,15);
passwordField.setText("grantisawesome"); //TODO REMOVE
passwordBox.getChildren().addAll(passwordLabel,passwordField);
loginBox.getChildren().add(passwordBox);
Label statusLabel = new Label("Status: Ready");
statusLabel.setTextFill(Color.GREEN);
statusLabel.setAlignment(Pos.BOTTOM_LEFT);
loginPane.setBottom(statusLabel);
HBox submitBox = new HBox();
submitBox.setSpacing(25);
submitBox.setAlignment(Pos.CENTER);
Button registerButton = new Button("Register");
registerButton.setOnAction(e -> {
boolean success = connectToServer(ipField.getText(),statusLabel);
if(!success)
return;
success = loginToServer(ClientAction.REGISTER,Parser.cleanString(usernameField.getText()),passwordField.getText(),statusLabel);
if(!success)
return;
openChat();
loginStage.close();
});
Button loginButton = new Button("Login");
loginButton.setOnAction(e -> {
boolean success = connectToServer(ipField.getText(),statusLabel);
if(!success)
return;
success = loginToServer(ClientAction.LOGIN,Parser.cleanString(usernameField.getText()),passwordField.getText(),statusLabel);
if(!success)
return;
openChat();
loginStage.close();
});
submitBox.getChildren().addAll(registerButton,loginButton);
loginBox.getChildren().add(submitBox);
loginPane.setCenter(loginBox);
// Create a scene and place it in the stage
Scene scene = new Scene(loginPane, 450, 200);
loginStage.setTitle("Login"); // Set the stage title
loginStage.setScene(scene); // Place the scene in the stage
loginStage.show(); // Display the stage
loginStage.setOnCloseRequest(event -> System.exit(0));
}
private void openPassword(){
Stage passwordStage = new Stage();
BorderPane passPane = new BorderPane();
passPane.setPadding(new Insets(10));
VBox passBox = new VBox();
passBox.setSpacing(10);
passBox.setAlignment(Pos.CENTER);
HBox currentBox = new HBox();
currentBox.setSpacing(10);
currentBox.setAlignment(Pos.CENTER);
passBox.getChildren().add(currentBox);
HBox newBox = new HBox();
newBox.setSpacing(10);
newBox.setAlignment(Pos.CENTER);
Label newLabel = new Label("New Pass: ");
PasswordField newField = new PasswordField();
addTextLimiter(newField,15);
newBox.getChildren().addAll(newLabel,newField);
passBox.getChildren().add(newBox);
HBox submitBox = new HBox();
submitBox.setAlignment(Pos.CENTER);
Button confirmButton = new Button("Confrim");
confirmButton.setOnAction(e -> {
String query = Parser.packageStrings(ClientAction.UPDATEPASSWORD,newField.getText());
sendQuery(query);
passwordStage.close();
});
submitBox.getChildren().add(confirmButton);
passBox.getChildren().add(submitBox);
passPane.setCenter(passBox);
Scene scene = new Scene(passPane);
passwordStage.setTitle("Change Password"); // Set the stage title
passwordStage.setScene(scene); // Place the scene in the stage
passwordStage.show(); // Display the stage
}
private void openAddGroup(){
Stage groupStage = new Stage();
BorderPane groupPane = new BorderPane();
groupPane.setPadding(new Insets(10));
//Group Name - Top
HBox nameBox = new HBox();
nameBox.setPadding(new Insets(0,0,5,0));
nameBox.setSpacing(5);
nameBox.setAlignment(Pos.CENTER_LEFT);
Label nameLabel = new Label("Group Name:");
nameLabel.setAlignment(Pos.CENTER_LEFT);
TextField nameField = new TextField();
addTextLimiter(nameField,32);
HBox.setHgrow(nameField,Priority.ALWAYS);
nameBox.getChildren().addAll(nameLabel,nameField);
groupPane.setTop(nameBox);
//Friends - Left
VBox friends = new VBox();
friends.setPadding(new Insets(0,5,0,0));
friends.setSpacing(5);
groupPane.setLeft(friends);
Label groupLabel = new Label("Friends");
List<Contact> compiled = Stream.concat(onlineFriends.values().stream(),offlineFriends.values().stream()).map(c -> new Contact(c.toString())).collect(Collectors.toList());
ContactList<Contact> items = new ContactList<>(compiled);
ContactList<Contact> includes = new ContactList<>();
friends.getChildren().addAll(groupLabel,items);
//mouse handler
items.getSelectionModel().selectionModeProperty().removeListener(items.listener);
items.getSelectionModel().selectedIndexProperty().addListener(e -> swapLists(items,includes));
includes.getSelectionModel().selectionModeProperty().removeListener(includes.listener);
includes.getSelectionModel().selectedIndexProperty().addListener(e -> swapLists(includes,items));
//In Group - Center
VBox included = new VBox();
included.setSpacing(5);
groupPane.setCenter(included);
Label includedLabel = new Label("Included");
included.getChildren().addAll(includedLabel,includes);
//Finish - Bottom
HBox createBox = new HBox();
createBox.setPadding(new Insets(5,0,0,0));
createBox.setAlignment(Pos.BOTTOM_CENTER);
Button createGroup = new Button("Finish");
createGroup.setOnAction(event -> {
List<Contact> inGroup = new ArrayList(includes.getItems());
if(inGroup.size()>0) {
String query = Parser.packageStrings(ClientAction.MAKECHAT,nameField.getText(),inGroup);
sendQuery(query);
}
groupStage.hide();
});
createGroup.setAlignment(Pos.BOTTOM_CENTER);
createBox.getChildren().add(createGroup);
groupPane.setBottom(createBox);
Scene scene = new Scene(groupPane);
groupStage.setHeight(500);
groupStage.setTitle("Create Group"); // Set the stage title
groupStage.setScene(scene); // Place the scene in the stage
groupStage.show(); // Display the stage
}
private void swapLists(ContactList<Contact> first, ContactList<Contact> second){
List<Contact> selectList = first.getSelectionModel().getSelectedItems();
if(selectList.size() > 0) {
final Contact selected = selectList.get(0);
if (selected != null) {
second.addItem(selected);
Platform.runLater(() -> first.removeItem(selected));
}
}
}
public static void addTextLimiter(final TextField tf, final int maxLength) {
tf.textProperty().addListener((ov, oldValue, newValue) -> {
if (tf.getText().length() > maxLength) {
String s = tf.getText().substring(0, maxLength);
tf.setText(s);
}
});
}
private void openChat(){
// prepWaitForServer(ServerAction.GROUPS);
String query = Parser.packageStrings(ClientAction.GETGROUPS);
sendQuery(query);
// waitForServer(ServerAction.GROUPS);
// prepWaitForServer(ServerAction.FRIENDS);
query = Parser.packageStrings(ClientAction.GETFRIENDS);
sendQuery(query);
// waitForServer(ServerAction.FRIENDS);
// prepWaitForServer(ServerAction.OFFLINEFRIENDS);
query = Parser.packageStrings(ClientAction.GETOFFLINEFRIENDS);
sendQuery(query);
// waitForServer(ServerAction.OFFLINEFRIENDS);
// prepWaitForServer(ServerAction.STRANGERS);
query = Parser.packageStrings(ClientAction.GETSTRANGERS);
sendQuery(query);
// waitForServer(ServerAction.STRANGERS);
groups = new HashMap<>();
onlineFriends = new HashMap<>();
onlineStrangers = new HashMap<>();
offlineFriends = new HashMap<>();
//TODO remove hardcode
/*
//get online people / friends
groups.put(1,new Contact("Senior Design",true,this)); groups.put(2,new Contact("HackDFW",true,this)); groups.put(3,new Contact("Frist Allo",true,this));
onlineFriends.put("Grant",new Contact("Grant",true,this));
o
offlineFriends.put("Rony",new Contact("Rony",true,this));
onlineStrangers.put("BruceBanner",new Contact("BruceBanner",false,this)); onlineStrangers.put("BilboBaggins",new Contact("BilboBaggins",false,this));
*/
//finish remove TODO
Stage chatStage = new Stage();
BorderPane chatPane = new BorderPane();
chatPane.setPadding(new Insets(5));
//Friends - Left
VBox friends = new VBox();
friends.setSpacing(5);
chatPane.setLeft(friends);
BorderPane groupTitle = new BorderPane();
VBox groupLabelAlign = new VBox();
Label groupLabel = new Label("Groups");
groupLabel.setAlignment(Pos.BOTTOM_CENTER);
groupLabelAlign.getChildren().add(groupLabel);
groupLabelAlign.setAlignment(Pos.BOTTOM_CENTER);
Button addGroupButton = new Button("Add Group");
addGroupButton.setOnAction(event -> openAddGroup());
groupTitle.setLeft(groupLabelAlign);
groupTitle.setRight(addGroupButton);
friends.getChildren().add(groupTitle);
groupsView = new ContactList<>();
friends.getChildren().add(groupsView);
Label onlineFriendsLabel = new Label("Online Friends");
friends.getChildren().add(onlineFriendsLabel);
onlineFriendsView = new ContactList<>();
friends.getChildren().add(onlineFriendsView);
Label offlineFriendsLabel = new Label("Offline Friends");
friends.getChildren().add(offlineFriendsLabel);
offlineFriendsView = new ContactList<>();
friends.getChildren().add(offlineFriendsView);
Label strangerLabel = new Label("Strangers");
friends.getChildren().add(strangerLabel);
onlineStrangersView = new ContactList<>();
friends.getChildren().add(onlineStrangersView);
//Chat - Right
BorderPane chat = new BorderPane();
chat.setPadding(new Insets(5));
chatPane.setCenter(chat);
BorderPane chatTop = new BorderPane();
HBox chatNameBox = new HBox();
chatNameBox.setSpacing(5);
Label chatName = new Label("Chat name: ");
currentChat = new Label("");
chatNameBox.getChildren().addAll(chatName,currentChat);
chatTop.setLeft(chatNameBox);
chat.setTop(chatTop);
Button changePassword = new Button("Update Password");
chatTop.setRight(changePassword);
changePassword.setOnAction(event -> openPassword());
chatMessages = new ListView<>();
chat.setCenter(chatMessages);
HBox sendMessageBox = new HBox();
sendMessageBox.setPadding(new Insets(10,0,0,0));
sendMessageBox.setSpacing(5);
TextField sendMessageField = new TextField();
Button sendMessageButton = new Button("Send");
sendMessageField.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
if(keyEvent.getCode()== KeyCode.ENTER){
submitMessage(sendMessageField);
}
});
sendMessageButton.setOnAction(event -> submitMessage(sendMessageField));
sendMessageBox.getChildren().addAll(sendMessageField,sendMessageButton);
HBox.setHgrow(sendMessageField, Priority.ALWAYS);
chat.setBottom(sendMessageBox);
//TODO REMOVE
// chatPane.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
// System.out.println("h: "+newValue.getHeight()+" w: "+newValue.getWidth());
// });
//TODO REMOVE HARDCODE
// onlineFriends.get(0).appendChat("[GRANT] Hey Anthony, can you ...");
// onlineFriends.get(0).appendChat("[ANTHONY] What do you need Grant, id be happy to help!");
// Create a scene and place it in the stage
Scene s = new Scene(chatPane,650,500);
chatStage.setTitle("Chat Window"); // Set the stage title
chatStage.setScene(s); // Place the scene in the stage
chatStage.show(); // Display the stage
chatStage.setOnCloseRequest(event -> System.exit(0));
}
private void submitMessage(TextField message){
currentContact.submitMessage(message.getText());
message.setText("");
}
private boolean connectToServer(String ip,Label statusLabel) {
try {
// Create a socket to connect to the server
@SuppressWarnings("resource")
Socket socket = new Socket(ip, 8000);
// Create an input stream to receive data from the server
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Create an output stream to send data to the server
toServer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}
catch (IOException ex) {
statusLabel.setText("Status: Failed");
statusLabel.setTextFill(Color.FIREBRICK);
return false;
}
return true;
}
private boolean loginToServer(ClientAction clientaction,String user, String pass,Label statusLabel){
try {
//setup new Thread to receive from Server
sa = new ServerRecieve(this,fromServer);
recieve = new Thread(sa);
recieve.start();
prepWaitForServer(ServerAction.LOGINSUCCESS);
// create query
String query = Parser.packageStrings(clientaction,user,pass);
//send server register
sendQuery(query);
//wait for response
waitForServer(ServerAction.LOGINSUCCESS);
System.out.println("LOGINSUCCESS RECEIVED");
System.out.println("clientid "+clientId);
if(clientId==-1)
throw new IOException();
return true;
} catch (IOException ex){
statusLabel.setText("Status: Failed");
statusLabel.setTextFill(Color.FIREBRICK);
}
return false;
}
public void sendQuery(String q){
try{
System.out.println(q);
toServer.write(q+'\n');
toServer.flush();
} catch (IOException ex){
ex.printStackTrace();
}
}
volatile boolean flags[] = new boolean[ServerAction.values().length];
public void prepWaitForServer(ServerAction sa){
flags[sa.ordinal()] = true;
}
public void waitForServer(ServerAction sa){
while(flags[sa.ordinal()]){}
}
public static void main(String[] args) {
launch(args);
}
public void updateContactList(ContactList<Contact> contactList, Collection<Contact> contacts) {
contactList.setList(contacts);
}
}
class ServerRecieve implements Runnable {
BufferedReader fromServer;
Client owner;
public ServerRecieve(Client c, BufferedReader fs){
owner = c;
fromServer = fs;
}
@Override
public void run() {
while(true){
try {
String line = fromServer.readLine().trim();
System.out.println(line);
String[] action = Parser.parseString(line);
ServerAction sa = ServerAction.valueOf(action[0]);
List<String> messages;
int groupId;
switch(sa){ //TODO ADD ALL SERVERACTIONS
case LOGINSUCCESS:
owner.clientId = Integer.parseInt(action[1]);
System.out.println("set clientid to "+Integer.parseInt(action[1]));
break;
case FRIENDS:
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); //ServerAction,
HashMap<String,Contact> friends = new HashMap<>();
messages.forEach(s -> friends.put(s, new Contact(s, true, owner)));
owner.updateContactList(owner.onlineFriendsView,friends.values());
owner.onlineFriends = friends;
friends.forEach((s,c) -> owner.sendQuery(Parser.packageStrings(ClientAction.GETMESSAGEHISTORY,s)));
break;
case OFFLINEFRIENDS:
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); //ServerAction,
HashMap<String,Contact> offlineFriends = new HashMap<>();
messages.forEach(s -> offlineFriends.put(s, new Contact(s, true, owner)));
owner.updateContactList(owner.offlineFriendsView,offlineFriends.values());
owner.offlineFriends = offlineFriends;
offlineFriends.forEach((s,c) -> owner.sendQuery(Parser.packageStrings(ClientAction.GETMESSAGEHISTORY,s)));
break;
case STRANGERS:
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); //ServerAction,
HashMap<String,Contact> strangers = new HashMap<>();
messages.forEach(s -> {
if(!owner.onlineFriends.containsKey(s)&&!owner.offlineFriends.containsKey(s))
strangers.put(s, new Contact(s, false, owner));
});
owner.onlineStrangers = strangers;
owner.updateContactList(owner.onlineStrangersView,strangers.values());
strangers.forEach((s,c) -> owner.sendQuery(Parser.packageStrings(ClientAction.GETMESSAGEHISTORY,s)));
break;
case GROUPS:
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); //ServerAction,
HashMap<Integer,Contact> groups = new HashMap<>();
for (int i = 0; i<messages.size(); i+=2) {
groupId = Integer.parseInt(messages.get(i));
groups.put(groupId,new Group(messages.get(i+1),groupId,owner));
}
owner.groups = groups;
owner.updateContactList(owner.groupsView,owner.groups.values());
groups.forEach((i,c) -> owner.sendQuery(Parser.packageStrings(ClientAction.GETGROUPMESSAGEHISTORY,i)));
break;
case MESSAGEHISTORY:
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); messages.remove(0); //ServerAction, username
Contact c;
if(owner.onlineFriends.containsKey(action[1])){
c = owner.onlineFriends.get(action[1]);
c.setChatHistory(messages);
} else if(owner.offlineFriends.containsKey(action[1])){
c = owner.offlineFriends.get(action[1]);
c.setChatHistory(messages);
} else if(owner.onlineStrangers.containsKey(action[1])){
c = owner.onlineStrangers.get(action[1]);
c.setChatHistory(messages);
}
break;
case GROUPMESSAGEHISTORY:
groupId = Integer.parseInt(action[1]);
messages = new ArrayList<>(Arrays.asList(action));
messages.remove(0); messages.remove(0); //ServerAction, groupId
owner.groups.get(groupId).setChatHistory(messages);
break;
case FRIENDADDED:
Contact newFriend = owner.onlineStrangers.remove(action[1]);
newFriend.setFriend(true);
owner.onlineFriends.put(action[1],newFriend);
owner.updateContactList(owner.onlineFriendsView,owner.onlineFriends.values());
owner.updateContactList(owner.onlineStrangersView,owner.onlineStrangers.values());
break;
case FRIENDREMOVED:
HashMap<String,Contact> oldList,newList;
Contact oldFriend;
if(owner.onlineFriends.containsKey(action[1])) {
oldList = owner.onlineFriends;
newList = owner.onlineStrangers;
} else {
oldList = owner.offlineFriends;
newList = new HashMap<>();
}
oldFriend = oldList.remove(action[1]);
oldFriend.setFriend(false);
newList.put(action[1],oldFriend);
owner.updateContactList(owner.onlineFriendsView,owner.onlineFriends.values());
owner.updateContactList(owner.offlineFriendsView,owner.offlineFriends.values());
owner.updateContactList(owner.onlineStrangersView,owner.onlineStrangers.values());
break;
case NEWGROUPMESSAGE:
owner.groups.get(Integer.parseInt(action[1])).appendChat(action[2]);
break;
case NEWMESSAGE:
if(owner.onlineFriends.containsKey(action[1])){
owner.onlineFriends.get(action[1]).appendChat(action[2]);
} else if(owner.offlineFriends.containsKey(action[1])){
owner.offlineFriends.get(action[1]).appendChat(action[2]);
} else if(owner.onlineStrangers.containsKey(action[1])){
owner.onlineStrangers.get(action[1]).appendChat(action[2]);
}
break;
case COMEONLINE:
if(action[1]!=null){
if(owner.offlineFriends.containsKey(action[1])){
owner.onlineFriends.put(action[1],owner.offlineFriends.remove(action[1]));
owner.updateContactList(owner.onlineFriendsView,owner.onlineFriends.values());
owner.updateContactList(owner.offlineFriendsView,owner.offlineFriends.values());
owner.sendQuery(Parser.packageStrings(ClientAction.GETMESSAGEHISTORY,action[1]));
} else{
owner.onlineStrangers.put(action[1],new Contact(action[1],false,owner));
owner.updateContactList(owner.onlineStrangersView,owner.onlineStrangers.values());
owner.sendQuery(Parser.packageStrings(ClientAction.GETMESSAGEHISTORY,action[1]));
}
}
break;
case WENTOFFLINE:
if(owner.onlineFriends.containsKey(action[1])){
owner.offlineFriends.put(action[1],owner.onlineFriends.remove(action[1]));
owner.updateContactList(owner.onlineFriendsView,owner.onlineFriends.values());
owner.updateContactList(owner.offlineFriendsView,owner.offlineFriends.values());
} else{
owner.onlineStrangers.remove(action[1]);
owner.updateContactList(owner.onlineStrangersView,owner.onlineStrangers.values());
}
break;
case GROUPADDED:
groupId = Integer.parseInt(action[1]);
Group g = new Group(action[2],groupId,owner);
owner.groups.put(groupId,g);
owner.updateContactList(owner.groupsView,owner.groups.values());
break;
case GROUPREMOVED:
groupId = Integer.parseInt(action[1]);
owner.groups.remove(groupId);
owner.updateContactList(owner.groupsView,owner.groups.values());
break;
}
owner.flags[sa.ordinal()]=false; //set flag for blocking
}
catch (IOException ex){
ex.printStackTrace();
}
}
}
}
class ContactList<E> extends ListView<E> {
ChangeListener listener;
public ContactList(){
super();
listener = (a,b,c) -> Platform.runLater(() -> getSelectionModel().select(-1));
getSelectionModel().selectedIndexProperty().addListener(listener);
}
public ContactList(Collection<E> items){
this();
setList(items);
}
public void setList(Collection<E> items){
Platform.runLater(() -> setItems(FXCollections.observableArrayList(items)));
}
public void removeItem(E item){
ArrayList<E> items = new ArrayList<>(getItems());
items.remove(item);
Platform.runLater(() -> setItems(FXCollections.observableArrayList(items)));
}
public void addItem(E item){
ArrayList<E> items = new ArrayList<>(getItems());
items.add(item);
Platform.runLater(() -> setItems(FXCollections.observableArrayList(items)));
}
}
class Plus extends Polygon{
public Plus(double width, double height) {
getPoints().addAll(
0.0, height/4,
width/4,height/4,
width/4,0.0,
3*width/4,0.0,
3*width/4,height/4,
width,height/4,
width,3*height/4,
3*width/4,3*height/4,
3*width/4,height,
width/4,height,
width/4,3*height/4,
0.0,3*height/4
);
}
}
class Minus extends Polygon{
public Minus(double width, double height) {
getPoints().addAll(
0.0, height/4,
width,height/4,
width,3*height/4,
0.0,3*height/4
);
}
}