169 lines
5.5 KiB
C#
169 lines
5.5 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// 다중 선택 드롭다운 속성 데이터 클래스입니다.
|
|
/// UI는 UTKMultiSelectDropdownPropertyItemView에서 담당합니다.
|
|
/// </summary>
|
|
public class UTKMultiSelectDropdownPropertyItem : UTKPropertyItemBase<List<string>>
|
|
{
|
|
#region Fields
|
|
private List<string> _choices;
|
|
#endregion
|
|
|
|
#region Properties
|
|
/// <summary>속성 타입</summary>
|
|
public override UTKPropertyType PropertyType => UTKPropertyType.MultiSelectDropdownList;
|
|
|
|
/// <summary>선택 가능한 항목 목록</summary>
|
|
public List<string> Choices
|
|
{
|
|
get => _choices;
|
|
set => _choices = value ?? new List<string>();
|
|
}
|
|
|
|
/// <summary>선택된 인덱스 목록</summary>
|
|
public List<int> SelectedIndices
|
|
{
|
|
get
|
|
{
|
|
if (Value == null || Value.Count == 0)
|
|
return new List<int>();
|
|
|
|
return Value
|
|
.Select(v => _choices.IndexOf(v))
|
|
.Where(i => i >= 0)
|
|
.OrderBy(i => i)
|
|
.ToList();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
/// <summary>
|
|
/// 다중 선택 드롭다운 속성을 생성합니다.
|
|
/// </summary>
|
|
/// <param name="id">고유 ID</param>
|
|
/// <param name="name">표시 이름</param>
|
|
/// <param name="choices">선택 항목 목록</param>
|
|
/// <param name="initialValues">초기 선택 값 목록</param>
|
|
/// <param name="isReadOnly">읽기 전용 여부</param>
|
|
/// <param name="showLabel">라벨 표시 여부</param>
|
|
public UTKMultiSelectDropdownPropertyItem(
|
|
string id,
|
|
string name,
|
|
List<string> choices,
|
|
List<string>? initialValues = null,
|
|
bool isReadOnly = false,
|
|
bool showLabel = true)
|
|
: base(id, name, initialValues ?? new List<string>())
|
|
{
|
|
_choices = choices ?? new List<string>();
|
|
|
|
// initialValues가 유효한지 확인하고 필터링
|
|
if (initialValues != null && initialValues.Count > 0)
|
|
{
|
|
var validValues = initialValues.Where(v => _choices.Contains(v)).ToList();
|
|
_value = validValues;
|
|
}
|
|
|
|
_isReadOnly = isReadOnly;
|
|
_showLabel = showLabel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 다중 선택 드롭다운 속성을 생성합니다 (인덱스 기반).
|
|
/// </summary>
|
|
/// <param name="id">고유 ID</param>
|
|
/// <param name="name">표시 이름</param>
|
|
/// <param name="choices">선택 항목 목록</param>
|
|
/// <param name="selectedIndices">초기 선택 인덱스 목록</param>
|
|
/// <param name="isReadOnly">읽기 전용 여부</param>
|
|
/// <param name="showLabel">라벨 표시 여부</param>
|
|
public UTKMultiSelectDropdownPropertyItem(
|
|
string id,
|
|
string name,
|
|
IEnumerable<string> choices,
|
|
IEnumerable<int>? selectedIndices = null,
|
|
bool isReadOnly = false,
|
|
bool showLabel = true)
|
|
: base(id, name, new List<string>())
|
|
{
|
|
_choices = choices?.ToList() ?? new List<string>();
|
|
|
|
if (selectedIndices != null)
|
|
{
|
|
var validValues = selectedIndices
|
|
.Where(i => i >= 0 && i < _choices.Count)
|
|
.Select(i => _choices[i])
|
|
.ToList();
|
|
_value = validValues;
|
|
}
|
|
|
|
_isReadOnly = isReadOnly;
|
|
_showLabel = showLabel;
|
|
}
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
/// <summary>선택 항목 추가</summary>
|
|
public void AddChoice(string choice)
|
|
{
|
|
if (!_choices.Contains(choice))
|
|
{
|
|
_choices.Add(choice);
|
|
}
|
|
}
|
|
|
|
/// <summary>선택 항목 제거</summary>
|
|
public bool RemoveChoice(string choice)
|
|
{
|
|
bool removed = _choices.Remove(choice);
|
|
if (removed && Value != null && Value.Contains(choice))
|
|
{
|
|
// 값 목록에서도 제거
|
|
var newValue = Value.Where(v => v != choice).ToList();
|
|
Value = newValue;
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
/// <summary>인덱스로 선택 설정</summary>
|
|
public void SetSelectedIndices(List<int> indices)
|
|
{
|
|
var validValues = indices
|
|
.Where(i => i >= 0 && i < _choices.Count)
|
|
.Select(i => _choices[i])
|
|
.Distinct()
|
|
.ToList();
|
|
Value = validValues;
|
|
}
|
|
|
|
/// <summary>값으로 선택 설정</summary>
|
|
public void SetSelectedValues(List<string> values)
|
|
{
|
|
var validValues = values
|
|
.Where(v => _choices.Contains(v))
|
|
.Distinct()
|
|
.ToList();
|
|
Value = validValues;
|
|
}
|
|
|
|
/// <summary>모든 항목 선택</summary>
|
|
public void SelectAll()
|
|
{
|
|
Value = new List<string>(_choices);
|
|
}
|
|
|
|
/// <summary>모든 선택 해제</summary>
|
|
public void ClearSelection()
|
|
{
|
|
Value = new List<string>();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|