2024-04-24 22:45:48 +04:00
|
|
|
|
using lab1.CollectionGenericObjects;
|
|
|
|
|
using lab1.Drawnings;
|
2024-08-29 23:07:11 +04:00
|
|
|
|
using lab1.Exceptions;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Collections.Generic;
|
2024-04-24 22:45:48 +04:00
|
|
|
|
|
|
|
|
|
namespace lab1;
|
|
|
|
|
/// <summary>
|
|
|
|
|
///Форма работы с компанией и её коллекцией
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormTrackedVehicleCollection : Form
|
|
|
|
|
{
|
2024-04-25 02:02:47 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly StorageCollection<DrawningTrackedVehicle> _storageCollection;
|
2024-08-29 23:07:11 +04:00
|
|
|
|
/// Логер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2024-04-24 22:45:48 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Компания
|
|
|
|
|
/// </summary>
|
|
|
|
|
private AbstractCompany? _company = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
2024-08-29 23:07:11 +04:00
|
|
|
|
public FormTrackedVehicleCollection(ILogger<FormTrackedVehicleCollection> logger)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-04-25 02:02:47 +04:00
|
|
|
|
_storageCollection = new();
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_logger.LogInformation("Форма загрузилась");
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выбор компании
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
|
|
|
|
private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-25 02:02:47 +04:00
|
|
|
|
panelCompanyTools.Enabled = false;
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление гусеничной машины
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
2024-08-29 19:32:46 +04:00
|
|
|
|
private void ButtonAddTrackedVehicle_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
FormTrackedVehicleConfig form = new();
|
|
|
|
|
form.Show();
|
|
|
|
|
// передать метод ✔
|
|
|
|
|
form.AddEvent(SetTrackedVehicle);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-24 22:45:48 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-29 19:32:46 +04:00
|
|
|
|
/// Добавление лодки в коллекцию
|
2024-04-24 22:45:48 +04:00
|
|
|
|
/// </summary>
|
2024-08-29 19:32:46 +04:00
|
|
|
|
/// <param name="boat"></param>
|
|
|
|
|
private void SetTrackedVehicle(DrawningTrackedVehicle trackedVehicle)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
if (_company == null || trackedVehicle == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_company + trackedVehicle != -1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
pictureBox.Image = _company.Show();
|
|
|
|
|
_logger.LogInformation("Добавлен объект:" + trackedVehicle.GetDataForSave);
|
|
|
|
|
}
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (ObjectNotFoundException) { }
|
|
|
|
|
catch (CollectionOverflowException ex)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-25 02:02:47 +04:00
|
|
|
|
|
2024-04-24 22:45:48 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonRemoveTrackedVehicle_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(maskedTextBox.Text);
|
2024-08-29 23:07:11 +04:00
|
|
|
|
int tempSize = TrackedVehicleSharingService.getAmountOfObjects();
|
|
|
|
|
try
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
if (_company - pos != null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
pictureBox.Image = _company.Show();
|
|
|
|
|
_logger.LogInformation("Удален объект по позиции" + pos);
|
|
|
|
|
}
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Передача объекта в другую форму
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_company == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DrawningTrackedVehicle? fighter = null;
|
|
|
|
|
int counter = 100;
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
|
|
|
|
while (fighter == null)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
fighter = _company.GetRandomObject();
|
|
|
|
|
counter--;
|
|
|
|
|
if (counter <= 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
FormFighter form = new();
|
|
|
|
|
{
|
|
|
|
|
SetTrackedVehicle(fighter);
|
|
|
|
|
};
|
|
|
|
|
form.ShowDialog();
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-24 22:45:48 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перерисовка коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonRefresh_Click_1(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_company == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pictureBox.Image = _company.Show();
|
|
|
|
|
}
|
|
|
|
|
private void pictureBox1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-25 02:02:47 +04:00
|
|
|
|
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 02:02:47 +04:00
|
|
|
|
|
2024-04-24 22:45:48 +04:00
|
|
|
|
|
|
|
|
|
private void FormTrackedVehicleCollection_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 02:02:47 +04:00
|
|
|
|
private void radioButtonMassive_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonCollectionAdd_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-04-25 02:02:47 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
CollectionType collectionType = CollectionType.None;
|
|
|
|
|
if (radioButtonMassive.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.Massive;
|
|
|
|
|
}
|
|
|
|
|
else if (radioButtonList.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.List;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
_logger.LogInformation("Коллекция добавлена" + textBoxCollectionName.Text);
|
2024-04-25 02:02:47 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-25 02:02:47 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-04-25 02:02:47 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonCollectionDel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// TODO прописать логику удаления элемента из коллекции
|
|
|
|
|
// нужно убедиться, что есть выбранная коллекция
|
|
|
|
|
// спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись
|
|
|
|
|
// удалить и обновить ListBox
|
2024-08-29 23:07:11 +04:00
|
|
|
|
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
2024-04-25 02:02:47 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
MessageBox.Show("Коллекция не выбрана");
|
2024-04-25 02:02:47 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
_logger.LogInformation("Коллекция:" + listBoxCollection.SelectedItem.ToString() + "удалена");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
|
|
|
|
}
|
2024-04-25 02:02:47 +04:00
|
|
|
|
}
|
2024-08-29 19:32:46 +04:00
|
|
|
|
|
2024-04-25 02:02:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление списка в listBoxCollection
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RerfreshListBoxItems()
|
|
|
|
|
{
|
|
|
|
|
listBoxCollection.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
string? colName = _storageCollection.Keys?[i];
|
|
|
|
|
if (!string.IsNullOrEmpty(colName))
|
|
|
|
|
{
|
|
|
|
|
listBoxCollection.Items.Add(colName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание компании
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void button1CreateCompany_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Коллекция не выбрана");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ICollectionGenericObjects<DrawningTrackedVehicle>? collection = _storageCollection[listBoxCollection.SelectedItem?.ToString() ?? string.Empty];
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Коллекция не проинициализирована");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
switch (comboBoxSelectorCompany.Text)
|
|
|
|
|
{
|
|
|
|
|
case "Хранилище":
|
|
|
|
|
_company = new TrackedVehicleSharingService(pictureBox.Width, pictureBox.Height, collection);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
panelCompanyTools.Enabled = true;
|
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-29 19:32:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Загрузка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// TODO продумать логику
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_storageCollection.LoadData(openFileDialog.FileName);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
RerfreshListBoxItems();
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (Exception ex)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат",
|
2024-08-29 19:32:46 +04:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
2024-08-29 19:32:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Сохранение"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
try
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
catch (Exception ex)
|
2024-08-29 19:32:46 +04:00
|
|
|
|
{
|
2024-08-29 23:07:11 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат",
|
2024-08-29 19:32:46 +04:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-08-29 23:07:11 +04:00
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
2024-08-29 23:07:11 +04:00
|
|
|
|
|
2024-08-29 19:32:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-24 22:45:48 +04:00
|
|
|
|
}
|