2024-05-13 22:40:26 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ProjectCruiser.CollectionGenericObjects;
|
2024-03-21 10:52:11 +04:00
|
|
|
|
using ProjectCruiser.Drawnings;
|
2024-05-14 23:17:44 +04:00
|
|
|
|
using System.Windows.Forms;
|
2024-03-21 10:52:11 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectCruiser
|
|
|
|
|
{
|
|
|
|
|
public partial class FormCruisersCollection : Form
|
|
|
|
|
{
|
2024-04-04 11:05:33 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранилише коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly StorageCollection<DrawningCruiser> _storageCollection;
|
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Компания
|
|
|
|
|
/// </summary>
|
|
|
|
|
private AbstractCompany? _company = null;
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
2024-05-13 22:40:26 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
2024-05-13 22:40:26 +04:00
|
|
|
|
public FormCruisersCollection(ILogger<FormCruisersCollection> logger)
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-04-04 11:05:33 +04:00
|
|
|
|
_storageCollection = new();
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger = logger;
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-04 11:05:33 +04:00
|
|
|
|
panelCompanyTools.Enabled = false;
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 22:40:26 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// добавление крейсера
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
2024-05-13 12:59:51 +04:00
|
|
|
|
private void ButtonAddCruiser_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
FormCruiserConfing form = new();
|
|
|
|
|
// TODO передать метод
|
|
|
|
|
|
|
|
|
|
form.Show();
|
|
|
|
|
form.AddEvent(SetCruiser);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// <summary>
|
2024-05-13 12:59:51 +04:00
|
|
|
|
/// Добавление крейсера в коллекцию
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// </summary>
|
2024-05-13 12:59:51 +04:00
|
|
|
|
/// <param name="cruiser"></param>
|
|
|
|
|
private void SetCruiser(DrawningCruiser? cruiser)
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
2024-05-13 12:59:51 +04:00
|
|
|
|
if (_company == null || cruiser == null)
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
try
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
var res = _company + cruiser;
|
2024-04-04 11:05:33 +04:00
|
|
|
|
MessageBox.Show("Объект добавлен");
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger.LogInformation($"Объект добавлен под индексом {res}");
|
2024-03-21 10:52:11 +04:00
|
|
|
|
pictureBoxCruiser.Image = _company.Show();
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
catch (Exception ex)
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show($"Объект не добавлен: {ex.Message}", "Результат", MessageBoxButtons.OK,
|
|
|
|
|
MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-13 12:59:51 +04:00
|
|
|
|
/// Удаление объекта
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// </summary>
|
2024-05-13 12:59:51 +04:00
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
2024-03-21 10:52:11 +04:00
|
|
|
|
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);
|
2024-05-13 22:40:26 +04:00
|
|
|
|
try
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
var res = _company - pos;
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
_logger.LogInformation($"Объект удален под индексом {pos}");
|
2024-03-21 10:52:11 +04:00
|
|
|
|
pictureBoxCruiser.Image = _company.Show();
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
catch (Exception ex)
|
2024-03-21 10:52:11 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Не удалось удалить объект",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 11:05:33 +04:00
|
|
|
|
/// <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))
|
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2024-04-04 11:05:33 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CollectionType collectionType = CollectionType.None;
|
|
|
|
|
if (radioButtonMassive.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.Massive;
|
|
|
|
|
}
|
|
|
|
|
else if (radioButtonList.Checked)
|
|
|
|
|
{
|
|
|
|
|
collectionType = CollectionType.List;
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
|
2024-05-14 23:17:44 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(textBoxCollectionName.Text, collectionType, string.Empty);
|
|
|
|
|
|
|
|
|
|
_storageCollection.AddCollection(collectionInfo);
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger.LogInformation("Добавление коллекции");
|
|
|
|
|
RerfreshListBoxItems();
|
2024-04-04 11:05:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2024-05-14 23:17:44 +04:00
|
|
|
|
|
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
|
|
|
|
|
|
|
|
|
_storageCollection.DelCollection(collectionInfo);
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger.LogInformation("Коллекция удалена");
|
2024-04-04 11:05:33 +04:00
|
|
|
|
RerfreshListBoxItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление списка в listBoxCollection
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RerfreshListBoxItems()
|
|
|
|
|
{
|
|
|
|
|
listBoxCollection.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
|
|
|
|
{
|
2024-05-14 23:17:44 +04:00
|
|
|
|
string? colName = _storageCollection.Keys?[i].Name;
|
2024-04-04 11:05:33 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(colName))
|
|
|
|
|
{
|
|
|
|
|
listBoxCollection.Items.Add(colName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-13 22:40:26 +04:00
|
|
|
|
/// Создание компании
|
2024-04-04 11:05:33 +04:00
|
|
|
|
/// </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;
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
|
2024-05-14 23:17:44 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty);
|
|
|
|
|
ICollectionGenericObjects<DrawningCruiser>? collection = _storageCollection[collectionInfo];
|
2024-04-04 11:05:33 +04:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Коллекция не проинициализирована");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
|
2024-04-04 11:05:33 +04:00
|
|
|
|
switch (comboBoxSelectorCompany.Text)
|
|
|
|
|
{
|
|
|
|
|
case "Хранилище":
|
|
|
|
|
_company = new CruiserDockingService(pictureBoxCruiser.Width, pictureBoxCruiser.Height, collection);
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger.LogInformation("Компания создана");
|
2024-04-04 11:05:33 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
panelCompanyTools.Enabled = true;
|
2024-05-14 23:17:44 +04:00
|
|
|
|
RerfreshListBoxItems();
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 12:59:51 +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)
|
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
try
|
2024-05-13 12:59:51 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
catch (Exception ex)
|
2024-05-13 12:59:51 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-21 10:52:11 +04:00
|
|
|
|
|
2024-05-13 12:59:51 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Загрузка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
try
|
2024-05-13 12:59:51 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
_storageCollection.LoadData(openFileDialog.FileName);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
RerfreshListBoxItems();
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
_logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
}
|
2024-05-13 22:40:26 +04:00
|
|
|
|
catch (Exception ex)
|
2024-05-13 12:59:51 +04:00
|
|
|
|
{
|
2024-05-13 22:40:26 +04:00
|
|
|
|
MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
2024-05-13 12:59:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-14 23:17:44 +04:00
|
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
|
}
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|