213 lines
8.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Bulldozer;
using ProjectBulldozer.Generics;
using ProjectBulldozer.Drawning;
using ProjectBulldozer.Exceptions;
using Microsoft.Extensions.Logging;
using System.ComponentModel;
using System.Xml.Linq;
namespace ProjectBulldozer
{
public partial class FormTractorCollections : Form
{
private readonly TractorGenericStorage _storage;
private readonly TractorGenericCollection<DrawingTractor, DrawingObjectTractor> _tractors;
readonly int countPlace = 10;
private readonly ILogger _logger;
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
{
if (obj + tractor)
{
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}. Не удалось добавить объект");
}
}
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("Ошибка загрузки");
}
}
}
}
}