212 lines
7.8 KiB
C#
212 lines
7.8 KiB
C#
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;
|
||
using DoubleDeckerbus.Generic;
|
||
using DoubleDeckerbus.Drawing;
|
||
using DoubleDeckerbus.Move_Strategy;
|
||
using Microsoft.Extensions.Logging;
|
||
using DoubleDeckerbus.Exceptions;
|
||
|
||
|
||
|
||
namespace DoubleDeckerbus
|
||
{
|
||
public partial class FormDoubleDeckerbusCollection : Form
|
||
{
|
||
private readonly BusesGenericStorage _storage;
|
||
private readonly ILogger _logger;
|
||
public FormDoubleDeckerbusCollection(ILogger<FormDoubleDeckerbusCollection> logger)
|
||
{
|
||
InitializeComponent();
|
||
_storage = new BusesGenericStorage(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]);
|
||
}
|
||
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 buttonAddStorage_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 listBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBus();
|
||
}
|
||
private void buttonDeleteStorage_Click(object sender, EventArgs e)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
||
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(name);
|
||
ReloadObjects();
|
||
_logger.LogInformation($"Удалён набор: {name}");
|
||
}
|
||
}
|
||
|
||
|
||
private void buttonAddBus_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
var formBusConfig = new FormBusConfig();
|
||
formBusConfig.AddEvent(AddBus);
|
||
formBusConfig.Show();
|
||
}
|
||
|
||
private void AddBus(DrawingBus selectedBus)
|
||
{
|
||
if (listBoxStorages.SelectedIndex == -1)
|
||
{
|
||
MessageBox.Show("Не выбран набор");
|
||
return;
|
||
}
|
||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
selectedBus.ChangeBordersPicture(Width, Height);
|
||
try
|
||
{
|
||
if (obj + selectedBus != -1)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
pictureBoxCollection.Image = obj.ShowBus();
|
||
_logger.LogInformation($"Добавлен объект: {selectedBus.EntityBus.BodyColor}");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
}
|
||
catch (StorageOverflowException ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning(ex.Message);
|
||
}
|
||
}
|
||
|
||
private void buttonDeleteBus_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;
|
||
}
|
||
try
|
||
{
|
||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||
if (obj - pos)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
pictureBoxCollection.Image = obj.ShowBus();
|
||
_logger.LogInformation($"Удалён объект по позиции : {pos}");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
}
|
||
}
|
||
catch (FormatException ex)
|
||
{
|
||
MessageBox.Show("Неверный формат ввода");
|
||
_logger.LogWarning("Неверный формат ввода");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
_logger.LogWarning(ex.Message);
|
||
}
|
||
}
|
||
private void buttonUpdate_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.ShowBus();
|
||
}
|
||
|
||
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($"Файл сохранён по пути: {saveFileDialog.FileName}");
|
||
}
|
||
catch (InvalidOperationException ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning(ex.Message);
|
||
}
|
||
}
|
||
}
|
||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
_storage.LoadData(openFileDialog.FileName);
|
||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
_logger.LogInformation($"Файл загружен по пути: {openFileDialog.FileName}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
_logger.LogWarning(ex.Message);
|
||
}
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
}
|
||
}
|