PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/FormHydroplaneCollection.cs

251 lines
8.8 KiB
C#
Raw Normal View History

2023-12-05 19:12:45 +04:00
using Microsoft.Extensions.Logging;
2023-10-25 02:19:13 +04:00
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;
2023-12-05 19:12:45 +04:00
using Hydroplane.Exceptions;
using Hydroplane.DrawningObjects;
using Hydroplane.Generics;
using Hydroplane.MovementStrategy;
namespace Hydroplane
{
public partial class FormHydroplaneCollection : Form
{
2023-10-25 10:59:54 +04:00
private readonly PlanesGenericStorage _storage;
2023-12-05 19:12:45 +04:00
private readonly ILogger _logger;
2023-10-25 10:59:54 +04:00
/// <summary>
/// Конструктор
/// </summary>
2023-12-05 19:12:45 +04:00
public FormHydroplaneCollection(ILogger<FormHydroplaneCollection> logger)
{
InitializeComponent();
2023-10-25 10:59:54 +04:00
_storage = new PlanesGenericStorage(DrawPlane.Width, DrawPlane.Height);
2023-12-05 19:12:45 +04:00
_logger = logger;
2023-10-25 02:19:13 +04:00
}
2023-10-25 10:59:54 +04:00
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
CollectionListBox.Items.Add(_storage.Keys[i]);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
2023-10-25 02:19:13 +04:00
2023-10-25 10:59:54 +04:00
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
2023-12-05 19:12:45 +04:00
MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-10-25 10:59:54 +04:00
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
2023-12-05 19:12:45 +04:00
_logger.LogInformation($"Added set: {SetTextBox.Text}");
2023-10-25 10:59:54 +04:00
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawPlane.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
2023-12-05 19:12:45 +04:00
string name = CollectionListBox.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show($"Delete Object {name}?", "Deleting",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
2023-10-25 10:59:54 +04:00
{
2023-12-05 19:12:45 +04:00
_storage.DelSet(name);
2023-10-25 10:59:54 +04:00
ReloadObjects();
2023-12-05 19:12:45 +04:00
_logger.LogInformation($"Deleted set: {name}");
2023-10-25 10:59:54 +04:00
}
}
2023-10-25 10:59:54 +04:00
private void ButtonAddPlane_Click(object sender, EventArgs e)
2023-10-25 02:19:13 +04:00
{
2023-10-25 10:59:54 +04:00
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
2023-12-05 19:12:45 +04:00
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormPlaneConfig formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlane);
formPlaneConfig.Show();
}
2023-12-05 19:12:45 +04:00
private void AddPlane(DrawningPlane plane)
{
if (CollectionListBox.SelectedIndex == -1)
2023-10-25 02:19:13 +04:00
{
2023-12-05 19:12:45 +04:00
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
_logger.LogWarning("Добавление пустого объекта");
return;
}
try
{
_ = obj + plane;
MessageBox.Show("Объект добавлен");
DrawPlane.Image = obj.ShowPlanes();
_logger.LogInformation($"plane added in set {CollectionListBox.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"plane not added in set {CollectionListBox.SelectedItem.ToString()}");
}
2023-10-25 02:19:13 +04:00
}
2023-10-25 10:59:54 +04:00
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemovePlane_Click(object sender, EventArgs e)
2023-10-25 02:19:13 +04:00
{
2023-10-25 10:59:54 +04:00
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
2023-12-05 19:12:45 +04:00
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
2023-10-25 10:59:54 +04:00
if (obj == null)
2023-10-25 02:19:13 +04:00
{
return;
}
2023-12-05 19:12:45 +04:00
if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No)
2023-10-25 02:19:13 +04:00
{
2023-10-25 10:59:54 +04:00
return;
2023-10-25 02:19:13 +04:00
}
2023-12-05 19:12:45 +04:00
try
2023-10-25 02:19:13 +04:00
{
2023-12-05 19:12:45 +04:00
int pos = Convert.ToInt32(PlaneTextBox.Text);
if (obj - pos != null)
{
MessageBox.Show("Object deleted");
DrawPlane.Image = obj.ShowPlanes();
_logger.LogInformation($"plane deleted in set {CollectionListBox.SelectedItem.ToString()}");
}
else
{
MessageBox.Show("Object not deleted");
_logger.LogWarning($"plane not deleted in set {CollectionListBox.SelectedItem.ToString()}");
}
2023-10-25 02:19:13 +04:00
}
2023-12-05 19:12:45 +04:00
catch (PlaneNotFoundException ex)
2023-10-25 02:19:13 +04:00
{
2023-12-05 19:12:45 +04:00
MessageBox.Show(ex.Message);
_logger.LogWarning($"PlaneNotFound: {ex.Message} in set {CollectionListBox.SelectedItem.ToString()}");
}
catch (Exception ex)
{
MessageBox.Show("Not input");
_logger.LogWarning("Not input");
2023-10-25 02:19:13 +04:00
}
}
2023-10-25 10:59:54 +04:00
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-11-18 18:35:07 +04:00
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
2023-10-25 02:19:13 +04:00
{
2023-10-25 10:59:54 +04:00
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
2023-12-05 19:12:45 +04:00
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
2023-10-25 10:59:54 +04:00
if (obj == null)
{
return;
}
DrawPlane.Image = obj.ShowPlanes();
}
2023-11-18 18:35:07 +04:00
private void SaveToolStripMenu_Click(object sender, EventArgs e)
{
if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
2023-12-05 19:12:45 +04:00
try
2023-11-18 18:35:07 +04:00
{
2023-12-05 19:12:45 +04:00
_storage.SaveData(SaveFileDialog.FileName);
MessageBox.Show("Saving complete", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"save in file {SaveFileDialog.FileName}");
2023-11-18 18:35:07 +04:00
}
2023-12-05 19:12:45 +04:00
catch (Exception ex)
2023-11-18 18:35:07 +04:00
{
2023-12-05 19:12:45 +04:00
MessageBox.Show($"Not saved: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Save to file {SaveFileDialog.FileName} not complete");
2023-11-18 18:35:07 +04:00
}
}
}
private void LoadToolStripMenu_Click(object sender, EventArgs args)
{
if (OpenFileDialog.ShowDialog() == DialogResult.OK)
{
2023-12-05 19:12:45 +04:00
try
2023-11-18 18:35:07 +04:00
{
2023-12-05 19:12:45 +04:00
_storage.LoadData(OpenFileDialog.FileName);
MessageBox.Show("Load complete", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
2023-11-18 18:35:07 +04:00
ReloadObjects();
2023-12-05 19:12:45 +04:00
_logger.LogInformation($"load from file {OpenFileDialog.FileName}");
2023-11-18 18:35:07 +04:00
}
2023-12-05 19:12:45 +04:00
catch (Exception ex)
2023-11-18 18:35:07 +04:00
{
2023-12-05 19:12:45 +04:00
MessageBox.Show($"Not loaded: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"load from file {OpenFileDialog.FileName} not complete");
2023-11-18 18:35:07 +04:00
}
}
}
}
}