363 lines
13 KiB
C#
363 lines
13 KiB
C#
using System.Numerics;
|
||
using Microsoft.Extensions.Logging;
|
||
using ProjectAirplaneWithRadar.CollectionGenericObjects;
|
||
using ProjectAirplaneWithRadar.Drawnings;
|
||
using ProjectAirplaneWithRadar.Exceptions;
|
||
|
||
namespace ProjectAirplaneWithRadar
|
||
{
|
||
/// <summary>
|
||
/// Форма работы с компанией и ее коллекцией
|
||
/// </summary>
|
||
public partial class FormAirplaneCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Хранилише коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawningAirplane> _storageCollection;
|
||
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormAirplaneCollection(ILogger<FormAirplaneCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new();
|
||
_logger = logger;
|
||
_logger.LogInformation("Форма загрузилась");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Выбор компании
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
panelCompanyTools.Enabled = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление самолета
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddAirplane_Click(object sender, EventArgs e)
|
||
{
|
||
FormAirplaneConfig form = new();
|
||
form.Show();
|
||
form.AddEvent(SetAirplane);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление самолета в коллекцию
|
||
/// </summary>
|
||
/// <param name="airplane"></param>
|
||
private void SetAirplane(DrawningAirplane airplane)
|
||
{
|
||
if (_company == null || airplane == null)
|
||
return;
|
||
|
||
try
|
||
{
|
||
if (_company + airplane != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBox.Image = _company.Show();
|
||
_logger.LogInformation("Добавлен объект: " + airplane.GetDataForSave());
|
||
}
|
||
}
|
||
catch (CollectionOverflowException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
catch (ObjectNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRemoveAirplane_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||
{
|
||
return;
|
||
}
|
||
|
||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||
|
||
try
|
||
{
|
||
if (_company - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBox.Image = _company.Show();
|
||
_logger.LogInformation("Удален объект по позиции " + pos);
|
||
}
|
||
} catch (PositionOutOfCollectionException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
} catch (ObjectNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Передача объекта в другую форму
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void buttonGoToCheck_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DrawningAirplane? plane = null;
|
||
int counter = 100;
|
||
while (plane == null)
|
||
{
|
||
try
|
||
{
|
||
plane = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
catch (PositionOutOfCollectionException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
catch (ObjectNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
}
|
||
|
||
if (plane == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormAirplaneWithRadar form = new()
|
||
{
|
||
SetAirplane = plane
|
||
};
|
||
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
//CollectionType collectionType = CollectionType.None;
|
||
//if (radioButtonMassive.Checked)
|
||
// collectionType = CollectionType.Massive;
|
||
//else if (radioButtonList.Checked)
|
||
// collectionType = CollectionType.List;
|
||
|
||
//_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||
//RefreshListBoxItems();
|
||
|
||
try
|
||
{
|
||
CollectionType collectionType = CollectionType.None;
|
||
if (radioButtonMassive.Checked)
|
||
{
|
||
collectionType = CollectionType.Massive;
|
||
}
|
||
else if (radioButtonList.Checked)
|
||
{
|
||
collectionType = CollectionType.List;
|
||
}
|
||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||
RefreshListBoxItems();
|
||
_logger.LogInformation("Коллекция добавлена " + textBoxCollectionName.Text);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollectionDel_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
//if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
//{
|
||
// return;
|
||
//}
|
||
|
||
//_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||
//RefreshListBoxItems();
|
||
|
||
try
|
||
{
|
||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||
RefreshListBoxItems();
|
||
_logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление списка в listBoxCollection
|
||
/// </summary>
|
||
private void RefreshListBoxItems()
|
||
{
|
||
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;
|
||
}
|
||
|
||
ICollectionGenericObjects<DrawningAirplane>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, collection);
|
||
break;
|
||
}
|
||
|
||
panelCompanyTools.Enabled = true;
|
||
RefreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия "Сохранение"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storageCollection.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обработка нажатия "Загрузка"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storageCollection.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
RefreshListBoxItems();
|
||
_logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName);
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |