// Copyright (c) 2023 schick Informatik // Description: Single dialog to edit times for all participant types // (we might use different controls at a later time) // using BreCalClient.misc.Model; using System; using System.Windows; using System.Windows.Media.Imaging; using Xceed.Wpf.Toolkit; namespace BreCalClient { /// /// Interaction logic for EditTimesControl.xaml /// public partial class EditTimesControl : Window, IEditTimesControl { #region Construction public EditTimesControl() { InitializeComponent(); } #endregion #region Properties public Times Times { get; set; } = new(); public Times? AgencyTimes { get; set; } = new(); public ShipcallControlModel ShipcallModel { get; set; } = new(); #endregion #region event handler private void Window_Loaded(object sender, RoutedEventArgs e) { this.EnableControls(); this.CopyToControls(); } private void buttonOK_Click(object sender, RoutedEventArgs e) { this.CopyToModel(); this.DialogResult = true; this.Close(); } private void buttonCancel_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; this.Close(); } private void buttonFixedOrder_Click(object sender, RoutedEventArgs e) { bool newValue = true; if (this.Times.EtaBerthFixed ?? false) newValue = false; SetLockButton(newValue); } private void datePickerETABerth_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { CheckOKButton(); } private void datePickerETDBerth_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { CheckOKButton(); } #endregion #region private methods private void CopyToModel() { this.Times.Remarks = this.textBoxRemarks.Text.Trim().Truncate(512); this.Times.EtaBerth = this.datePickerETABerth.Value; this.Times.EtdBerth = this.datePickerETDBerth.Value; this.Times.EtaIntervalEnd = this.datePickerETABerth_End.Value; this.Times.EtdIntervalEnd = this.datePickerETDBerth_End.Value; this.Times.LockTime = this.datePickerLockTime.Value; this.Times.ZoneEntry = this.datePickerZoneEntry.Value; this.Times.Ata = this.datePickerATA.Value; this.Times.Atd = this.datePickerATD.Value; } private void CopyToControls() { this.textBoxRemarks.Text = this.Times.Remarks; this.datePickerETABerth.Value = this.Times.EtaBerth; if(this.datePickerETABerth.IsEnabled && (this.Times.EtaBerth == null) && (this.AgencyTimes?.EtaBerth != null) && (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival)) { this.datePickerETABerth.Value = this.AgencyTimes.EtaBerth; WatermarkTextBox? tb = this.datePickerETABerth.Template.FindName("PART_TextBox", this.datePickerETABerth) as WatermarkTextBox; if (tb != null) { tb.Focus(); tb.SelectAll(); } } this.datePickerETDBerth.Value = this.Times.EtdBerth; if(this.datePickerETDBerth.IsEnabled && (this.Times.EtdBerth == null) && (this.AgencyTimes?.EtdBerth != null) && ((ShipcallModel.Shipcall?.Type == ShipcallType.Departure) || (ShipcallModel.Shipcall?.Type == ShipcallType.Shifting))) { this.datePickerETDBerth.Value = this.AgencyTimes.EtdBerth; WatermarkTextBox? tb = this.datePickerETDBerth.Template.FindName("PART_TextBox", this.datePickerETDBerth) as WatermarkTextBox; if (tb != null) tb.SelectAll(); } this.datePickerLockTime.Value = this.Times.LockTime; this.datePickerZoneEntry.Value = this.Times.ZoneEntry; this.datePickerATA.Value = this.Times.Ata; this.datePickerATD.Value = this.Times.Atd; this.datePickerETABerth_End.Value = this.Times.EtaIntervalEnd; if (this.datePickerETABerth_End.IsEnabled && (this.Times.EtaIntervalEnd == null) && (this.Times.EtaBerth == null) && (this.AgencyTimes?.EtaIntervalEnd != null) && (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival)) { this.datePickerETABerth_End.Value = this.AgencyTimes.EtaIntervalEnd; //if (this.datePickerETABerth_End.Template.FindName("PART_TextBox", this.datePickerETABerth_End) is WatermarkTextBox tb) { tb.Focus(); tb.SelectAll(); } } this.datePickerETDBerth_End.Value = this.Times.EtdIntervalEnd; if (this.datePickerETDBerth_End.IsEnabled && (this.Times.EtdIntervalEnd == null) && (this.Times.EtdBerth == null) && (this.AgencyTimes?.EtdIntervalEnd != null) && ((ShipcallModel.Shipcall?.Type == ShipcallType.Departure) || (ShipcallModel.Shipcall?.Type == ShipcallType.Shifting))) { this.datePickerETDBerth_End.Value = this.AgencyTimes.EtdIntervalEnd; //if (this.datePickerETDBerth_End.Template.FindName("PART_TextBox", this.datePickerETDBerth_End) is WatermarkTextBox tb) { tb.Focus(); tb.SelectAll(); } } if (this.ShipcallModel.Shipcall?.Type != ShipcallType.Shifting) { int displayIndex = this.ShipcallModel.Shipcall?.TimeRefPoint ?? 0; if (displayIndex > 0) { this.labelETA.Content = string.Format("ETA {0}", BreCalLists.TimeRefs[displayIndex]); this.labelETD.Content = string.Format("ETD {0}", BreCalLists.TimeRefs[displayIndex]); } else { this.labelETA.Content = BreCalClient.Resources.Resources.textETABerth; this.labelETD.Content = BreCalClient.Resources.Resources.textETDBerth; } } switch (ShipcallModel.Shipcall?.Type) { case ShipcallType.Arrival: this.labelETA.FontWeight = FontWeights.Bold; this.datePickerETABerth.ContextMenu.IsEnabled = false; break; case ShipcallType.Departure: case ShipcallType.Shifting: this.labelETD.FontWeight = FontWeights.Bold; this.datePickerETDBerth.ContextMenu.IsEnabled = false; break; } this.SetLockButton(this.Times.EtaBerthFixed ?? false); } private void EnableControls() { Extensions.ParticipantType pType = (Extensions.ParticipantType) this.Times.ParticipantType; // setting visibility if (pType != Extensions.ParticipantType.MOORING) { this.labelATA.Visibility = Visibility.Hidden; this.datePickerATA.Visibility = Visibility.Hidden; this.labelATD.Visibility = Visibility.Hidden; this.datePickerATD.Visibility = Visibility.Hidden; } else { if(ShipcallModel.Shipcall?.Type == ShipcallType.Arrival) { this.labelATD.Visibility = Visibility.Hidden; this.datePickerATD.Visibility = Visibility.Hidden; } if (ShipcallModel.Shipcall?.Type == ShipcallType.Departure) { this.labelATA.Visibility = Visibility.Hidden; this.datePickerATA.Visibility = Visibility.Hidden; } } if (ShipcallModel.Shipcall?.Type != ShipcallType.Arrival) { this.rowETA.Height = new(0); } else { this.rowETD.Height = new(0); } // setting en/dis-abled if (this.Times.ParticipantId != App.Participant.Id) { this.buttonFixedOrder.IsEnabled = false; return; // if this is not "my" entry, there is no editing! } this.datePickerETABerth.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival); this.datePickerETABerth_End.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival); this.datePickerETDBerth.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Departure || ShipcallModel.Shipcall?.Type == ShipcallType.Shifting); this.datePickerETDBerth_End.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Departure || ShipcallModel.Shipcall?.Type == ShipcallType.Shifting); this.textBoxRemarks.IsReadOnly = false; switch (pType) { case Extensions.ParticipantType.MOORING: this.datePickerATA.IsEnabled = true; this.datePickerATD.IsEnabled = true; break; case Extensions.ParticipantType.PORT_ADMINISTRATION: this.datePickerLockTime.IsEnabled = true; break; case Extensions.ParticipantType.TUG: case Extensions.ParticipantType.PILOT: this.datePickerZoneEntry.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival); break; } CheckOKButton(); } private void SetLockButton(bool newValue) { if (newValue) { this.Times.EtaBerthFixed = true; this.imageFixedOrder.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/lock.png", UriKind.RelativeOrAbsolute)); this.buttonFixedOrder.ToolTip = BreCalClient.Resources.Resources.textTooltipUnSetFixedOrder; } else { this.Times.EtaBerthFixed = false; this.imageFixedOrder.Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/lock_open.png", UriKind.RelativeOrAbsolute)); this.buttonFixedOrder.ToolTip = BreCalClient.Resources.Resources.textTooltipSetFixedOrder; } } private void CheckOKButton() { Extensions.ParticipantType pType = (Extensions.ParticipantType)this.Times.ParticipantType; if (pType != Extensions.ParticipantType.PORT_ADMINISTRATION) this.buttonOK.IsEnabled = (ShipcallModel.Shipcall?.Type == ShipcallType.Arrival) ? this.datePickerETABerth.Value.HasValue : this.datePickerETDBerth.Value.HasValue; else this.buttonOK.IsEnabled = true; } #endregion #region clear value event handler private void contextMenuItemClearETA_Click(object sender, RoutedEventArgs e) { this.datePickerETABerth.Value = null; } private void contextMenuItemClearETD_Click(object sender, RoutedEventArgs e) { this.datePickerETDBerth.Value = null; } private void contextMenuItemClearLockTime_Click(object sender, RoutedEventArgs e) { this.datePickerLockTime.Value = null; } private void contextMenuItemClearZoneEntry_Click(object sender, RoutedEventArgs e) { this.datePickerZoneEntry.Value = null; } private void contextMenuItemClearATA_Click(object sender, RoutedEventArgs e) { this.datePickerATA.Value = null; } private void contextMenuItemClearATD_Click(object sender, RoutedEventArgs e) { this.datePickerATD.Value = null; } private void contextMenuItemClearETA_End_Click(object sender, RoutedEventArgs e) { this.datePickerETABerth_End.Value = null; } private void contextMenuItemClearETD_End_Click(object sender, RoutedEventArgs e) { this.datePickerETDBerth_End.Value = null; } #endregion } }