271 lines
10 KiB
C#
271 lines
10 KiB
C#
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;
|
||
using Microsoft.VisualBasic.Logging;
|
||
using AirBomber.Exceptions;
|
||
using System.Xml.Linq;
|
||
using System.Linq.Expressions;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace AirBomber
|
||
{
|
||
public partial class FormPlaneCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly PlanesGenericStorage _storage;
|
||
private readonly ILogger _logger;
|
||
public FormPlaneCollection(ILogger<FormPlaneCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new PlanesGenericStorage(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]);
|
||
}
|
||
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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonAddPlane_Click(object sender, EventArgs e)
|
||
{
|
||
var formPlaneConfig = new FormPlaneConfig();
|
||
// TODO DONE
|
||
formPlaneConfig.AddEvent(AddPlane);
|
||
formPlaneConfig.Show();
|
||
}
|
||
private void AddPlane(DrawningAirPlane plane)
|
||
{
|
||
plane._pictureWidth = pictureBoxCollection.Width;
|
||
plane._pictureHeight = pictureBoxCollection.Height;
|
||
|
||
if (listBoxStorages.SelectedIndex == -1) return;
|
||
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
_ = obj + plane;
|
||
MessageBox.Show("Объект добавлен");
|
||
_logger.LogInformation("Объект добавлен");
|
||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}");
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonRemovePlane_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;
|
||
}
|
||
try
|
||
{
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
_logger.LogInformation("Объект удален");
|
||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
_logger.LogWarning("Не удалось удалить объект");
|
||
}
|
||
}
|
||
catch (PlaneNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Неверный ввод");
|
||
_logger.LogWarning("Неверный ввод");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обновление рисунка по набору
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
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.ShowPlanes();
|
||
}
|
||
/// <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);
|
||
_logger.LogWarning("Не все данные заполнены");
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxStorageName.Text);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
||
}
|
||
private void buttonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
string Name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show($"Удалить набор {Name}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(Name);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удален набор: {Name}");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обработка нажатия "Сохранение"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Сохранение прошло успешно");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Не сохранилось: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обработка нажатия "Загрузка"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// TODO продумать логику DONE
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Загрузка прошла успешно");
|
||
ReloadObjects();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"Не загрузилось: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Сортировка по типу
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType());
|
||
/// <summary>
|
||
/// Сортировка по цвету
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor());
|
||
/// <summary>
|
||
/// Сортировка по сравнителю
|
||
/// </summary>
|
||
/// <param name="comparer"></param>
|
||
private void ComparePlanes(IComparer<DrawningAirPlane?> comparer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||
}
|
||
}
|
||
}
|