diff --git a/src/RoleEditor/ENIDataGrid.cs b/src/RoleEditor/ENIDataGrid.cs new file mode 100644 index 0000000..3d670e0 --- /dev/null +++ b/src/RoleEditor/ENIDataGrid.cs @@ -0,0 +1,288 @@ +// Copyright (c) 2017 schick Informatik +// Description: DataGrid mit etwas "verbesserten" Funktionen +// + +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +using System.Windows.Controls.Primitives; +using System.Collections.Generic; +using brecal.model; +using System.Globalization; +using System.Threading; + +namespace RoleEditor +{ + /// + /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file. + /// + /// Step 1a) Using this custom control in a XAML file that exists in the current project. + /// Add this XmlNamespace attribute to the root element of the markup file where it is + /// to be used: + /// + /// xmlns:enictrl="clr-namespace:ENI2.Controls" + /// + /// + /// Step 1b) Using this custom control in a XAML file that exists in a different project. + /// Add this XmlNamespace attribute to the root element of the markup file where it is + /// to be used: + /// + /// xmlns:enictrl="clr-namespace:ENI2.Controls;assembly=ENI2.Controls" + /// + /// You will also need to add a project reference from the project where the XAML file lives + /// to this project and Rebuild to avoid compilation errors: + /// + /// Right click on the target project in the Solution Explorer and + /// "Add Reference"->"Projects"->[Browse to and select this project] + /// + /// + /// Step 2) + /// Go ahead and use your control in the XAML file. + /// + /// + /// + /// + public class ENIDataGrid : DataGrid + { + /* + static ENIDataGrid() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(ENIDataGrid), new FrameworkPropertyMetadata(typeof(ENIDataGrid))); + } + */ + + // das hier bildet 1:1 das Kontext-Menü des ANSW ab + + public event Action? EditRequested; + public event Action? DeleteRequested; + public event Action? CreateRequested; + public event Action? RefreshGrid; + + public event Action? PrintRequested; + public event Action? ExportRequested; + public event Action? ShowTextRequested; + + public void Initialize() + { + + this.MouseDoubleClick += dataGrid_MouseDoubleClick; + this.PreviewKeyDown += ENIDataGrid_PreviewKeyDown; + + this.ContextMenu = new ContextMenu(); + this.CanUserAddRows = false; + this.IsReadOnly = false; + + MenuItem addItem = new MenuItem(); + + addItem.Header = RoleEditor.Resources.ResourceManager.GetString("textAdd"); + addItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.add) }; + addItem.Click += new RoutedEventHandler(this.addItem); + this.ContextMenu.Items.Add(addItem); + + MenuItem deleteItem = new MenuItem(); + deleteItem.Header = RoleEditor.Resources.ResourceManager.GetString("textDelete"); + deleteItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.delete) }; + deleteItem.Click += this.deleteItem; + this.ContextMenu.Items.Add(deleteItem); + + MenuItem editItem = new MenuItem(); + editItem.Header = RoleEditor.Resources.ResourceManager.GetString("textEdit"); + editItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.edit) }; + editItem.Click += this.editItem; + this.ContextMenu.Items.Add(editItem); + + /* + this.ContextMenu.Items.Add(new Separator()); + + MenuItem printItem = new MenuItem(); + printItem.Header = RoleEditor.Resources.ResourceManager.GetString("textPrint"); + printItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.printer) }; + printItem.Click += this.printItem; + this.ContextMenu.Items.Add(printItem); + + MenuItem exportItem = new MenuItem(); + exportItem.Header = RoleEditor.Resources.ResourceManager.GetString("textExport"); + exportItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.floppy_disk_blue) }; + exportItem.Click += this.exportItem; + this.ContextMenu.Items.Add(exportItem); + + MenuItem showTextItem = new MenuItem(); + showTextItem.Header = RoleEditor.Resources.ResourceManager.GetString("textShowAsText"); + showTextItem.Icon = new Image { Source = Util.LoadImage(RoleEditor.Resources.document_plain) }; + showTextItem.Click += this.showTextItem; + this.ContextMenu.Items.Add(showTextItem); + */ + } + + private void ENIDataGrid_PreviewKeyDown(object sender, KeyEventArgs e) + { + if(sender is ENIDataGrid) + { + var grid = sender as ENIDataGrid; + if (Key.Delete == e.Key) + this.deleteItem(null, null); + } + } + + #region public + + public DataGridRow GetRow(int index) + { + DataGridRow row = (DataGridRow)this.ItemContainerGenerator.ContainerFromIndex(index); + if(row == null) + { + this.UpdateLayout(); + this.ScrollIntoView(this.Items[index]); + row = (DataGridRow)this.ItemContainerGenerator.ContainerFromIndex(index); + } + return row; + } + + public DataGridCell? GetCell(DataGridRow row, int column) + { + if (row != null) + { + DataGridCellsPresenter? presenter = GetVisualChild(row); + + if (presenter == null) + { + this.ScrollIntoView(row, this.Columns[column]); + presenter = GetVisualChild(row); + } + + DataGridCell? cell = (DataGridCell?)presenter?.ItemContainerGenerator.ContainerFromIndex(column); + return cell; + } + return null; + } + + public DataGridCell? GetCell(int rowIndex, int columnIndex) + { + DataGridRow row = this.GetRow(rowIndex); + return this.GetCell(row, columnIndex); + } + + #endregion + + #region protected + + protected void addItem(object sender, RoutedEventArgs e) + { + Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us"); + if (!this.IsReadOnly) + { + this.CreateRequested?.Invoke(); + } + e.Handled = true; + } + + protected void deleteItem(object? sender, RoutedEventArgs? e) + { + if((this.SelectedItems != null) && (this.SelectedItems.Count > 0) && !this.IsReadOnly) + { + MessageBoxResult result = MessageBox.Show("Are your sure?", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question); + if (result == MessageBoxResult.Yes) + { + List deleteList = new List(); + foreach (DbEntity deleteItem in this.SelectedItems) + deleteList.Add(deleteItem); + + foreach (DbEntity deleteItem in deleteList) + { + if (deleteItem != null) + { + this.DeleteRequested?.Invoke(deleteItem); + } + } + + this.RefreshGrid?.Invoke(); + + } + } + } + + protected void editItem(object sender, RoutedEventArgs e) + { + if((this.SelectedItems != null) && (this.SelectedItems.Count == 1) && !this.IsReadOnly) + { + if (this.SelectedItems[0] is DbEntity selectedEntity) + this.EditRequested?.Invoke(selectedEntity); + } + } + + protected void printItem(object sender, RoutedEventArgs e) + { + if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1) ) + { + if (this.SelectedItems[0] is DbEntity selectedEntity) + this.PrintRequested?.Invoke(selectedEntity); + } + } + + protected void exportItem(object sender, RoutedEventArgs e) + { + if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1)) + { + if (this.SelectedItems[0] is DbEntity selectedEntity) + this.ExportRequested?.Invoke(selectedEntity); + } + } + + protected void showTextItem(object sender, RoutedEventArgs e) + { + if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1)) + { + if (this.SelectedItems[0] is DbEntity selectedEntity) + this.ShowTextRequested?.Invoke(selectedEntity); + } + } + + #endregion + + #region private + + private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) + { + if (sender != null) + { + if ((sender is DataGrid grid) && (grid.SelectedItems != null) && (grid.SelectedItems.Count == 1) && !this.IsReadOnly) + { + DataGridRow? dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow; + if (grid.SelectedItem is DbEntity selectedEntity) + this.EditRequested?.Invoke(selectedEntity); + } + } + } + + #endregion + + #region private static + + private static T? GetVisualChild(Visual parent) where T : Visual + { + T? child = default; + int numVisuals = VisualTreeHelper.GetChildrenCount(parent); + for (int i = 0; i < numVisuals; i++) + { + Visual? v = (Visual)VisualTreeHelper.GetChild(parent, i); + child = v as T; + if (child == null) + { + child = GetVisualChild(v); + } + if (child != null) + { + break; + } + } + return child; + } + + #endregion + + } +} diff --git a/src/RoleEditor/EditBerthDialog.xaml b/src/RoleEditor/EditBerthDialog.xaml new file mode 100644 index 0000000..d143baf --- /dev/null +++ b/src/RoleEditor/EditBerthDialog.xaml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +