Files
DP44/DataPRO/Modules/ISO/ExtraProperties/Model/ExtraPropertyModel.cs
2026-04-17 14:55:32 -04:00

170 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DTS.Common.ISO.ExtraProperties;
using DTS.Common.Controls;
using DTS.Common.Enums;
using DTS.Common.Events;
using DTS.Common.Interface.ISO.ExtraProperties;
using Prism.Ioc;
using Prism.Events;
namespace ExtraProperties.Model
{
public class ExtraPropertyModel : DTS.Common.Base.BasePropertyChanged, IExtraProperty
{
//in case this object is held onto longer before cleanup, attempt to empty
//private storage
~ExtraPropertyModel()
{
_key = null;
_value = null;
PasteCommand = null;
}
public ExtraPropertyModel(IExtraProperty extraProperty)
{
RegisterCommands();
Key = extraProperty.Key;
Value = extraProperty.Value;
}
public ExtraPropertyModel()
{
RegisterCommands();
}
public ExtraPropertyModel(KeyValuePair<string, string> extraProperty)
{
RegisterCommands();
Key = extraProperty.Key;
Value = extraProperty.Value;
}
/// <summary>
/// status of the item in the UI
/// </summary>
private UIItemStatus _itemStatus = UIItemStatus.None;
public UIItemStatus ItemStatus
{
get => _itemStatus;
set => SetProperty(ref _itemStatus, value, "ItemStatus");
}
/// <summary>
/// the code (isocode or usercode) value
/// </summary>
private string _key;
public string Key
{
get => _key;
set => SetProperty(ref _key, value, "Key");
}
/// <summary>
/// the name (iso channel name or user channel name)
/// associated with code
/// </summary>
private string _value;
public string Value
{
get => _value;
set => SetProperty(ref _value, value, "Value");
}
public ICommand PasteCommand { get; set; }
/// <summary>
/// this could be passed into the constructors in the future, but since it's only used in one place right now, I'll just put it here
/// </summary>
public const string PASTE_ID = "ExtraProperty";
private void RegisterCommands()
{
PasteCommand = new PasteCommandClass(PASTE_ID);
CommandManager.RegisterClassCommandBinding(GetType(),
new CommandBinding(PasteCommand, Paste));
}
private void Paste(object sender, ExecutedRoutedEventArgs e)
{
}
}
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 IExtraProperty extraProperty))
{
if (tb.DataContext is ChannelCodeBuilder ccb)
{
extraProperty = ccb.DataContext as IExtraProperty;
}
else if (tb.DataContext is ChannelNameBuilder cnb)
{
extraProperty = cnb.DataContext as IExtraProperty;
}
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
extraProperty.Key = extraProperty.Key;
extraProperty.Value = extraProperty.Value;
eventAggregator.GetEvent<TextPastedEvent>().Publish(new TextPastedArgs(text, extraProperty, 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;
}
}
}