289 lines
8.6 KiB
C#
289 lines
8.6 KiB
C#
using ProjectCruiser.CollectionGenericObj;
|
|
using ProjectCruiser.DrawningSamples;
|
|
using Microsoft.Extensions.Logging;
|
|
using ProjectCruiser.Exceptions;
|
|
// using NLog.Extensions.Logging;
|
|
|
|
namespace ProjectCruiser;
|
|
|
|
public partial class ServiceForm2 : Form
|
|
{
|
|
// Компания
|
|
private AbstractCompany? _company = null;
|
|
|
|
private readonly StorageCollection<DrawningBase> _storageCollection;
|
|
|
|
// Логер
|
|
private readonly ILogger _logger;
|
|
|
|
// Конструктор > logger
|
|
public ServiceForm2(ILogger<ServiceForm2> logger)
|
|
{
|
|
InitializeComponent();
|
|
_storageCollection = new();
|
|
_logger = logger;
|
|
_logger.LogInformation("> Form is loaded successfully");
|
|
}
|
|
|
|
// Выбор компании
|
|
private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
toolPanel.Enabled = false;
|
|
}
|
|
|
|
// Добавление корабля
|
|
private void btnAddTransport_Click(object sender, EventArgs e)
|
|
{
|
|
EditorForm3 form3 = new();
|
|
form3.AddEvent(CreateObject);
|
|
form3.Show();
|
|
}
|
|
|
|
// Создание объекта класса-перемещения
|
|
private void CreateObject(DrawningBase? ship)
|
|
{
|
|
try
|
|
{
|
|
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();
|
|
|
|
_logger.LogInformation("> Adding object succeed {ship} at {count} position", ship, count);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("[!] Failed to add object\n" + ex.Message);
|
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
}
|
|
}
|
|
|
|
// Удаление объекта
|
|
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;
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
|
|
|
try
|
|
{
|
|
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();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
RefreshListBoxItems();
|
|
}
|
|
|
|
private void btnCollectionDel_Click(object sender, EventArgs e)
|
|
{
|
|
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
|
|
{
|
|
MessageBox.Show("Collection was not choosed");
|
|
return;
|
|
} 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);
|
|
}
|
|
catch (NullReferenceException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
}
|
|
}
|
|
|
|
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,
|
|
pictureBox.Height, collection);
|
|
break;
|
|
}
|
|
|
|
toolPanel.Enabled = true; // block of buttons at the right bottom
|
|
RefreshListBoxItems();
|
|
}
|
|
|
|
// saving to file
|
|
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
|
MessageBox.Show(" < Saved succesfully >",
|
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
_logger.LogInformation("Saving to file : {filename}", saveFileDialog.FileName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// loading from file
|
|
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
_storageCollection.LoadData(openFileDialog.FileName);
|
|
// LoadData() : Exceptions
|
|
// FileNotFoundException
|
|
// NullReferenceException
|
|
// InvalidDataException
|
|
// IndexOutOfRangeException
|
|
// CollectionOverflowException
|
|
|
|
MessageBox.Show(" < Loaded succesfully >",
|
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
_logger.LogInformation("Loading from file : {Filename}", openFileDialog.FileName);
|
|
|
|
RefreshListBoxItems();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("< Failed to load >" + ex.Message,
|
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|