S7Explorer/Models/S7Object.cs

120 lines
3.3 KiB
C#

using System.Collections.ObjectModel;
using System.ComponentModel;
using Newtonsoft.Json;
namespace S7Explorer.Models
{
public enum S7ObjectType
{
Project,
Device,
Folder,
DataBlock,
FunctionBlock,
Function,
Organization,
Symbol
}
public class S7Object : INotifyPropertyChanged
{
private string _name;
private string _description;
private bool _isExpanded;
public string Id { get; set; }
public string Number { get; set; }
[DisplayName("Nombre")]
public string Name
{
get => _name;
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
}
[DisplayName("Descripción")]
public string Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
[Browsable(false)]
public S7ObjectType ObjectType { get; set; }
[Browsable(false)]
public string IconSource => GetIconPath();
[Browsable(false)]
[JsonIgnore]
public S7Object Parent { get; set; }
[Browsable(false)]
public ObservableCollection<S7Object> Children { get; set; } = new ObservableCollection<S7Object>();
[Browsable(false)]
public bool IsExpanded
{
get => _isExpanded;
set
{
if (_isExpanded != value)
{
_isExpanded = value;
OnPropertyChanged(nameof(IsExpanded));
}
}
}
// Para búsquedas de texto
[Browsable(false)]
public bool ContainsText(string searchText)
{
if (string.IsNullOrWhiteSpace(searchText))
return false;
searchText = searchText.ToLowerInvariant();
return Name?.ToLowerInvariant().Contains(searchText) == true ||
Description?.ToLowerInvariant().Contains(searchText) == true ||
Number?.ToLowerInvariant().Contains(searchText) == true;
}
private string GetIconPath()
{
return ObjectType switch
{
S7ObjectType.Project => "/Resources/project.png",
S7ObjectType.Device => "/Resources/device.png",
S7ObjectType.Folder => "/Resources/folder.png",
S7ObjectType.DataBlock => "/Resources/db.png",
S7ObjectType.FunctionBlock => "/Resources/fb.png",
S7ObjectType.Function => "/Resources/fc.png",
S7ObjectType.Organization => "/Resources/ob.png",
S7ObjectType.Symbol => "/Resources/symbol.png",
_ => "/Resources/default.png"
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}