-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathController.java
More file actions
247 lines (232 loc) · 8.99 KB
/
Copy pathController.java
File metadata and controls
247 lines (232 loc) · 8.99 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
package sample;
import com.sun.jdi.event.ExceptionEvent;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.SerializationUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.*;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
public class Controller implements Initializable {
@FXML
Pane pane;
@FXML
Button buttonLoad;
@FXML
Label labelInfo;
@FXML
TextField textFieldServerIp;
@FXML
ComboBox comboBoxMode;
String[] modes;
//Label labelClientId;
//TextField textFieldClientId;
HBox hBoxBottom;
JSONObject jsonObjectRates;
public void getDateFromServer() {
try {
Socket socket = new Socket(getServerIpString(), 5000);
Calendar calendar = ((Calendar) getObjectFromServer(socket));
labelInfo.setText(" Seconds : " + calendar.get(Calendar.SECOND) + " Minutes : "
+ calendar.get(Calendar.MINUTE) + " Hours : " + calendar.get(Calendar.HOUR)
+ " Day : " + calendar.get(Calendar.DAY_OF_MONTH) + " Month : " + calendar.get(Calendar.MONTH)
+ " Year : " + calendar.get(Calendar.YEAR));
socket.close();
} catch (Exception ex) {
labelInfo.setText("Error. Couldn't load data from server." + ex.getMessage());
}
}
public void getAphorismFromServer() {
try {
Socket socket = new Socket(getServerIpString(), 6000);
String aphorism = (String) getObjectFromServer(socket);
labelInfo.setText(aphorism);
socket.close();
} catch (Exception ex) {
labelInfo.setText("Error. Couldn't load data from server." + ex.getMessage());
}
}
private Object getObjectFromServer(Socket socket) throws Exception {
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
int packetSize = dataInputStream.readInt();
byte[] bytes = new byte[packetSize];
dataInputStream.read(bytes);
return SerializationUtils.deserialize(bytes);
}
private String getServerIpString() {
String serverIp;
if (textFieldServerIp.getText() == "")
serverIp = "localhost";
else
serverIp = textFieldServerIp.getText();
return serverIp;
}
private void listenToServerEvents() {
try {
labelInfo.setText("");
Socket socket = new Socket(getServerIpString(), 7000);
Thread readingThread = new Thread() {
public void run() {
try {
while (true) {
TextField textFieldId = (TextField) hBoxBottom.getChildren().get(1);
sendData(socket, Integer.parseInt(textFieldId.getText()));
String serverMessage = (String) getObjectFromServer(socket);
if (!serverMessage.equals(""))
Platform.runLater(() -> labelInfo.setText(labelInfo.getText() + "\n" + serverMessage));
}
} catch (Exception ex) {
}
}
};
readingThread.start();
} catch (Exception ex) {
labelInfo.setText(labelInfo.getText() + "\n" + ex.getMessage());
}
}
private void sendData(Socket socket, Object object) throws Exception {
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
byte[] bytes = SerializationUtils.serialize((Serializable) object);
dataOutputStream.writeInt(bytes.length);
dataOutputStream.write(bytes);
dataOutputStream.flush();
}
public void setButtonEvent() {
switch ((String) comboBoxMode.getSelectionModel().getSelectedItem()) {
case "Date":
deleteBottomLine();
buttonLoad.setOnAction((e) -> getDateFromServer());
break;
case "Aphorism":
deleteBottomLine();
buttonLoad.setOnAction((e) -> getAphorismFromServer());
break;
case "Events log": {
deleteBottomLine();
createClientIdLine();
buttonLoad.setOnAction((e) -> listenToServerEvents());
break;
}
case "Currency": {
deleteBottomLine();
createCurrencyLine();
buttonLoad.setOnAction((e) -> showCurrencyRelation());
break;
}
}
}
private void showCurrencyRelation()
{
try {
double leftPrice = 1.0 / getCurrencyValue(getCurrencyCombobox(0).getSelectionModel().getSelectedItem());
double rightPrice = 1.0 / getCurrencyValue(getCurrencyCombobox(1).getSelectionModel().getSelectedItem());
labelInfo.setText("1 = " + rightPrice / leftPrice);
}
catch (Exception ex)
{
labelInfo.setText("Please set values properly. "+ex.getMessage());
}
}
private void createClientIdLine() {
if (hBoxBottom == null) {
hBoxBottom = new HBox();
Label labelClientId = new Label();
labelClientId.setText("Client id");
TextField textFieldClientId = new TextField();
hBoxBottom.getChildren().addAll(labelClientId, textFieldClientId);
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().add(hBoxBottom);
}
}
private double getCurrencyValue(String currency)
{
return jsonObjectRates.getDouble(currency);
}
private void createCurrencyLine() {
if (hBoxBottom == null) {
hBoxBottom = new HBox();
ComboBox comboBoxLeftValue = new ComboBox();
comboBoxLeftValue.setMinSize(pane.getWidth() / 2, comboBoxLeftValue.getHeight());
ComboBox comboBoxRightValue = new ComboBox();
comboBoxRightValue.setMinSize(pane.getWidth() / 2, comboBoxLeftValue.getHeight());
hBoxBottom.getChildren().addAll(comboBoxLeftValue, comboBoxRightValue);
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().add(hBoxBottom);
ArrayList<String> values=getCurrecniesListFromSite();
ComboBox<String> comboBoxLeft=getCurrencyCombobox(0);
ComboBox<String> comboBoxRight=getCurrencyCombobox(1);
comboBoxLeft.getItems().addAll(values);
comboBoxRight.getItems().addAll(values);
}
}
private void deleteBottomLine() {
if (hBoxBottom != null) {
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().remove(hBoxBottom);
hBoxBottom = null;
}
}
private ComboBox<String> getCurrencyCombobox(int index)
{
return (ComboBox<String>) hBoxBottom.getChildren().get(index);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
labelInfo.setWrapText(true);
modes = new String[]{"Date", "Aphorism", "Events log", "Currency"};
comboBoxMode.getItems().addAll(modes);
}
private ArrayList<String> getCurrecniesListFromSite() {
try {
jsonObjectRates = getJsonObjectRates();
Iterator iterator = jsonObjectRates.keys();
ArrayList<String> names = new ArrayList<String>();
while (iterator.hasNext()) {
names.add( (String) iterator.next());
}
return names;
} catch (Exception ex) {
}
return null;
}
private JSONObject getJsonObjectRates() throws Exception
{
JSONObject jsonObject = readJsonFromUrl("https://openexchangerates.org/api/latest.json?app_id=5a11aa4d56564bbdb101289a6fa338d7");
JSONObject jsonRates = jsonObject.getJSONObject("rates");
return jsonRates;
}
//Copied code
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}