SaveStorage
This commit is contained in:
parent
ee13b9ac0c
commit
1e46cc1b61
@ -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)
|
||||
{
|
||||
|
@ -14,10 +14,10 @@ public class DrawingPolymorphBus<T extends EntityBus, U extends IDrawingDoors> {
|
||||
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;
|
||||
|
@ -166,5 +166,12 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
|
||||
_setBuses.Insert((T)(DrawingObjectBus.Create(items)));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void LoadData(String data)
|
||||
{
|
||||
_setBuses.Insert((T)(DrawingObjectBus.Create(data)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MapsCollection {
|
||||
HashMap<String, MapWithSetBusesGeneric<IDrawingObject, AbstractMap>> _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<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
|
||||
{
|
||||
File file = new File(filename);
|
||||
@ -96,27 +118,45 @@ 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;
|
||||
}
|
||||
if (!str.contains("MapsCollectionStorage")) {
|
||||
_mapStorages.clear();
|
||||
while ((str = reader.readLine()) != null)
|
||||
{
|
||||
while ((str = reader.readLine()) != null) {
|
||||
String[] elem = str.split(String.format("\\%c", separatorDict));
|
||||
AbstractMap map = switch (elem[1])
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -26,12 +26,13 @@
|
||||
<AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7">
|
||||
<children>
|
||||
<Canvas fx:id="canvasBus" height="745.0" width="573.0" />
|
||||
<TitledPane expanded="false" text="Сохранение">
|
||||
<TitledPane text="Сохранение">
|
||||
<content>
|
||||
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
|
||||
<children>
|
||||
<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="ButtonLoad1" layoutX="-1.0" layoutY="100.0" mnemonicParsing="false" onAction="#ButtonSaveStorage_Click" prefHeight="50.0" prefWidth="202.0" text="Сохранить хранилище" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</content>
|
||||
|
Loading…
Reference in New Issue
Block a user