Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
12 KiB

using MultiTerm.Core.ViewModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Linq;
using System.Collections.Specialized;
using MultiTerm.Protocols.Model;
using System.Collections.ObjectModel;
namespace MultiTerm.Wpf.CustomControl;
public class MultiFormatDataView : Control
{
private static readonly Dictionary<StackPanel, MultiFormatDataView> itemParentPairs = new();
private const string itemsControlTemplateName = "PART_ItemsControl";
private const string buttonClearTemplateName = "PART_ButtonClear";
private const string textBoxCharOnlyViewTemplateName = "PART_CharOnlyTextBox";
private ListBox? itemsControl;
private TextBox? tbCharOnlyView;
private ICollectionView? collectionView;
#region Dependency Properties
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register("DataSource",
typeof(ICommunicationDataViewModel<ByteDataViewModel, ExtendedByte>), typeof(MultiFormatDataView),
new PropertyMetadata(null, OnDataSourcePropertyChanged));
public static readonly DependencyProperty RealizedItemsCountProperty =
DependencyProperty.Register("RealizedItemsCount",
typeof(uint), typeof(MultiFormatDataView),
new PropertyMetadata((uint)0, OnRealizedItemsCountChanged));
public static readonly DependencyProperty ItemLoadedProperty =
DependencyProperty.RegisterAttached("ItemLoaded",
typeof(bool),
typeof(MultiFormatDataView),
new UIPropertyMetadata(false, OnItemLoaded));
public static readonly DependencyProperty ItemUnloadedProperty =
DependencyProperty.RegisterAttached("ItemUnloaded",
typeof(bool),
typeof(MultiFormatDataView),
new UIPropertyMetadata(false, OnItemUnloaded));
public static readonly RoutedEvent ClearRequestedEvent;
/// <summary>
/// .NET Property for DataSourceProperty.
/// </summary>
[Bindable(true)]
public ICommunicationDataViewModel<ByteDataViewModel, ExtendedByte> DataSource
{
get { return (ICommunicationDataViewModel<ByteDataViewModel, ExtendedByte>)GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
/// <summary>
/// .NET Property for RealizedItemsCount.
/// </summary>
[Bindable(true)]
public uint RealizedItemsCount
{
get { return (uint)GetValue(RealizedItemsCountProperty); }
set { SetValue(RealizedItemsCountProperty, value); }
}
/// <summary>
/// .NET Property for ItemLoaded.
/// </summary>
public bool ItemLoaded
{
get { return (bool)GetValue(ItemLoadedProperty); }
set { SetValue(ItemLoadedProperty, value); }
}
/// <summary>
/// .NET Property for ItemUnloaded.
/// </summary>
public bool ItemUnloaded
{
get { return (bool)GetValue(ItemUnloadedProperty); }
set { SetValue(ItemUnloadedProperty, value); }
}
/// <summary>
/// .NET Property for <see cref="ClearRequestedEvent"/>
/// </summary>
public event RoutedEventHandler ClearRequested
{
add { this.AddHandler(ClearRequestedEvent, value); }
remove { this.RemoveHandler(ClearRequestedEvent, value); }
}
#endregion
static MultiFormatDataView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView)));
ClearRequestedEvent = EventManager.RegisterRoutedEvent("ClearRequested",
RoutingStrategy.Bubble, typeof(RoutedEventArgs),
typeof(MultiFormatDataView));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.Unloaded += MultiFormatDataView_Unloaded;
// get itemsControl from template
if (GetTemplateChild(itemsControlTemplateName) is ListBox listBox)
{
this.itemsControl = listBox;
this.itemsControl.SelectionChanged += ItemsControl_SelectionChanged;
}
else
{
throw new Exception($"Implementation fault, {itemsControlTemplateName} not found in template.");
}
// get textBox from template
if (GetTemplateChild(textBoxCharOnlyViewTemplateName) is TextBox tb)
{
this.tbCharOnlyView = tb;
this.tbCharOnlyView.SelectionChanged += TextBoxCharOnlyView_SelectionChanged;
this.tbCharOnlyView.TextChanged += TextBoxCharOnlyView_TextChanged;
}
else
{
throw new Exception($"Implementation fault, {textBoxCharOnlyViewTemplateName} not found in template.");
}
// get button from template, ignore if it does not exist
if (GetTemplateChild(buttonClearTemplateName) is Button button)
{
button.Click += OnClearButtonClicked; ;
}
}
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// extract instance and guard null
if (d is not MultiFormatDataView mfdv) { return; }
// manually create collection view
ICollectionView cv = CollectionViewSource.GetDefaultView(mfdv.DataSource.Data);
// add grouping
PropertyGroupDescription groupDescription = new(nameof(ByteDataViewModel.LineIdentifier));
cv.GroupDescriptions.Add(groupDescription);
// add live grouping
if (cv is ICollectionViewLiveShaping cvLiveShaping && cvLiveShaping.CanChangeLiveGrouping)
{
cvLiveShaping.LiveGroupingProperties.Add(nameof(ByteDataViewModel.LineIdentifier));
cvLiveShaping.IsLiveGrouping = true;
}
// save collection view
mfdv.collectionView = cv;
// apply collection view as itemssource
mfdv.itemsControl!.ItemsSource = cv;
// register to collection changed event
if (mfdv.DataSource.Data is INotifyCollectionChanged incc)
{
incc.CollectionChanged += mfdv.Data_CollectionChanged;
}
// create binding for textbox (DataAsString)
var binding = new Binding()
{
RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
Path = new PropertyPath($"{nameof(mfdv.DataSource)}.{nameof(mfdv.DataSource.DataAsString)}")
};
BindingOperations.SetBinding(mfdv.tbCharOnlyView, TextBox.TextProperty, binding);
}
private void Data_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
// TEMP scroll to added item if not null
//if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null && e.NewItems[0] != null)
//{
// this.itemsControl!.ScrollIntoView(e.NewItems[0]);
//}
}
/// <summary>
/// On Unload remove event handler.
/// </summary>
private void MultiFormatDataView_Unloaded(object sender, RoutedEventArgs e)
{
if (DataSource is INotifyCollectionChanged incc)
{
incc.CollectionChanged -= Data_CollectionChanged;
}
}
private void OnClearButtonClicked(object sender, RoutedEventArgs e)
{
// raise clear requested event
RoutedEventArgs args = new(ClearRequestedEvent);
RaiseEvent(args);
}
#region Selected Items handling
private void ItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// if there are no changes => return
if (e.AddedItems.Count <= 0 && e.RemovedItems.Count <= 0) { return; }
// if there is something selected in the textbox => clear selection first
if (this.tbCharOnlyView != null && this.tbCharOnlyView.SelectionLength > 0) { this.tbCharOnlyView.Select(0, 0); }
// otherwise update internal list
foreach (ByteDataViewModel item in e.RemovedItems)
{
this.DataSource.Selected.Remove(item);
}
foreach (ByteDataViewModel item in e.AddedItems)
{
this.DataSource.Selected.Add(item);
}
}
private void TextBoxCharOnlyView_SelectionChanged(object sender, RoutedEventArgs e)
{
var newSelection = new ObservableCollection<ByteDataViewModel>();
int selectionStartIndex = this.tbCharOnlyView!.SelectionStart;
// TEMP OLD
// extract text from the beginning to the start of the selected text
var textFromBeginningToStartOfSelection = this.tbCharOnlyView!.Text.Substring(0, selectionStartIndex);
// count amount of manually introduced newline sequences in this text section (these to not exist in the data source!)
var foundManuallyIntroducedNewlineSequenceCharacters = 0;
var foundAmountOfLines = textFromBeginningToStartOfSelection.Split(CommunicationDataViewModel.NewlineSequence).Length;
// any newline sequences introduced (more than one lines found)
if (foundAmountOfLines > 1)
{
// calculated amount of characters that were introduced
foundManuallyIntroducedNewlineSequenceCharacters = (foundAmountOfLines - 1) * CommunicationDataViewModel.NewlineSequence.Length;
}
// iterate through length of selection
for (int i = 0; i < this.tbCharOnlyView!.SelectionLength; i++)
{
// subtracting the counted newline sequences and adding i (length)
int elementPositionInCollection = selectionStartIndex - foundManuallyIntroducedNewlineSequenceCharacters + i;
// add element to new selection list
newSelection.Add(this.DataSource.Data.ElementAt(elementPositionInCollection));
// next item does not exist => break loop
if(this.DataSource.Data.ElementAtOrDefault(elementPositionInCollection + 1) == null)
{
break;
}
}
// update property
this.DataSource.Selected = newSelection;
}
private void TextBoxCharOnlyView_TextChanged(object sender, TextChangedEventArgs e)
{
this.tbCharOnlyView?.ScrollToEnd();
}
#endregion
#region Realized Item Count
private static void OnRealizedItemsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// NOP
}
private static void OnItemLoaded(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// extract instance and guard null
if (d is not StackPanel stackPanel) { return; }
// check if value was set to true
if (e.NewValue is bool boolean && boolean == true)
{
// find visual parent of correct type, throw exception if not found
var parentMFDV = UIHelper.FindVisualParent<MultiFormatDataView>(stackPanel) ??
throw new NullReferenceException($"Could not find parent of type " +
$"{nameof(MultiFormatDataView)} in {nameof(stackPanel)}");
// add to static dictionary
itemParentPairs.Add(stackPanel, parentMFDV);
// increment counter
parentMFDV.RealizedItemsCount++;
}
}
private static void OnItemUnloaded(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// extract instance and guard null
if (d is not StackPanel stackPanel) { return; }
// check if value was set to true
if (e.NewValue is bool boolean && boolean == true)
{
// get parent from static dictionary
var parentMFDV = itemParentPairs[stackPanel];
// remove the element from the dictionary
itemParentPairs.Remove(stackPanel);
// decrement counter
parentMFDV.RealizedItemsCount--;
}
}
#endregion
}