From f329788ce7654d217e067b018508f1d61bacd24e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 21:53:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=206=20=D1=85=D0=B0?= =?UTF-8?q?=D1=80=D0=B4=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=D1=82=D1=8C=20=D0=B1=D0=B0=D0=B3=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetLocomotives.java | 40 ++++++++++++++++++++- MapsCollection.java | 64 +++++++++++++++++++++++++++++++++- SetLocomotivesGeneric.java | 4 +++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index d30af27..18610d1 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -263,8 +263,9 @@ public class FormMapWithSetLocomotives extends JComponent { dialog.setVisible(true); } }); - + // Сохранения и загрузки JFileChooser fileChooser = new JFileChooser(); + // Сохранение всех хранилищ JButton saveButton = new JButton("Save"); saveButton.addActionListener(e -> { fileChooser.setDialogTitle("Saving"); @@ -278,6 +279,7 @@ public class FormMapWithSetLocomotives extends JComponent { }); statusPanel.add(saveButton); + // Загрузка всех хранилищ JButton loadButton = new JButton("Load"); loadButton.addActionListener(e -> { fileChooser.setDialogTitle("Loading"); @@ -293,6 +295,42 @@ public class FormMapWithSetLocomotives extends JComponent { }); statusPanel.add(loadButton); + // Сохранение выбранной карты + JButton saveMapButton = new JButton("Save Map"); + saveMapButton.addActionListener(e -> { + fileChooser.setDialogTitle("Saving Map"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(listBoxMaps.getSelectedValue().toString() == null) return; + if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Map saving success"); + } + else JOptionPane.showMessageDialog(null, "Map saving fail"); + repaint(); + } + }); + statusPanel.add(saveMapButton); + + // Загрузка одной карты + JButton loadMapButton = new JButton("Load Map"); + loadMapButton.addActionListener(e -> { + fileChooser.setDialogTitle("Loading"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Load Map success"); + } + else JOptionPane.showMessageDialog(null, "Load Map failed"); + } + ReloadMaps(); + repaint(); + }); + statusPanel.add(loadMapButton); + formFrame.getContentPane().add(this); formFrame.setVisible(true); } diff --git a/MapsCollection.java b/MapsCollection.java index e236bb8..1459e92 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -1,5 +1,6 @@ import java.io.*; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; public class MapsCollection { @@ -69,6 +70,67 @@ public class MapsCollection { return true; } + // Сохранение одной выбранной карты + public boolean SaveMap(String map_name, String filename) { + File file = new File(filename); + MapWithSetLocomotivesGeneric map = Get(map_name); + if (file.exists()) + { + file.delete(); + } + try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { + bw.write("SingleMap\n"); + bw.write("" + map_name + separatorDict +map.GetData(separatorDict.charAt(0), separatorData) + "\n"); + } + catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + + // Загрузка одной карты + public boolean LoadMap(String filename){ + File file = new File(filename); + if (!file.exists()) + { + return false; + } + try { + BufferedReader br = new BufferedReader(new FileReader(filename)); + String curLine = br.readLine(); + if (!curLine.contains("SingleMap")) { + return false; + } + + curLine = br.readLine(); + var elems = curLine.split(separatorDict); + if (_mapStorages.containsKey(elems[0])) { + _mapStorages.get(elems[0])._setLocomotives.Clear(); + _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + return true; + } + AbstractMap map = null; + switch (elems[1]) + { + case "class SimpleMap": + map = new SimpleMap(); + break; + case "class SpikeMap": + map = new SpikeMap(); + break; + case "class RailMap": + map = new RailMap(); + break; + } + _mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + + } catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + /// Загрузка информации по локомотивам в депо из файла public boolean LoadData(String filename) { @@ -90,7 +152,7 @@ public class MapsCollection { AbstractMap map = null; switch (elems[1]) - { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + { case "class SimpleMap": map = new SimpleMap(); break; diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index 72f1f94..9aff477 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -34,6 +34,10 @@ public class SetLocomotivesGeneric return result; } + public void Clear() { + _places.clear(); + } + public T Get(int position) { if (position >= _maxCount || position < 0)