SaveStorage

This commit is contained in:
shadowik 2022-12-11 02:16:37 +04:00
parent ee13b9ac0c
commit 1e46cc1b61
5 changed files with 105 additions and 25 deletions

View File

@ -114,16 +114,16 @@ public class ControllerMapWithSetBus {
{ {
return; return;
} }
Stage stageArmoredVehicleConfig = new Stage(); Stage busStage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBusConfig.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBusConfig.fxml"));
Scene sceneArmoredVehicle = new Scene(fxmlLoader.load()); Scene sceneBus = new Scene(fxmlLoader.load());
stageArmoredVehicleConfig.setScene(sceneArmoredVehicle); busStage.setScene(sceneBus);
stageArmoredVehicleConfig.show(); busStage.show();
ControllerBusConfig controllerArmoredVehicleConfig = fxmlLoader.getController(); ControllerBusConfig controllerBusConfig = fxmlLoader.getController();
controllerArmoredVehicleConfig.AddEvent(this::AddBus); controllerBusConfig.AddEvent(this::AddBus);
controllerArmoredVehicleConfig.SetStage(stageArmoredVehicleConfig); controllerBusConfig.SetStage(busStage);
FirstIncome(); FirstIncome();
} }
@ -146,6 +146,7 @@ public class ControllerMapWithSetBus {
{ {
alert = new Alert(Alert.AlertType.ERROR, "Не удалось добавить объект", ButtonType.OK); alert = new Alert(Alert.AlertType.ERROR, "Не удалось добавить объект", ButtonType.OK);
} }
showStorage();
alert.showAndWait(); alert.showAndWait();
} }
@ -343,6 +344,37 @@ public class ControllerMapWithSetBus {
infoAlert.showAndWait(); 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() { private void showStorage() {
if (selected == null) if (selected == null)
{ {

View File

@ -14,10 +14,10 @@ public class DrawingPolymorphBus<T extends EntityBus, U extends IDrawingDoors> {
doors = new Object[doorsCount]; doors = new Object[doorsCount];
} }
public int AddEntity(T boat) public int AddEntity(T bus)
{ {
if(busesCount < buses.length){ if(busesCount < buses.length){
buses[busesCount] = boat; buses[busesCount] = bus;
return busesCount++; return busesCount++;
} }
return -1; return -1;

View File

@ -166,5 +166,12 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
_setBuses.Insert((T)(DrawingObjectBus.Create(items))); _setBuses.Insert((T)(DrawingObjectBus.Create(items)));
} }
} }
@SuppressWarnings("unchecked")
public void LoadData(String data)
{
_setBuses.Insert((T)(DrawingObjectBus.Create(data)));
}
} }

View File

@ -9,6 +9,7 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects;
public class MapsCollection { public class MapsCollection {
HashMap<String, MapWithSetBusesGeneric<IDrawingObject, AbstractMap>> _mapStorages; HashMap<String, MapWithSetBusesGeneric<IDrawingObject, AbstractMap>> _mapStorages;
@ -85,6 +86,27 @@ public class MapsCollection {
return true; 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<IDrawingObject, AbstractMap> 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 public boolean LoadData(String filename) throws IOException
{ {
File file = new File(filename); File file = new File(filename);
@ -96,26 +118,44 @@ public class MapsCollection {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) try (BufferedReader reader = new BufferedReader(new FileReader(filename)))
{ {
String str = reader.readLine(); String str = reader.readLine();
if (str == null || !str.contains("MapsCollection")) if (str == null || (!str.contains("MapsCollection") && !str.contains("MapsCollectionStorage")))
{ {
return false; return false;
} }
_mapStorages.clear(); if (!str.contains("MapsCollectionStorage")) {
while ((str = reader.readLine()) != null) _mapStorages.clear();
{ while ((str = reader.readLine()) != null) {
String[] elem = str.split(String.format("\\%c", separatorDict)); String[] elem = str.split(String.format("\\%c", separatorDict));
AbstractMap map = switch (elem[1]) AbstractMap map = switch (elem[1]) {
{ case "SimpleMap" -> new SimpleMap();
case "SimpleMap" -> new SimpleMap(); case "WaterMap" -> new WaterMap();
case "WaterMap" -> new WaterMap(); default -> null;
default -> null; };
}; _mapStorages.put(elem[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map));
_mapStorages.put(elem[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map)); if (elem.length == 3) {
if (elem.length == 3) _mapStorages.get(elem[0]).LoadData(elem[2].split(String.format("%c", separatorData)));
{ }
_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; return true;
} }

View File

@ -26,12 +26,13 @@
<AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7"> <AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7">
<children> <children>
<Canvas fx:id="canvasBus" height="745.0" width="573.0" /> <Canvas fx:id="canvasBus" height="745.0" width="573.0" />
<TitledPane expanded="false" text="Сохранение"> <TitledPane text="Сохранение">
<content> <content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children> <children>
<Button fx:id="ButtonSave" layoutX="-1.0" mnemonicParsing="false" onAction="#ButtonSave_Click" prefHeight="50.0" prefWidth="202.0" text="Сохранить" /> <Button fx:id="ButtonSave" layoutX="-1.0" mnemonicParsing="false" onAction="#ButtonSave_Click" prefHeight="50.0" prefWidth="202.0" text="Сохранить" />
<Button fx:id="ButtonLoad" layoutX="-1.0" layoutY="50.0" mnemonicParsing="false" onAction="#ButtonLoad_Click" prefHeight="50.0" prefWidth="202.0" text="Загрузить" /> <Button fx:id="ButtonLoad" layoutX="-1.0" layoutY="50.0" mnemonicParsing="false" onAction="#ButtonLoad_Click" prefHeight="50.0" prefWidth="202.0" text="Загрузить" />
<Button fx:id="ButtonLoad1" layoutX="-1.0" layoutY="100.0" mnemonicParsing="false" onAction="#ButtonSaveStorage_Click" prefHeight="50.0" prefWidth="202.0" text="Сохранить хранилище" />
</children> </children>
</AnchorPane> </AnchorPane>
</content> </content>