81 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using XSDVisualiser.Models;
namespace XSDVisualiser.Desktop.ViewModels
{
public class MainWindowViewModel(SchemaModel model) : INotifyPropertyChanged
{
private SchemaModel _model = model ?? throw new ArgumentNullException(nameof(model));
private SchemaNode? _selectedRootElement;
private SchemaNode? _selectedNode;
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public SchemaModel Model
{
get => _model;
set
{
if (ReferenceEquals(_model, value)) return;
_model = value ?? throw new ArgumentNullException(nameof(value));
OnPropertyChanged();
OnPropertyChanged(nameof(RootElements));
OnPropertyChanged(nameof(VisibleRootElements));
}
}
public IEnumerable<SchemaNode> RootElements => _model?.RootElements ?? Enumerable.Empty<SchemaNode>();
public SchemaNode? SelectedRootElement
{
get => _selectedRootElement;
set
{
if (EqualityComparer<SchemaNode?>.Default.Equals(_selectedRootElement, value)) return;
_selectedRootElement = value;
OnPropertyChanged();
OnPropertyChanged(nameof(VisibleRootElements));
}
}
public SchemaNode? SelectedNode
{
get => _selectedNode;
set
{
if (EqualityComparer<SchemaNode?>.Default.Equals(_selectedNode, value)) return;
_selectedNode = value;
OnPropertyChanged();
}
}
public IEnumerable<SchemaNode> VisibleRootElements => SelectedRootElement != null ? [SelectedRootElement] : RootElements;
private string? _currentFilePath;
public string? CurrentFilePath
{
get => _currentFilePath;
set
{
if (string.Equals(_currentFilePath, value, StringComparison.Ordinal)) return;
_currentFilePath = value;
OnPropertyChanged();
OnPropertyChanged(nameof(CurrentFileName));
OnPropertyChanged(nameof(CurrentDirectory));
}
}
public string? CurrentFileName => string.IsNullOrEmpty(_currentFilePath) ? null : System.IO.Path.GetFileName(_currentFilePath);
public string? CurrentDirectory => string.IsNullOrEmpty(_currentFilePath) ? null : System.IO.Path.GetDirectoryName(_currentFilePath);
}
}