PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/ServiceForm2.cs

289 lines
8.6 KiB
C#
Raw Normal View History

using ProjectCruiser.CollectionGenericObj;
using ProjectCruiser.DrawningSamples;
using Microsoft.Extensions.Logging;
2024-06-16 19:46:37 +04:00
using ProjectCruiser.Exceptions;
// using NLog.Extensions.Logging;
namespace ProjectCruiser;
public partial class ServiceForm2 : Form
{
// Компания
2024-06-15 09:05:36 +04:00
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningBase> _storageCollection;
// Логер
private readonly ILogger _logger;
// Конструктор > logger
public ServiceForm2(ILogger<ServiceForm2> logger)
{
InitializeComponent();
2024-06-15 09:05:36 +04:00
_storageCollection = new();
_logger = logger;
2024-06-16 19:46:37 +04:00
_logger.LogInformation("> Form is loaded successfully");
}
// Выбор компании
private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
2024-06-15 09:05:36 +04:00
toolPanel.Enabled = false;
}
// Добавление корабля
private void btnAddTransport_Click(object sender, EventArgs e)
{
EditorForm3 form3 = new();
form3.AddEvent(CreateObject);
form3.Show();
}
// Создание объекта класса-перемещения
private void CreateObject(DrawningBase? ship)
{
2024-06-16 19:46:37 +04:00
try
{
2024-06-16 19:46:37 +04:00
if (_company == null || ship == null)
{
throw new NullReferenceException(" > No existing collections to save");
}
int count = _company + ship;
MessageBox.Show("> Object was added");
pictureBox.Image = _company.Show();
2024-06-16 19:46:37 +04:00
_logger.LogInformation("> Adding object succeed {ship} at {count} position", ship, count);
}
2024-06-16 19:46:37 +04:00
catch (Exception ex)
{
2024-06-16 19:46:37 +04:00
MessageBox.Show("[!] Failed to add object\n" + ex.Message);
_logger.LogError("< Error > : {Message}", ex.Message);
}
}
// Удаление объекта
2024-06-16 19:46:37 +04:00
private void btnRemoveShip_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)
|| _company == null) return;
if (MessageBox.Show("[*] Remove object: Are you sure?", "Remove",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
2024-06-16 19:46:37 +04:00
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
2024-06-16 19:46:37 +04:00
try
{
2024-06-16 19:46:37 +04:00
if (_company - pos != null)
{
MessageBox.Show("> Object was removed");
pictureBox.Image = _company.Show();
_logger.LogInformation("Object at " +
pos + "position was deleted successfully");
}
}
catch (Exception ex)
{
MessageBox.Show("[!] Failed to remove object");
_logger.LogError("< Error > : {Message}", ex.Message);
}
}
// Передача объекта в другую форму
private void btnChooseforTest_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
DrawningBase? ship = null;
int counter = 100;
while (ship == null)
{
try
{
ship = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}
if (ship == null)
{
return;
}
OceanForm1 form = new() { SetShip = ship };
form.ShowDialog();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
pictureBox.Image = _company.Show();
}
2024-06-15 09:05:36 +04:00
private void btnCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTxtBoxCName.Text) || (!rBtnList.Checked && !rBtnArray.Checked))
{
MessageBox.Show("Enter correct data or choose an option", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collType = CollectionType.None;
if (rBtnArray.Checked)
{
collType = CollectionType.Array;
}
else if (rBtnList.Checked)
{
collType = CollectionType.List;
}
2024-06-16 19:46:37 +04:00
try
{
_storageCollection.AddCollection(maskedTxtBoxCName.Text, collType);
_logger.LogInformation("Adding collection succeed : {Name}, {Type}", maskedTxtBoxCName.Text, collType);
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex.Message);
_logger.LogError("< Error > : {Message}", ex.Message);
}
2024-06-15 09:05:36 +04:00
RefreshListBoxItems();
}
private void btnCollectionDel_Click(object sender, EventArgs e)
{
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
{
MessageBox.Show("Collection was not choosed");
return;
2024-06-16 19:46:37 +04:00
} if (MessageBox.Show("Are you sure?", "Removing",
MessageBoxButtons.OK, MessageBoxIcon.Question)
!= DialogResult.OK) return;
try
{
_storageCollection.DelCollection(listBox.SelectedItem.ToString());
RefreshListBoxItems();
_logger.LogInformation("Removing collection succeed : {Name}", listBox.SelectedItem.ToString);
2024-06-15 09:05:36 +04:00
}
2024-06-16 19:46:37 +04:00
catch (NullReferenceException ex)
2024-06-15 09:05:36 +04:00
{
2024-06-16 19:46:37 +04:00
Console.WriteLine(ex.Message);
_logger.LogError("< Error > : {Message}", ex.Message);
2024-06-15 09:05:36 +04:00
}
}
private void RefreshListBoxItems()
{
listBox.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{
string? collName = _storageCollection.Keys?[i];
if (!string.IsNullOrEmpty(collName))
{
listBox.Items.Add(collName);
}
}
}
private void btnCreateCompany_Click(object sender, EventArgs e)
{
if (listBox.SelectedIndex < 0 || listBox.SelectedItem == null)
{
MessageBox.Show("Collection wasn't choosed");
return;
}
ICollectionGenObj<DrawningBase>? collection =
_storageCollection[listBox.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Collection wasn't initialized");
return;
}
switch (comboBoxArrList.Text)
{
case "Storage":
_company = new ShipSharingService(pictureBox.Width,
2024-06-16 19:46:37 +04:00
pictureBox.Height, collection);
2024-06-15 09:05:36 +04:00
break;
}
toolPanel.Enabled = true; // block of buttons at the right bottom
RefreshListBoxItems();
}
2024-06-15 13:28:42 +04:00
// saving to file
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
2024-06-15 13:28:42 +04:00
{
_storageCollection.SaveData(saveFileDialog.FileName);
2024-06-15 13:28:42 +04:00
MessageBox.Show(" < Saved succesfully >",
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Saving to file : {filename}", saveFileDialog.FileName);
2024-06-15 13:28:42 +04:00
}
catch (Exception ex)
2024-06-15 13:28:42 +04:00
{
MessageBox.Show(ex.Message, "Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("< Error > : {Message}", ex.Message);
2024-06-15 13:28:42 +04:00
}
}
}
// loading from file
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
2024-06-15 13:28:42 +04:00
{
_storageCollection.LoadData(openFileDialog.FileName);
2024-06-16 19:46:37 +04:00
// LoadData() : Exceptions
// FileNotFoundException
// NullReferenceException
// InvalidDataException
// IndexOutOfRangeException
// CollectionOverflowException
2024-06-15 13:28:42 +04:00
MessageBox.Show(" < Loaded succesfully >",
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
2024-06-16 19:46:37 +04:00
_logger.LogInformation("Loading from file : {Filename}", openFileDialog.FileName);
2024-06-15 13:28:42 +04:00
RefreshListBoxItems();
}
catch (Exception ex)
2024-06-15 13:28:42 +04:00
{
MessageBox.Show("< Failed to load >" + ex.Message,
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("< Error > : {Message}", ex.Message);
2024-06-15 13:28:42 +04:00
}
}
}
}