370 lines
17 KiB
Java
370 lines
17 KiB
Java
import javax.swing.*;
|
||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.io.File;
|
||
import java.util.HashMap;
|
||
import java.util.function.Consumer;
|
||
|
||
import org.apache.log4j.Logger;
|
||
public class FormMapWithSetWarships extends JFrame{
|
||
private JPanel mainPanel;
|
||
private JPanel PictureBox;
|
||
private JPanel GroupBoxTools;
|
||
private JComboBox СomboBoxSelectorMap;
|
||
private JButton ButtonAddWarship;
|
||
private JButton ButtonRemoveWarship;
|
||
private JButton ButtonShowStorage;
|
||
private JButton ButtonShowOnMap;
|
||
private JTextField TextBoxPosition;
|
||
private JButton ButtonRight;
|
||
private JButton ButtonUp;
|
||
private JButton ButtonDown;
|
||
private JButton ButtonLeft;
|
||
private JButton deletedWarshipButtom;
|
||
private JPanel MapPanel;
|
||
private JTextField TextFieldMap;
|
||
private JButton CreateMapButton;
|
||
private JButton DeleteMapButton;
|
||
private JList ListBoxMaps;
|
||
private JMenuItem saveToolStripMenuItem;
|
||
private JMenuItem loadToolStripMenuItem;
|
||
private JMenuItem saveMapToolStripMenuItem;
|
||
private JMenuItem loadMapToolStripMenuItem;
|
||
private JMenuBar menuStrip;
|
||
private JMenu menu;
|
||
private Image bufferedImage;
|
||
private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
|
||
private MapsCollection _mapsCollection;
|
||
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
|
||
put("SimpleMap", new SimpleMap());
|
||
put("LineMap", new LineMap());
|
||
}};
|
||
|
||
private Logger _logger;
|
||
|
||
public FormMapWithSetWarships(Logger logger){
|
||
this();
|
||
this._logger=logger;;
|
||
}
|
||
|
||
public FormMapWithSetWarships(){
|
||
InitializeComponent();
|
||
}
|
||
|
||
@Override
|
||
public void paint(Graphics g) {
|
||
super.paint(g);
|
||
if (bufferedImage != null) {
|
||
PictureBox.paintComponents(bufferedImage.getGraphics());
|
||
PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
|
||
}
|
||
}
|
||
|
||
private void ReloadMaps()
|
||
{
|
||
int index = ListBoxMaps.getSelectedIndex();
|
||
ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0]));
|
||
|
||
if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize()))
|
||
{
|
||
ListBoxMaps.setSelectedIndex(0);
|
||
}
|
||
else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize())
|
||
{
|
||
ListBoxMaps.setSelectedIndex(index);
|
||
}
|
||
repaint();
|
||
}
|
||
|
||
private void InitializeComponent(){
|
||
|
||
setContentPane(mainPanel);
|
||
setTitle("Warship");
|
||
setSize(554, 548);
|
||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||
setVisible(true);
|
||
|
||
_mapsCollection = new MapsCollection(getWidth(), getHeight());
|
||
СomboBoxSelectorMap.removeAllItems();
|
||
for(String elem:_mapsDict.keySet()){
|
||
СomboBoxSelectorMap.addItem(elem);
|
||
}
|
||
|
||
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
|
||
ButtonUp.setIcon(iconUp);
|
||
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
|
||
ButtonDown.setIcon(iconDown);
|
||
Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg");
|
||
ButtonLeft.setIcon(iconLeft);
|
||
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
|
||
ButtonRight.setIcon(iconRight);
|
||
|
||
File root = new File("D://2.1//RPP");
|
||
|
||
//сохранение
|
||
saveToolStripMenuItem.addActionListener(e -> {
|
||
JFileChooser fs = new JFileChooser();
|
||
fs.setCurrentDirectory(root);
|
||
fs.setAcceptAllFileFilterUsed(false);
|
||
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
|
||
fs.addChoosableFileFilter(filter);
|
||
fs.setDialogTitle("Save");
|
||
int result = fs.showSaveDialog(null);
|
||
if (result == JFileChooser.APPROVE_OPTION) {
|
||
try {
|
||
File selectedFile = fs.getSelectedFile();
|
||
_mapsCollection.SaveData(selectedFile.getPath());
|
||
_logger.info("Успешное сохранение в файл: "+selectedFile.getPath());
|
||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||
} catch (Exception ex) {
|
||
_logger.error("Данные не сохранились: "+ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Не сохранилось: "+ex.getMessage(), "Результат",JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
});
|
||
|
||
//загрузка
|
||
loadToolStripMenuItem.addActionListener(e -> {
|
||
JFileChooser fs = new JFileChooser();
|
||
fs.setCurrentDirectory(root);
|
||
fs.setAcceptAllFileFilterUsed(false);
|
||
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
|
||
fs.addChoosableFileFilter(filter);
|
||
fs.setDialogTitle("Load");
|
||
int result = fs.showSaveDialog(null);
|
||
if (result == JFileChooser.APPROVE_OPTION) {
|
||
try {
|
||
File selectedFile = fs.getSelectedFile();
|
||
_mapsCollection.LoadData(selectedFile.getPath());
|
||
ReloadMaps();
|
||
_logger.info("Успешная загрузка из файла: "+selectedFile.getPath());
|
||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||
} catch(Exception ex) {
|
||
_logger.error("Не загрузилось: "+ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Не загрузилось: "+ex.getMessage(), "Результат",JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
});
|
||
|
||
//сохранение карты
|
||
saveMapToolStripMenuItem.addActionListener(e -> {
|
||
JFileChooser fs = new JFileChooser();
|
||
fs.setCurrentDirectory(root);
|
||
fs.setAcceptAllFileFilterUsed(false);
|
||
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
|
||
fs.addChoosableFileFilter(filter);
|
||
fs.setDialogTitle("SaveMap");
|
||
int result = fs.showSaveDialog(null);
|
||
if (result == JFileChooser.APPROVE_OPTION) {
|
||
try {
|
||
File selectedFile = fs.getSelectedFile();
|
||
_mapsCollection.SaveMap(selectedFile.getPath(), ListBoxMaps.getSelectedValue().toString());
|
||
_logger.info("Успешное сохранение карты в файл: "+selectedFile.getPath());
|
||
JOptionPane.showMessageDialog(null, "Сохранение карты прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||
} catch(Exception ex) {
|
||
_logger.error("Не сохранилась карта: "+ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Не сохранилась карта: "+ex.getMessage(), "Результат",JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
});
|
||
|
||
//загрузка карты
|
||
loadMapToolStripMenuItem.addActionListener(e -> {
|
||
JFileChooser fs = new JFileChooser();
|
||
fs.setCurrentDirectory(root);
|
||
fs.setAcceptAllFileFilterUsed(false);
|
||
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
|
||
fs.addChoosableFileFilter(filter);
|
||
fs.setDialogTitle("LoadMap");
|
||
int result = fs.showSaveDialog(null);
|
||
if (result == JFileChooser.APPROVE_OPTION) {
|
||
try {
|
||
File selectedFile = fs.getSelectedFile();
|
||
_mapsCollection.LoadMap(selectedFile.getPath());
|
||
JOptionPane.showMessageDialog(null, "Загрузка карты прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||
ReloadMaps();
|
||
_logger.info("Успешная загрузка карты из файла: "+selectedFile.getPath());
|
||
}catch(Exception ex) {
|
||
_logger.error("Не загрузилась карта: "+ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Не загрузилась карта", "Результат", JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
});
|
||
|
||
ListBoxMaps.addListSelectionListener(e -> {
|
||
if(ListBoxMaps.getSelectedIndex() == -1)
|
||
return;
|
||
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
|
||
_logger.info("Переход на карту "+ListBoxMaps.getSelectedValue());
|
||
repaint();
|
||
});
|
||
|
||
DeleteMapButton.addActionListener(e -> {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
if(JOptionPane.showConfirmDialog(this,"Удалить карту " + ListBoxMaps.getSelectedValue().toString() + "?",
|
||
"Удаление",JOptionPane.YES_NO_OPTION) == 0)
|
||
{
|
||
_logger.info("Удалена карта: " + ListBoxMaps.getSelectedValue().toString());
|
||
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
|
||
ReloadMaps();
|
||
}
|
||
});
|
||
|
||
CreateMapButton.addActionListener(e -> {
|
||
if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText() == null || TextFieldMap.getText().equals(""))
|
||
{
|
||
_logger.info("Не все данные были заполнены при добавлении карты");
|
||
JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
|
||
{
|
||
_logger.info("Нет карты с названием: " + СomboBoxSelectorMap.getSelectedItem());
|
||
JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
|
||
ReloadMaps();
|
||
_logger.info("Добавлена карта: " + TextFieldMap.getText());
|
||
});
|
||
|
||
deletedWarshipButtom.addActionListener(e -> {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
return;
|
||
DrawingObjectWarship warship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
|
||
if (warship == null) {
|
||
JOptionPane.showMessageDialog(null, "Стек пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
FormWarship form = new FormWarship(warship);
|
||
form.setSize(1000, 700);
|
||
form.setVisible(true);
|
||
});
|
||
|
||
ButtonAddWarship.addActionListener(e -> {
|
||
FormWarshipConfig formWarshipConfig = new FormWarshipConfig();
|
||
formWarshipConfig.AddEvent(newWarship ->{
|
||
try{
|
||
if(ListBoxMaps.getSelectedIndex() == -1){
|
||
return;
|
||
}
|
||
if(newWarship != null){
|
||
DrawingObjectWarship warship = new DrawingObjectWarship(newWarship);
|
||
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Plus(warship) >= 0) {
|
||
_logger.info("Добавлен объект: " + warship);
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
|
||
repaint();
|
||
}
|
||
else {
|
||
_logger.info("Не удалось добавить объект: " + warship);
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||
}
|
||
}
|
||
}catch (StorageOverflowException ex){
|
||
_logger.warn("Ошибка переполнения хранилища: " + ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Ошибка переполнения хранилища: " + ex.getMessage());
|
||
}catch (Exception ex){
|
||
_logger.fatal("Неизвестная ошибка добавления: " + ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Неизвестная ошибка: " + ex.getMessage());
|
||
}
|
||
|
||
});
|
||
formWarshipConfig.setSize(1000, 450);
|
||
formWarshipConfig.setVisible(true);
|
||
});
|
||
|
||
ButtonRemoveWarship.addActionListener(e -> {
|
||
|
||
if (TextBoxPosition.getText().isEmpty())
|
||
{
|
||
return;
|
||
}
|
||
int result = JOptionPane.showConfirmDialog(null,
|
||
"Удалить объект?",
|
||
"Удаление",
|
||
JOptionPane.YES_NO_OPTION,
|
||
JOptionPane.WARNING_MESSAGE);
|
||
if (result == JOptionPane.NO_OPTION)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Integer.parseInt(TextBoxPosition.getText());
|
||
try {
|
||
var deletedWarship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Minus(pos);
|
||
if (deletedWarship != null)
|
||
{
|
||
_logger.info("Удален объект: " + deletedWarship);
|
||
JOptionPane.showMessageDialog(this,
|
||
"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
|
||
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
|
||
repaint();
|
||
}
|
||
else
|
||
{
|
||
_logger.info("Не удалось удалить объект по позиции: " + pos);
|
||
JOptionPane.showMessageDialog(this,
|
||
"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
}catch (WarshipNotFoundException ex){
|
||
_logger.warn("Ошибка удаления: " + ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Ошибка удаления: " + ex.getMessage());
|
||
}catch (Exception ex){
|
||
_logger.fatal("Неизвестная ошибка удаления: " + ex.getMessage());
|
||
JOptionPane.showMessageDialog(null, "Неизвестная ошибка: " + ex.getMessage());
|
||
}
|
||
});
|
||
|
||
ButtonShowStorage.addActionListener(e -> {
|
||
if(_mapWarshipsCollectionGeneric == null)
|
||
{
|
||
return;
|
||
}
|
||
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
|
||
repaint();
|
||
});
|
||
|
||
ButtonShowOnMap.addActionListener(e -> {
|
||
if (_mapWarshipsCollectionGeneric == null)
|
||
{
|
||
return;
|
||
}
|
||
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
|
||
repaint();
|
||
});
|
||
|
||
ButtonUp.addActionListener(e -> {
|
||
if (_mapWarshipsCollectionGeneric != null) {
|
||
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
ButtonDown.addActionListener(e -> {
|
||
if (_mapWarshipsCollectionGeneric != null) {
|
||
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
ButtonRight.addActionListener(e -> {
|
||
if (_mapWarshipsCollectionGeneric != null) {
|
||
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
ButtonLeft.addActionListener(e -> {
|
||
if (_mapWarshipsCollectionGeneric != null) {
|
||
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left);
|
||
repaint();
|
||
}
|
||
});
|
||
}
|
||
}
|