308 lines
11 KiB
C#
308 lines
11 KiB
C#
using Microsoft.VisualBasic.Logging;
|
||
using Microsoft.Extensions.Logging;
|
||
using ProjectAircraftCarrier.DrawingObjects;
|
||
using ProjectAircraftCarrier.Generics;
|
||
using ProjectAircraftCarrier.Exceptions;
|
||
namespace ProjectAircraftCarrier
|
||
{
|
||
/// <summary>
|
||
/// Форма для работы с набором объектов класса DrawningCar
|
||
/// </summary>
|
||
public partial class FormWarshipCollection : Form
|
||
{
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly WarshipsGenericStorage _storage;
|
||
/// <summary>
|
||
/// Логер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public FormWarshipCollection(ILogger<FormWarshipCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new WarshipsGenericStorage(pictureBoxCollection.Width,
|
||
pictureBoxCollection.Height);
|
||
_logger = logger;
|
||
}
|
||
/// <summary>
|
||
/// Заполнение listBoxObjects
|
||
/// </summary>
|
||
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].Name);
|
||
}
|
||
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;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Добавление набора в коллекцию
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
||
{
|
||
MessageBox.Show("Not all data is filled in", "Error",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxStorageName.Text);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Added set: {textBoxStorageName.Text}");
|
||
}
|
||
/// <summary>
|
||
/// Выбор набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ListBoxObjects_SelectedIndexChanged(object sender,
|
||
EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image =
|
||
_storage[listBoxStorages.SelectedItem?.ToString() ??
|
||
string.Empty]?.ShowWarships();
|
||
}
|
||
/// <summary>
|
||
/// Удаление набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
string name = listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty;
|
||
if (MessageBox.Show($"Delete an object {name}?", "Deletion",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
||
== DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Deleted set: {name}");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddWarship_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
FormWarshipConfig form = new();
|
||
form.Show();
|
||
Action<DrawingWarship>? warshipDelegate = new((warship) =>
|
||
{
|
||
try
|
||
{
|
||
bool q = obj + warship;
|
||
MessageBox.Show("Object Added");
|
||
_logger.LogInformation($"Object added to collection " +
|
||
$"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
pictureBoxCollection.Image = obj.ShowWarships();
|
||
}
|
||
catch (StorageOverflowException ex)
|
||
{
|
||
_logger.LogWarning($"Collection " +
|
||
$"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}" +
|
||
$"is full");
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
catch (ArgumentException)
|
||
{
|
||
_logger.LogWarning($"Object being added already exists in" +
|
||
$" the collection " +
|
||
$"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
MessageBox.Show("Object being added already exists in " +
|
||
"the collection");
|
||
}
|
||
});
|
||
form.AddEvent(warshipDelegate);
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRemoveWarship_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Delete an object?", "Deletion",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
|
||
DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
try
|
||
{
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Object deleted");
|
||
_logger.LogInformation($"Object has been removed from the " +
|
||
$"collection " +
|
||
$"{listBoxStorages.SelectedItem.ToString() ?? string.Empty} " +
|
||
$"at position {pos}");
|
||
pictureBoxCollection.Image = obj.ShowWarships();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Failed to delete an object");
|
||
_logger.LogWarning($"Failed to remove object from the " +
|
||
$"collection " +
|
||
$"{listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
}
|
||
}
|
||
catch (WarshipNotFoundException ex)
|
||
{
|
||
_logger.LogWarning($"No number was entered");
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обновление рисунка по набору
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxCollection.Image = obj.ShowWarships();
|
||
}
|
||
/// <summary>
|
||
/// Обработка нажатия "Сохранение"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Save was successful", "Result",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"File {saveFileDialog.FileName} " +
|
||
$"successfuly saved");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogWarning("Failed to save");
|
||
MessageBox.Show($"Not preserved: {ex.Message}",
|
||
"Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обработка нажатия "Загрузка"
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Load was successful", "Result",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"File {openFileDialog.FileName} " +
|
||
$"successfully loaded");
|
||
foreach (var collection in _storage.Keys)
|
||
{
|
||
listBoxStorages.Items.Add(collection);
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogWarning("Failed to load");
|
||
MessageBox.Show($"Didn't load: {ex.Message}", "Result",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Сортировка по типу
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) =>
|
||
CompareWarships(new WarshipCompareByType());
|
||
/// <summary>
|
||
/// Сортировка по цвету
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) =>
|
||
CompareWarships(new WarshipCompareByColor());
|
||
/// <summary>
|
||
/// Сортировка по сравнителю
|
||
/// </summary>
|
||
/// <param name="comparer"></param>
|
||
private void CompareWarships(IComparer<DrawingWarship?> comparer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollection.Image = obj.ShowWarships();
|
||
}
|
||
}
|
||
}
|