225 lines
8.4 KiB
C#
225 lines
8.4 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectLainer.DrawningObjects;
|
||
using ProjectLainer.Exceptions;
|
||
using ProjectLainer.Generics;
|
||
using ProjectLainer.MovementStrategy;
|
||
|
||
namespace ProjectLainer
|
||
{
|
||
public partial class FormLainerCollection : Form
|
||
{
|
||
private readonly LainersGenericStorage _storage;
|
||
private readonly ILogger _logger;
|
||
public FormLainerCollection(ILogger<FormLainerCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new LainersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||
_logger = logger;
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
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();
|
||
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
||
|
||
}
|
||
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||
if (MessageBox.Show($"Удалить объект {name}?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удален набор: {name}");
|
||
}
|
||
}
|
||
|
||
private void ButtonAddLainer_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
var formLinerConfig = new FormLainerConfig();
|
||
formLinerConfig.AddEvent(AddLainer);
|
||
formLinerConfig.Show();
|
||
}
|
||
|
||
private void ButtonRemove_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("Удалить объект?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
try
|
||
{
|
||
if (obj - pos)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBoxCollection.Image = obj.ShowLainers();
|
||
_logger.LogInformation($"удален лайнер в набор :{listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
}
|
||
}
|
||
catch (LainerNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning("ошибка лайнер не найден");
|
||
}
|
||
}
|
||
|
||
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.ShowLainers();
|
||
}
|
||
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLainers();
|
||
}
|
||
private void AddLainer(DrawingEntity lainer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
if (obj + lainer)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBoxCollection.Image = obj.ShowLainers();
|
||
_logger.LogInformation($"добавлен лайнер в набор: {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||
}
|
||
}
|
||
catch (OverflowException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning("ошибка переполнения");
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning("ошибка, лайнер не был добавлен, возможно такой объект уже есть");
|
||
}
|
||
|
||
}
|
||
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadToolStripItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
foreach (var collection in _storage.Keys)
|
||
{
|
||
listBoxStorages.Items.Add(collection);
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLainers(new LainerCompareByType());
|
||
|
||
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLainers(new LainerCompareByColor());
|
||
|
||
|
||
private void CompareLainers(IComparer<DrawingEntity?> comparer)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollection.Image = obj.ShowLainers();
|
||
}
|
||
}
|
||
}
|