340 lines
12 KiB
C#
340 lines
12 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectCruiser.CollectionGenericObjects;
|
||
using ProjectCruiser.Drawnings;
|
||
using System.Windows.Forms;
|
||
|
||
namespace ProjectCruiser
|
||
{
|
||
public partial class FormCruisersCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Хранилише коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawningCruiser> _storageCollection;
|
||
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Логер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormCruisersCollection(ILogger<FormCruisersCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new();
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e)
|
||
{
|
||
panelCompanyTools.Enabled = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// добавление крейсера
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddCruiser_Click(object sender, EventArgs e)
|
||
{
|
||
FormCruiserConfing form = new();
|
||
// TODO передать метод
|
||
|
||
form.Show();
|
||
form.AddEvent(SetCruiser);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление крейсера в коллекцию
|
||
/// </summary>
|
||
/// <param name="cruiser"></param>
|
||
private void SetCruiser(DrawningCruiser? cruiser)
|
||
{
|
||
if (_company == null || cruiser == null)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
var res = _company + cruiser;
|
||
MessageBox.Show("Объект добавлен");
|
||
_logger.LogInformation($"Объект добавлен под индексом {res}");
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Объект не добавлен: {ex.Message}", "Результат", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление объекта
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRemoveCruiser_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBoxPosision.Text) || _company == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("удалить объект?", "удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(maskedTextBoxPosision.Text);
|
||
try
|
||
{
|
||
var res = _company - pos;
|
||
MessageBox.Show("Объект удален");
|
||
_logger.LogInformation($"Объект удален под индексом {pos}");
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Не удалось удалить объект",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
||
}
|
||
}
|
||
|
||
private void ButtonGetToTest_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DrawningCruiser? cruiser = null;
|
||
int counter = 100;
|
||
while (cruiser == null)
|
||
{
|
||
cruiser = _company.GetRandomObject();
|
||
counter--;
|
||
if (counter <= 0)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
if (cruiser == null)
|
||
{
|
||
return;
|
||
}
|
||
FormCruiser form = new()
|
||
{
|
||
SetCruiser = cruiser
|
||
};
|
||
form.ShowDialog();
|
||
|
||
}
|
||
|
||
private void ButtonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollecctionAdd_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;
|
||
}
|
||
|
||
CollectionInfo collectionInfo = new CollectionInfo(textBoxCollectionName.Text, collectionType, string.Empty);
|
||
|
||
_storageCollection.AddCollection(collectionInfo);
|
||
_logger.LogInformation("Добавление коллекции");
|
||
RerfreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаленние коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollectionDel_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
|
||
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
||
|
||
_storageCollection.DelCollection(collectionInfo);
|
||
_logger.LogInformation("Коллекция удалена");
|
||
RerfreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление списка в listBoxCollection
|
||
/// </summary>
|
||
private void RerfreshListBoxItems()
|
||
{
|
||
listBoxCollection.Items.Clear();
|
||
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
||
{
|
||
string? colName = _storageCollection.Keys?[i].Name;
|
||
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;
|
||
}
|
||
|
||
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
||
ICollectionGenericObjects<DrawningCruiser>? collection = _storageCollection[collectionInfo];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new CruiserDockingService(pictureBoxCruiser.Width, pictureBoxCruiser.Height, collection);
|
||
_logger.LogInformation("Компания создана");
|
||
break;
|
||
}
|
||
panelCompanyTools.Enabled = true;
|
||
RerfreshListBoxItems();
|
||
|
||
}
|
||
|
||
/// <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);
|
||
RerfreshListBoxItems();
|
||
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Сортировка по типу
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonSortByType_Click(object sender, EventArgs e)
|
||
{
|
||
CompareCruisers(new DrawningCruiserCompareByType());
|
||
}
|
||
|
||
/// <summary>
|
||
/// Сортировка по цвету
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e)
|
||
{
|
||
CompareCruisers(new DrawningCruiserCompareByColor());
|
||
}
|
||
|
||
/// <summary>
|
||
/// Сортировка по сравнителю
|
||
/// </summary>
|
||
/// <param name="comparer">Сравнитель объектов</param>
|
||
private void CompareCruisers(IComparer<DrawningCruiser?> comparer)
|
||
{
|
||
if (_company == null)
|
||
{
|
||
return;
|
||
}
|
||
_company.Sort(comparer);
|
||
pictureBoxCruiser.Image = _company.Show();
|
||
}
|
||
}
|
||
}
|