diff --git a/src/Generics/PlaneGenericStorage.java b/src/Generics/PlaneGenericStorage.java index a7f6f7e..eee48e6 100644 --- a/src/Generics/PlaneGenericStorage.java +++ b/src/Generics/PlaneGenericStorage.java @@ -1,16 +1,127 @@ package Generics; import drawing_objects.DrawingPlane; +import drawing_objects.ExtentionDrawingPlane; import movement_strategy.DrawingObjectPlane; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; public class PlaneGenericStorage { - final HashMap> planeStorages; + public HashMap> planeStorages; private final int _pictureWidth; private final int _pictureHeight; + private static final char _separatorForKeyValue = '|'; + private final char _separatorRecords = ';'; + private static final char _separatorForObject = ':'; + + public void SaveData(File f) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("PlaneStorage\n"); + for(Map.Entry> record : planeStorages.entrySet()){ + StringBuilder records = new StringBuilder(); + for(DrawingPlane elem : record.getValue().GetPlanes()) + { + records.append(String.format("%s%c", ExtentionDrawingPlane.GetDataForSave(elem, _separatorForObject), + _separatorRecords)); + } + data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString())); + } + if(data.length() == 0) + return; + FileWriter writer = new FileWriter(f); + writer.write(data.toString()); + writer.flush(); + writer.close(); + } + public boolean LoadData(File f) throws FileNotFoundException { + if(!f.exists()) + return false; + System.out.println("Gay"); + StringBuilder bufferTextFromFile =new StringBuilder(); + Scanner s = new Scanner(f); + while(s.hasNext()) + bufferTextFromFile.append(s.next() + "\n"); + s.close(); + var strs = bufferTextFromFile.toString().split("\n"); + if(strs == null || strs.length == 0) + return false; + if (!strs[0].startsWith("PlaneStorage")) + return false; + planeStorages.clear(); + for(String data : strs){ + String st = new String("\\" + Character.toString( _separatorForKeyValue)); + String[]record = data.split(st); + if (record.length != 2) + continue; + PlanesGenericCollection collection = + new PlanesGenericCollection<>(_pictureWidth, _pictureHeight); + String[] set = record[1].split(Character.toString(_separatorRecords)); + + for(int i = set.length -1; i >=0; i--){ + String elem = set[i]; + DrawingPlane plane = ExtentionDrawingPlane.CreateDrawingPlane(elem, + _separatorForObject, _pictureWidth, _pictureHeight); + if (plane != null) + { + if (!(collection.Insert(plane))) + { + return false; + } + } + } + planeStorages.put(record[0], collection); + } + return true; + } + + public boolean LoadCollection(File f) throws FileNotFoundException { + if(!f.exists()) + return false; + StringBuilder bufferTextFromFile =new StringBuilder(); + Scanner s = new Scanner(f); + while(s.hasNext()) + bufferTextFromFile.append(s.next() + "\n"); + s.close(); + var strs = bufferTextFromFile.toString().split("\n"); + if(strs == null || strs.length == 0) + return false; + if (!strs[0].startsWith("PlaneCollection")) + return false; + String collectionName = strs[1]; + PlanesGenericCollection collection = getCollection(collectionName); + if(collection == null) + collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight); + else + collection.Clear(); + String[] planesInfo = strs[2].split(Character.toString(PlanesGenericCollection._separatorRecords)); + for(int i = planesInfo.length-1; i >= 0; i--){ + String data = planesInfo[i]; + DrawingPlane plane = ExtentionDrawingPlane.CreateDrawingPlane(data, + PlanesGenericCollection._separatorForObject, _pictureWidth, _pictureHeight); + if (plane != null) + { + if (!(collection.Insert(plane))) + { + return false; + } + } + } + AddSetFromFile(collectionName, collection); + return true; + } + public PlaneGenericStorage(int pictureWidth, int pictureHeight){ planeStorages = new HashMap<>(); _pictureWidth = pictureWidth; @@ -25,12 +136,26 @@ public class PlaneGenericStorage { planeStorages.put(name, new PlanesGenericCollection<>(_pictureWidth, _pictureHeight)); } - public void DelSet(String name){ + public void AddSetFromFile(String name, PlanesGenericCollection toAdd){ + if(planeStorages.containsKey(name)){ + planeStorages.remove(name); + } + planeStorages.put(name, toAdd); + } + + public void DelSet(String name, PlaneTrashCollection trashBox){ if(!planeStorages.containsKey(name)) return; + PlanesGenericCollection cur = planeStorages.get(name); + for(int i = 0; i < cur.size(); i++) + trashBox.Push(cur.Get(i)); planeStorages.remove(name); } + public PlanesGenericCollection GetCollection(String collectionName){ + return planeStorages.get(collectionName); + } + public PlanesGenericCollection getCollection(String name){ if(!planeStorages.containsKey(name)) return null; diff --git a/src/Generics/PlanesGenericCollection.java b/src/Generics/PlanesGenericCollection.java index de2de07..78b450b 100644 --- a/src/Generics/PlanesGenericCollection.java +++ b/src/Generics/PlanesGenericCollection.java @@ -1,11 +1,18 @@ package Generics; import drawing_objects.DrawingPlane; +import drawing_objects.ExtentionDrawingPlane; import movement_strategy.IMoveableObject; import java.awt.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; public class PlanesGenericCollection { + public static char _separatorRecords = ';'; + public static char _separatorForObject = ':'; /// /// Ширина окна прорисовки /// @@ -13,7 +20,7 @@ public class PlanesGenericCollection /// Высота окна прорисовки /// - private final int _pictureHeight; + private final int pictureHeight; /// /// Размер занимаемого объектом места (ширина) /// @@ -25,7 +32,38 @@ public class PlanesGenericCollection /// Набор объектов /// - private final SetGeneric collection; + private SetGeneric collection; + public ArrayList GetPlanes(){ + return collection.getPlanes(collection.getCount()); + } + + public boolean SaveData(File f, String name) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("PlaneCollection\n"); + data.append(String.format("%s\n", name)); + StringBuilder records = new StringBuilder(); + for(DrawingPlane elem : GetPlanes()) + { + records.append(String.format("%s%c", ExtentionDrawingPlane.GetDataForSave(elem, _separatorForObject), + _separatorRecords)); + } + data.append(records); + if(data.length() == 0) + return false; + FileWriter writer = new FileWriter(f); + writer.write(data.toString()); + writer.flush(); + writer.close(); + return true; + } + + public void Clear(){ + collection = new SetGeneric<>(pictureWidth * pictureHeight); + } /// /// Конструктор /// @@ -36,7 +74,7 @@ public class PlanesGenericCollection(width * height); } @@ -89,13 +127,13 @@ public class PlanesGenericCollection diff --git a/src/drawing_objects/DrawingPlane.java b/src/drawing_objects/DrawingPlane.java index a2485db..823bf39 100644 --- a/src/drawing_objects/DrawingPlane.java +++ b/src/drawing_objects/DrawingPlane.java @@ -13,7 +13,7 @@ public class DrawingPlane { public EntityPlane getEntityPlane() { return entityPlane; } - private IDrawEngines drawingEngines; + public IDrawEngines drawingEngines; public int pictureWidth; public int pictureHeight; protected int startPosX; diff --git a/src/drawing_objects/ExtentionDrawingPlane.java b/src/drawing_objects/ExtentionDrawingPlane.java new file mode 100644 index 0000000..9eab414 --- /dev/null +++ b/src/drawing_objects/ExtentionDrawingPlane.java @@ -0,0 +1,79 @@ +package drawing_objects; + +import entities.EntityAirBomber; + +import java.awt.*; + +public class ExtentionDrawingPlane { + private static String getName(Color col){ + if(col.equals(Color.RED)) + return "RED"; + if(col.equals(Color.GREEN)) + return "GREEN"; + if(col.equals(Color.BLUE)) + return "BLUE"; + if(col.equals(Color.YELLOW)) + return "YELLOW"; + if(col.equals(Color.WHITE)) + return "WHITE"; + if(col.equals(Color.GRAY)) + return "GRAY"; + if(col.equals(Color.BLACK)) + return "BLACK"; + if(col.equals(Color.MAGENTA)) + return "MAGENTA"; + return null; + } + + private static Color getColor(String col){ + if(col.equals("RED")) + return Color.RED; + if(col.equals("GREEN")) + return Color.GREEN; + if(col.equals("BLUE")) + return Color.BLUE; + if(col.equals("YELLOW")) + return Color.YELLOW; + if(col.equals("WHITE")) + return Color.WHITE; + if(col.equals("GRAY")) + return Color.GRAY; + if(col.equals("BLACK")) + return Color.BLACK; + if(col.equals("MAGENTA")) + return Color.MAGENTA; + return null; + } + public static DrawingPlane CreateDrawingPlane(String info, char separatorForObject, + int width, int height){ + String[] strs = info.split(Character.toString(separatorForObject)); + if(strs.length == 5){ + return new DrawingPlane(Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), getColor(strs[2]), width, height, Integer.parseInt(strs[3]), Integer.parseInt(strs[4])); + } + if(strs.length == 8){ + return new DrawingAirBomber(Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), getColor(strs[2]), + getColor(strs[5]), Boolean.parseBoolean(strs[6]), + Boolean.parseBoolean(strs[7]), width, height, + Integer.parseInt(strs[3]), Integer.parseInt(strs[4])); + } + return null; + } + public static String GetDataForSave(DrawingPlane drawingPlane, char separatorForObject){ + var plane = drawingPlane; + var entity = plane.entityPlane; + if(entity == null) + return null; + var str = String.format("%d%c%d%c%s%c%d%c%d", entity.getSpeed(), separatorForObject, (int)entity.getWeight(), + separatorForObject, getName(entity.bodyColor), separatorForObject, plane.drawingEngines.getType(), separatorForObject, plane.drawingEngines.getNumber()); + if(!(entity instanceof EntityAirBomber)){ + return str; + } + var nstr = String.format("%s%c%s%c%b%c%b", str, separatorForObject, + getName(((EntityAirBomber) entity).additionalColor), separatorForObject, + ((EntityAirBomber) entity).getBombs(), separatorForObject, + ((EntityAirBomber) entity).getFuel()); + return nstr; + } +} diff --git a/src/form/FramePlaneCollection.java b/src/form/FramePlaneCollection.java index 6913822..6b970cd 100644 --- a/src/form/FramePlaneCollection.java +++ b/src/form/FramePlaneCollection.java @@ -9,8 +9,10 @@ import Generics.PlanesGenericCollection; import PlaneHard.FormPlaneConfig; import drawing_objects.DrawingPlane; import movement_strategy.DrawingObjectPlane; +import form.TxtSaveFilter; import java.awt.*; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; @@ -46,6 +48,22 @@ public class FramePlaneCollection extends JFrame { super.repaint(); } }; + JMenuBar menuFile = new JMenuBar(); + JMenu file = new JMenu("Файл"); + menuFile.add(file); + JMenuItem saveFile = new JMenuItem("Сохранить"); + saveFile.addActionListener(e -> saveFile_Click()); + JMenuItem loadFile = new JMenuItem("Загрузить"); + loadFile.addActionListener(e -> loadFile_Click()); + JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию"); + saveCollection.addActionListener(e -> saveCollection_Click()); + JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию"); + loadCollection.addActionListener(e -> loadCollection_Click()); + file.add(saveCollection); + file.add(loadCollection); + file.add(saveFile); + file.add(loadFile); + super.setJMenuBar(menuFile); pictureBoxCollection.setPreferredSize(new Dimension(700, 600)); JButton buttonAddPlane = new JButton("Добавить самолет"); textFieldNumber = new TextField(); @@ -114,6 +132,73 @@ public class FramePlaneCollection extends JFrame { add(panelTools, BorderLayout.EAST); add(pictureBoxCollection, BorderLayout.CENTER); } + + private void saveFile_Click(){ + JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop"); + fc.addChoosableFileFilter(new TxtSaveFilter()); + int retrieval = fc.showSaveDialog(null); + + if (retrieval == JFileChooser.APPROVE_OPTION) { + File file = new File(fc.getSelectedFile() + "." + "txt"); + + try { + storage.SaveData(file); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + private void loadFile_Click() { + JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + storage.LoadData(file); + reloadObjects(); + super.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + + private void saveCollection_Click() { + JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop"); + fc.addChoosableFileFilter(new TxtSaveFilter()); + int retrieval = fc.showSaveDialog(null); + + if (retrieval == JFileChooser.APPROVE_OPTION) { + File file = new File(fc.getSelectedFile() + "." + "txt"); + + try { + if(listStorages.getSelectedIndex() == -1) { + return; + } + storage.planeStorages.get(listStorages.getSelectedValue()).SaveData(file, listStorages.getSelectedValue()); + reloadObjects(); + super.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + + private void loadCollection_Click() { + JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + storage.LoadCollection(file); + reloadObjects(); + super.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + private void reloadObjects(){ int index = listStorages.getSelectedIndex(); listModel.clear(); @@ -142,7 +227,7 @@ public class FramePlaneCollection extends JFrame { public void buttonDeleteSet_Click() { if (listStorages.getSelectedIndex() == -1) return; - storage.DelSet(listStorages.getSelectedValue()); + storage.DelSet(listStorages.getSelectedValue(), trashCollection); reloadObjects(); } public void buttonAddPlaneClick() { diff --git a/src/form/TxtSaveFilter.java b/src/form/TxtSaveFilter.java new file mode 100644 index 0000000..8500de9 --- /dev/null +++ b/src/form/TxtSaveFilter.java @@ -0,0 +1,20 @@ +package form; + +import javax.swing.filechooser.FileFilter; +import java.io.File; + +public class TxtSaveFilter extends FileFilter { + @Override + public boolean accept(File f) { + if (f.isDirectory()) { + return false; + } + String s = f.getName().toLowerCase(); + return s.endsWith(".txt"); + } + + @Override + public String getDescription() { + return "*.txt"; + } +}