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.
380 lines
14 KiB
380 lines
14 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 MultiTerm.Wpf.CustomControl.ValueConverter;
|
|
using System.Diagnostics.Metrics;
|
|
|
|
namespace MultiTerm.Wpf.CustomControl;
|
|
|
|
public class MultiFormatDataView : Control
|
|
{
|
|
private static readonly Dictionary<StackPanel, MultiFormatDataView> itemParentPairs = new();
|
|
private const string itemsControlTemplateName = "itemsControl";
|
|
private const string buttonClearTemplateName = "btnClear";
|
|
private const string selectorTemplateName = "comboBoxSelector";
|
|
private const string textBoxCharOnlyViewTemplateName = "textBoxCharactersOnlyView";
|
|
private ListBox? itemsControl;
|
|
private TextBox? tbCharOnlyView;
|
|
|
|
#region Dependency Properties
|
|
public static readonly DependencyProperty DataSourceProperty =
|
|
DependencyProperty.Register("DataSource",
|
|
typeof(IEnumerable), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(null, OnDataSourcePropertyChanged));
|
|
|
|
public static readonly DependencyProperty SelectedItemsProperty =
|
|
DependencyProperty.Register("SelectedItems",
|
|
typeof(IList), typeof(MultiFormatDataView),
|
|
new FrameworkPropertyMetadata(OnSelectedItemsPropertyChanged)
|
|
{
|
|
BindsTwoWayByDefault = false
|
|
});
|
|
|
|
public static readonly DependencyProperty SelectorItemsSourceProperty =
|
|
DependencyProperty.Register("SelectorItemsSource",
|
|
typeof(IEnumerable), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(null, OnSelectorItemsSourceChanged));
|
|
|
|
public static readonly DependencyProperty SelectorSelectedItemProperty =
|
|
DependencyProperty.Register("SelectorSelectedItem",
|
|
typeof(object), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(null, OnSelectorSelectedItemChanged));
|
|
|
|
public static readonly DependencyProperty SelectorDescriptionProperty =
|
|
DependencyProperty.Register("SelectorDescription",
|
|
typeof(string), typeof(MultiFormatDataView),
|
|
new PropertyMetadata(string.Empty, OnSelectorDescriptionChanged));
|
|
|
|
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 DataSource.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public IEnumerable DataSource
|
|
{
|
|
get { return (IEnumerable)GetValue(DataSourceProperty); }
|
|
set { SetValue(DataSourceProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for SelectedItems.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public IList SelectedItems
|
|
{
|
|
get { return (IList)GetValue(SelectedItemsProperty); }
|
|
set { SetValue(SelectedItemsProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for SelectorItemsSource.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public IEnumerable SelectorItemsSource
|
|
{
|
|
get { return (IEnumerable)GetValue(SelectorItemsSourceProperty); }
|
|
set { SetValue(SelectorItemsSourceProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for SelectorSelectedItem.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public string SelectorSelectedItem
|
|
{
|
|
get { return (string)GetValue(SelectorSelectedItemProperty); }
|
|
set { SetValue(SelectorSelectedItemProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// .NET Property for SelectorDescription.
|
|
/// </summary>
|
|
[Bindable(true)]
|
|
public string SelectorDescription
|
|
{
|
|
get { return (string)GetValue(SelectorDescriptionProperty); }
|
|
set { SetValue(SelectorDescriptionProperty, 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.SelectedItems = new List<DataViewModel>();
|
|
|
|
// 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 button from template, ignore if it does not exist
|
|
if (GetTemplateChild(buttonClearTemplateName) is Button button)
|
|
{
|
|
button.Click += OnClearButtonClicked; ;
|
|
}
|
|
|
|
// get textBox from template, ignore if it does not exist
|
|
if (GetTemplateChild(textBoxCharOnlyViewTemplateName) is TextBox tb)
|
|
{
|
|
this.tbCharOnlyView = tb;
|
|
this.tbCharOnlyView.SelectionChanged += TextBoxCharOnlyView_SelectionChanged;
|
|
}
|
|
}
|
|
|
|
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// extract instance and guard null
|
|
if (d is not MultiFormatDataView mfdv) { return; }
|
|
|
|
// extract instance of new Value and check if correct type
|
|
if (e.NewValue is not IEnumerable<DataViewModel> newDataSource)
|
|
{
|
|
throw new ArgumentException($"{nameof(MultiFormatDataView)}: {nameof(DataSourceProperty)} must be of type {nameof(IEnumerable<DataViewModel>)}");
|
|
}
|
|
|
|
// TODO REMOVE
|
|
//// validate that no characters were removed
|
|
//if(oldDataSource != null && oldDataSource.Count() > newDataSource.Count())
|
|
//{
|
|
// throw new NotImplementedException($"{nameof(MultiFormatDataView)} cannot handle removing single items from DataSource. " +
|
|
// $"Only adding and clearing ({nameof(DataSourceProperty)} = null) are supported.");
|
|
//}
|
|
|
|
//// iterate through data, adding content to textbox
|
|
//int prevCounter = newDataSource.First().LineIdentifier;
|
|
//for(int i = 0; i < newDataSource.Count(); i++)
|
|
//{
|
|
// DataViewModel item = newDataSource.ElementAt(i);
|
|
// DataViewModel? oldItem = oldDataSource?.ElementAtOrDefault(i);
|
|
|
|
// // if old item at this position exists and it equals the new item => skip
|
|
// if(oldItem != null && oldItem.Equals(item))
|
|
// {
|
|
// continue;
|
|
// }
|
|
|
|
// // new item found: add it as content to textbox
|
|
// if (mfdv.tbCharOnlyView != null)
|
|
// {
|
|
// // newline if previous counter is lower than this
|
|
// if (item.LineIdentifier > prevCounter)
|
|
// {
|
|
// mfdv.tbCharOnlyView.Text += "\n";
|
|
// prevCounter = item.LineIdentifier;
|
|
// }
|
|
// // add character (text)
|
|
// mfdv.tbCharOnlyView.Text += item.DisplayStringUtf16;
|
|
// }
|
|
//}
|
|
|
|
// add group property to support grouping of VirtualizingWrapPanel
|
|
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(e.NewValue);
|
|
PropertyGroupDescription groupDescription = new(nameof(DataViewModel.LineIdentifier));
|
|
view.GroupDescriptions.Add(groupDescription);
|
|
}
|
|
|
|
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 (DataViewModel item in e.RemovedItems)
|
|
{
|
|
this.SelectedItems.Remove(item);
|
|
}
|
|
foreach (DataViewModel item in e.AddedItems)
|
|
{
|
|
this.SelectedItems.Add(item);
|
|
}
|
|
}
|
|
|
|
private void TextBoxCharOnlyView_SelectionChanged(object sender, RoutedEventArgs e)
|
|
{
|
|
var newSelection = new List<DataViewModel>();
|
|
int selectionStartIndex = this.tbCharOnlyView!.SelectionStart;
|
|
// 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 foundManuallyIntroducedNewlineSequences = textFromBeginningToStartOfSelection.Count((x) => x == DataViewModelToStringConverter.NewlineSequence);
|
|
// convert datasource
|
|
var collection = ((IEnumerable<DataViewModel>)this.DataSource);
|
|
|
|
// 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 - foundManuallyIntroducedNewlineSequences + i;
|
|
// add element to new selection list
|
|
newSelection.Add(collection.ElementAt(elementPositionInCollection));
|
|
// next item does not exist => break loop
|
|
if(collection.ElementAtOrDefault(elementPositionInCollection + 1) == null)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
// update property
|
|
this.SelectedItems = newSelection;
|
|
}
|
|
|
|
private static void OnSelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// NOP
|
|
}
|
|
#endregion
|
|
|
|
#region Selector (ComboBox) handling
|
|
private static void OnSelectorItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// extract instance and guard null
|
|
//if (d is not MultiFormatDataView mfdv) { return; }
|
|
// extract instance of new Value and guard null
|
|
//if (e.NewValue is not IEnumerable enumerable) { return; }
|
|
|
|
// nothing to do
|
|
}
|
|
|
|
private static void OnSelectorDescriptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// nothing to do
|
|
}
|
|
|
|
private static void OnSelectorSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
// nothing to do
|
|
}
|
|
|
|
#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
|
|
}
|
|
|