JD2022-TU1/main/tools/framework/JD.Application.TeaBox/View/PropertyGrid/PropertyGridSynchronizer.cs

94 lines
No EOL
3.2 KiB
C#

using System;
using System.Linq;
using JD.Collections;
using JD.Collections.Extensions;
using JD.Sequencers.Windowlesses;
using TeaBox;
using TeaBox.Core.Source.Gui;
using TeaBox.ObjectModel;
namespace JD.Application.View.PropertyGrid
{
/// <summary>
/// Responsible of showing the right content in the property grid.
///
/// Whenever the selection changes in the track view, the data source is updated to show the selected item.
/// </summary>
public class PropertyGridSynchronizer : IDisposable
{
public int MaxSelectedObjectCount = 100;
private readonly DataSourceContainer _gridDataSource;
private readonly Selection _selection;
private readonly IGridEditableDocument _document;
private object[] _editableSelection;
public PropertyGridSynchronizer(Selection selection, IGridEditableDocument document)
{
_gridDataSource = new DataSourceContainer("PropertyGridSynchronizer");
_selection = selection;
_document = document;
_selection.CollectionChanged += Selection_CollectionChanged;
Selection_CollectionChanged(null, null);
}
public void Dispose()
{
_gridDataSource.Dispose();
EditableSelection = new object[0];
}
public DataSourceContainer DataSource
{
get { return _gridDataSource; }
}
private object[] EditableSelection
{
get { return _editableSelection; }
set { _editableSelection = value; }
}
private void RefreshDataSourceName()
{
if (EditableSelection.IsEmpty())
{
_gridDataSource.DataSourceName = _document.TitleName;
}
else
{
// Show the selected items with the right name in the selection combo box
_gridDataSource.DataSourceName = EditableSelection.Length == 1
? EditableSelection.First().ToString()
: $"{EditableSelection.Length} selected objects";
}
}
private void RefreshDataSource()
{
if (EditableSelection.IsEmpty())
{
_gridDataSource.SetDataSource(_document.MainObject, null);
}
else if (_editableSelection.Length > MaxSelectedObjectCount)
{
string warning = $"{_editableSelection.Length} objects. Can edit maximum {MaxSelectedObjectCount} at a time";
_gridDataSource.SetDataSource(warning, null);
}
else
{
_gridDataSource.SetDataSources(EditableSelection, null);
}
}
void Selection_CollectionChanged(object sender, CollectionChangedEventArgs<WindowlessControl> e)
{
if (!_selection.IsUpdating)
{
EditableSelection = _selection == null ? new object[0] : _selection.Select(_document.ToPropertyGrid).NotNulls().ToArray();
RefreshDataSourceName();
RefreshDataSource();
}
}
}
}