2023-10-21 14:10:32 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection.Metadata.Ecma335;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Cruiser.Drawing;
|
|
|
|
|
using Cruiser.Generics;
|
|
|
|
|
using Cruiser.MovementStrategy;
|
2023-12-18 15:13:53 +04:00
|
|
|
|
using Cruiser.Exceptions;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Xml.Linq;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
|
|
|
|
|
namespace Cruiser
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Форма для работы с набором объектов класса DrawingCruiser
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormCruiserCollection : Form
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Набор объектов
|
|
|
|
|
/// </summary>
|
2023-11-24 11:41:50 +04:00
|
|
|
|
private readonly CruisersGenericStorage _storage;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// <summary>
|
2023-12-18 15:13:53 +04:00
|
|
|
|
/// Логер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
/// <summary>
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
2023-12-18 15:13:53 +04:00
|
|
|
|
public FormCruiserCollection(ILogger<FormCruiserCollection> logger)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2023-11-24 11:41:50 +04:00
|
|
|
|
_storage = new CruisersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger = logger;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
2023-11-24 11:41:50 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Заполнение listBoxObjects
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReloadObjects()
|
|
|
|
|
{
|
|
|
|
|
int index = listBoxStorages.SelectedIndex;
|
|
|
|
|
listBoxStorages.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storage.Keys.Count; i++)
|
|
|
|
|
{
|
2023-12-18 16:26:08 +04:00
|
|
|
|
listBoxStorages.Items.Add(_storage.Keys[i].Name);
|
2023-11-24 11:41:50 +04:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора в коллекцию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonAddObject_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Придумайте имя набору", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(textBoxStorageName.Text);
|
|
|
|
|
ReloadObjects();
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonDelObject_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; //добавил для удаления повторяющегося кода
|
|
|
|
|
_storage.DelSet(name);
|
2023-11-24 11:41:50 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogInformation($"Удален набор: {name}");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выбор набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ListBoxObjects_SelectedIndexChanged(object sender,
|
|
|
|
|
EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCruiser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
2023-11-24 12:12:46 +04:00
|
|
|
|
private void AddCruiser(DrawingCruiser cruiser)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogWarning($"Добавление круизера не удалось (индекс вне границ)");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogWarning($"Добавление круизера не удалось (нет хранилища)");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-18 15:13:53 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if ((obj + cruiser))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
_logger.LogInformation($"Добавление круизера успешно {listBoxStorages.SelectedItem.ToString()}");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowCruiser();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
}
|
|
|
|
|
catch (ApplicationException ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonRemoveCar_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogWarning($"Удаление круизера не удалось (индекс вне границ)");
|
2023-10-21 14:10:32 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-24 15:30:37 +04:00
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogWarning($"Удаление круизера не удалось (нет хранилища)");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_logger.LogWarning($"Удаление круизера не удалось (выбран вариант 'Нет')");
|
2023-11-24 11:41:50 +04:00
|
|
|
|
return;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(textBoxNumber.Text);
|
2023-12-18 16:26:08 +04:00
|
|
|
|
try
|
2023-12-18 15:13:53 +04:00
|
|
|
|
{
|
|
|
|
|
if (obj - pos != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Удаление круизера успешно {listBoxStorages.SelectedItem.ToString()} {pos}");
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowCruiser();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Удаление круизера не удалось(обьект не найден)");
|
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
}
|
|
|
|
|
catch (CruiserNotFoundException ex)
|
2023-12-18 15:13:53 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Удаление круизера не удалось {ex.Message}");
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление рисунка по набору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowCruiser();
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
2023-11-24 12:12:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonAddCruiser_Click(object sender, EventArgs e)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-11-24 12:12:46 +04:00
|
|
|
|
var formCruiserConfig = new FormCruiserConfig();
|
|
|
|
|
formCruiserConfig.Show();
|
|
|
|
|
formCruiserConfig.AddEvent(AddCruiser);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
2023-12-03 15:02:10 +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)
|
|
|
|
|
{
|
|
|
|
|
if (_storage.SaveData(saveFileDialog.FileName))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Загрузка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (_storage.LoadData(openFileDialog.FileName))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сортировка по типу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByType());
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сортировка по цвету
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCruiser(new CruiserCompareByColor());
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сортировка по сравнителю
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="comparer"></param>
|
|
|
|
|
private void CompareCruiser(IComparer<DrawingCruiser?> comparer)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
obj.Sort(comparer);
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowCruiser();
|
|
|
|
|
}
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|