git_bsmd/ENI2/Controls/HazardMaterialControl.xaml.cs

134 lines
5.1 KiB
C#

using bsmd.database;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace ENI2.Controls
{
/// <summary>
/// Interaction logic for HazardMaterialControl.xaml
/// </summary>
public partial class HazardMaterialControl : UserControl
{
public HazardMaterialControl()
{
InitializeComponent();
this.dataGridHazardMaterial.ItemsSource = HAZPosTemplate.Templates;
var view = CollectionViewSource.GetDefaultView(HAZPosTemplate.Templates);
view.SortDescriptions.Add(new SortDescription(nameof(HAZPosTemplate.Description), ListSortDirection.Ascending));
_ = HAZPosTemplate.EnsureLoadedAsync();
this.columnHazard.ItemsSource = BuildEnumItems(IBCPosition.hazards);
this.columnFlashpoint.ItemsSource = BuildEnumItems(IBCPosition.flashpointInformations);
this.columnPollution.ItemsSource = BuildEnumItems(IBCPosition.pollutionCategories);
this.columnIMSBC_HAZ.ItemsSource = BuildEnumItems(IMSBCPosition.hazardClass);
this.columnTemplateType.ItemsSource = Enum.GetValues(typeof(HAZPosTemplate.SublistType));
this.dataGridHazardMaterial.ContextMenu = new ContextMenu();
MenuItem addItem = new MenuItem
{
Header = Properties.Resources.textAdd,
Icon = new System.Windows.Controls.Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Resources/add.png")) }
};
addItem.Click += AddItem_Click;
this.dataGridHazardMaterial.ContextMenu.Items.Add(addItem);
MenuItem delItem = new MenuItem
{
Header = Properties.Resources.textDelete,
Icon = new System.Windows.Controls.Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Resources/delete.png")) }
};
delItem.Click += DelItem_Click;
this.dataGridHazardMaterial.ContextMenu.Items.Add(delItem);
}
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
_ = SaveTemplatesAsync();
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
AddNewTemplate();
}
private void dataGridHazardMaterial_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Row?.Item is HAZPosTemplate template)
{
template.IsDirty = true;
}
}
private void AddItem_Click(object sender, RoutedEventArgs e)
{
AddNewTemplate();
}
private async void DelItem_Click(object sender, RoutedEventArgs e)
{
if (this.dataGridHazardMaterial.SelectedItems.Count > 0)
{
if (MessageBox.Show("Are you sure to delete the selected values?", Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) ==
MessageBoxResult.Yes)
{
var selectedItems = new List<HAZPosTemplate>();
foreach (HAZPosTemplate item in this.dataGridHazardMaterial.SelectedItems)
selectedItems.Add(item);
foreach (HAZPosTemplate item in selectedItems)
{
int result = await DBManagerAsync.DeleteAsync(item);
if (result == 1 || item.IsNew)
{
HAZPosTemplate.Templates.Remove(item);
}
}
}
}
}
private void AddNewTemplate()
{
var template = new HAZPosTemplate
{
TemplateType = HAZPosTemplate.SublistType.IMSBC,
IsDirty = true
};
HAZPosTemplate.Templates.Add(template);
this.dataGridHazardMaterial.SelectedItem = template;
this.dataGridHazardMaterial.ScrollIntoView(template);
}
private async Task SaveTemplatesAsync()
{
int totalSaves = 0;
foreach (var template in HAZPosTemplate.Templates)
{
if (template.IsNew || template.IsDirty)
{
totalSaves += await DBManagerAsync.SaveAsync(template);
}
}
if (totalSaves > 0)
{
MessageBox.Show($"{totalSaves} hazard materials saved", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private static List<KeyValuePair<int, string>> BuildEnumItems(IReadOnlyList<string> values)
{
var items = new List<KeyValuePair<int, string>>(values.Count);
for (int i = 0; i < values.Count; i++)
{
items.Add(new KeyValuePair<int, string>(i, values[i]));
}
return items;
}
}
}