PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/FormAirplaneCollection.cs

229 lines
8.3 KiB
C#
Raw Normal View History

2023-10-15 17:11:27 +04:00
using ProjectAirFighter.DrawningObjects;
using ProjectAirFighter.Generics;
2023-12-08 22:44:38 +04:00
using Microsoft.Extensions.Logging;
using ProjectAirFighter.Exceptions;
2023-10-15 17:11:27 +04:00
using ProjectAirFighter.MovementStrategy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2023-12-08 22:44:38 +04:00
using System.Xml.Linq;
using Serilog;
using Microsoft.VisualBasic.Logging;
using Log = Serilog.Log;
2023-10-15 17:11:27 +04:00
namespace ProjectAirFighter
{
public partial class FormAirplaneCollection : Form
{
2023-10-27 00:17:04 +04:00
private readonly AirplanesGenericStorage _storage;
2023-10-15 17:11:27 +04:00
public FormAirplaneCollection()
{
InitializeComponent();
2023-10-27 00:17:04 +04:00
_storage = new AirplanesGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
2023-11-24 22:30:21 +04:00
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
2023-12-08 22:44:38 +04:00
try
2023-11-24 22:30:21 +04:00
{
2023-12-08 22:44:38 +04:00
_storage.SaveData(saveFileDialog.FileName);
2023-11-24 22:30:21 +04:00
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
2023-12-08 22:44:38 +04:00
catch (Exception ex)
2023-11-24 22:30:21 +04:00
{
2023-12-08 22:44:38 +04:00
Log.Warning("Не удалось сохранить");
MessageBox.Show($"Не сохранилось: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-11-24 22:30:21 +04:00
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
2023-12-08 22:44:38 +04:00
try
2023-11-24 22:30:21 +04:00
{
2023-12-08 22:44:38 +04:00
_storage.LoadData(openFileDialog.FileName);
2023-11-24 22:30:21 +04:00
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
2023-12-08 22:44:38 +04:00
ReloadObjects();
2023-11-24 22:30:21 +04:00
}
2023-12-08 22:44:38 +04:00
catch (Exception ex)
2023-11-24 22:30:21 +04:00
{
2023-12-08 22:44:38 +04:00
Log.Warning("Не удалось загрузить");
MessageBox.Show($"Не загрузилось: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-11-24 22:30:21 +04:00
}
}
}
2023-10-27 00:17:04 +04:00
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
2023-11-24 22:30:21 +04:00
2023-11-19 00:11:44 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAirplanes();
2023-10-27 00:17:04 +04:00
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
2023-12-08 22:44:38 +04:00
Log.Information($"Добавлен набор: {textBoxStorageName.Text}");
2023-10-27 00:17:04 +04:00
2023-10-15 17:11:27 +04:00
}
private void deleteAirplaneButton_Click(object sender, EventArgs e)
{
2023-10-27 00:17:04 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-10-15 17:11:27 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
2023-10-27 00:17:04 +04:00
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
2023-10-15 17:11:27 +04:00
{
return;
}
2023-12-08 22:44:38 +04:00
try
2023-10-15 17:11:27 +04:00
{
2023-12-08 22:44:38 +04:00
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
var q = obj - pos;
2023-10-15 17:11:27 +04:00
MessageBox.Show("Объект удален");
2023-12-08 22:44:38 +04:00
Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
2023-10-27 00:17:04 +04:00
pictureBoxCollection.Image = obj.ShowAirplanes();
2023-10-15 17:11:27 +04:00
}
2023-12-08 22:44:38 +04:00
catch (AirplaneNotFoundException ex)
2023-10-15 17:11:27 +04:00
{
2023-12-08 22:44:38 +04:00
Log.Warning($"Не получилось удалить объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show(ex.Message);
}
catch (FormatException ex)
{
Log.Warning($"Было введено не число");
MessageBox.Show("Введите число");
2023-10-15 17:11:27 +04:00
}
}
private void updateCollectionButton_Click(object sender, EventArgs e)
{
2023-10-27 00:17:04 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAirplanes();
2023-10-15 17:11:27 +04:00
}
private void addAirplaneButton_Click(object sender, EventArgs e)
{
2023-10-27 00:17:04 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-11-19 00:11:44 +04:00
FormAirplaneConfig form = new();
form.Show();
Action<DrawningAirplane>? airplaneDelegate = new((m) =>
2023-10-15 17:11:27 +04:00
{
2023-12-08 22:44:38 +04:00
try
2023-10-15 17:11:27 +04:00
{
2023-12-08 22:44:38 +04:00
bool q = obj + m;
2023-10-15 17:11:27 +04:00
MessageBox.Show("Объект добавлен");
2023-12-08 22:44:38 +04:00
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
2023-10-27 00:17:04 +04:00
pictureBoxCollection.Image = obj.ShowAirplanes();
2023-10-15 17:11:27 +04:00
}
2023-12-08 22:44:38 +04:00
catch (StorageOverflowException ex)
2023-10-15 17:11:27 +04:00
{
MessageBox.Show("Не удалось добавить объект");
2023-12-08 22:44:38 +04:00
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
2023-10-15 17:11:27 +04:00
}
2023-11-19 00:11:44 +04:00
});
form.AddEvent(airplaneDelegate);
2023-10-15 17:11:27 +04:00
}
2023-10-27 00:17:04 +04:00
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes();
2023-11-24 22:30:21 +04:00
2023-10-27 00:17:04 +04:00
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
2023-12-08 22:44:38 +04:00
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
2023-10-27 00:17:04 +04:00
MessageBoxIcon.Question) == DialogResult.Yes)
{
2023-12-08 22:44:38 +04:00
string name = listBoxStorages.SelectedItem.ToString()
?? string.Empty;
_storage.DelSet(name);
2023-10-27 00:17:04 +04:00
ReloadObjects();
2023-12-08 22:44:38 +04:00
Log.Information($"Удален набор: {name}");
2023-10-27 00:17:04 +04:00
}
}
2023-10-15 17:11:27 +04:00
}
}