From 47e81cce43495a2cad67296414d47bc77bcdf538 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Tue, 6 Dec 2022 15:01:05 +0400 Subject: [PATCH] =?UTF-8?q?LabWork06:=20=D0=9B=D0=B0=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningCrossRollers.java | 4 + DrawningObjectExcavator.java | 12 +++ DrawningRollers.java | 4 + DrawningSquaredRollers.java | 4 + FormMapWithSetTracktor.java | 86 +++++++++++++++++++++- IDrawningObject.java | 1 + IDrawningRollers.java | 1 + MapWithSetTracktorGeneric.java | 21 ++++++ MapsCollection.java | 131 ++++++++++++++++++++++++++++++++- RollersCount.java | 15 +++- SetTracktorGeneric.java | 4 + TracktorSerde.java | 69 +++++++++++++++++ 12 files changed, 348 insertions(+), 4 deletions(-) create mode 100644 TracktorSerde.java diff --git a/DrawningCrossRollers.java b/DrawningCrossRollers.java index 8e2bbef..0d781ee 100644 --- a/DrawningCrossRollers.java +++ b/DrawningCrossRollers.java @@ -18,6 +18,10 @@ public class DrawningCrossRollers implements IDrawningRollers{ this.colorRollers = colorRollers; } + public int getRollersCount() { + return rollersCount.getValue(); + } + public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){ Color penColor = Color.BLACK; Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers; diff --git a/DrawningObjectExcavator.java b/DrawningObjectExcavator.java index 46ebbde..365575a 100644 --- a/DrawningObjectExcavator.java +++ b/DrawningObjectExcavator.java @@ -39,4 +39,16 @@ public class DrawningObjectExcavator implements IDrawningObject{ public void drawningObject(Graphics2D g) { _tracktor.DrawTransport(g); } + + public String getInfo() { + if (_tracktor == null) { + return null; + } + + return TracktorSerde.serialize(_tracktor); + } + + public static IDrawningObject create(String data) { + return new DrawningObjectExcavator(TracktorSerde.deserialize(data)); + } } diff --git a/DrawningRollers.java b/DrawningRollers.java index 100f1b7..efc2a8a 100644 --- a/DrawningRollers.java +++ b/DrawningRollers.java @@ -18,6 +18,10 @@ public class DrawningRollers implements IDrawningRollers { this.colorRollers = colorRollers; } + public int getRollersCount() { + return rollersCount.getValue(); + } + public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){ Color penColor = Color.BLACK; Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers; diff --git a/DrawningSquaredRollers.java b/DrawningSquaredRollers.java index dcae709..7d74340 100644 --- a/DrawningSquaredRollers.java +++ b/DrawningSquaredRollers.java @@ -18,6 +18,10 @@ public class DrawningSquaredRollers implements IDrawningRollers { this.colorRollers = colorRollers; } + public int getRollersCount() { + return rollersCount.getValue(); + } + public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){ Color penColor = Color.BLACK; Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers; diff --git a/FormMapWithSetTracktor.java b/FormMapWithSetTracktor.java index 334733b..c9644d4 100644 --- a/FormMapWithSetTracktor.java +++ b/FormMapWithSetTracktor.java @@ -1,13 +1,16 @@ import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; import java.awt.*; +import java.io.IOException; import java.text.ParseException; import java.util.HashMap; import java.util.LinkedList; import java.util.Optional; public class FormMapWithSetTracktor extends JFrame { + private JMenuBar menuBar; private JPanel ContentPanel; private JPanel pictureBox; private JPanel toolsGroup; @@ -51,6 +54,88 @@ public class FormMapWithSetTracktor extends JFrame { } _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight()); + + menuBar = new JMenuBar(); + + JMenu fileMenu = new JMenu("Файл"); + menuBar.add(fileMenu); + + JMenuItem saveMenuItem = new JMenuItem("Сохранить"); + saveMenuItem.addActionListener(e -> { + JFileChooser dialog = new JFileChooser(); + dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt")); + dialog.showSaveDialog(this); + + try { + if (_mapsCollection.saveData(dialog.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + fileMenu.add(saveMenuItem); + + JMenuItem loadMenuItem = new JMenuItem("Загрузить"); + loadMenuItem.addActionListener(e -> { + JFileChooser dialog = new JFileChooser(); + dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt")); + dialog.showOpenDialog(this); + + try { + if (_mapsCollection.loadData(dialog.getSelectedFile().getAbsolutePath())) { + reloadMaps(); + JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + fileMenu.add(loadMenuItem); + + JMenuItem saveMapMenuItem = new JMenuItem("Сохранить карту"); + saveMapMenuItem.addActionListener(e -> { + JFileChooser dialog = new JFileChooser(); + dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt")); + dialog.showSaveDialog(this); + + try { + if (_mapsCollection.saveMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""), dialog.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + fileMenu.add(saveMapMenuItem); + + JMenuItem loadMapMenuItem = new JMenuItem("Загрузить карту"); + loadMapMenuItem.addActionListener(e -> { + JFileChooser dialog = new JFileChooser(); + dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt")); + dialog.showOpenDialog(this); + + try { + if (_mapsCollection.loadMap(dialog.getSelectedFile().getAbsolutePath())) { + reloadMaps(); + JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + }); + fileMenu.add(loadMapMenuItem); + + setJMenuBar(menuBar); + comboBoxMapSelector.removeAllItems(); for (var key : _mapsDict.keySet()) { comboBoxMapSelector.addItem(key); @@ -87,7 +172,6 @@ public class FormMapWithSetTracktor extends JFrame { } }); - buttonAddTracktor.addActionListener(e -> { FormTracktorConfig form = new FormTracktorConfig(); form.addListener(tracktor -> { diff --git a/IDrawningObject.java b/IDrawningObject.java index d1648a1..8d5d4c3 100644 --- a/IDrawningObject.java +++ b/IDrawningObject.java @@ -6,4 +6,5 @@ public interface IDrawningObject { void moveObject(Direction direction); void drawningObject(Graphics2D g); float[] getCurrentPosition(); + String getInfo(); } diff --git a/IDrawningRollers.java b/IDrawningRollers.java index 511c22c..00ea1c8 100644 --- a/IDrawningRollers.java +++ b/IDrawningRollers.java @@ -4,4 +4,5 @@ public interface IDrawningRollers { void setRollersCount(int count); void setColor(Color color); void DrawRollers(Graphics2D g, float _startPosX, float _startPosY); + int getRollersCount(); } diff --git a/MapWithSetTracktorGeneric.java b/MapWithSetTracktorGeneric.java index ce03b01..a9231fe 100644 --- a/MapWithSetTracktorGeneric.java +++ b/MapWithSetTracktorGeneric.java @@ -19,6 +19,10 @@ public class MapWithSetTracktorGeneric = 0; i--) { + _setTracktor.insert((T) DrawningObjectExcavator.create(records[i])); + } + } } diff --git a/MapsCollection.java b/MapsCollection.java index 77796b0..553c9ce 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -1,12 +1,16 @@ +import java.io.*; import java.util.HashMap; import java.util.Set; public class MapsCollection { - private final HashMap> _mapsStorage; + private final HashMap> _mapsStorage; private final int _pictureWidth; private final int _pictureHeight; + private final char separatorDict = '|'; + private final char separatorData = ';'; + public Set getKeys() { return _mapsStorage.keySet(); } @@ -27,7 +31,130 @@ public class MapsCollection { _mapsStorage.remove(name); } - public MapWithSetTracktorGeneric getMap(String name) { + public MapWithSetTracktorGeneric getMap(String name) { return _mapsStorage.getOrDefault(name, null); } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public boolean saveData(String filename) throws IOException { + File file = new File(filename); + + if (file.exists()) { + file.delete(); + } + + file.createNewFile(); + + try (PrintWriter writer = new PrintWriter(file)) { + writer.println("MapsCollection"); + + for (var storage : _mapsStorage.entrySet()) { + writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData))); + } + } + + return true; + } + + public boolean loadData(String filename) throws IOException { + File file = new File(filename); + + if (!file.exists()) { + return false; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String currentLine = reader.readLine(); + + if (currentLine == null || !currentLine.contains("MapsCollection")) { + return false; + } + + _mapsStorage.clear(); + while ((currentLine = reader.readLine()) != null) { + var elements = currentLine.split(String.format("\\%c", separatorDict)); + + AbstractMap map = switch (elements[1]) { + case "SimpleMap" -> new SimpleMap(); + case "DumpMap" -> new DumpMap(); + default -> null; + }; + + _mapsStorage.put(elements[0], new MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, map)); + _mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?")); + } + } + + return true; + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public boolean saveMap(String mapName, String filename) throws IOException { + File file = new File(filename); + + if (file.exists()) { + file.delete(); + } + + file.createNewFile(); + + MapWithSetTracktorGeneric map = _mapsStorage.getOrDefault(mapName, null); + + if (map == null) { + return false; + } + + try (PrintWriter writer = new PrintWriter(file)) { + writer.println("Map"); + writer.println(mapName); + writer.println(map.getMap().getClass().getSimpleName()); + + for (var tracktor : map._setTracktor.getTracktors()) { + writer.println(tracktor.getInfo()); + } + } + + return true; + } + + public boolean loadMap(String filename) throws IOException { + File file = new File(filename); + + if (!file.exists()) { + return false; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String currentLine = reader.readLine(); + + if (currentLine == null || !currentLine.contains("Map")) { + return false; + } + + String mapName = reader.readLine(); + + MapWithSetTracktorGeneric map; + if (_mapsStorage.containsKey(mapName)) { + map = _mapsStorage.get(mapName); + if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) { + return false; + } + map._setTracktor.clear(); + } else { + map = switch (reader.readLine()) { + case "SimpleMap" -> new MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap()); + case "DumpMap" -> new MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, new DumpMap()); + default -> null; + }; + } + + while ((currentLine = reader.readLine()) != null) { + map._setTracktor.insert(DrawningObjectExcavator.create(currentLine)); + } + + _mapsStorage.put(mapName, map); + } + + return true; + } } diff --git a/RollersCount.java b/RollersCount.java index 7b9cdbe..38dbc92 100644 --- a/RollersCount.java +++ b/RollersCount.java @@ -1,5 +1,18 @@ public enum RollersCount { Four, Five, - Six + Six; + + public int getValue() { + return switch (this) { + case Four -> 4; + case Five -> 5; + case Six -> 6; + }; + } + + @Override + public String toString() { + return Integer.toString(getValue()); + } } diff --git a/SetTracktorGeneric.java b/SetTracktorGeneric.java index c6606cc..f4ff735 100644 --- a/SetTracktorGeneric.java +++ b/SetTracktorGeneric.java @@ -49,4 +49,8 @@ public class SetTracktorGeneric { { return _places; } + + public void clear() { + _places.clear(); + } } diff --git a/TracktorSerde.java b/TracktorSerde.java new file mode 100644 index 0000000..c61da8a --- /dev/null +++ b/TracktorSerde.java @@ -0,0 +1,69 @@ +import java.awt.*; + +public class TracktorSerde { // Tracktor Serialization/Deserialization + private static final char _separatorForObject = ':'; + + public static DrawningTracktor deserialize(String info) { + String[] strings = info.split(Character.toString(_separatorForObject)); + + int speed = Integer.parseInt(strings[0]); + float weight = Float.parseFloat(strings[1]); + Color bodyColor = new Color(Integer.parseInt(strings[2])); + IDrawningRollers rollers = switch (strings[3]) { + case "DrawningRollers" -> new DrawningRollers(Integer.parseInt(strings[4]), bodyColor); + case "DrawningCrossRollers" -> new DrawningCrossRollers(Integer.parseInt(strings[4]), bodyColor); + case "DrawningSquaredRollers" -> new DrawningSquaredRollers(Integer.parseInt(strings[4]), bodyColor); + default -> null; + }; + + if (strings.length == 5) { + EntityTracktor entity = new EntityTracktor(speed, weight, bodyColor); + + return new DrawningTracktor(entity, rollers); + } + + if (strings.length == 8) { + Color dopColor = new Color(Integer.parseInt(strings[5])); + boolean bucket = Boolean.parseBoolean(strings[6]); + boolean supports = Boolean.parseBoolean(strings[7]); + + EntityTrackedVehicle entity = new EntityTrackedVehicle(speed, weight, bodyColor, dopColor, bucket, supports); + + return new DrawningTrackedVehicle(entity, rollers); + } + + return null; + } + + public static String serialize(DrawningTracktor drawingTracktor) { + EntityTracktor tracktor = drawingTracktor.getTracktor(); + + String result = String.format( + "%d%c%s%c%d%c%s%c%d", + tracktor.getSpeed(), + _separatorForObject, + tracktor.getWeight(), + _separatorForObject, + tracktor.getBodyColor().getRGB(), + _separatorForObject, + drawingTracktor.getRollers().getClass().getSimpleName(), + _separatorForObject, + drawingTracktor.getRollers().getRollersCount() + ); + + if (!(tracktor instanceof EntityTrackedVehicle trackedVehicle)) { + return result; + } + + return String.format( + "%s%c%d%c%b%c%b", + result, + _separatorForObject, + trackedVehicle.getDopColor().getRGB(), + _separatorForObject, + trackedVehicle.getBucket(), + _separatorForObject, + trackedVehicle.getSupports() + ); + } +}