-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathFinder.java
554 lines (505 loc) · 21.7 KB
/
PathFinder.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
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.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.*;
// *** VT2021, Inlämningsuppgift, del 2
// Grupp 096
// joas47
public class PathFinder extends Application {
private static final String SAVE_FILE_NAME = "europa.graph";
private static final String IMAGE_FILE_NAME = "file:europa.gif";
private static final int SCENE_WIDTH = 600;
private static final int SCENE_HEIGHT = 80;
private static final int BUTTON_SPACING = 10;
private static final int NODE_TEXT_Y_OFFSET = 24;
private static final int NODE_TEXT_FONT_SIZE = 15;
private static final int CONNECTION_LINE_WIDTH = 3;
private static final int SCENE_WIDTH_OFFSET = 15;
private static final int SCENE_HEIGHT_OFFSET = 100;
private static final int BUTTON_PADDING = 8;
private BorderPane bp = new BorderPane();
private Scene scene;
private Stage primaryStage;
private Button newPlaceBt;
private Image image;
private Pane pane = new Pane();
private ImageView imageView = new ImageView();
private ListGraph<GraphNode> listGraph = new ListGraph<>();
private GraphNode selectedNode1;
private GraphNode selectedNode2;
private boolean unsavedChanges = true;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
// MenuBar
Menu menu = new Menu("File");
menu.setId("menuFile");
MenuItem newMap = new MenuItem("New Map");
newMap.setId("menuNewMap");
newMap.setOnAction(new NewMapHandler());
MenuItem open = new MenuItem("Open");
open.setId("menuOpenFile");
open.setOnAction(new OpenHandler());
MenuItem save = new MenuItem("Save");
save.setId("menuSaveFile");
save.setOnAction(new SaveHandler());
MenuItem saveImage = new MenuItem("Save Image");
saveImage.setId("menuSaveImage");
saveImage.setOnAction(new SaveImageHandler());
MenuItem exit = new MenuItem("Exit");
exit.setId("menuExit");
exit.setOnAction(new ExitHandler());
menu.getItems().addAll(newMap, open, save, saveImage, exit);
MenuBar menuBar = new MenuBar();
menuBar.setId("menu");
menuBar.getMenus().add(menu);
VBox menuVbox = new VBox(menuBar);
// Buttons
Button findPathBt = new Button("Find Path");
findPathBt.setId("btnFindPath");
findPathBt.setOnAction(new FindPathHandler());
Button showConnectionBt = new Button("Show Connection");
showConnectionBt.setId("btnShowConnection");
showConnectionBt.setOnAction(new ShowConnectionHandler());
newPlaceBt = new Button("New Place");
newPlaceBt.setId("btnNewPlace");
newPlaceBt.setOnAction(new NewPlaceHandler());
Button newConnectionBt = new Button("New Connection");
newConnectionBt.setId("btnNewConnection");
newConnectionBt.setOnAction(new NewConnectionHandler());
Button changeConnectionBt = new Button("Change Connection");
changeConnectionBt.setId("btnChangeConnection");
changeConnectionBt.setOnAction(new ChangeConnectionHandler());
HBox buttonsHBox = new HBox(findPathBt, showConnectionBt, newPlaceBt,
newConnectionBt, changeConnectionBt);
buttonsHBox.setPadding(new Insets(BUTTON_PADDING));
buttonsHBox.setSpacing(BUTTON_SPACING);
buttonsHBox.setAlignment(Pos.TOP_CENTER);
// BorderPane
bp.setTop(menuVbox);
bp.setCenter(buttonsHBox);
bp.setBottom(pane);
pane.setId("outputArea");
// Scene
scene = new Scene(bp, SCENE_WIDTH, SCENE_HEIGHT);
primaryStage.setTitle("PathFinder");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(new WindowCloseHandler());
primaryStage.show();
}
private Alert unsavedChangesAlert() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
"Unsaved changes, continue anyway?");
alert.setTitle("Warning!");
alert.setHeaderText(null);
return alert;
}
private void errorAlert(String s) {
Alert timeAlert = new Alert(Alert.AlertType.ERROR, s);
timeAlert.setTitle("Error!");
timeAlert.setHeaderText(null);
timeAlert.showAndWait();
}
private void insertNode(String name, double x, double y) {
GraphNode node = new GraphNode(x, y, name);
node.setOnMouseClicked(new NodeClickHandler());
node.setId(name);
Text text = new Text(x, y + NODE_TEXT_Y_OFFSET, name);
text.setFont(Font.font("Arial", FontWeight.BOLD, NODE_TEXT_FONT_SIZE));
text.setDisable(true);
pane.getChildren().addAll(node, text);
listGraph.add(node);
}
private void insertConnection(String from, String to, String by, int weight) {
Set<GraphNode> nodes = listGraph.getNodes();
GraphNode fromNode = null;
GraphNode toNode = null;
for (GraphNode graphNode : nodes) {
if (graphNode.getName().equals(from)) {
fromNode = graphNode;
} else if (graphNode.getName().equals(to)) {
toNode = graphNode;
}
}
Line connection = new Line(Objects.requireNonNull(fromNode).getCenterX(),
fromNode.getCenterY(), Objects.requireNonNull(toNode).getCenterX(),
toNode.getCenterY());
connection.setStrokeWidth(CONNECTION_LINE_WIDTH);
connection.setDisable(true);
if (listGraph.getEdgeBetween(fromNode, toNode) == null) {
listGraph.connect(fromNode, toNode, by, weight);
pane.getChildren().add(connection);
}
}
private void resetGraph() {
selectedNode1 = null;
selectedNode2 = null;
Set<GraphNode> set = listGraph.getNodes();
for (GraphNode graphNode : set) {
listGraph.remove(graphNode);
}
unsavedChanges = false;
}
private AlertWithFields getAlertWithFields() {
AlertWithFields alert = new AlertWithFields();
alert.setTitle("Connection");
alert.setHeaderText("Connection from " + selectedNode1.getName() + " to "
+ selectedNode2.getName());
return alert;
}
private class WindowCloseHandler implements EventHandler<WindowEvent> {
@Override
public void handle(WindowEvent windowEvent) {
if (pane.getChildren().size() != 0 && unsavedChanges) {
Alert alert = unsavedChangesAlert();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.CANCEL)) {
windowEvent.consume();
}
} else {
Platform.exit();
}
}
}
private class ShowConnectionHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
try {
if (listGraph.getEdgeBetween(selectedNode1, selectedNode2) != null) {
AlertWithFields alert = getAlertWithFields();
alert.getNameField().setText(
listGraph.getEdgeBetween(selectedNode1, selectedNode2).getName());
alert.getTimeField().setText(String.valueOf(
listGraph.getEdgeBetween(selectedNode1, selectedNode2).getWeight()));
alert.getNameField().setEditable(false);
alert.getTimeField().setEditable(false);
alert.showAndWait();
} else {
errorAlert("No connections between places exist!");
}
} catch (NoSuchElementException e) {
errorAlert("Two places must be selected!");
}
}
}
}
private class NewConnectionHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
try {
if (listGraph.getEdgeBetween(selectedNode1, selectedNode2) == null) {
AlertWithFields alert = getAlertWithFields();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.OK)) {
if (!alert.getName().isBlank()) {
insertConnection(selectedNode1.getName(), selectedNode2.getName(),
alert.getName(), alert.getTime());
unsavedChanges = true;
} else {
errorAlert("Name-field can't be empty!");
}
}
} else {
errorAlert("Connection already exists between locations!");
}
} catch (NoSuchElementException e) {
errorAlert("Two places must be selected!");
} catch (NumberFormatException e) {
errorAlert("Time-field must contain number!");
} catch (IllegalArgumentException e) {
errorAlert("Time can't be negative!");
}
}
}
}
private class ChangeConnectionHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
try {
if (listGraph.getEdgeBetween(selectedNode1, selectedNode2) != null) {
AlertWithFields alert = getAlertWithFields();
alert.getNameField().setText(listGraph.getEdgeBetween(selectedNode1, selectedNode2).getName());
alert.getNameField().setEditable(false);
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.OK)) {
listGraph.setConnectionWeight(selectedNode1, selectedNode2, alert.getTime());
unsavedChanges = true;
}
} else {
errorAlert("No connection between places exist!");
}
} catch (NoSuchElementException e) {
errorAlert("Two places must be selected!");
} catch (NumberFormatException e) {
errorAlert("Time-field must contain number!");
} catch (IllegalArgumentException e) {
errorAlert("Time can't be negative!");
}
}
}
}
private class SaveHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
try {
FileWriter writer = new FileWriter(SAVE_FILE_NAME);
writer.write(IMAGE_FILE_NAME + '\n');
Set<GraphNode> nodes = listGraph.getNodes();
for (Iterator<GraphNode> iterator = nodes.iterator(); iterator.hasNext(); ) {
GraphNode node = iterator.next();
writer.write(node.getName() + ";" + node.getCenterX() + ";" + node.getCenterY());
if (iterator.hasNext()) {
writer.write(";");
} else {
writer.write('\n');
}
}
for (GraphNode node : nodes) {
Set<Edge<GraphNode>> edges = listGraph.getEdgesFrom(node);
for (Edge<GraphNode> edge : edges) {
writer.write(node.getName() + ";" + edge.getDestination()
+ ";" + edge.getName() + ";" + edge.getWeight() + '\n');
}
}
writer.close();
unsavedChanges = false;
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR, "IO-error " + e.getMessage());
alert.showAndWait();
}
}
}
}
private class FindPathHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
if (selectedNode1 != null && selectedNode2 != null) {
if (listGraph.pathExists(selectedNode1, selectedNode2)) {
List<Edge<GraphNode>> path = listGraph.getPath(selectedNode1, selectedNode2);
StringBuilder string = new StringBuilder();
int totalTime = 0;
for (Edge<GraphNode> edge : path) {
string.append(edge.toString()).append('\n');
totalTime += edge.getWeight();
}
string.append("Total ").append(totalTime);
Alert alert = new Alert(Alert.AlertType.INFORMATION);
TextArea textArea = new TextArea();
textArea.setEditable(false);
textArea.setText(string.toString());
alert.setHeaderText("The path from " + selectedNode1.getName()
+ " to " + selectedNode2.getName() + ":");
alert.getDialogPane().setContent(textArea);
alert.showAndWait();
} else {
errorAlert("No path exists between" + selectedNode1.getName()
+ " and " + selectedNode2.getName());
}
} else {
errorAlert("Two places must be selected!");
}
}
}
}
private class ExitHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0 && unsavedChanges) {
Alert alert = unsavedChangesAlert();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.OK)) {
Platform.exit();
}
} else {
Platform.exit();
}
}
}
private class NewMapHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() == 0) {
newMap();
} else if (unsavedChanges) {
Alert alert = unsavedChangesAlert();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.OK)) {
newMap();
}
} else {
newMap();
}
}
private void newMap() {
resetGraph();
if (image == null) {
image = new Image(IMAGE_FILE_NAME);
}
addImageToBP();
}
}
private void addImageToBP() {
imageView.setImage(image);
pane.getChildren().clear();
pane.getChildren().add(imageView);
primaryStage.setWidth(image.getWidth() + SCENE_WIDTH_OFFSET);
primaryStage.setHeight(image.getHeight() + SCENE_HEIGHT_OFFSET);
bp.setBottom(pane);
}
private class SaveImageHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
WritableImage writeableImage = bp.getBottom().snapshot(
new SnapshotParameters(), null);
File capture = new File("capture.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(writeableImage, null), "png", capture);
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR, "IO-error " + e.getMessage());
alert.showAndWait();
}
}
}
}
private class NewPlaceHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
if (pane.getChildren().size() != 0) {
scene.setCursor(Cursor.CROSSHAIR);
newPlaceBt.setDisable(true);
bp.getBottom().setOnMouseClicked(new MouseClickHandler());
}
}
private class MouseClickHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent mouseEvent) {
double x = mouseEvent.getX();
double y = mouseEvent.getY();
TextInputDialog dialog = new TextInputDialog();
dialog.setContentText("Name of place:");
dialog.setTitle("Name");
dialog.setHeaderText(null);
Optional<String> result = dialog.showAndWait();
if (result.isPresent() && !result.get().isBlank()) {
String name = result.get();
insertNode(name, x, y);
unsavedChanges = true;
} else {
errorAlert("Name-field can't be empty!");
}
newPlaceBt.setDisable(false);
bp.getBottom().setOnMouseClicked(null);
scene.setCursor(Cursor.DEFAULT);
}
}
}
private class OpenHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
try {
if (pane.getChildren().size() != 0 && unsavedChanges) {
Alert alert = unsavedChangesAlert();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get().equals(ButtonType.OK)) {
openGraph();
}
} else {
openGraph();
}
} catch (FileNotFoundException e) {
Alert alert = new Alert(Alert.AlertType.WARNING, "No such file!");
alert.setTitle("File not found");
alert.setHeaderText(null);
alert.showAndWait();
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR, "IO-error " + e.getMessage());
alert.showAndWait();
}
}
private void openGraph() throws IOException {
resetGraph();
FileReader infile = new FileReader("europa.graph");
BufferedReader in = new BufferedReader(infile);
String line;
line = in.readLine();
image = new Image(line);
addImageToBP();
// GraphNodes
line = in.readLine();
String[] split = line.split(";");
for (int i = 0; i < split.length; i += 3) {
String name = split[i];
double x = Double.parseDouble(split[i + 1]);
double y = Double.parseDouble(split[i + 2]);
insertNode(name, x, y);
}
// Connections
for (line = in.readLine(); line != null; line = in.readLine()) {
String[] split2 = line.split(";");
String from = split2[0];
String to = split2[1];
String by = split2[2];
int weight = Integer.parseInt(split2[3]);
insertConnection(from, to, by, weight);
}
}
}
private class NodeClickHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent mouseEvent) {
GraphNode n = (GraphNode) mouseEvent.getSource();
if (selectedNode1 == null) {
if (selectedNode2 != null) {
selectedNode1 = selectedNode2;
selectedNode2 = n;
} else {
selectedNode1 = n;
n.toggleMarked();
}
} else if (selectedNode2 == null && n != selectedNode1) {
selectedNode2 = n;
n.toggleMarked();
} else if (selectedNode1 == n) {
if (selectedNode2 != null) {
selectedNode1 = selectedNode2;
n.toggleMarked();
selectedNode2 = null;
} else {
selectedNode1 = null;
n.toggleMarked();
}
} else if (selectedNode2 == n) {
selectedNode2 = null;
n.toggleMarked();
}
}
}
}