247 lines
9.4 KiB
C#
247 lines
9.4 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using RoadTrain.DrawningObjects;
|
||
using RoadTrain.Generics;
|
||
using RoadTrain.Exceptions;
|
||
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
|
||
{
|
||
private readonly RoadTrainGenericStorage _storage;
|
||
private readonly ILogger _logger;
|
||
public FormTrainCollection(ILogger<FormTrainCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new RoadTrainGenericStorage(pictureBoxCollection.Width,
|
||
pictureBoxCollection.Height);
|
||
_logger = logger;
|
||
}
|
||
/// <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].Name);
|
||
}
|
||
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;
|
||
}
|
||
}
|
||
private void AddTrain(DrawningRoadTrain train)
|
||
{
|
||
train._pictureWidth = pictureBoxCollection.Width;
|
||
train._pictureHeight = pictureBoxCollection.Height;
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
_ = obj + train;
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBoxCollection.Image = obj.ShowTrains();
|
||
_logger.LogInformation($"Обьект добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
catch (StorageOverflowException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
catch (ArgumentException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
private void ButtonAddTrain_Click(object sender, EventArgs e)
|
||
{
|
||
var formTrainConfig = new FormTrainConfig();
|
||
formTrainConfig.AddEvent(AddTrain);
|
||
formTrainConfig.Show();
|
||
}
|
||
|
||
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(InputTextBox.Text);
|
||
try
|
||
{
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBoxCollection.Image = obj.ShowTrains();
|
||
_logger.LogInformation($"Обьект удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить обьект");
|
||
_logger.LogWarning($"Обьект не удален из набора {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
catch (TrainNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"Обьект не найден: {ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
}
|
||
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||
{
|
||
{
|
||
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);
|
||
_logger.LogWarning($"Добавление набора неуспешно Не все данные заполнены");
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxStorageName.Text);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}");
|
||
}
|
||
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
_logger.LogWarning($"Удаление набора неуспешно");
|
||
return;
|
||
}
|
||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||
if (MessageBox.Show($"Удалить объект {name}?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удален набор: {name}");
|
||
}
|
||
}
|
||
|
||
private void ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image =
|
||
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
|
||
}
|
||
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"Сохранено в файл {saveFileDialog.FileName}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не сохранено: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Сохранение в файл {saveFileDialog.FileName} не удалось");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Загрузка из файла {openFileDialog.FileName}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не сохранено: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Загрузка из файла {openFileDialog.FileName} не удалось");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) =>
|
||
CompareTrains(new TrainCompareByType());
|
||
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) =>
|
||
CompareTrains(new TrainCompareByColor());
|
||
|
||
/// <summary>
|
||
/// Сортировка по сравнителю
|
||
/// </summary>
|
||
/// <param name="comparer"></param>
|
||
private void CompareTrains(IComparer<DrawningRoadTrain?> comparer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollection.Image = obj.ShowTrains();
|
||
}
|
||
|
||
}
|
||
}
|