209 lines
8.2 KiB
C#
Raw Permalink Normal View History

2023-10-04 12:52:06 +04:00
using Lab.DrawningObjects;
using Lab.Generics;
using Lab.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2023-11-28 13:51:41 +04:00
using Microsoft.Extensions.Logging;
2023-10-04 12:52:06 +04:00
namespace Lab
{
public partial class CollectionsFrame : Form
{
2023-10-18 12:24:01 +04:00
private readonly CarsGenericStorage _storage;
2023-11-28 13:51:41 +04:00
private readonly ILogger _logger;
public CollectionsFrame(ILogger<CollectionsFrame> logger)
2023-10-04 12:52:06 +04:00
{
InitializeComponent();
2023-10-18 12:24:01 +04:00
_storage = new CarsGenericStorage(DrawTank.Width, DrawTank.Height);
2023-11-28 13:51:41 +04:00
_logger = logger;
2023-10-04 12:52:06 +04:00
}
2023-10-18 12:24:01 +04:00
private void ReloadObjects()
{
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
foreach (var key in _storage.Keys)
{
CollectionListBox.Items.Add(key);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-11-28 13:51:41 +04:00
_logger.LogWarning("Пустое название набора");
2023-10-18 12:24:01 +04:00
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
2023-11-28 13:51:41 +04:00
_logger.LogInformation($"Добавлен набор: {SetTextBox.Text}");
2023-10-18 12:24:01 +04:00
}
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawTank.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCars();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
2023-11-28 13:51:41 +04:00
_logger.LogWarning("Удаление невыбранного набора");
2023-10-18 12:24:01 +04:00
return;
}
2023-11-28 13:51:41 +04:00
string name = CollectionListBox.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"Удалить объект {name}?", "Удаление", MessageBoxButtons.YesNo,
2023-10-18 12:24:01 +04:00
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(CollectionListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
2023-11-28 13:51:41 +04:00
_logger.LogInformation($"Удален набор: {name}");
2023-10-18 12:24:01 +04:00
}
}
2023-11-15 10:25:49 +04:00
2023-11-14 23:39:34 +04:00
private void AddTanker(DrawTanker tanker)
2023-10-18 12:24:01 +04:00
{
2023-11-15 10:25:49 +04:00
2023-10-18 12:24:01 +04:00
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
2023-11-28 13:51:41 +04:00
_logger.LogWarning("Добавление пустого объекта");
2023-10-18 12:24:01 +04:00
return;
}
2023-11-28 13:51:41 +04:00
try
{
_ = obj + tanker;
2023-11-14 23:39:34 +04:00
MessageBox.Show("Объект добавлен");
DrawTank.Image = obj.ShowCars();
2023-11-28 13:51:41 +04:00
_logger.LogInformation($"Добавлен объект в набор {CollectionListBox.SelectedItem.ToString()}");
2023-11-14 23:39:34 +04:00
}
2023-11-28 13:51:41 +04:00
catch (Exception ex)
2023-10-04 12:52:06 +04:00
{
2023-11-14 23:39:34 +04:00
MessageBox.Show("Не удалось добавить объект");
2023-11-28 13:51:41 +04:00
_logger.LogWarning($"{ex.Message} в наборе {CollectionListBox.SelectedItem.ToString()}");
2023-11-14 23:39:34 +04:00
}
}
private void ButtonAddCar_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
2023-10-04 12:52:06 +04:00
}
2023-11-14 23:39:34 +04:00
FormTankerConfig form = new FormTankerConfig();
form.Show();
form.AddEvent(AddTanker);
2023-10-04 12:52:06 +04:00
}
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
2023-10-18 12:24:01 +04:00
if (CollectionListBox.SelectedIndex == -1)
{
2023-11-28 13:51:41 +04:00
_logger.LogWarning("Удаление объекта из несуществующего набора");
2023-10-18 12:24:01 +04:00
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
2023-10-04 12:52:06 +04:00
{
return;
}
2023-10-18 12:24:01 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
return;
2023-10-04 12:52:06 +04:00
}
2023-10-18 12:24:01 +04:00
int pos = Convert.ToInt32(CarTextBox.Text);
2023-11-28 13:51:41 +04:00
try
2023-10-04 12:52:06 +04:00
{
2023-11-28 13:51:41 +04:00
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawTank.Image = obj.ShowCars();
_logger.LogInformation($"Удален объект из набора {CollectionListBox.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Не удалось удалить объект");
_logger.LogWarning($"Не удалось удалить объект из набора {CollectionListBox.SelectedItem.ToString()}");
}
2023-10-04 12:52:06 +04:00
}
2023-11-28 13:51:41 +04:00
catch (CarNotFoundException ex)
2023-10-04 12:52:06 +04:00
{
2023-11-28 13:51:41 +04:00
MessageBox.Show(ex.Message);
_logger.LogWarning($"{ex.Message} из набора {CollectionListBox.SelectedItem.ToString()}");
2023-10-04 12:52:06 +04:00
}
}
2023-10-18 12:24:01 +04:00
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
2023-10-04 12:52:06 +04:00
{
2023-10-18 12:24:01 +04:00
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawTank.Image = obj.ShowCars();
2023-10-04 12:52:06 +04:00
}
2023-11-15 10:25:49 +04:00
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
2023-11-28 13:51:41 +04:00
try
2023-11-15 10:25:49 +04:00
{
2023-11-28 13:51:41 +04:00
_storage.SaveData(saveFileDialog.FileName);
2023-11-15 10:25:49 +04:00
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
2023-11-28 13:51:41 +04:00
_logger.LogInformation($"Сохранение наборов в файл {saveFileDialog.FileName}");
2023-11-15 10:25:49 +04:00
}
2023-11-28 13:51:41 +04:00
catch (Exception ex)
2023-11-15 10:25:49 +04:00
{
2023-11-28 13:51:41 +04:00
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Не удалось сохранить наборы с ошибкой: {ex.Message}");
2023-11-15 10:25:49 +04:00
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
2023-11-28 13:51:41 +04:00
try
2023-11-15 10:25:49 +04:00
{
2023-11-28 13:51:41 +04:00
_storage.LoadData(openFileDialog.FileName);
2023-11-15 10:25:49 +04:00
ReloadObjects();
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
2023-11-28 13:51:41 +04:00
_logger.LogInformation($"Загрузились наборы из файла {openFileDialog.FileName}");
2023-11-15 10:25:49 +04:00
}
2023-11-28 13:51:41 +04:00
catch (Exception ex)
2023-11-15 10:25:49 +04:00
{
2023-11-28 13:51:41 +04:00
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Не удалось сохранить наборы с ошибкой: {ex.Message}");
2023-11-15 10:25:49 +04:00
}
}
}
2023-10-04 12:52:06 +04:00
}
}