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

253 lines
7.3 KiB
C#
Raw Normal View History

using ProjectCruiser.CollectionGenericObj;
using ProjectCruiser.DrawningSamples;
using Microsoft.Extensions.Logging;
// 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;
}
// Выбор компании
private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
2024-06-15 09:05:36 +04:00
toolPanel.Enabled = false;
}
2024-06-15 13:28:42 +04:00
// Color picker (default : random) <...>
// Добавление корабля
private void btnAddTransport_Click(object sender, EventArgs e)
{
EditorForm3 form3 = new();
// TODO передать метод :
form3.AddEvent(CreateObject);
form3.Show();
}
// Создание объекта класса-перемещения
private void CreateObject(DrawningBase? ship)
{
if (_company == null || ship == null)
{
return;
}
if (_company + ship != -1)
{
MessageBox.Show("> Object was added");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("[!] Failed to add object");
}
}
// Удаление объекта
private void btnRemoveCar_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);
if (_company - Convert.ToInt32(maskedTextBoxPosition.Text) != null)
{
MessageBox.Show("> Object was removed");
pictureBox.Image = _company.Show();
}
else MessageBox.Show("[!] Failed to remove object");
}
// Передача объекта в другую форму
private void btnChooseforTest_Click(object sender, EventArgs e)
{
// Add EXCEPTIONS [!]
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;
}
_storageCollection.AddCollection(maskedTxtBoxCName.Text, collType);
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;
}
_storageCollection.DelCollection(listBox.SelectedItem.ToString());
RefreshListBoxItems();
}
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();
}
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-15 13:28:42 +04:00
MessageBox.Show(" < Loaded succesfully >",
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
_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
}
}
}
}