245 lines
7.7 KiB
Java
245 lines
7.7 KiB
Java
import javax.swing.*;
|
||
import java.io.*;
|
||
import java.util.*;
|
||
|
||
//класс для хранения коллекции карт
|
||
public class MapsCollection
|
||
{
|
||
//словарь (хранилище) с картами
|
||
public HashMap<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> _mapStorage;
|
||
|
||
//возвращение списка названий карт
|
||
public ArrayList<String> Keys()
|
||
{
|
||
return new ArrayList<>(_mapStorage.keySet());
|
||
}
|
||
|
||
//ширина окна отрисовки
|
||
private final int _pictureWidth;
|
||
|
||
//высота окна отрисовки
|
||
private final int _pictureHeight;
|
||
|
||
//разделитель для записи информации по элементу словаря в файл
|
||
private final char separatorDict = '|';
|
||
|
||
//разделитель для записи коллекции данных в файл
|
||
private final char separatorData = ';';
|
||
|
||
//конструктор
|
||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||
{
|
||
_mapStorage = new HashMap<>();
|
||
_pictureWidth = pictureWidth;
|
||
_pictureHeight = pictureHeight;
|
||
}
|
||
|
||
//добавление карты
|
||
public void AddMap(String name, AbstractMap map)
|
||
{
|
||
if(_mapStorage.containsKey(name))
|
||
{
|
||
return;
|
||
}
|
||
|
||
_mapStorage.put(name, new MapWithSetPlanesGeneric<>(_pictureWidth, _pictureHeight, map));
|
||
}
|
||
|
||
//удаление карты
|
||
public void DelMap(String name)
|
||
{
|
||
_mapStorage.remove(name);
|
||
}
|
||
|
||
//сохранение информации по самолётам в ангарах в файл
|
||
public Boolean SaveData(String filename)
|
||
{
|
||
File file = new File(filename);
|
||
|
||
if (file.exists())
|
||
{
|
||
file.delete();
|
||
}
|
||
|
||
try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename)))
|
||
{
|
||
writter.write(String.format("MapsCollection" + System.lineSeparator()));
|
||
|
||
for(Map.Entry<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> entry : _mapStorage.entrySet())
|
||
{
|
||
writter.write("" + entry.getKey() + separatorDict + entry.getValue().GetData(separatorDict, separatorData) + "\n");
|
||
}
|
||
}
|
||
catch (IOException e) {
|
||
JOptionPane.showMessageDialog(null, e.getMessage());
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//загрузка нформации по по самолётам в ангарах из файла
|
||
public Boolean LoadData(String filename)
|
||
{
|
||
File file = new File(filename);
|
||
|
||
if (!file.exists())
|
||
{
|
||
return false;
|
||
}
|
||
|
||
try (BufferedReader reader = new BufferedReader(new FileReader(filename)))
|
||
{
|
||
String str = "";
|
||
|
||
//если не содержит такую запись или пустой файл
|
||
if ((str = reader.readLine()) == null || !str.contains("MapsCollection"))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
_mapStorage.clear();
|
||
|
||
while ((str = reader.readLine()) != null)
|
||
{
|
||
var element = str.split(String.format("\\%c", separatorDict));
|
||
AbstractMap map = null;
|
||
|
||
switch (element[1])
|
||
{
|
||
case "SimpleMap":
|
||
map = new SimpleMap();
|
||
break;
|
||
case "DesertStormMap":
|
||
map = new DesertStormMap();
|
||
break;
|
||
case "StarWarsMap":
|
||
map = new StarWarsMap();
|
||
break;
|
||
}
|
||
|
||
_mapStorage.put(element[0], new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight,
|
||
map));
|
||
|
||
_mapStorage.get(element[0]).LoadData(element[2].split(String.valueOf(separatorData)));
|
||
}
|
||
}
|
||
catch (IOException e)
|
||
{
|
||
JOptionPane.showMessageDialog(null, e.getMessage());
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//сохранение информации по самолётам в ангарах в файл
|
||
public Boolean SaveOneData(String filename, String mapName)
|
||
{
|
||
File file = new File(filename);
|
||
|
||
if (file.exists())
|
||
{
|
||
file.delete();
|
||
}
|
||
|
||
try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename)))
|
||
{
|
||
writter.write(String.format("SaveOneMap" + System.lineSeparator() + "Data of map:"));
|
||
|
||
for(Map.Entry<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> entry : _mapStorage.entrySet())
|
||
{
|
||
if(entry.getKey() == mapName)
|
||
{
|
||
writter.write("" + entry.getKey() + separatorDict + entry.getValue().GetData(separatorDict, separatorData) + "\n");
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch (IOException e) {
|
||
JOptionPane.showMessageDialog(null, e.getMessage());
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//загрузка нформации по по самолётам в ангарах из файла
|
||
public Boolean LoadOneData(String filename)
|
||
{
|
||
File file = new File(filename);
|
||
|
||
if (!file.exists())
|
||
{
|
||
return false;
|
||
}
|
||
|
||
try (BufferedReader reader = new BufferedReader(new FileReader(filename)))
|
||
{
|
||
String str = "";
|
||
|
||
//если не содержит такую запись или пустой файл
|
||
if ((str = reader.readLine()) == null || !str.contains("SaveOneMap"))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
while ((str = reader.readLine()) != null)
|
||
{
|
||
var element = str.split(String.format("\\%c", separatorDict));
|
||
AbstractMap map = null;
|
||
|
||
//добавление в коллекцию с устранением лишнего слова в названии
|
||
try {
|
||
switch (element[1]) {
|
||
case "SimpleMap":
|
||
map = new SimpleMap();
|
||
break;
|
||
case "DesertStormMap":
|
||
map = new DesertStormMap();
|
||
break;
|
||
case "StarWarsMap":
|
||
map = new StarWarsMap();
|
||
break;
|
||
}
|
||
|
||
//если имя загружаемой карты уже есть в коллекции
|
||
if (_mapStorage.get(element[0]) != null) {
|
||
_mapStorage.remove(element[0]);
|
||
}
|
||
|
||
_mapStorage.put(element[0], new MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight,
|
||
map));
|
||
|
||
_mapStorage.get(element[0]).LoadData(element[2].split(String.valueOf(separatorData)));
|
||
} catch (Exception ex) { }
|
||
}
|
||
}
|
||
catch (IOException e)
|
||
{
|
||
JOptionPane.showMessageDialog(null, e.getMessage());
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//Доступ к аэродрому
|
||
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> get(String index)
|
||
{
|
||
if(_mapStorage.containsKey(index))
|
||
{
|
||
return _mapStorage.get(index);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public IDrawningObject Get(String name, int index)
|
||
{
|
||
if(_mapStorage.containsKey(name))
|
||
{
|
||
return _mapStorage.get(name).GetPlaneInList(index);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|