// Copyright (c) 2017 schick Informatik // Description: Detailansicht für ATD, TIEFD, POBD, BKRD // using System; using System.Windows; using bsmd.database; using ENI2.EditControls; using ENI2.Util; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace ENI2.DetailViewControls { /// /// Interaction logic for ArrivalNotificationDetailControl.xaml /// public partial class DepartureNotificationDetailControl : DetailBaseControl { private Message _atdMessage; private Message _tiefdMessage; private Message _pobdMessage; private Message _bkrdMessage; private Message _brkaMessage; bool startupComplete = false; public DepartureNotificationDetailControl() { InitializeComponent(); this.Loaded += ArrivalNotificationDetailControl_Loaded; } private void ArrivalNotificationDetailControl_Loaded(object sender, RoutedEventArgs e) { this.RegisterDateTimePickerChange(this.dateTimePickerATD, Message.NotificationClass.ATD); this.RegisterDoubleUpDownChange(this.doubleUpDownDraught, Message.NotificationClass.TIEFD); this.RegisterIntegerUpDownChange(this.integerUpDownCrewMemberOnBoard, Message.NotificationClass.POBD); this.RegisterIntegerUpDownChange(this.integerUpDownPassengersOnBoard, Message.NotificationClass.POBD); this.RegisterIntegerUpDownChange(this.integerUpDownPersonsOnBoard, Message.NotificationClass.POBD); this.RegisterIntegerUpDownChange(this.integerUpDownStowawaysOnBoard, Message.NotificationClass.POBD); this.dataGridBKRD.CellEditEnding += (obj, ev) => { this.SublistElementChanged(Message.NotificationClass.BKRD); }; startupComplete = true; } public override void Initialize() { base.Initialize(); foreach (Message aMessage in this.Messages) { if (aMessage.MessageNotificationClass == Message.NotificationClass.ATD) { this._atdMessage = aMessage; this.ControlMessages.Add(aMessage); } if (aMessage.MessageNotificationClass == Message.NotificationClass.TIEFD) { this._tiefdMessage = aMessage; this.ControlMessages.Add(aMessage); } if (aMessage.MessageNotificationClass == Message.NotificationClass.POBD) { this._pobdMessage = aMessage; this.ControlMessages.Add(aMessage); } if (aMessage.MessageNotificationClass == Message.NotificationClass.BKRD) { this._bkrdMessage = aMessage; this.ControlMessages.Add(aMessage); } if (aMessage.MessageNotificationClass == Message.NotificationClass.BKRA) { this._brkaMessage = aMessage; this.ControlMessages.Add(aMessage); } } #region ATD if (this._atdMessage == null) { this._atdMessage = this.Core.CreateMessage(Message.NotificationClass.ATD); this.Messages.Add(this._atdMessage); } ATD atd = null; if (this._atdMessage.Elements.Count > 0) atd = this._atdMessage.Elements[0] as ATD; if (atd == null) { atd = new ATD(); atd.MessageCore = this.Core; atd.MessageHeader = this._atdMessage; _atdMessage.Elements.Add(atd); } this.atdGroupBox.DataContext = atd; // no, see ATA //if (!atd.ATDPortOfCall.HasValue) //{ // this.dateTimePickerATD.Minimum = DateTime.Now.AddDays(-14); // this.dateTimePickerATD.Maximum = DateTime.Now.AddDays(14); // this.dateTimePickerATD.ClipValueToMinMax = true; //} #endregion #region TIEFD if (this._tiefdMessage == null) { this._tiefdMessage = this.Core.CreateMessage(Message.NotificationClass.TIEFD); DBManager.Instance.Save(this._tiefdMessage); this.Messages.Add(this._tiefdMessage); } TIEFD tiefd = null; if (this._tiefdMessage.Elements.Count > 0) tiefd = this._tiefdMessage.Elements[0] as TIEFD; if (tiefd == null) { tiefd = new TIEFD(); tiefd.MessageCore = this.Core; tiefd.MessageHeader = this._tiefdMessage; _tiefdMessage.Elements.Add(tiefd); DBManager.Instance.Save(tiefd); } this.tiefdGroupBox.DataContext = tiefd; #endregion #region POBD if (this._pobdMessage == null) { this._pobdMessage = this.Core.CreateMessage(Message.NotificationClass.POBD); this.Messages.Add(this._pobdMessage); } POBD pobd = null; if (this._pobdMessage.Elements.Count > 0) pobd = this._pobdMessage.Elements[0] as POBD; if (pobd == null) { pobd = new POBD(); pobd.MessageCore = this.Core; pobd.MessageHeader = this._pobdMessage; _pobdMessage.Elements.Add(pobd); } this.pobdGroupBox.DataContext = pobd; #endregion #region BKRD if (this._bkrdMessage == null) { this._bkrdMessage = this.Core.CreateMessage(Message.NotificationClass.BKRD); this.Messages.Add(this._bkrdMessage); } this.dataGridBKRD.Initialize(); this.dataGridBKRD.ItemsSource = this._bkrdMessage.Elements; this.dataGridBKRD.AddingNewItem += DataGridBKRD_AddingNewItem; this.dataGridBKRD.EditRequested += DataGridBKRD_EditRequested; this.dataGridBKRD.DeleteRequested += DataGridBKRD_DeleteRequested; this.dataGridBKRD.CreateRequested += DataGridBKRD_CreateRequested; // Extra Menüpunkt um alle Bunker Positionen nach BKRA zu kopieren this.dataGridBKRD.ContextMenu.Items.Add(new Separator()); MenuItem copyBKRItem = new MenuItem(); copyBKRItem.Header = string.Format(Properties.Resources.textCopyTo, "BKRA"); copyBKRItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/documents.png")) }; copyBKRItem.Click += this.copyBKR; this.dataGridBKRD.ContextMenu.Items.Add(copyBKRItem); #endregion } #region BKRD grid events private void DataGridBKRD_CreateRequested() { this.dataGridBKRD.CancelEdit(); this.dataGridBKRD.CancelEdit(); EditBKRDialog ebd = new EditBKRDialog(); ebd.IsDeparture = true; ebd.BRKD = new BRKD(); ebd.BRKD.Identifier = BRKD.GetNewIdentifier(_bkrdMessage.Elements); ebd.BRKD.MessageHeader = this._bkrdMessage; ebd.AddClicked += () => { ebd.CopyValuesToEntity(); if(!this._bkrdMessage.Elements.Contains(ebd.BRKD)) this._bkrdMessage.Elements.Add(ebd.BRKD); this.dataGridBKRD.Items.Refresh(); ebd.BRKD = new BRKD(); ebd.BRKD.MessageHeader = _bkrdMessage; ebd.BRKD.Identifier = BRKD.GetNewIdentifier(_bkrdMessage.Elements); this.SublistElementChanged(Message.NotificationClass.BKRD); }; if (ebd.ShowDialog() ?? false) { if(!_bkrdMessage.Elements.Contains(ebd.BRKD)) _bkrdMessage.Elements.Add(ebd.BRKD); this.dataGridBKRD.Items.Refresh(); this.SublistElementChanged(Message.NotificationClass.BKRD); } } private void DataGridBKRD_DeleteRequested(DatabaseEntity obj) { if (obj is BRKD brkd) { this.dataGridBKRD.CancelEdit(); this.dataGridBKRD.CancelEdit(); // are you sure dialog is in base class this._bkrdMessage.Elements.Remove(brkd); DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(brkd); DatabaseEntity.ResetIdentifiers(this._bkrdMessage.Elements); this.SublistElementChanged(Message.NotificationClass.BKRD); this.dataGridBKRD.Items.Refresh(); } } private void DataGridBKRD_EditRequested(DatabaseEntity obj) { this.dataGridBKRD.CancelEdit(DataGridEditingUnit.Row); this.dataGridBKRD.CommitEdit(DataGridEditingUnit.Row, true); EditBKRDialog eld = new EditBKRDialog(); eld.IsDeparture = true; eld.BRKD = obj as BRKD; eld.AddClicked += () => { eld.CopyValuesToEntity(); if(!_bkrdMessage.Elements.Contains(eld.BRKD)) _bkrdMessage.Elements.Add(eld.BRKD); this.dataGridBKRD.Items.Refresh(); eld.BRKD = new BRKD(); eld.BRKD.Identifier = BRKD.GetNewIdentifier(_bkrdMessage.Elements); eld.BRKD.MessageHeader = _bkrdMessage; this.SublistElementChanged(Message.NotificationClass.BKRD); }; if (eld.ShowDialog() ?? false) { if (!_bkrdMessage.Elements.Contains(eld.BRKD)) _bkrdMessage.Elements.Add(eld.BRKD); this.dataGridBKRD.ItemsSource = null; this.dataGridBKRD.ItemsSource = this._bkrdMessage.Elements; this.SublistElementChanged(Message.NotificationClass.BKRD); } } private void DataGridBKRD_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e) { this.DataGridBKRD_CreateRequested(); } private void copyBKR(object sender, RoutedEventArgs e) { if (this.dataGridBKRD.SelectedItems != null) { foreach (BRKD brkd in this.dataGridBKRD.SelectedItems) { BRKA copyBRKA = new BRKA(); copyBRKA.MessageHeader = this._brkaMessage; copyBRKA.CopyFromBKRD(brkd); copyBRKA.Identifier = DatabaseEntity.GetNewIdentifier(this._brkaMessage.Elements); this._brkaMessage.Elements.Add(copyBRKA); this.SublistElementChanged(Message.NotificationClass.BKRA); } } } #endregion #region Highlighting public override void HighlightErrorMessageContainer() { if (this._atdMessage.HasErrors) HighlightService.HighlightControl(this.atdGroupBox, HighlightService.HighlightStyle.ERROR, this._atdMessage); if (this._tiefdMessage.HasErrors) HighlightService.HighlightControl(this.tiefdGroupBox, HighlightService.HighlightStyle.ERROR, this._tiefdMessage); if (this._pobdMessage.HasErrors) HighlightService.HighlightControl(this.pobdGroupBox, HighlightService.HighlightStyle.ERROR, this._pobdMessage); if (this._bkrdMessage.HasErrors) HighlightService.HighlightControl(this.bkrdGroupBox, HighlightService.HighlightStyle.ERROR, this._bkrdMessage); } public override void HighlightViolationMessageContainer() { if (this._atdMessage.HasViolations) HighlightService.HighlightControl(this.atdGroupBox, HighlightService.HighlightStyle.VIOLATION, this._atdMessage); if (this._tiefdMessage.HasViolations) HighlightService.HighlightControl(this.tiefdGroupBox, HighlightService.HighlightStyle.VIOLATION, this._tiefdMessage); if (this._pobdMessage.HasViolations) HighlightService.HighlightControl(this.pobdGroupBox, HighlightService.HighlightStyle.VIOLATION, this._pobdMessage); if (this._bkrdMessage.HasViolations) HighlightService.HighlightControl(this.bkrdGroupBox, HighlightService.HighlightStyle.VIOLATION, this._bkrdMessage); } #endregion #region special datetimepicker validation popup (OMG) private void dateTimePickerATD_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (startupComplete && this.dateTimePickerATD.Value.HasValue) { DateTime setTime = this.dateTimePickerATD.Value.Value; if ((setTime > DateTime.Now.AddDays(14)) || (setTime < DateTime.Now.AddDays(-14))) MessageBox.Show("ATD value may be invalid", "Date implausible", MessageBoxButton.OK, MessageBoxImage.Warning); } } #endregion } }