Добавлены функции загрузки и сохранения одной карты.
This commit is contained in:
parent
f119611d50
commit
c3ea53e15f
@ -276,6 +276,18 @@
|
|||||||
<text value="Сохранить"/>
|
<text value="Сохранить"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
|
<component id="885e8" class="javax.swing.JMenuItem" binding="MenuItemLoadOneMapData">
|
||||||
|
<constraints/>
|
||||||
|
<properties>
|
||||||
|
<text value="Загрузить карту"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="ddbf9" class="javax.swing.JMenuItem" binding="MenuItemSaveOneMapData">
|
||||||
|
<constraints/>
|
||||||
|
<properties>
|
||||||
|
<text value="Сохранить карту"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
</children>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
<hspacer id="2b204">
|
<hspacer id="2b204">
|
||||||
|
@ -2,14 +2,12 @@ import javax.imageio.ImageIO;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.event.ListSelectionEvent;
|
import javax.swing.event.ListSelectionEvent;
|
||||||
import javax.swing.event.ListSelectionListener;
|
import javax.swing.event.ListSelectionListener;
|
||||||
import javax.swing.filechooser.FileSystemView;
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class FormMapWithSetPlanesGeneric extends JFrame{
|
public class FormMapWithSetPlanesGeneric extends JFrame{
|
||||||
@ -36,6 +34,8 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
|
|||||||
private JMenu MenuChoiceOperation;
|
private JMenu MenuChoiceOperation;
|
||||||
private JMenuItem MenuItemLoadData;
|
private JMenuItem MenuItemLoadData;
|
||||||
private JMenuItem MenuItemSaveData;
|
private JMenuItem MenuItemSaveData;
|
||||||
|
private JMenuItem MenuItemLoadOneMapData;
|
||||||
|
private JMenuItem MenuItemSaveOneMapData;
|
||||||
|
|
||||||
//объект от коллекции карт
|
//объект от коллекции карт
|
||||||
private final MapsCollection _mapsCollection;
|
private final MapsCollection _mapsCollection;
|
||||||
@ -377,7 +377,7 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//обработка нажатия загрузки
|
//обработка нажатия загрузки всех карт
|
||||||
MenuItemLoadData.addActionListener(new ActionListener() {
|
MenuItemLoadData.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -401,7 +401,7 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//обработка нажатия сохранения
|
//обработка нажатия сохранения всех карт
|
||||||
MenuItemSaveData.addActionListener(new ActionListener() {
|
MenuItemSaveData.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -423,6 +423,53 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//загрузка одной карты
|
||||||
|
MenuItemLoadOneMapData.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser jfc = new JFileChooser();
|
||||||
|
int result = jfc.showOpenDialog(null);
|
||||||
|
|
||||||
|
if (result == JFileChooser.APPROVE_OPTION)
|
||||||
|
{
|
||||||
|
if (_mapsCollection.LoadOneData(jfc.getSelectedFile().getPath()))
|
||||||
|
{
|
||||||
|
ReloadMaps();
|
||||||
|
JOptionPane.showMessageDialog(null,"Загрузка данных прошла успешно",
|
||||||
|
"Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Ошибка загрузки данных",
|
||||||
|
"Результат", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//сохранение одной карты
|
||||||
|
MenuItemSaveOneMapData.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser jfc = new JFileChooser();
|
||||||
|
int result = jfc.showSaveDialog(null);
|
||||||
|
|
||||||
|
if (result == JFileChooser.APPROVE_OPTION)
|
||||||
|
{
|
||||||
|
if (_mapsCollection.SaveOneData(jfc.getSelectedFile().getPath(), ListBoxMaps.getSelectedValue().toString()))
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null,"Сохранение прошло успешно",
|
||||||
|
"Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не сохранилось",
|
||||||
|
"Результат", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createUIComponents()
|
private void createUIComponents()
|
||||||
|
@ -131,6 +131,95 @@ public class MapsCollection
|
|||||||
return true;
|
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("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;
|
||||||
|
}
|
||||||
|
|
||||||
//Доступ к аэродрому
|
//Доступ к аэродрому
|
||||||
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> get(String index)
|
public MapWithSetPlanesGeneric<IDrawningObject, AbstractMap> get(String index)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user