2024-03-13 19:49:07 +04:00
|
|
|
|
using ProjectElectroTrans.CollectionGenericObjects;
|
|
|
|
|
using ProjectElectroTrans.Drawnings;
|
2024-04-24 23:26:19 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ProjectElectroTrans.Exceptions;
|
2024-03-13 19:49:07 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectElectroTrans;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Форма работы с компанией и ее коллекцией
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormTransCollection : Form
|
|
|
|
|
{
|
2024-03-28 00:10:11 +04:00
|
|
|
|
/// <summary>
|
2024-04-10 19:18:28 +04:00
|
|
|
|
/// Хранилише коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly StorageCollection<DrawingTrans> _storageCollection;
|
|
|
|
|
|
2024-03-13 19:49:07 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Компания
|
|
|
|
|
/// </summary>
|
|
|
|
|
private AbstractCompany? _company = null;
|
|
|
|
|
|
2024-04-24 23:26:19 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2024-03-13 19:49:07 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
2024-04-24 23:26:19 +04:00
|
|
|
|
public FormTransCollection(ILogger<FormTransCollection> logger)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-03-28 00:10:11 +04:00
|
|
|
|
_storageCollection = new();
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger = logger;
|
2024-03-13 19:49:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выбор компании
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
switch (comboBoxSelectorCompany.Text)
|
|
|
|
|
{
|
|
|
|
|
case "Хранилище":
|
2024-04-10 19:18:28 +04:00
|
|
|
|
_company = new TransDepoService(pictureBox.Width, pictureBox.Height,
|
|
|
|
|
new MassiveGenericObjects<DrawingTrans>());
|
2024-03-13 19:49:07 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 19:18:28 +04:00
|
|
|
|
private void ButtonAddTrans_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
FormTransConfig form = new();
|
2024-03-13 19:49:07 +04:00
|
|
|
|
|
2024-04-10 19:18:28 +04:00
|
|
|
|
form.Show();
|
|
|
|
|
form.AddEvent(SetTrans);
|
|
|
|
|
}
|
2024-03-13 19:49:07 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание объекта класса-перемещения
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">Тип создаваемого объекта</param>
|
2024-04-10 19:18:28 +04:00
|
|
|
|
private void SetTrans(DrawingTrans? drawingTrans)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
2024-04-10 19:18:28 +04:00
|
|
|
|
if (_company == null || drawingTrans == null)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-24 23:26:19 +04:00
|
|
|
|
try
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
var res = _company + drawingTrans;
|
2024-03-13 19:49:07 +04:00
|
|
|
|
MessageBox.Show("Объект добавлен");
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation($"Объект добавлен под индексом {res}");
|
2024-03-13 19:49:07 +04:00
|
|
|
|
pictureBox.Image = _company.Show();
|
|
|
|
|
}
|
2024-04-24 23:26:19 +04:00
|
|
|
|
catch (Exception ex)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
MessageBox.Show($"Объект не добавлен: {ex.Message}", "Результат", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
2024-03-13 19:49:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonRemoveTrans_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 19:18:28 +04:00
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) !=
|
|
|
|
|
DialogResult.Yes)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
2024-04-24 23:26:19 +04:00
|
|
|
|
try
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
var res = _company - pos;
|
2024-03-13 19:49:07 +04:00
|
|
|
|
MessageBox.Show("Объект удален");
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation($"Объект удален под индексом {pos}");
|
2024-03-13 19:49:07 +04:00
|
|
|
|
pictureBox.Image = _company.Show();
|
|
|
|
|
}
|
2024-04-24 23:26:19 +04:00
|
|
|
|
catch (Exception ex)
|
2024-03-13 19:49:07 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Не удалось удалить объект",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
2024-03-13 19:49:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Передача объекта в другую форму
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonGoToCheck_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_company == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawingTrans? car = null;
|
|
|
|
|
int counter = 100;
|
|
|
|
|
while (car == null)
|
|
|
|
|
{
|
|
|
|
|
car = _company.GetRandomObject();
|
|
|
|
|
counter--;
|
|
|
|
|
if (counter <= 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (car == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FormElectroTrans form = new()
|
|
|
|
|
{
|
2024-03-28 00:10:11 +04:00
|
|
|
|
SetTrans = car
|
2024-03-13 19:49:07 +04:00
|
|
|
|
};
|
|
|
|
|
form.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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 00:10:11 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-04-10 19:18:28 +04:00
|
|
|
|
/// Добавление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
|
2024-03-28 00:10:11 +04:00
|
|
|
|
{
|
2024-04-10 19:18:28 +04:00
|
|
|
|
if (string.IsNullOrEmpty(textBoxCollectionName.Text) ||
|
|
|
|
|
(!radioButtonList.Checked && !radioButtonMassive.Checked))
|
2024-03-28 00:10:11 +04:00
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CollectionType collectionType = CollectionType.None;
|
|
|
|
|
if (radioButtonMassive.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.Massive;
|
|
|
|
|
}
|
|
|
|
|
else if (radioButtonList.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.List;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 23:26:19 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
|
|
|
|
_logger.LogInformation("Добавление коллекции");
|
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 00:10:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonCollectionDel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Коллекция не выбрана");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-10 19:18:28 +04:00
|
|
|
|
|
|
|
|
|
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) !=
|
|
|
|
|
DialogResult.Yes)
|
2024-03-28 00:10:11 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-10 19:18:28 +04:00
|
|
|
|
|
2024-03-28 00:10:11 +04:00
|
|
|
|
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation("Коллекция удалена");
|
2024-03-28 00:10:11 +04:00
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 19:18:28 +04:00
|
|
|
|
ICollectionGenericObjects<DrawingTrans>? collection =
|
|
|
|
|
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
2024-03-28 00:10:11 +04:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Коллекция не проинициализирована");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (comboBoxSelectorCompany.Text)
|
|
|
|
|
{
|
|
|
|
|
case "Хранилище":
|
|
|
|
|
_company = new TransDepoService(pictureBox.Width, pictureBox.Height, collection);
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation("Компания создана");
|
2024-03-28 00:10:11 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
panelCompanyTools.Enabled = true;
|
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
}
|
2024-04-10 21:03:23 +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-04-24 23:26:19 +04:00
|
|
|
|
try
|
2024-04-10 21:03:23 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
2024-04-10 21:03:23 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation("Сохранение в файл: {filename}",
|
|
|
|
|
saveFileDialog.FileName);
|
2024-04-10 21:03:23 +04:00
|
|
|
|
}
|
2024-04-24 23:26:19 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-10 21:03:23 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат",
|
2024-04-10 21:03:23 +04:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
2024-04-10 21:03:23 +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)
|
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
try
|
2024-04-10 21:03:23 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_storageCollection.LoadData(openFileDialog.FileName);
|
2024-04-10 21:03:23 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2024-04-24 23:26:19 +04:00
|
|
|
|
_logger.LogInformation($"Загрузка прошла успешно в {openFileDialog.FileName}");
|
|
|
|
|
|
2024-04-10 21:03:23 +04:00
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
}
|
2024-04-24 23:26:19 +04:00
|
|
|
|
catch (Exception ex)
|
2024-04-10 21:03:23 +04:00
|
|
|
|
{
|
2024-04-24 23:26:19 +04:00
|
|
|
|
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-04-10 21:03:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 19:49:07 +04:00
|
|
|
|
}
|