-
Notifications
You must be signed in to change notification settings - Fork 0
/
FXMLDocumentController.java
537 lines (477 loc) · 15.3 KB
/
FXMLDocumentController.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
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
/*
* 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 worldcountries;
import java.io.*;
import static java.lang.System.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;
import javafx.event.ActionEvent;
import javafx.fxml.*;
import javafx.scene.media.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javax.sound.sampled.*;
/**
*
* @author Lleaf
*/
public class FXMLDocumentController implements Initializable {
private Label label;
@FXML
private MenuItem editBt;
@FXML
private MenuItem closeBt;
@FXML
private MenuItem freeModeBt;
@FXML
private MenuItem countryBt;
@FXML
private MenuItem capitalBt;
@FXML
private MenuItem flagBt;
@FXML
private MenuItem darkBt;
@FXML
private MenuItem lightBt;
@FXML
private VBox Vbox;
@FXML
private ImageView FlagImg;
@FXML
private TextField countryTf;
@FXML
private TextField capitalTf;
@FXML
private Button saveBt;
@FXML
private Button deleteBt;
@FXML
private Button nextBt;
@FXML
private ProgressBar progressBar;
@FXML
private ImageView mapImg;
@FXML
private ImageView markerImg;
@FXML
private ScrollPane scrollPane;
@FXML
private Button PreviousBt;
@FXML
private MenuItem AllBt;
@FXML
private MenuItem AfricaBt;
@FXML
private MenuItem AmericaBt;
@FXML
private MenuItem AsiaBt;
@FXML
private MenuItem EuropeBt;
@FXML
private MenuItem OceniaBt;
@FXML
private MenuItem AmericanStatesBt;
@FXML
private Menu continentMb;
@FXML
private MenuItem doneMi;
@FXML
private Menu modeMb;
@FXML
private CheckMenuItem soundBt;
// Global Variables
private int x;
private int mode;
private Country tempCountry;
private ArrayList<Country> countries;
private String DataFile = "C:\\Users\\shabi\\Documents\\NetBeansProjects\\WorldCountries\\Resources\\Data_ALL.ser";
private String path;
private Media media;
private MediaPlayer media_player;
@Override
public void initialize(URL url, ResourceBundle rb) {
x = 0;
nextBt.setVisible(false);
saveBt.setVisible(false);
deleteBt.setVisible(false);
PreviousBt.setVisible(false);
progressBar.setVisible(false);
soundBt.setSelected(true);
tempCountry = new Country();
ReadFile();
setMap();
scrollPane.setHvalue(0.42337164750957856);
scrollPane.setVvalue(0.16975308641975312);
}
@FXML
private void Edit(ActionEvent event) {
continentMb.setDisable(true);
modeMb.setDisable(true);
x = 0;
mode = 2;
ReadFile();
//countries = tempCountry.getCountries();
updateGUI();
FreeMode(event);
//DeleteDashes();
}
// This method deletes dashes of a country's name
private void DeleteDashes() {
//Get ride of dashes
for (int i = 0; i < countries.size(); i++) {
countries.get(i).setName(countries.get(i).getName().replace("-", " "));
countries.get(i).setCapital(countries.get(i).getCapital().replace("-", " "));
}
}
@FXML
private void FreeMode(ActionEvent event) {
x = 0;
mode = 2;
countryTf.setDisable(false);
capitalTf.setDisable(false);
progressBar.setVisible(true);
nextBt.setVisible(true);
PreviousBt.setVisible(true);
saveBt.setVisible(true);
deleteBt.setVisible(true);
updateGUI();
//Next(event);
}
@FXML
private void Country(ActionEvent event) {
mode = 3;
Collections.shuffle(countries);
countryTf.setDisable(true);
capitalTf.setDisable(false);
progressBar.setVisible(true);
nextBt.setVisible(true);
saveBt.setVisible(false);
deleteBt.setVisible(false);
PreviousBt.setVisible(true);
NewSetup(event);
}
@FXML
private void Capital(ActionEvent event) {
mode = 4;
Collections.shuffle(countries);
countryTf.setDisable(false);
capitalTf.setDisable(true);
progressBar.setVisible(true);
nextBt.setVisible(true);
saveBt.setVisible(false);
deleteBt.setVisible(false);
PreviousBt.setVisible(true);
NewSetup(event);
}
@FXML
private void Flag(ActionEvent event) {
mode = 5;
Collections.shuffle(countries);
countryTf.setDisable(false);
capitalTf.setDisable(false);
progressBar.setVisible(true);
nextBt.setVisible(true);
saveBt.setVisible(false);
deleteBt.setVisible(false);
PreviousBt.setVisible(true);
NewSetup(event);
}
@FXML
private void Dark(ActionEvent event) {
}
@FXML
private void Light(ActionEvent event) {
System.out.println("H: " + scrollPane.getHvalue() + " V: " + scrollPane.getVvalue());
}
@FXML
private void Next(ActionEvent event) {
setupNext();
}
private void setupNext() {
if (mode != 2) {
if (!(countryTf.getText().toUpperCase().trim().equals(countries.get(x).getName().toUpperCase())
&& capitalTf.getText().toUpperCase().trim().equals(countries.get(x).getCapital().toUpperCase()))) {
if (soundBt.isSelected()) {
PlaySound("Wrong.mp3");
}
return;
}
}
if (soundBt.isSelected()) {
PlaySound("Right.wav");
}
ConfigureX();
updateGUI();
}
private void updateGUI() {
switch (mode) {
case 2:
countryTf.clear();
capitalTf.clear();
countryTf.setText(countries.get(x).getName());
capitalTf.setText(countries.get(x).getCapital());
break;
case 3:
countryTf.setText(countries.get(x).getName());
capitalTf.clear();
break;
case 4:
countryTf.clear();
capitalTf.setText(countries.get(x).getCapital());
break;
case 5:
countryTf.clear();
capitalTf.clear();
break;
}
scrollPane.setHvalue(countries.get(x).getScrollBarH());
scrollPane.setVvalue(countries.get(x).getScrollBarV());
markerImg.setTranslateX(countries.get(x).getMarkerX());
markerImg.setTranslateY(countries.get(x).getMarkerY());
progressBar.setProgress(x / countries.size());
try {
File file = new File(countries.get(x).getFlagPath());
String localUrl = file.toURI().toURL().toString();
FlagImg.setImage(new Image(localUrl));
} catch (MalformedURLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void ConfigureX() {
if (x == countries.size() - 1) {
x = 0;
} else {
x++;
}
}
private void NewSetup(ActionEvent event) {
x = 0;
updateGUI();
}
@FXML
private void Save(ActionEvent event) {
countries.get(x).setName(countryTf.getText());
countries.get(x).setCapital(capitalTf.getText());
countries.get(x).setScrollBarH(scrollPane.getHvalue());
countries.get(x).setScrollBarV(scrollPane.getVvalue());
countries.get(x).setMarkerX(markerImg.getTranslateX());
countries.get(x).setMarkerY(markerImg.getTranslateY());
SaveData(event);
}
private void SaveData(ActionEvent event) {
File file = new File(DataFile);
if (file.delete()) {
// Do nothing
} else {
System.out.println("Failed to delete the file");
}
try {
FileOutputStream fileOut = new FileOutputStream(DataFile);
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(countries);
fileOut.close();
objOut.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@FXML
private void mapClicked(MouseEvent event) {
markerImg.setTranslateX(event.getX() - 110);
markerImg.setTranslateY(event.getY() - 79);
}
@FXML
private void Delete(ActionEvent event) {
if (mode == 2) {
countries.remove(countries.get(x));
SaveData(event);
}
}
@FXML
private void Previous(ActionEvent event) {
x = x - 2;
Next(event);
}
private void ReadFile() {
try {
// Read Data
FileInputStream fileIn = new FileInputStream(DataFile);
ObjectInputStream objIn = new ObjectInputStream(fileIn);
countries = (ArrayList<Country>) objIn.readObject();
fileIn.close();
objIn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void Search(String country) {
}
private void ReadAndKeep(String continent) {
try {
FileInputStream fileIn = new FileInputStream(DataFile);
ObjectInputStream objIn = new ObjectInputStream(fileIn);
ArrayList<Country> TempCountries = (ArrayList<Country>) objIn.readObject();
countries.clear();
//ArrayList<Country> TempCountries = tempCountry.getCountries();
for (int i = 0; i < TempCountries.size(); i++) {
if (TempCountries.get(i).getContinent().equals(continent)) {
countries.add(TempCountries.get(i));
}
}
if (mode != 2) {
Collections.shuffle(countries);
}
fileIn.close();
objIn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@FXML
private void Hint(MouseEvent event) {
if (!countryTf.isDisable()) {
countryTf.clear();
countryTf.setText(countries.get(x).getName());
}
if (!capitalTf.isDisable()) {
capitalTf.clear();
capitalTf.setText(countries.get(x).getCapital());
}
}
@FXML
private void ExitApp(ActionEvent event) {
exit(0);
}
@FXML
private void allContinents(ActionEvent event) {
setMap();
ReadFile();
updateGUI();
}
@FXML
private void Africa(ActionEvent event) {
setMap();
ReadAndKeep("Africa");
updateGUI();
//System.out.print("Africa: " + countries.size());
}
@FXML
private void America(ActionEvent event) {
setMap();
ReadAndKeep("America");
x = 0;
updateGUI();
//System.out.print("America: " + countries.size());
}
@FXML
private void Asia(ActionEvent event) {
setMap();
ReadAndKeep("Asia");
x = 0;
updateGUI();
//System.out.print("Asia: " + countries.size());
}
@FXML
private void Europe(ActionEvent event) {
setMap();
ReadAndKeep("Europe");
x = 0;
updateGUI();
//System.out.print("Europe: " + countries.size());
}
@FXML
private void Ocenia(ActionEvent event) {
setMap();
ReadAndKeep("Oceania");
x = 0;
updateGUI();
//System.out.print("Ocenia: " + countries.size());
}
@FXML
private void NextFrCountry(ActionEvent event) {
if (mode == 2) {
for (int i = 0; i < countries.size(); i++) {
if (countries.get(i).getName().toUpperCase().equals(countryTf.getText().toUpperCase().trim())) {
x = i;
updateGUI();
}
}
} else {
setupNext();
}
}
@FXML
private void NextFrCapital(ActionEvent event) {
if (mode == 2) {
for (int i = 0; i < countries.size(); i++) {
if (countries.get(i).getCapital().toUpperCase().equals(capitalTf.getText().toUpperCase().trim())) {
x = i;
updateGUI();
}
}
} else {
setupNext();
}
}
@FXML
private void AmericanStates(ActionEvent event) {
//getStates();
//SaveData(event);
try {
File map = new File(countries.get(x).getPathToResources() + "AmericanStatesMap.png");
File marker = new File(countries.get(x).getPathToResources() + "Marker.png");
String localUrlmap = map.toURI().toURL().toString();
String localUrlmarker = marker.toURI().toURL().toString();
mapImg.setImage(new Image(localUrlmap));
markerImg.setImage(new Image(localUrlmarker));
} catch (Exception ex) {
System.out.print("\n*******************************American States");
ex.printStackTrace();
}
ReadAndKeep("United States");
x = 0;
updateGUI();
//System.out.print("States: " + countries.size());
}
public void setMap() {
try {
File map = new File(countries.get(x).getPathToResources() + "WorldMapDark.png");
File marker = new File(countries.get(x).getPathToResources() + "Marker.png");
String localUrlmap = map.toURI().toURL().toString();
String localUrlmarker = marker.toURI().toURL().toString();
mapImg.setImage(new Image(localUrlmap));
markerImg.setImage(new Image(localUrlmarker));
} catch (Exception ex) {
ex.printStackTrace();
}
}
@FXML
private void doneEdit(ActionEvent event) {
continentMb.setDisable(false);
modeMb.setDisable(false);
try {
FileWriter writer = new FileWriter("MyFile.txt", true);
for (int i = 0; i < countries.size(); i++) {
writer.write(i + " " + countries.get(i).getName() + " " + countries.get(i).getCapital() + " "
+ countries.get(i).getAbbr() + " " + countries.get(i).getContinent() + " "
+ countries.get(i).getMarkerX() + " " + countries.get(i).getMarkerY() + " "
+ countries.get(i).getScrollBarH() + " " + countries.get(i).getScrollBarV() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void PlaySound(String sound) {
path = tempCountry.getPathToResources() + sound;
media = new Media(new File(path).toURI().toString());
media_player = new MediaPlayer(media);
media_player.play();
}
}