-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowController.java
164 lines (149 loc) · 5.1 KB
/
WindowController.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chat.app;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javafx.scene.control.TextArea;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Vector;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
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.HBox;
/**
* FXML Controller class
*
* @author manvir
*/
public class WindowController implements Initializable {
@FXML
private Label prompt;
@FXML
private Button button;
@FXML
private ListView userList;
@FXML
private TextArea outputArea;
@FXML
private TextField inputArea;
private Socket client;
private ObjectInputStream in;
private ObjectOutputStream out;
private ArrayList<Integer> array = new ArrayList();
private int forlist = 0;
@FXML private CheckBox cb;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
prompt.setText("Enter name: ");
}
private void updateList(Vector<Person> list) {
if(forlist == 0){
userList.getItems().clear();
array.clear();
}
for (int i = forlist; i < list.size(); ++i) {
HBox hbox = new HBox(5);
cb = new CheckBox();
hbox.getChildren().addAll(cb,new Label(list.get(i).getName()));
final int pass = i;
cb.setOnAction(e->ifCheckBoxClicked(e,pass));
userList.getItems().add(hbox);
}
forlist = list.size();
System.out.println("forlist: "+ forlist);
}
private void connect() {
Thread t = new Thread(new Thread() {
public void run() {
try {
client = new Socket("localhost", 43331);
out = new ObjectOutputStream(client.getOutputStream());
in = new ObjectInputStream(client.getInputStream());
String username = inputArea.getText();
out.writeObject(username);
prompt.setVisible(false);
inputArea.clear();
while (true) {
Object msg = in.readObject();
if (msg instanceof String) {
outputArea.appendText((String) msg);
} else {
System.out.println("new list! " + ((Vector) msg).size());
Platform.runLater(new Runnable() {
@Override
public void run() {
if(((Vector)msg).size() < forlist){
forlist = 0;
}
updateList((Vector<Person>) msg);
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
@FXML
private void ifKeyPressed(KeyEvent e) {
// if enter send message/connect
if (e.getCode().equals(KeyCode.ENTER)) {
buttonPressed();
}
}
@FXML
private void ifCheckBoxClicked(ActionEvent e, int i){
for(int a = 0; a < array.size();++a){
if(array.get(a) == (Object)i){
array.remove(a);
System.out.println("array.remove aftersize:"+ array.size());
return;
}
}
array.add(i);
for(int a = 0; a < array.size();++a)
System.out.println(array.get(a)+ " this is the size "+ array.size());
}
@FXML
private void buttonPressed() {
if (client == null) {
System.out.println("button pressed");
connect();
return;
}
try {
String msg = inputArea.getText();
out.writeObject(msg);
inputArea.clear();
System.out.println("arraysize before sending it:"+ array.size());
out.writeObject(array.size());
for(int i = 0; i < array.size(); ++i){
out.writeObject(array.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}