2022-11-18 23:30:03 +04:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.io.*;
|
2022-11-06 18:08:19 +04:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
//класс для хранения коллекции карт
|
2022-11-06 17:32:49 +04:00
|
|
|
|
public class MapsCollection
|
|
|
|
|
{
|
2022-11-06 18:08:19 +04:00
|
|
|
|
//словарь (хранилище) с картами
|
2022-11-18 23:30:03 +04:00
|
|
|
|
public HashMap<String, MapWithSetPlanesGeneric<IDrawningObject, AbstractMap>> _mapStorage;
|
2022-11-06 18:08:19 +04:00
|
|
|
|
|
|
|
|
|
//возвращение списка названий карт
|
2022-11-07 10:19:28 +04:00
|
|
|
|
public ArrayList<String> Keys()
|
|
|
|
|
{
|
|
|
|
|
return new ArrayList<>(_mapStorage.keySet());
|
|
|
|
|
}
|
2022-11-06 18:08:19 +04:00
|
|
|
|
|
|
|
|
|
//ширина окна отрисовки
|
|
|
|
|
private final int _pictureWidth;
|
|
|
|
|
|
|
|
|
|
//высота окна отрисовки
|
|
|
|
|
private final int _pictureHeight;
|
|
|
|
|
|
2022-11-18 23:30:03 +04:00
|
|
|
|
//разделитель для записи информации по элементу словаря в файл
|
|
|
|
|
private final char separatorDict = '|';
|
|
|
|
|
|
|
|
|
|
//разделитель для записи коллекции данных в файл
|
|
|
|
|
private final char separatorData = ';';
|
|
|
|
|
|
2022-11-06 18:08:19 +04:00
|
|
|
|
//конструктор
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 23:30:03 +04:00
|
|
|
|
//сохранение информации по самолётам в ангарах в файл
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 10:05:18 +04:00
|
|
|
|
//сохранение информации по самолётам в ангарах в файл
|
|
|
|
|
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("MapsCollection" + System.lineSeparator()));
|
|
|
|
|
|
|
|
|
|
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("MapsCollection"))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//если имя загружаемой карты уже есть в коллекции
|
|
|
|
|
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 (IOException e)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-06 18:08:19 +04:00
|
|
|
|
//Доступ к аэродрому
|
2022-11-18 23:30:03 +04:00
|
|
|
|
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> get(String index)
|
2022-11-06 18:28:59 +04:00
|
|
|
|
{
|
|
|
|
|
if(_mapStorage.containsKey(index))
|
|
|
|
|
{
|
|
|
|
|
return _mapStorage.get(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 23:30:03 +04:00
|
|
|
|
public IDrawningObject Get(String name, int index)
|
2022-11-06 18:08:19 +04:00
|
|
|
|
{
|
|
|
|
|
if(_mapStorage.containsKey(name))
|
|
|
|
|
{
|
|
|
|
|
return _mapStorage.get(name).GetPlaneInList(index);
|
|
|
|
|
}
|
2022-11-06 17:32:49 +04:00
|
|
|
|
|
2022-11-06 18:08:19 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-06 17:32:49 +04:00
|
|
|
|
}
|