PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/FormLocomotiveCollection.cs

356 lines
11 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using ProjectElectricLocomotive.CollectionGenericObjects;
2024-03-14 14:26:53 +04:00
using ProjectElectricLocomotive.Drawnings;
using ProjectElectricLocomotive.Exceptions;
2024-03-14 14:26:53 +04:00
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;
2024-03-14 14:26:53 +04:00
namespace ProjectElectricLocomotive;
/// <summary>
/// Форма работы с компанией и ее коллекцией
/// </summary>
public partial class FormLocomotiveCollection : Form
{
2024-03-28 09:57:08 +04:00
/// <summary>
/// Хранилище коллекций
/// </summary>
private readonly StorageCollection<DrawningLocomotive> _storageCollection;
/// <summary>
/// Логер
/// </summary>
private readonly ILogger _logger;
2024-03-14 14:26:53 +04:00
/// <summary>
/// Компания
/// </summary>
private AbstractCompany? _company = null;
/// <summary>
/// Конструктор
/// </summary>
public FormLocomotiveCollection(ILogger<FormLocomotiveCollection> logger)
2024-03-14 14:26:53 +04:00
{
InitializeComponent();
2024-03-28 09:57:08 +04:00
_storageCollection = new();
_logger = logger;
_logger.LogInformation("Форма загрузилась");
2024-03-14 14:26:53 +04:00
}
/// <summary>
/// Выбор компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
2024-03-28 09:57:08 +04:00
panelCompanyTools.Enabled = false;
2024-03-14 14:26:53 +04:00
}
2024-04-01 20:21:57 +04:00
2024-04-25 17:03:43 +04:00
2024-04-01 20:21:57 +04:00
2024-03-14 14:26:53 +04:00
/// <summary>
2024-04-01 20:21:57 +04:00
/// Добавление локомотива
2024-03-14 14:26:53 +04:00
/// </summary>
2024-04-01 20:21:57 +04:00
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddLocomotive_Click(object sender, EventArgs e)
2024-03-14 14:26:53 +04:00
{
2024-04-01 20:21:57 +04:00
FormElectricLocomotiveConfig form = new();
//TODO передать метод
form.Show();
2024-04-09 18:48:06 +04:00
form.AddEvent(SetLocomotive);
form.Show();
2024-04-01 20:21:57 +04:00
}
2024-03-28 09:57:08 +04:00
2024-04-01 20:21:57 +04:00
/// <summary>
/// Добавление автомобиля в коллекцию
/// </summary>
/// <param name="locomotive"></param>
2024-04-09 18:48:06 +04:00
private void SetLocomotive(DrawningLocomotive? locomotive)
2024-04-01 20:21:57 +04:00
{
try
2024-04-01 20:21:57 +04:00
{
if (_company == null || locomotive == null)
{
return;
}
if (_company + locomotive != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
_logger.LogInformation("Добавлен объект: " + locomotive.GetDataForSave());
}
2024-03-14 14:26:53 +04:00
}
catch (ObjectNotFoundException) { }
catch (CollectionOverflowException ex)
2024-03-14 14:26:53 +04:00
{
//MessageBox.Show("Не удалось добавить объект");
MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", ex.Message);
2024-03-14 14:26:53 +04:00
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDelLocomotive_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
2024-03-14 14:26:53 +04:00
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
int pos = Convert.ToInt32(maskedTextBox.Text);
try
{
if (_company - pos != null)
2024-03-14 14:26:53 +04:00
{
MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show();
_logger.LogInformation("Удален объект по позиции " + pos);
2024-03-14 14:26:53 +04:00
}
}
catch (Exception ex)
{
//MessageBox.Show("Не удалось удалить объект");
MessageBox.Show(ex.Message);
_logger.LogError("Ошибка: {Message}", ex.Message);
}
2024-03-14 14:26:53 +04:00
}
/// <summary>
/// Перерисовка коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonGoToCheck_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
DrawningLocomotive? locomotive = null;
int counter = 100;
try
2024-03-14 14:26:53 +04:00
{
while (locomotive == null)
2024-03-14 14:26:53 +04:00
{
locomotive = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
2024-03-14 14:26:53 +04:00
}
FormlectricLocomotive form = new()
{
SetLocomotive = locomotive
};
form.ShowDialog();
2024-03-14 14:26:53 +04:00
}
catch (Exception ex)
2024-03-14 14:26:53 +04:00
{
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-03-14 14:26:53 +04:00
}
}
/// <summary>
/// Передача объекта в другую форму
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRefresh_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
pictureBox.Image = _company.Show();
}
2024-03-28 09:57:08 +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 buttonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningLocomotive>? collection =
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
2024-04-01 17:28:49 +04:00
case "Депо":
2024-03-28 09:57:08 +04:00
_company = new LocomotiveDepo(pictureBox.Width,
pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
2024-03-14 14:26:53 +04:00
2024-03-28 09:57:08 +04:00
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCollectionDel_Click(object sender, EventArgs e)
{
2024-04-01 17:28:49 +04:00
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
try
2024-04-01 17:28:49 +04:00
{
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
_logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена");
}
catch (Exception ex)
{
_logger.LogError("Ошибка: {Message}", ex.Message);
2024-04-01 17:28:49 +04:00
}
2024-03-28 09:57:08 +04:00
}
/// <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);
2024-03-28 09:57:08 +04:00
return;
}
try
2024-03-28 09:57:08 +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-03-28 09:57:08 +04:00
}
catch (Exception ex)
2024-03-28 09:57:08 +04:00
{
_logger.LogError("Ошибка: {Message}", ex.Message);
2024-03-28 09:57:08 +04:00
}
}
2024-04-25 17:03:43 +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)
{
try
2024-04-25 17:03:43 +04:00
{
_storageCollection.SaveData(saveFileDialog.FileName);
2024-04-25 17:03:43 +04:00
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
2024-04-25 17:03:43 +04:00
}
catch (Exception ex)
2024-04-25 17:03:43 +04:00
{
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Ошибка: {Message}", ex.Message);
2024-04-25 17:03:43 +04:00
}
2024-04-25 17:03:43 +04:00
}
2024-04-25 17:03:43 +04:00
}
/// <summary>
/// Обработка нажатия "Загрузка"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
2024-04-25 17:03:43 +04:00
{
_storageCollection.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
2024-04-25 17:03:43 +04:00
RerfreshListBoxItems();
_logger.LogInformation("Сохранение в файл: {filename}", openFileDialog.FileName);
2024-04-25 17:03:43 +04:00
}
catch (Exception ex)
2024-04-25 17:03:43 +04:00
{
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Ошибка: {Message}", ex.Message);
2024-04-25 17:03:43 +04:00
}
}
}
2024-03-28 09:57:08 +04:00
}