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

225 lines
6.4 KiB
C#

using ProjectCruiser.CollectionGenericObj;
using ProjectCruiser.DrawningSamples;
namespace ProjectCruiser;
public partial class ServiceForm2 : Form
{
// Компания
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningBase> _storageCollection;
public ServiceForm2()
{
InitializeComponent();
_storageCollection = new();
}
// Выбор компании
private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
toolPanel.Enabled = false;
}
// 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)
{
if (_company == null)
{
return;
}
DrawningBase? car = null;
int counter = 100;
while (car == null)
{
car = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (car == null)
{
return;
}
OceanForm1 form = new() { SetShip = car };
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;
}
_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();
}
// saving to file
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storageCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show(" < Saved succesfully >",
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("< Failed to save >", "Result :",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// loading from file
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storageCollection.LoadData(openFileDialog.FileName))
{
MessageBox.Show(" < Loaded succesfully >",
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshListBoxItems();
}
else
{
MessageBox.Show("< Failed to load >", "Result :",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}