235 lines
9.5 KiB
C#
235 lines
9.5 KiB
C#
using Bulldozer;
|
||
using ProjectBulldozer.Generics;
|
||
using ProjectBulldozer.Drawning;
|
||
using ProjectBulldozer.Exceptions;
|
||
using Microsoft.Extensions.Logging;
|
||
namespace ProjectBulldozer
|
||
{
|
||
public partial class FormTractorCollections : Form
|
||
{
|
||
private readonly TractorGenericStorage _storage;
|
||
|
||
private readonly ILogger _logger;
|
||
readonly int countPlace = 12;
|
||
public FormTractorCollections()
|
||
{
|
||
InitializeComponent();
|
||
_storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height);
|
||
}
|
||
public FormTractorCollections(ILogger<FormTractorCollections> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height);
|
||
_logger = logger;
|
||
}
|
||
private void ReloadObjects()
|
||
{
|
||
int index = listBoxStorage.SelectedIndex;
|
||
|
||
listBoxStorage.Items.Clear();
|
||
for (int i = 0; i < _storage.Keys.Count; i++)
|
||
{
|
||
listBoxStorage.Items.Add(_storage.Keys[i]);
|
||
}
|
||
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
|
||
{
|
||
listBoxStorage.SelectedIndex = 0;
|
||
}
|
||
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
|
||
{
|
||
listBoxStorage.SelectedIndex = index;
|
||
}
|
||
}
|
||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
||
{
|
||
MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning($"!!Ошибка!! Не все данные заполнены");
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxStorageName.Text);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
||
}
|
||
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollections.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTractors();
|
||
}
|
||
private void ButtonRemoveObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorage.SelectedIndex == -1)
|
||
{
|
||
_logger.LogWarning($"Удаление не выбранного набора");
|
||
return;
|
||
}
|
||
string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty;
|
||
if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удален набор: {name}");
|
||
}
|
||
}
|
||
private void ButtonAddTractor_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorage.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var formBulldozerConfig = new FormBulldozerConfig();
|
||
formBulldozerConfig.AddEvent(AddTractor);
|
||
formBulldozerConfig.Show();
|
||
}
|
||
private void AddTractor(DrawingTractor tractor)
|
||
{
|
||
tractor._pictureWidth = pictureBoxCollections.Width;
|
||
tractor._pictureHeight = pictureBoxCollections.Height;
|
||
|
||
if (listBoxStorage.SelectedIndex == -1) return;
|
||
|
||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||
string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty;
|
||
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
|
||
int addedIndex = obj + tractor;
|
||
if (addedIndex != -1 && addedIndex < countPlace)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBoxCollections.Image = obj.ShowTractors();
|
||
_logger.LogInformation($"В набор {name} добавлен объект");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
_logger.LogWarning("Не удалось добавить объект");
|
||
}
|
||
}
|
||
catch (StorageOverflowException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"{ex.Message}. Не удалось добавить объект");
|
||
}
|
||
}
|
||
private void ButtonRemoveTractor_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorage.SelectedIndex == -1) return;
|
||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
if (maskedTextBoxNumber.Text == "") { return; }
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
try
|
||
{
|
||
string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty;
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBoxCollections.Image = obj.ShowTractors();
|
||
_logger.LogInformation($"Из набора {name} удален объект");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
_logger.LogWarning("Не удалось удалить объект");
|
||
}
|
||
}
|
||
catch (BulldozerNotFoundException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning($"{ex.Message}. Не удалось удалить объект");
|
||
}
|
||
}
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorage.SelectedIndex == -1) return;
|
||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxCollections.Image = obj.ShowTractors();
|
||
}
|
||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.SaveData(saveFileDialog.FileName);
|
||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Сохранение");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning("Ошибка сохранения");
|
||
}
|
||
}
|
||
}
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
ReloadObjects();
|
||
MessageBox.Show("Загрузка произошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation("Загрузка");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning("Ошибка загрузки");
|
||
}
|
||
}
|
||
}
|
||
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByType());
|
||
|
||
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTractors(new TractorCompareByColor());
|
||
|
||
private void CompareTractors(IComparer<DrawingTractor?> comparer)
|
||
{
|
||
if (listBoxStorage.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
obj.Sort(comparer);
|
||
pictureBoxCollections.Image = obj.ShowTractors();
|
||
}
|
||
}
|
||
}
|
||
|
||
|