using Liner.Drawing; using Liner.Generics; using Liner.MovingStrategies; 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 Microsoft.Extensions.Logging; using Liner.Exceptions; using System.Xml.Linq; namespace Liner { /// /// Форма для работы с набором объектов класса DrawingLiner /// public partial class FormLinerCollection : Form { /// /// Набор объектов /// private readonly LinersGenericStorage _storage; /// /// Логер /// private readonly ILogger _logger; /// /// Конструктор /// public FormLinerCollection(ILogger logger) { InitializeComponent(); _storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); _logger = logger; } /// /// Добавление объекта в набор /// /// /// private void ButtonAddLiner_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { _logger.LogWarning($"Добавление лайнера не удалось (индекс вне границ)"); return; } var formLinerConfig = new FormLinerConfig(); formLinerConfig.AddEvent(AddLiner); formLinerConfig.Show(); } private void AddLiner(DrawingLiner liner) { if (listBoxStorages.SelectedIndex == -1) { _logger.LogWarning($"Добавление лайнера не удалось (индекс вне границ)"); return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { _logger.LogWarning($"Добавление лайнера не удалось (нет хранилища)"); return; } try { if ((obj + liner) != -1) { MessageBox.Show("Объект добавлен"); _logger.LogInformation($"Добавление лайнера успешно {listBoxStorages.SelectedItem.ToString()}"); pictureBoxCollection.Image = obj.ShowLiners(); } else { MessageBox.Show("Не удалось добавить объект"); } } catch (ApplicationException ex) { MessageBox.Show(ex.Message); } } /// /// Удаление объекта из набора /// /// /// private void ButtonRemoveLiner_Click(object sender, EventArgs e) { int pos = 0; //фикс недосмотра if (listBoxStorages.SelectedIndex == -1) { _logger.LogWarning($"Удаление лайнера не удалось (индекс вне границ)"); return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { _logger.LogWarning($"Удаление лайнера не удалось (нет хранилища)"); return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { _logger.LogWarning($"Удаление лайнера не удалось (выбран вариант 'Нет')"); return; } if (textBoxNumber.Text != "") //улучшил проверку { if(!int.TryParse(textBoxNumber.Text, out pos)) { _logger.LogWarning($"Удаление лайнера не удалось (неверный формат)"); return; } } else { _logger.LogWarning($"Удаление лайнера не удалось (пустое поле ввода)"); MessageBox.Show("Не удалось удалить объект"); return; } try { if (obj - pos) { _logger.LogInformation($"Удаление лайнера успешно {listBoxStorages.SelectedItem.ToString()} {pos}"); MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowLiners(); } else { _logger.LogWarning($"Удаление лайнера не удалось(обьект не найден)"); MessageBox.Show("Не удалось удалить объект"); } } catch (LinerNotFoundException ex) { _logger.LogWarning($"Удаление лайнера не удалось {ex.Message}"); MessageBox.Show(ex.Message); } } /// /// Добавление набора в коллекцию /// /// /// private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) { _logger.LogWarning($"Обновление набора не удалось (не все данные заполнены)"); MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(textBoxStorageName.Text); _logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text} "); ReloadObjects(); } /// /// Выбор набора /// /// /// private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLiners(); } /// /// Удаление набора /// /// /// private void ButtonRemoveObject_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { _logger.LogWarning($"Удаление набора не удалось (индекс вне границ)"); return; } if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; _storage.DelSet(name); _logger.LogInformation($"Удаление набора успешно: {name}"); ReloadObjects(); } } /// /// Обновление рисунка по набору /// /// /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { _logger.LogWarning($"Обновление объектов не удалось (индекс вне границ)"); return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { _logger.LogWarning($"Обновление объектов не удалось (нет хранилища)"); return; } _logger.LogInformation($"Обновление объектов успешно"); pictureBoxCollection.Image = obj.ShowLiners(); } /// /// Заполнение listBoxObjects /// 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 SaveToolStripMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { _storage.SaveData(saveFileDialog.FileName); _logger.LogInformation($"Cохранение успешно"); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch(Exception 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); _logger.LogInformation($"Загрузка успешна"); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadObjects(); } catch (Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Загрузка не удалась {ex.Message}"); } } } } }