diff --git a/src/DrawningObjects/DrawningAirplane.java b/src/DrawningObjects/DrawningAirplane.java index 67cf53a..8bf6da9 100644 --- a/src/DrawningObjects/DrawningAirplane.java +++ b/src/DrawningObjects/DrawningAirplane.java @@ -25,7 +25,6 @@ public class DrawningAirplane { public void ChangeIDraw(IDraw obj){ DrawningIlluminators = obj; obj.ChangeIlluminatorNumb(_illuminatorsNumb); - //obj.ChangeIlluminatorNumb(_illuminatorsNumb); obj.ChangeX(_startPosX); obj.ChangeY(_startPosY); } @@ -33,6 +32,25 @@ public class DrawningAirplane { _illuminatorsNumb = numb; DrawningIlluminators.ChangeIlluminatorNumb(numb); } + public int GetIDrawType(){ + if(DrawningIlluminators instanceof DrawningIlluminators) + return 0; + if(DrawningIlluminators instanceof DrawningIlluminatorsCirc) + return 1; + if(DrawningIlluminators instanceof DrawningIlluminatorsQuad) + return 2; + return -1; + } + public void ChangeIDraw(int variant){ + IDraw cur; + if(variant == 0) + cur = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY); + else if(variant == 1) + cur = new DrawningIlluminatorsCirc(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY); + else + cur = new DrawningIlluminatorsQuad(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY); + ChangeIDraw(cur); + } public DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height){ if(width <= _airplaneWidth || height <= _airplaneHeight) return; diff --git a/src/DrawningObjects/ExtentionDrawningAirplane.java b/src/DrawningObjects/ExtentionDrawningAirplane.java new file mode 100644 index 0000000..bde9da2 --- /dev/null +++ b/src/DrawningObjects/ExtentionDrawningAirplane.java @@ -0,0 +1,93 @@ +package src.DrawningObjects; + +import src.Entities.EntityAirplaneWithRadar; +import java.awt.*; +public class ExtentionDrawningAirplane { + private static String getName(Color col) { + if (col.equals(Color.RED)) + return new String("RED"); + if (col.equals(Color.GREEN)) + return new String("GREEN"); + if (col.equals(Color.BLUE)) + return new String("BLUE"); + if (col.equals(Color.YELLOW)) + return new String("YELLOW"); + if (col.equals(Color.WHITE)) + return new String("WHITE"); + if (col.equals(Color.GRAY)) + return new String("GRAY"); + if (col.equals(Color.BLACK)) + return new String("BLACK"); + if (col.equals(Color.PINK)) + return new String("PINK"); + 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("PINK")) + return Color.PINK; + return null; + } + public static DrawningAirplane CreateDrawningAirplane(String info, char separatorForObject, int width, int height){ + String[] strs = info.split(Character.toString(separatorForObject)); + if(strs.length == 5){ + DrawningAirplane toRet = new DrawningAirplane( + Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), + getColor(strs[2]), + width, + height); + toRet.ChangeIlluminatorNumb(Integer.parseInt(strs[5])); + toRet.ChangeIDraw(Integer.parseInt(strs[6])); + return toRet; + } + if(strs.length == 8){ + DrawningAirplane toRet = new DrawningAirplaneWithRadar( + Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), + getColor(strs[2]), + getColor(strs[3]), + Boolean.parseBoolean(strs[7]), + Boolean.parseBoolean(strs[8]), + width, + height); + toRet.ChangeIlluminatorNumb(Integer.parseInt(strs[5])); + toRet.ChangeIDraw(Integer.parseInt(strs[6])); + return toRet; + } + return null; + } + public static String GetDataForSave(DrawningAirplane drawningAirplane, char separatorForObject){ + var airplane = drawningAirplane.EntityAirplane; + if(airplane == null) + return null; + String colName = getName(airplane.BodyColor); + var str = String.format("%d%c%d%c%s%c%d%c%d", + airplane.Speed(), + separatorForObject, (int)airplane.Weight(), + separatorForObject, getName(airplane.BodyColor), + separatorForObject, drawningAirplane._illuminatorsNumb, + separatorForObject, drawningAirplane.GetIDrawType()); + if(!(airplane instanceof EntityAirplaneWithRadar)){ + return str; + } + var nstr = String.format("%s%c%b%c%b%c%s", str, separatorForObject, + ((EntityAirplaneWithRadar) airplane).Radar(), separatorForObject, + ((EntityAirplaneWithRadar) airplane).DopBak(), separatorForObject, + getName(((EntityAirplaneWithRadar) airplane).AdditionalColor), separatorForObject); + return nstr; + } +} diff --git a/src/FormAirplaneCollection.java b/src/FormAirplaneCollection.java index c338c92..5a9bbaa 100644 --- a/src/FormAirplaneCollection.java +++ b/src/FormAirplaneCollection.java @@ -5,13 +5,36 @@ import src.Generics.AirplaneGenericCollection; import src.MovementStrategy.DrawningObjectAirplane; import src.Generics.AirplaneGenericStorage; import src.Generics.AirplaneTrashCollection; +import src.DrawningObjects.ExtentionDrawningAirplane; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import javax.swing.filechooser.FileFilter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; +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"; + } +} public class FormAirplaneCollection { private final AirplaneGenericStorage _storage; @@ -41,6 +64,17 @@ public class FormAirplaneCollection { } public FormAirplaneCollection() { + JMenuBar menuFile = new JMenuBar(); + JMenu file = new JMenu("Файл"); + menuFile.add(file); + JMenuItem saveFile = new JMenuItem("Сохранить"); + JMenuItem loadFile = new JMenuItem("Загрузить"); + JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию"); + JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию"); + file.add(saveCollection); + file.add(loadCollection); + file.add(saveFile); + file.add(loadFile); AirplaneTrashCollection _trashCollection = new AirplaneTrashCollection<>(); JButton callTrashButton = new JButton("Мусор"); _storage = new AirplaneGenericStorage(pictureBoxWidth, pictureBoxHeight); @@ -53,7 +87,6 @@ public class FormAirplaneCollection { listBoxStorages = new JList<>(listBoxModel); scrollPane.setViewportView(listBoxStorages); JButton delStorageButton = new JButton("Удалить набор"); - //toolBox.setBounds(623,12, 227, 80); JFrame collectionFrame = new JFrame(); collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); collectionFrame.setSize(880, 497); @@ -75,11 +108,85 @@ public class FormAirplaneCollection { toolBox.add(callTrashButton); toolBox.add(refreshButton); collectionFrame.add(toolBox); + collectionFrame.setJMenuBar(menuFile); collectionFrame.add(canv); collectionFrame.setVisible(true); canv._storage = _storage; canv.listBoxStorages = listBoxStorages; + saveFile.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\1\\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); + } + } + } + }); + saveCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое"); + fc.addChoosableFileFilter(new TxtSaveFilter()); + int retrieval = fc.showSaveDialog(null); + if (retrieval == JFileChooser.APPROVE_OPTION) { + File file = new File(fc.getSelectedFile() + "." + "txt"); + + try { + if(listBoxStorages.getSelectedIndex() == -1) { + return; + } + _storage._airplaneStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue()); + ReloadObjects(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + }); + loadFile.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + _storage.LoadData(file); + canv._storage =_storage; + ReloadObjects(); + canv.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + } + }); + loadCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + _storage.LoadCollection(file); + ReloadObjects(); + canv.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + }); addStorageButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -89,18 +196,16 @@ public class FormAirplaneCollection { ReloadObjects(); } }); - delStorageButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (listBoxStorages.getSelectedIndex() == -1) { return; } - _storage.DelSet(listBoxStorages.getSelectedValue()); + _storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection); ReloadObjects(); } }); - callTrashButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -111,7 +216,6 @@ public class FormAirplaneCollection { _trashCollection.Pop(); } }); - addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/src/Generics/AirplaneGenericCollection.java b/src/Generics/AirplaneGenericCollection.java index 74bc037..95b0620 100644 --- a/src/Generics/AirplaneGenericCollection.java +++ b/src/Generics/AirplaneGenericCollection.java @@ -1,16 +1,50 @@ package src.Generics; import src.DrawningObjects.DrawningAirplane; import src.MovementStrategy.IMoveableObject; +import src.DrawningObjects.ExtentionDrawningAirplane; +import src.MovementStrategy.DrawningObjectAirplane; import java.awt.*; import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; public class AirplaneGenericCollection { + public static char _separatorRecords = ';'; + public static char _separatorForObject = ':'; private final int _pictureWidth; private final int _pictureHeight; private final int _placeSizeWidth = 210; private final int _placeSizeHeight = 78; - private final SetGeneric _collection; + private SetGeneric _collection; + public ArrayList GetAirplanes(){ + return _collection.GetAirplanes(_collection.Count); + } + + public boolean SaveData(File f, String name) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("MonorailCollection\n"); + data.append(String.format("%s\n", name)); + StringBuilder records = new StringBuilder(); + for(DrawningAirplane elem : GetAirplanes()) + { + records.append(String.format("%s%c", ExtentionDrawningAirplane.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 AirplaneGenericCollection(int picWidth, int picHeight){ int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; @@ -79,4 +113,7 @@ public class AirplaneGenericCollection(_pictureWidth * _pictureHeight); + } } diff --git a/src/Generics/AirplaneGenericStorage.java b/src/Generics/AirplaneGenericStorage.java index 9b6330d..1ec2e24 100644 --- a/src/Generics/AirplaneGenericStorage.java +++ b/src/Generics/AirplaneGenericStorage.java @@ -1,15 +1,116 @@ package src.Generics; -import java.util.Dictionary; -import java.util.HashMap; -import java.util.List; -import java.util.Queue; +import javax.xml.crypto.dsig.keyinfo.KeyValue; +import java.io.*; +import java.util.*; import java.util.stream.Collectors; import src.MovementStrategy.DrawningObjectAirplane; import src.DrawningObjects.DrawningAirplane; +import src.DrawningObjects.ExtentionDrawningAirplane; public class AirplaneGenericStorage { - final HashMap> _airplaneStorages; + public final HashMap> _airplaneStorages; + private static final char _separatorForKeyValue = '|'; + private final char _separatorRecords = ';'; + private static final char _separatorForObject = ':'; + + public boolean SaveData(File f) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("AirplaneStorage\n"); + for(Map.Entry> record : _airplaneStorages.entrySet()){ + StringBuilder records = new StringBuilder(); + for(DrawningAirplane elem : record.getValue().GetAirplanes()) + { + records.append(String.format("%s%c", ExtentionDrawningAirplane.GetDataForSave(elem, _separatorForObject), _separatorRecords)); + } + data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString())); + } + if(data.length() == 0) + return false; + FileWriter writer = new FileWriter(f); + writer.write(data.toString()); + writer.flush(); + writer.close(); + return true; + } + public boolean LoadData(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("MonorailStorage")) + return false; + _airplaneStorages.clear(); + for(String data : strs){ + String st = new String("\\" + Character.toString( _separatorForKeyValue)); + String[]record = data.split(st); + if (record.length != 2) + continue; + AirplaneGenericCollection collection = + new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight); + String[] set = record[1].split(Character.toString(_separatorRecords)); + for(int i = set.length -1; i >=0; i--){ + String elem = set[i]; + DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(elem, + _separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection.Insert(airplane))) + { + return false; + } + } + } + _airplaneStorages.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("AirplaneCollection")) + return false; + String collectionName = strs[1]; + AirplaneGenericCollection collection = GetCollection(collectionName); + if(collection == null) + collection = new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight); + else + collection.Clear(); + String[] airplanesInfo = strs[2].split(Character.toString(AirplaneGenericCollection._separatorRecords)); + for(int i = airplanesInfo.length-1; i >= 0; i--){ + String data = airplanesInfo[i]; + DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(data, + AirplaneGenericCollection._separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection.Insert(airplane))) + { + return false; + } + } + } + AddSetFromFile(collectionName, collection); + return true; + } public List Keys(){ if(_airplaneStorages == null) return null; @@ -30,9 +131,13 @@ public class AirplaneGenericStorage { _airplaneStorages.put(name, new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight)); } - public void DelSet(String name){ + public void DelSet(String name, AirplaneTrashCollection trashBox){ if(!_airplaneStorages.containsKey(name)) return; + AirplaneGenericCollection cur = _airplaneStorages.get(name); + for(int i = 0; i < cur.Size(); i++) + trashBox.Push(cur.Get(i)); + _airplaneStorages.remove(name); } @@ -45,4 +150,15 @@ public class AirplaneGenericStorage { public DrawningAirplane Get(String collectionName, int position){ return _airplaneStorages.get(collectionName).Get(position); } + + public AirplaneGenericCollection GetCollection(String collectionName){ + return _airplaneStorages.get(collectionName); + } + + public void AddSetFromFile(String name, AirplaneGenericCollection toAdd){ + if(_airplaneStorages.containsKey(name)){ + _airplaneStorages.remove(name); + } + _airplaneStorages.put(name, toAdd); + } }