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.
355 lines
14 KiB
355 lines
14 KiB
using MultiTerm.Core.ViewModel;
|
|
using System;
|
|
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 System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
|
|
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 itemsTextBoxTemplateName = "PART_ItemsTextBox";
|
|
private const string buttonClearTemplateName = "PART_ButtonClear";
|
|
private const string realizedItemsCountTemplateName = "PART_RealizedItemsCountDisplay";
|
|
private const string selectedAbsoluteTimeTextBlockTemplateName = "PART_SelectedAbsoluteTimeTextBlock";
|
|
private const string selectedTimediffTextBlockTemplateName = "PART_SelectedTimediffTextBlock";
|
|
private ListBox? itemsControl;
|
|
private ICollectionView? collectionView;
|
|
private TextBox? itemsTextBox;
|
|
private TextBlock? selectedAbsoluteTimeTextBlock;
|
|
private TextBlock? selectedTimediffTextBlock;
|
|
|
|
#region Dependency Properties
|
|
public static readonly DependencyProperty DataSourceProperty =
|
|
DependencyProperty.Register("DataSource",
|
|
typeof(MultiFormatDataViewModel), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(null, OnDataSourcePropertyChanged));
|
|
|
|
public static readonly DependencyProperty IsTimeDisplayedProperty =
|
|
DependencyProperty.Register("IsTimeDisplayed",
|
|
typeof(bool), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(false));
|
|
|
|
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 <see cref="DataSourceProperty"/>.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public MultiFormatDataViewModel DataSource
|
|
{
|
|
get { return (MultiFormatDataViewModel)GetValue(DataSourceProperty); }
|
|
set { SetValue(DataSourceProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for <see cref="IsTimeDisplayedProperty"/>.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public bool IsTimeDisplayed
|
|
{
|
|
get { return (bool)GetValue(IsTimeDisplayedProperty); }
|
|
set { SetValue(IsTimeDisplayedProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for <see cref="RealizedItemsCountProperty"/>.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public uint RealizedItemsCount
|
|
{
|
|
get { return (uint)GetValue(RealizedItemsCountProperty); }
|
|
set { SetValue(RealizedItemsCountProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for <see cref="ItemLoadedProperty"/>.
|
|
/// </summary>
|
|
public bool ItemLoaded
|
|
{
|
|
get { return (bool)GetValue(ItemLoadedProperty); }
|
|
set { SetValue(ItemLoadedProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for <see cref="RealizedItemsCountProperty"/>.
|
|
/// </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();
|
|
|
|
// 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 itemsTextBox from template
|
|
if (GetTemplateChild(itemsTextBoxTemplateName) is TextBox tb)
|
|
{
|
|
this.itemsTextBox = tb;
|
|
this.itemsTextBox.SelectionChanged += TextBoxCharOnlyView_SelectionChanged;
|
|
this.itemsTextBox.TextChanged += TextBoxCharOnlyView_TextChanged;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Implementation fault, {itemsTextBoxTemplateName} not found in template.");
|
|
}
|
|
|
|
// register to button event
|
|
if (GetTemplateChild(buttonClearTemplateName) is Button button)
|
|
{
|
|
button.Click += OnClearButtonClicked; ;
|
|
}
|
|
|
|
// hide realized items count when not debugging
|
|
if (Debugger.IsAttached == false)
|
|
{
|
|
if (GetTemplateChild(realizedItemsCountTemplateName) is UIElement realizedItemsUiElement)
|
|
{
|
|
realizedItemsUiElement.Visibility = Visibility.Hidden;
|
|
}
|
|
}
|
|
|
|
// get time textblocks
|
|
if (GetTemplateChild(selectedAbsoluteTimeTextBlockTemplateName) is TextBlock satTb)
|
|
{
|
|
this.selectedAbsoluteTimeTextBlock = satTb;
|
|
}
|
|
if (GetTemplateChild(selectedTimediffTextBlockTemplateName) is TextBlock stdTb)
|
|
{
|
|
this.selectedTimediffTextBlock = stdTb;
|
|
}
|
|
}
|
|
|
|
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// extract instance and guard null
|
|
if (d is not MultiFormatDataView mfdv) { return; }
|
|
// guard DataSource is null
|
|
if (mfdv.DataSource == null) { 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 bindings
|
|
CreateTemplateBinding($"{nameof(mfdv.DataSource)}.{nameof(mfdv.DataSource.DataAsString)}", mfdv.itemsTextBox, TextBox.TextProperty);
|
|
CreateTemplateBinding($"{nameof(mfdv.DataSource)}.{nameof(mfdv.DataSource.SelectedDataFirstAbsoluteTime)}", mfdv.selectedAbsoluteTimeTextBlock, TextBlock.TextProperty);
|
|
CreateTemplateBinding($"{nameof(mfdv.DataSource)}.{nameof(mfdv.DataSource.SelectedDataTimediff)}", mfdv.selectedTimediffTextBlock, TextBlock.TextProperty);
|
|
}
|
|
|
|
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]);
|
|
//}
|
|
}
|
|
|
|
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)
|
|
{
|
|
// get sender as listbox
|
|
if(e.OriginalSource is not ListBox senderListBox) { throw new ArgumentException($"{nameof(ItemsControl_SelectionChanged)} got non expected type as sender {sender}"); }
|
|
|
|
// if there is something selected in the textbox => clear selection first
|
|
if (this.itemsTextBox != null && this.itemsTextBox.SelectionLength > 0) { this.itemsTextBox.Select(0, 0); }
|
|
|
|
// get selected items from list box
|
|
var selectedItems = senderListBox.SelectedItems.OfType<ByteDataViewModel>().ToList();
|
|
|
|
// sort items according to time (uses IComparable of ByteDataViewModel)
|
|
// wpf does some odd sorting. described here: https://stackoverflow.com/questions/50155415/wpf-listview-selecteditems-returns-wrong-item-order-if-you-shift-select-from-b
|
|
// sorting the items by ticks provides a solution in this case
|
|
selectedItems.Sort();
|
|
|
|
// genereate new observable collection
|
|
this.DataSource.Selected = new ObservableCollection<ByteDataViewModel>(selectedItems);
|
|
}
|
|
|
|
private void TextBoxCharOnlyView_SelectionChanged(object sender, RoutedEventArgs e)
|
|
{
|
|
var newSelection = new ObservableCollection<ByteDataViewModel>();
|
|
int selectionStartIndex = this.itemsTextBox!.SelectionStart;
|
|
// extract text from the beginning to the start of the selected text
|
|
var textFromBeginningToStartOfSelection = this.itemsTextBox!.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(MultiFormatDataViewModel.NewlineSequence).Length;
|
|
// any newline sequences introduced (more than one lines found)
|
|
if (foundAmountOfLines > 1)
|
|
{
|
|
// calculated amount of characters that were introduced
|
|
foundManuallyIntroducedNewlineSequenceCharacters = (foundAmountOfLines - 1) * MultiFormatDataViewModel.NewlineSequence.Length;
|
|
}
|
|
|
|
// iterate through length of selection
|
|
for (int i = 0; i < this.itemsTextBox!.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.itemsTextBox?.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
|
|
|
|
#region Helpers
|
|
private static void CreateTemplateBinding(string propertyPath, DependencyObject? dependencyObject, DependencyProperty dependencyProperty)
|
|
{
|
|
// ignore when dependency object is null
|
|
if(dependencyObject == null) { return; }
|
|
|
|
// create
|
|
var binding = new Binding()
|
|
{
|
|
RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
|
|
Path = new PropertyPath(propertyPath)
|
|
};
|
|
|
|
// bind
|
|
BindingOperations.SetBinding(dependencyObject, dependencyProperty, binding);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|