Files

188 lines
6.5 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DTS.Common.Base;
using DTS.Common.Controls;
using DTS.Common.Enums;
using DTS.Common.Enums.Channels;
using DTS.Common.Events;
using DTS.Common.Interface.Channels.ChannelCodes;
using Prism.Events;
using Prism.Ioc;
namespace DTS.Common.Classes.ChannelCodes
{
public class ChannelCode : BasePropertyChanged, IChannelCode
{
//in case this object is held longer before cleanup, make sure code and name are emptied
~ChannelCode()
{
_code = null;
_name = null;
}
private int _id = -1;
public int Id
{
get => _id;
set => SetProperty(ref _id, value, "Id");
}
private UIItemStatus _itemStatus = UIItemStatus.None;
public UIItemStatus ItemStatus
{
get => _itemStatus;
set => SetProperty(ref _itemStatus, value, "ItemStatus");
}
protected string _code = string.Empty;
public string Code
{
get => _code;
set => SetProperty(ref _code, value, "Code");
}
protected string _name = string.Empty;
public string Name
{
get => _name;
set => SetProperty(ref _name, value, "Name");
}
public ChannelEnumsAndConstants.ChannelCodeType CodeType { get; set; }
public const string PASTE_ID = "ChannelCode";
// putting a null here avoids wasteful exception that was being thrown during UI construction
// DTM 2020-12-07
public ICommand PasteCommand { get; set; } = null;
public ChannelCode(IDataReader reader, IReadOnlyDictionary<short, string> channelTypeLookup)
{
var codeType = ChannelEnumsAndConstants.ChannelCodeType.User;
var iCodeType = Utility.GetShort(reader, "CodeTypeInt");
if (channelTypeLookup.ContainsKey(iCodeType))
{
var key = channelTypeLookup[iCodeType];
switch (key)
{
case ChannelEnumsAndConstants.UserCodeTypeString:
codeType = ChannelEnumsAndConstants.ChannelCodeType.User;
break;
case ChannelEnumsAndConstants.IsoCodeTypeString:
codeType = ChannelEnumsAndConstants.ChannelCodeType.ISO;
break;
}
}
Id = Utility.GetInt(reader, "Id");
Code = Utility.GetString(reader, "Code");
Name = Utility.GetString(reader, "Name");
CodeType = codeType;
}
public ChannelCode()
{
CodeType = ChannelEnumsAndConstants.ChannelCodeType.ISO;
}
public ChannelCode(IChannelCode channelCode)
{
Id = channelCode.Id;
Code = channelCode.Code;
CodeType = channelCode.CodeType;
Name = channelCode.Name;
}
public override bool Equals(object obj)
{
if (!(obj is ChannelCode other))
{
return false;
}
return Code.Equals(other.Code)
&& Name.Equals(other.Name)
&& CodeType.Equals(other.CodeType);
}
}
public class PasteCommandClass : ICommand
{
public string Id { get; }
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
try
{
if (!(parameter is TextBox tb))
{
return;
}
if (!(tb.DataContext is IChannelCode channelCode))
{
if (tb.DataContext is ChannelCodeBuilder ccb)
{
channelCode = ccb.DataContext as IChannelCode;
}
else if (tb.DataContext is ChannelNameBuilder cnb)
{
channelCode = cnb.DataContext as IChannelCode;
}
else
{
return;
}
}
if (!Clipboard.ContainsText())
{
return;
}
var text = Clipboard.GetText();
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (1 == lines.Length)
{
var line = lines[0];
if (line.IndexOfAny(new[] { ',', ';', '\t' }) < 0)
{
//this is a single field paste, don't do any further processing, let textchanged take care of it
eventAggregator.GetEvent<PageModifiedEvent>().Publish(new PageModifiedArg(PageModifiedArg.Status.Modified, null));
return;
}
}
//wipe out the built in effect of the paste
channelCode.Code = channelCode.Code;
channelCode.Name = channelCode.Name;
eventAggregator.GetEvent<TextPastedEvent>().Publish(new TextPastedArgs(text, channelCode, Id, tb.Tag));
}
catch (Exception ex)
{
eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new[] { ex.Message }, null));
}
}
public event EventHandler CanExecuteChanged;
public PasteCommandClass(string id)
{
Id = id;
}
}
/// <summary>
/// this defines a function to control the behavior of setting an isocode
/// it was done for
/// 14033 finish/clean up from removing CFC filter from digital input on sensor db
/// this allows channels to handle incoming isocodes from the UI and coerce them to a new value
/// </summary>
/// <param name="val"></param>
/// <param name="uniqueISOCodesRequired"></param>
/// <param name="useISOCodeFilterMapping"></param>
/// <returns></returns>
public delegate string CoerceISOCodeDelegate(string val, bool uniqueISOCodesRequired, bool useISOCodeFilterMapping);
}