2023-12-05 19:23:48 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using RoadTrain.DrawningObjects;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
using RoadTrain.Generics;
|
2023-12-05 19:23:48 +04:00
|
|
|
|
using RoadTrain;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
using RoadTrain.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;
|
|
|
|
|
|
|
|
|
|
namespace RoadTrain
|
|
|
|
|
{
|
|
|
|
|
public partial class FormTrainCollection : Form
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
private readonly RoadTrainGenericStorage _storage;
|
2023-12-05 19:23:48 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
public FormTrainCollection(ILogger<FormTrainCollection> logger)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2023-10-24 18:58:46 +04:00
|
|
|
|
_storage = new RoadTrainGenericStorage(pictureBoxCollection.Width,
|
|
|
|
|
pictureBoxCollection.Height);
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger = logger;
|
2023-10-24 18:58:46 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Заполнение listBoxObjects
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReloadObjects()
|
|
|
|
|
{
|
|
|
|
|
int index = listBoxStorages.SelectedIndex;
|
|
|
|
|
listBoxStorages.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storage.Keys.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.Items.Add(_storage.Keys[i]);
|
|
|
|
|
}
|
|
|
|
|
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
|
|
|
|
|
>= listBoxStorages.Items.Count))
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
|
|
|
|
|
index < listBoxStorages.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.SelectedIndex = index;
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
2023-11-09 17:11:55 +04:00
|
|
|
|
private void AddTrain(DrawningRoadTrain train)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
2023-11-09 17:11:55 +04:00
|
|
|
|
train._pictureWidth = pictureBoxCollection.Width;
|
|
|
|
|
train._pictureHeight = pictureBoxCollection.Height;
|
2023-10-24 18:58:46 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-09 17:11:55 +04:00
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
2023-10-24 18:58:46 +04:00
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
try
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_ = obj + train;
|
2023-11-09 17:11:55 +04:00
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrains();
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogInformation($"Обьект добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
catch (StorageOverflowException ex)
|
2023-11-09 17:11:55 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
2023-11-09 17:11:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void ButtonAddTrain_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var formTrainConfig = new FormTrainConfig();
|
|
|
|
|
formTrainConfig.AddEvent(AddTrain);
|
|
|
|
|
formTrainConfig.Show();
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление",
|
2023-10-24 18:58:46 +04:00
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(InputTextBox.Text);
|
2023-12-05 19:23:48 +04:00
|
|
|
|
try
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
if (obj - pos != null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrains();
|
|
|
|
|
_logger.LogInformation($"Обьект удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось удалить обьект");
|
|
|
|
|
_logger.LogWarning($"Обьект не удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
catch (TrainNotFoundException ex)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning($"Обьект не найден: {ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrains();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonAddObject_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogWarning($"Добавление набора неуспешно Не все данные заполнены");
|
2023-10-24 18:58:46 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(textBoxStorageName.Text);
|
|
|
|
|
ReloadObjects();
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}");
|
2023-10-24 18:58:46 +04:00
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
|
2023-10-24 18:58:46 +04:00
|
|
|
|
private void ButtonDelObject_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogWarning($"Удаление набора неуспешно");
|
2023-10-24 18:58:46 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
|
|
|
|
if (MessageBox.Show($"Удалить объект {name}?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
2023-10-24 18:58:46 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_storage.DelSet(name);
|
2023-10-24 18:58:46 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogInformation($"Удален набор: {name}");
|
2023-10-24 18:58:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxCollection.Image =
|
|
|
|
|
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
2023-11-23 00:09:21 +04:00
|
|
|
|
|
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
try
|
2023-11-23 00:09:21 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_storage.SaveData(saveFileDialog.FileName);
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
_logger.LogInformation($"Сохранено в файл {saveFileDialog.FileName}");
|
2023-11-23 00:09:21 +04:00
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
catch (Exception ex)
|
2023-11-23 00:09:21 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
MessageBox.Show($"Не сохранено: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogWarning($"Сохранение в файл {saveFileDialog.FileName} не удалось");
|
2023-11-23 00:09:21 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
try
|
2023-11-23 00:09:21 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_storage.LoadData(openFileDialog.FileName);
|
|
|
|
|
MessageBox.Show("Загрузка прошла успешно", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-11-23 00:09:21 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-05 19:23:48 +04:00
|
|
|
|
_logger.LogInformation($"Загрузка из файла {openFileDialog.FileName}");
|
2023-11-23 00:09:21 +04:00
|
|
|
|
}
|
2023-12-05 19:23:48 +04:00
|
|
|
|
catch (Exception ex)
|
2023-11-23 00:09:21 +04:00
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
MessageBox.Show($"Не сохранено: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogWarning($"Загрузка из файла {openFileDialog.FileName} не удалось");
|
2023-11-23 00:09:21 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|