From 1e46cc1b6123950c3639fdd3e12612d618bf8e3d Mon Sep 17 00:00:00 2001 From: shadowik Date: Sun, 11 Dec 2022 02:16:37 +0400 Subject: [PATCH] SaveStorage --- .../ControllerMapWithSetBus.java | 46 ++++++++++-- .../doubledeckerbus/DrawingPolymorphBus.java | 4 +- .../MapWithSetBusesGeneric.java | 7 ++ .../doubledeckerbus/MapsCollection.java | 70 +++++++++++++++---- .../doubledeckerbus/FormMapWithSetBus.fxml | 3 +- 5 files changed, 105 insertions(+), 25 deletions(-) diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index dc860b7..5e07172 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -114,16 +114,16 @@ public class ControllerMapWithSetBus { { return; } - Stage stageArmoredVehicleConfig = new Stage(); + Stage busStage = new Stage(); FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBusConfig.fxml")); - Scene sceneArmoredVehicle = new Scene(fxmlLoader.load()); + Scene sceneBus = new Scene(fxmlLoader.load()); - stageArmoredVehicleConfig.setScene(sceneArmoredVehicle); - stageArmoredVehicleConfig.show(); + busStage.setScene(sceneBus); + busStage.show(); - ControllerBusConfig controllerArmoredVehicleConfig = fxmlLoader.getController(); - controllerArmoredVehicleConfig.AddEvent(this::AddBus); - controllerArmoredVehicleConfig.SetStage(stageArmoredVehicleConfig); + ControllerBusConfig controllerBusConfig = fxmlLoader.getController(); + controllerBusConfig.AddEvent(this::AddBus); + controllerBusConfig.SetStage(busStage); FirstIncome(); } @@ -146,6 +146,7 @@ public class ControllerMapWithSetBus { { alert = new Alert(Alert.AlertType.ERROR, "Не удалось добавить объект", ButtonType.OK); } + showStorage(); alert.showAndWait(); } @@ -343,6 +344,37 @@ public class ControllerMapWithSetBus { infoAlert.showAndWait(); } + @FXML + private void ButtonSaveStorage_Click(ActionEvent event) throws IOException { + Alert infoAlert; + Stage stage = (Stage)(canvasBus.getScene().getWindow()); + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Save"); + + FileChooser.ExtensionFilter fileExtensions = new FileChooser.ExtensionFilter("TEXT files (*.txt)", + "*.txt"); + fileChooser.getExtensionFilters().add(fileExtensions); + + File selectedDirectory = fileChooser.showSaveDialog(stage); + if (selectedDirectory != null) + { + String filepath = selectedDirectory.getPath(); + if (_mapsCollection.SaveStorage(filepath, listViewMaps.getSelectionModel().getSelectedItem())) + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Save was successful", ButtonType.OK); + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not saved", ButtonType.OK); + } + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not saved", ButtonType.OK); + } + infoAlert.showAndWait(); + } + private void showStorage() { if (selected == null) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java index 9dc1725..70d67d7 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java @@ -14,10 +14,10 @@ public class DrawingPolymorphBus { doors = new Object[doorsCount]; } - public int AddEntity(T boat) + public int AddEntity(T bus) { if(busesCount < buses.length){ - buses[busesCount] = boat; + buses[busesCount] = bus; return busesCount++; } return -1; diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index cd33e38..fc95811 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -166,5 +166,12 @@ public class MapWithSetBusesGeneric> _mapStorages; @@ -85,6 +86,27 @@ public class MapsCollection { return true; } + public boolean SaveStorage(String filename, String key) throws IOException + { + Files.deleteIfExists(Paths.get(filename)); + + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), + StandardCharsets.UTF_8))) + { + writer.write("MapsCollectionStorage" + System.lineSeparator()); + MapWithSetBusesGeneric storage = _mapStorages.get(key); + String[] data = storage.GetData(separatorDict, separatorData).split("\\|"); + + writer.write(key + ":" + data[0] + System.lineSeparator()); + for (var bus : data[1].split(";")) { + writer.write(bus); + writer.write(System.lineSeparator()); + } + + } + return true; + } + public boolean LoadData(String filename) throws IOException { File file = new File(filename); @@ -96,26 +118,44 @@ public class MapsCollection { try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String str = reader.readLine(); - if (str == null || !str.contains("MapsCollection")) + if (str == null || (!str.contains("MapsCollection") && !str.contains("MapsCollectionStorage"))) { return false; } - _mapStorages.clear(); - while ((str = reader.readLine()) != null) - { - String[] elem = str.split(String.format("\\%c", separatorDict)); - AbstractMap map = switch (elem[1]) - { - case "SimpleMap" -> new SimpleMap(); - case "WaterMap" -> new WaterMap(); - default -> null; - }; - _mapStorages.put(elem[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map)); - if (elem.length == 3) - { - _mapStorages.get(elem[0]).LoadData(elem[2].split(String.format("%c", separatorData))); + if (!str.contains("MapsCollectionStorage")) { + _mapStorages.clear(); + while ((str = reader.readLine()) != null) { + String[] elem = str.split(String.format("\\%c", separatorDict)); + AbstractMap map = switch (elem[1]) { + case "SimpleMap" -> new SimpleMap(); + case "WaterMap" -> new WaterMap(); + default -> null; + }; + _mapStorages.put(elem[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map)); + if (elem.length == 3) { + _mapStorages.get(elem[0]).LoadData(elem[2].split(String.format("%c", separatorData))); + } } } + else { + String[] data = reader.readLine().split(":"); + if (_mapStorages.containsKey(data[0])) { + _mapStorages.remove(data[0]); + } + AbstractMap map = switch (data[1]) { + case "SimpleMap" -> new SimpleMap(); + case "WaterMap" -> new WaterMap(); + default -> null; + }; + _mapStorages.put(data[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map)); + String k = reader.readLine(); + while (k != null) { + _mapStorages.get(data[0]).LoadData(k); + k = reader.readLine(); + } + + + } } return true; } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml index 6888adb..1bf0fe7 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml @@ -26,12 +26,13 @@ - +