-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
672 lines (608 loc) · 27.2 KB
/
Main.java
File metadata and controls
672 lines (608 loc) · 27.2 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
//Author: Dennis Eriksson Berg
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
public class Main extends Application {
//Datastrukturer
Map<Position, Place> positionPlaceMap = new HashMap<>();
Map<String, ArrayList<Place>> placeCategoryMap = new HashMap<>();
Map<String, ArrayList<Place>> placeNameMap = new HashMap<>();
ArrayList<Place> markedPlaceList = new ArrayList<>();
//Globala Variabler
private Pane panel;
private VBox vBoxRootTop;
private ImageView bg;
private ImageView bg1;
private Image image;
private Button ny;
private Button hideCategory;
private RadioButton named;
private RadioButton described;
private TextField searchField;
private Button searchButton;
private Button hideButton;
private Button removeButton;
private Button coordinateButton;
private ClickHandler clickHandler = new ClickHandler();
private boolean changeFlag = false;
private boolean saveFlag = false;
private ListView listView;
private boolean mapLoaded = false;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Inlupp 2. Author: Dennis Eriksson Berg, Deer7807@student.su.se");
BorderPane root = new BorderPane();
panel = new Pane();
root.setCenter(panel);
vBoxRootTop = new VBox();
root.setTop(vBoxRootTop);
//lägger till menybar
MenuBar menuBar = new MenuBar();
VBox vBoxTop = new VBox(menuBar);
//Lägger till meny
Menu meny1 = new Menu("File");
menuBar.getMenus().add(meny1);
//Lägger till menyitems dropdown
MenuItem loadMap = new MenuItem("New Map");
MenuItem loadPlaces = new MenuItem("Load Places");
MenuItem save = new MenuItem("Save");
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new ExitHandler());
meny1.getItems().addAll(loadMap, loadPlaces, save, exit);
//Action event för att ladda in en karta.
loadMap.setOnAction(event -> {
if (changeFlag == false) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose your map");
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
image = new Image(file.toURI().toString());
bg = new ImageView(image);
panel.getChildren().add(bg);
mapLoaded = true;
}
} else {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("OBS! Det finns osparade ändringar. Vill ni ladda ny karta ändå?");
Optional<ButtonType> res = alert.showAndWait();
if (res.isPresent() && res.get().equals(ButtonType.CANCEL)) {
event.consume();
}
if (res.isPresent() && res.get().equals(ButtonType.OK)) {
deletsPlaces();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose your map");
File fil = fileChooser.showOpenDialog(primaryStage);
if (fil != null) {
if(bg != null){
panel.getChildren().remove(bg);
}
image = new Image(fil.toURI().toString());
bg1 = new ImageView(image);
panel.getChildren().add(bg1);
mapLoaded = true;
}
}
}
});
//Action event för att ladda in platser
loadPlaces.setOnAction(event -> {
if (mapLoaded == true) {
if (changeFlag == false) {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose file containing Places");
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",");
String placeType = tokens[0];
String category = tokens[1].replace("\n", "").replace("\r", "");
double x = Double.parseDouble(tokens[2]);
double y = Double.parseDouble(tokens[3]);
String name = tokens[4];
Position p = new Position(x, y);
if (placeType.equals("Named")) {
NamedPlace namedPlace = new NamedPlace(p, name, category);
addNewPlace(namedPlace);
panel.getChildren().add(namedPlace);
} else {
String description = tokens[5];
DescribedPlace describedPlace = new DescribedPlace(p, name, description, category);
addNewPlace(describedPlace);
panel.getChildren().add(describedPlace);
}
}
br.close();
in.close();
changeFlag = true;
}
} catch (FileNotFoundException e) {
new Alert(Alert.AlertType.ERROR, "Fel!").showAndWait();
} catch (IOException e) {
new Alert(Alert.AlertType.ERROR, "Fel!").showAndWait();
}
} else {
try {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Obs! Det finns osparade ändringar, vill ni ladda nya platser ändå?");
Optional<ButtonType> res = alert.showAndWait();
if (res.isPresent() && res.get().equals(ButtonType.CANCEL)) {
event.consume();
}
if (res.isPresent() && res.get().equals(ButtonType.OK)) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose file containing Places");
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
deletsPlaces();
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",");
String placeType = tokens[0];
String category = tokens[1].replace("\n", "").replace("\r", "");
double x = Double.parseDouble(tokens[2]);
double y = Double.parseDouble(tokens[3]);
String name = tokens[4];
Position p = new Position(x, y);
if (placeType.equals("Named")) {
NamedPlace namedPlace = new NamedPlace(p, name, category);
addNewPlace(namedPlace);
panel.getChildren().add(namedPlace);
} else {
String description = tokens[5];
DescribedPlace describedPlace = new DescribedPlace(p, name, description, category);
addNewPlace(describedPlace);
panel.getChildren().add(describedPlace);
}
}
br.close();
in.close();
}
}
} catch (FileNotFoundException e) {
new Alert(Alert.AlertType.ERROR, "Fel!").showAndWait();
} catch (IOException e) {
new Alert(Alert.AlertType.ERROR, "Fel!").showAndWait();
}
}
}
});
save.setOnAction(event -> {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Spara");
File file = fileChooser.showSaveDialog(primaryStage);
FileWriter utfil = new FileWriter(file);
PrintWriter out = new PrintWriter(utfil);
for (Place p : positionPlaceMap.values()) {
if (p instanceof DescribedPlace) {
DescribedPlace d = (DescribedPlace) p;
out.println("Described" + "," + d.getCategory() + "," + d.getNewPosition().getX()
+ "," + d.getNewPosition().getY() + "," + d.getName() + "," + d.getDescription());
} else {
out.println("Named," + p.getCategory() + "," + p.getNewPosition().getX() +
"," + p.getNewPosition().getY() + "," + p.getName());
}
System.out.println("Saved");
}
utfil.close();
out.close();
changeFlag = false;
} catch (IOException e) {
new Alert(Alert.AlertType.ERROR, "Fel!").showAndWait();
}
});
HBox hBoxTop = new HBox();
hBoxTop.setPadding(new Insets(5));
hBoxTop.setSpacing(5);
hBoxTop.setAlignment(Pos.CENTER);
ny = new Button("New");
ny.setOnAction(new NyButtonHandler());
VBox radioVBoxTop = new VBox();
named = new RadioButton("Named");
described = new RadioButton("Described");
named.setSelected(true);
ToggleGroup radio = new ToggleGroup();
named.setToggleGroup(radio);
described.setToggleGroup(radio);
radioVBoxTop.getChildren().addAll(named, described);
searchField = new TextField("Search");
searchButton = new Button("Search");
searchButton.setOnAction(new SearchHandler());
hideButton = new Button("Hide");
hideButton.setOnAction(new HideButtonHandler());
removeButton = new Button("Remove");
removeButton.setOnAction(new RemoveButtonHandler());
coordinateButton = new Button("Coordinates");
coordinateButton.setOnAction(new CoordinateButtonHandler());
hBoxTop.getChildren().addAll(ny, radioVBoxTop, searchField, searchButton, hideButton, removeButton, coordinateButton);
vBoxRootTop.getChildren().addAll(vBoxTop, hBoxTop);
//FlowPane för högersidan
VBox right = new VBox();
right.setPadding(new Insets(10));
right.setSpacing(5);
right.setAlignment(Pos.CENTER);
root.setRight(right);
//Lista med alternativ som kan väljas i ListView
ObservableList<String> menyVal = FXCollections.observableArrayList("Bus", "Underground", "Train");
//ListView för kategorierna
listView = new ListView(menyVal);
listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
listView.setMaxSize(140, 80);
listView.getSelectionModel().selectedItemProperty().addListener(new ListHandler());
//knapp för HideCategory
hideCategory = new Button("Hide Category");
hideCategory.setOnAction(new HideCategoryButtonHandler());
right.getChildren().addAll(new Label("Category"), listView, hideCategory);
placeCategoryMap.put("Bus", new ArrayList<Place>());
placeCategoryMap.put("None", new ArrayList<Place>());
placeCategoryMap.put("Underground", new ArrayList<Place>());
placeCategoryMap.put("Train", new ArrayList<Place>());
//Sätter upp scenen (För att programmet ska funka).
primaryStage.setOnHiding(windowEvent -> {
if (changeFlag == true) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Osparade ändringar. Avsluta ändå?");
Optional<ButtonType> res = alert.showAndWait();
if (res.isPresent() && res.get().equals(ButtonType.CANCEL))
windowEvent.consume();
if (res.isPresent() && res.get().equals(ButtonType.OK)) {
System.exit(0);
}
} else {
System.exit(0);
}
});
Scene scene = new Scene(root, 1100, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private void newPlace(Position pos) {
String name = null;
String description = null;
Place newPlace = null;
String category = (String) listView.getSelectionModel().getSelectedItem();
if (category == null) {
category = "None";
}
if (named.isSelected()) {
try {
NamedAlert dialog = new NamedAlert();
dialog.setHeaderText("Registrera plats");
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
name = dialog.getNamn();
if (name.trim().isEmpty()) {
Alert msg = new Alert(Alert.AlertType.ERROR, "Tomt namn!");
msg.showAndWait();
return;
}
newPlace = new NamedPlace(pos, name, category);
panel.getChildren().add(newPlace);
}
} catch (Exception e) {
System.out.println("Hejzan");
Alert exception = new Alert(Alert.AlertType.ERROR);
exception.setContentText(e.getMessage());
exception.showAndWait();
}
}
if (described.isSelected()) {
try {
DescribedAlert dialog = new DescribedAlert();
dialog.setHeaderText("Registrera plats & beskrivning");
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
name = dialog.getName();
if (name.trim().isEmpty()) {
Alert msg = new Alert(Alert.AlertType.ERROR, "Tomt namn!");
msg.showAndWait();
return;
}
description = dialog.getDescription();
if (description.trim().isEmpty()) {
Alert msg1 = new Alert(Alert.AlertType.ERROR, "Tom Beskrivning");
msg1.showAndWait();
return;
}
newPlace = new DescribedPlace(pos, name, description, category);
panel.getChildren().add(newPlace);
}
} catch (Exception e) {
Alert exception = new Alert(Alert.AlertType.ERROR);
exception.setContentText(e.getMessage());
exception.showAndWait();
}
}
if (newPlace != null) {
addNewPlace(newPlace);
changeFlag = true;
}
}
private void addNewPlace(Place place) {
if (place != null) {
if (!positionPlaceMap.equals(place)) {
place.setOnMouseClicked(new ClickTriangle());
placeCategoryMap.get(place.getCategory()).add(place);
positionPlaceMap.put(place.getNewPosition(), place);
if (placeNameMap.containsKey(place.getName())) {
placeNameMap.get(place.getName()).add(place);
} else {
ArrayList<Place> list = new ArrayList<>();
placeNameMap.put(place.getName(), list);
placeNameMap.get(place.getName()).add(place);
}
} else {
Alert msg = new Alert(Alert.AlertType.ERROR, "Platsen finns redan!");
msg.showAndWait();
}
}
}
private void deletsPlaces() {
ArrayList<Place> temp = new ArrayList<>(positionPlaceMap.values());
for (Place p : temp) {
removeEverything(p);
}
}
private void removeEverything(Place p) {
removeMarkedStateAll();
p.setMarked(false);
p.setVisible(false);
String name = p.getName();
String category = p.getCategory();
Position position = p.getNewPosition();
positionPlaceMap.remove(position, p);
placeCategoryMap.get(category).remove(position);
placeNameMap.get(name).remove(p);
if (placeNameMap.get(name).isEmpty()) {
ArrayList<Place> temp = placeNameMap.get(name);
placeNameMap.remove(name, temp);
}
}
class NyButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (mapLoaded == true) {
panel.addEventHandler(MouseEvent.MOUSE_CLICKED, clickHandler);
panel.setCursor(Cursor.CROSSHAIR);
ny.setDisable(true);
}
}
}
private void removeMarkedStateAll() {
for (Place place : markedPlaceList) {
place.setMarked(false);
}
markedPlaceList.clear();
}
private void removePlaces(Place place) {
String name = place.getName();
String category = place.getCategory();
Position position = place.getNewPosition();
if (place.getMarked() == true) {
positionPlaceMap.remove(position, place);
placeCategoryMap.remove(category, place);
placeNameMap.remove(name, place);
panel.getChildren().remove(place);
place.setMarked(false);
changeFlag = true;
}
}
class RemoveButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
for (Place place : markedPlaceList) {
removePlaces(place);
}
markedPlaceList.clear();
}
}
private void hidePlaces(Place place) {
if (place.getMarked() == true) {
place.setVisible(false);
}
}
class HideButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
for (Place place : markedPlaceList) {
hidePlaces(place);
}
}
}
class ClickHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
double x = event.getX();
double y = event.getY();
System.out.println("Skapar nyPos");
Position nyPos = new Position(x, y);
//Läggs i slutet
panel.removeEventHandler(MouseEvent.MOUSE_CLICKED, clickHandler);
panel.setCursor(Cursor.DEFAULT);
ny.setDisable(false);
//Skicka position till newPlace
newPlace(nyPos);
//Renssa listView efter val har gjorts.
listView.getSelectionModel().clearSelection();
}
}
class ExitHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (changeFlag == true) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Osparade ändringar. Avsluta ändå?");
Optional<ButtonType> res = alert.showAndWait();
if (res.isPresent() && res.get().equals(ButtonType.CANCEL))
event.consume();
if (res.isPresent() && res.get().equals(ButtonType.OK)) {
System.exit(0);
}
} else {
System.exit(0);
}
}
}
class ClickTriangle implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
Place place = (Place) event.getSource();
if (event.getButton() == MouseButton.PRIMARY) {
if (!place.getMarked()) {
place.setMarked(true);
markedPlaceList.add(place);
System.out.println("Platsen är markerad" + place.getNewPosition().getX() + "[]" + place.getNewPosition().getY());
} else {
markedPlaceList.remove(place);
place.setMarked(false);
System.out.println("platsen är inte längre markerad");
}
}
if (event.getButton() == MouseButton.SECONDARY) {
if (place instanceof NamedPlace) {
Alert message = new Alert(Alert.AlertType.INFORMATION, place.getName() + " [" + place.getNewPosition().getX() + " ," + place.getNewPosition().getY() + "]");
message.showAndWait();
}
if (place instanceof DescribedPlace) {
DescribedPlace describedPlace = (DescribedPlace) place;
Alert message1 = new Alert(Alert.AlertType.INFORMATION, place.getName() + " [" + place.getNewPosition().getX() + " ," + place.getNewPosition().getY() + "]" + "\n" + describedPlace.getDescription());
message1.showAndWait();
}
}
}
}
class HideCategoryButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
removeMarkedStateAll();
String category = (String) listView.getSelectionModel().getSelectedItem();
if (placeCategoryMap.get(category) != null) {
ArrayList<Place> temp = placeCategoryMap.get(category);
for (Place p : temp) {
markedPlaceList.add(p);
p.setVisible(false);
}
}
listView.getSelectionModel().clearSelection();
}
}
class ListHandler implements ChangeListener<String> {
@Override
public void changed(ObservableValue obs, String old, String nev) {
System.out.println(old + " changed to " + nev);
if (placeCategoryMap.get(nev) != null) {
ArrayList<Place> temp = placeCategoryMap.get(nev);
for (Place p : temp) {
p.setVisible(true);
}
}
}
}
class CoordinateButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
try {
CoordinateButtonAlert dialog = new CoordinateButtonAlert();
dialog.setHeaderText("Sök koordinater");
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
String x = dialog.getXCor();
Double xd = Double.parseDouble(x);
System.out.println("xd: " + xd);
if (xd < 0) {
Alert msg = new Alert(Alert.AlertType.ERROR, "Ogiltig inmatning!");
msg.showAndWait();
return;
}
String y = dialog.getYCor();
Double yd = Double.parseDouble(y);
System.out.println("yd: " + yd);
if (yd < 0) {
Alert msg1 = new Alert(Alert.AlertType.ERROR, "Ogiltig inmatning");
msg1.showAndWait();
return;
}
Position position = new Position(xd, yd);
if (positionPlaceMap.containsKey(position)) {
System.out.println("inne i if satsen");
Place place = positionPlaceMap.get(position);
removeMarkedStateAll();
place.setMarked(true);
place.setVisible(true);
markedPlaceList.add(place);
} else {
Alert msg2 = new Alert(Alert.AlertType.ERROR, "Platsen finns inte i registret!");
msg2.showAndWait();
return;
}
}
} catch (Exception e) {
Alert exception = new Alert(Alert.AlertType.ERROR);
exception.setContentText(e.getMessage());
exception.showAndWait();
}
}
}
class SearchHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (searchField.getText() != "Search" && searchField.getText() != null) {
removeMarkedStateAll();
String text = searchField.getText();
System.out.println(Collections.singletonList(placeNameMap));
if (placeNameMap.keySet().contains(text)) {
System.out.println("texten finns i placenamemap");
ArrayList<Place> temp = placeNameMap.get(text);
for (Place place : temp) {
markedPlaceList.add(place);
place.setVisible(true);
place.setMarked(true);
}
} else {
new Alert(Alert.AlertType.ERROR, "Ingen plats finns med det angivna namnet");
}
} else {
new Alert(Alert.AlertType.ERROR, "Fel!");
}
}
}
//Main metod
public static void main(String[] args) {
launch(args);
}
}