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

225 lines
6.4 KiB
C#
Raw Normal View History

using ProjectCruiser.CollectionGenericObj;
using ProjectCruiser.DrawningSamples;
namespace ProjectCruiser;
public partial class ServiceForm2 : Form
{
// Компания
2024-06-15 09:05:36 +04:00
private AbstractCompany? _company = null;
private readonly StorageCollection<DrawningBase> _storageCollection;
public ServiceForm2()
{
InitializeComponent();
2024-06-15 09:05:36 +04:00
_storageCollection = new();
}
// Выбор компании
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)
{
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();
}
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)
{
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);
}
}
}
}