JD2022-TU1/main/tools/modules/JD.SceneExplorer/ItemAdapters/DefaultTreeListBoxItemAdapter.cs

229 lines
9 KiB
C#

using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using ActiproSoftware.Windows.Controls.Grids;
using JD.SceneExplorer.ViewModels;
using JD.WPF.Models;
namespace JD.SceneExplorer.ItemAdapters
{
/// <summary>
/// An adapter that can provide an item's hierarchy and visual state data for usage in a <see cref="TreeListBox"/>.
/// This default implementation is intended to specifically adapt <see cref="TreeViewItemData"/>
/// and is geared for high-performance due to the various get/set method overrides
/// instead of using bindings for updates.
/// </summary>
public class DefaultTreeListBoxItemAdapter : TreeListBoxItemAdapter
{
/////////////////////////////////////////////////////////////////////////////////////////////////////
// OBJECT
/////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new instance of the <see cref="DefaultTreeListBoxItemAdapter"/> class.
/// </summary>
public DefaultTreeListBoxItemAdapter()
{
// Setting these properties tells the adapter which properties to watch for INotifyPropertyChanged updates
// so that the UI can receive the updated values without binding usage
ChildrenPath = "Children";
IsEditingPath = "IsEditing";
IsExpandedPath = "IsExpanded";
IsLoadingPath = "IsLoading";
IsSelectablePath = "IsSelectable";
IsSelectedPath = "IsSelected";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC PROCEDURES
/////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Returns an <see cref="IEnumerable"/> that will be used to provide child items for the specified parent item.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>An <see cref="IEnumerable"/> that will be used to provide child items for the specified parent item.</returns>
public override IEnumerable GetChildren(TreeListBox ownerControl, object item)
{
if (item is ObjectWithRef model)
{
return model.Children;
}
return new ObservableCollection<ObjectWithRef>();
}
/// <summary>
/// Returns whether the specified item is draggable.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is draggable; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsDraggable(TreeListBox ownerControl, object item)
{
return true;
}
/// <summary>
/// Returns whether the specified item is editable.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is editable; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsEditable(TreeListBox ownerControl, object item)
{
return false;
}
/// <summary>
/// Returns whether the specified item is currently being edited.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is currently being edited; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsEditing(TreeListBox ownerControl, object item)
{
return false;
}
/// <summary>
/// Returns whether the specified item is expanded.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is expanded; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsExpanded(TreeListBox ownerControl, object item)
{
if (item is RootSceneTree)
{
return true;
}
if (item is ObjectWithRef model)
{
return model.IsExpanded;
}
return false;
}
/// <inheritdoc />
public override TreeItemExpandability GetExpandability(TreeListBox ownerControl, object item, int depth)
{
if (item is ObjectWithRef model)
{
return model.Children.Any() ? TreeItemExpandability.Auto : TreeItemExpandability.Never;
}
return base.GetExpandability(ownerControl, item, depth);
}
/// <summary>
/// Returns whether the specified item is currently loading children asynchronously.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is currently loading children asynchronously; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsLoading(TreeListBox ownerControl, object item)
{
return false;
}
/// <summary>
/// Returns whether the specified item is capable of being selected.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is capable of being selected; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsSelectable(TreeListBox ownerControl, object item)
{
return true;
}
/// <summary>
/// Returns whether the specified item is selected.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>
/// <c>true</c> if the specified item is selected; otherwise, <c>false</c>.
/// </returns>
public override bool GetIsSelected(TreeListBox ownerControl, object item)
{
return (item is ObjectWithRef model && model.IsSelected);
}
/// <summary>
/// Returns the item's string path.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>The item's string path.</returns>
public override string GetPath(TreeListBox ownerControl, object item)
{
return (item is ObjectWithRef model ? model.FriendlyName : null);
}
/// <summary>
/// Returns the item's text by which to match when searching.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to examine.</param>
/// <returns>The item's string search text.</returns>
public override string GetSearchText(TreeListBox ownerControl, object item)
{
return (item is ObjectWithRef model ? model.FriendlyName : null);
}
/// <summary>
/// Sets whether the specified item is currently being edited.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to update.</param>
/// <param name="value">The new value.</param>
public override void SetIsEditing(TreeListBox ownerControl, object item, bool value)
{
}
/// <summary>
/// Sets whether the specified item is expanded.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to update.</param>
/// <param name="value">The new value.</param>
public override void SetIsExpanded(TreeListBox ownerControl, object item, bool value)
{
if (item is ObjectWithRef model)
{
model.IsExpanded = value;
}
}
/// <summary>
/// Sets whether the specified item is selected.
/// </summary>
/// <param name="ownerControl">The owner control.</param>
/// <param name="item">The item to update.</param>
/// <param name="value">The new value.</param>
public override void SetIsSelected(TreeListBox ownerControl, object item, bool value)
{
if (item is ObjectWithRef model)
{
model.IsSelected = value;
}
}
}
}