207 lines
7.3 KiB
C#
207 lines
7.3 KiB
C#
using ProjectMonorail.Scripts.Monorail.CollectionGenericObjects;
|
||
using ProjectMonorail.Scripts.Monorail.Drawnings;
|
||
|
||
namespace ProjectMonorail
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public partial class FormMonorailCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Хранилище коллекций
|
||
/// </summary>
|
||
private readonly StorageCollection<DrawingMonorail> _storageCollection;
|
||
|
||
/// <summary>
|
||
/// Компания
|
||
/// </summary>
|
||
private AbstractCompany? _company = null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormMonorailCollection()
|
||
{
|
||
InitializeComponent();
|
||
_storageCollection = new StorageCollection<DrawingMonorail>();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new DepotSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingMonorail>());
|
||
break;
|
||
}
|
||
}
|
||
|
||
private void ButtonAddMonorail_Click(object sender, EventArgs e)
|
||
{
|
||
FormMonorailConfig form = new FormMonorailConfig();
|
||
form.Show();
|
||
form.AddEventListener_Monorail(SetMonorail);
|
||
}
|
||
|
||
private void SetMonorail(DrawingMonorail monorail)
|
||
{
|
||
if (monorail == null || _company == null) return;
|
||
|
||
if (_company + monorail != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
}
|
||
|
||
private void buttonRemoveMonorail_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) return;
|
||
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
|
||
|
||
int position = Convert.ToInt32(maskedTextBox.Text);
|
||
|
||
if (_company - position != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
}
|
||
}
|
||
|
||
private void ButtonGoToCheck_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null) return;
|
||
|
||
DrawingMonorail? monorail = null;
|
||
int coutner = 100;
|
||
|
||
while (monorail == null && coutner-- > 0)
|
||
{
|
||
monorail = _company.GetRandomObject();
|
||
}
|
||
|
||
if (monorail == null) return;
|
||
|
||
FormMonorail form = new FormMonorail()
|
||
{
|
||
SetMonorail = monorail
|
||
};
|
||
form.ShowDialog();
|
||
}
|
||
|
||
private void ButtonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
if (_company == null) return;
|
||
pictureBox.Image = _company.Show();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление списка в listBoxCollection
|
||
/// </summary>
|
||
private void RerfreshListBoxItems()
|
||
{
|
||
listBoxCollection.Items.Clear();
|
||
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
|
||
{
|
||
string? colName = _storageCollection.Keys?[i];
|
||
if (!string.IsNullOrEmpty(colName))
|
||
{
|
||
listBoxCollection.Items.Add(colName);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
CollectionType collectionType = CollectionType.None;
|
||
if (radioButtonMassive.Checked)
|
||
{
|
||
collectionType = CollectionType.Massive;
|
||
}
|
||
else if (radioButtonList.Checked)
|
||
{
|
||
collectionType = CollectionType.List;
|
||
}
|
||
_storageCollection.AddCollection(textBoxCollectionName.Text,
|
||
collectionType);
|
||
RerfreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление коллекции
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCollectionDelete_Click(object sender, EventArgs e)
|
||
{
|
||
// TODO прописать логику удаления элемента из коллекции
|
||
// нужно убедиться, что есть выбранная коллекция
|
||
// спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись
|
||
// удалить и обновить ListBox
|
||
if (listBoxCollection.SelectedItem == null || listBoxCollection.SelectedIndex < 0)
|
||
{
|
||
MessageBox.Show("Коллекция для удаления не выбрана");
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
|
||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||
RerfreshListBoxItems();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание компании
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonCreateCompany_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Коллекция не выбрана");
|
||
return;
|
||
}
|
||
ICollectionGenericObjects<DrawingMonorail>? collection =
|
||
_storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
|
||
if (collection == null)
|
||
{
|
||
MessageBox.Show("Коллекция не проинициализирована");
|
||
return;
|
||
}
|
||
switch (comboBoxSelectorCompany.Text)
|
||
{
|
||
case "Хранилище":
|
||
_company = new DepotSharingService(pictureBox.Width,
|
||
pictureBox.Height, collection);
|
||
break;
|
||
}
|
||
panelCompanyTools.Enabled = true;
|
||
RerfreshListBoxItems();
|
||
}
|
||
}
|
||
}
|