2014-07-28 17:18:18 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-09-07 11:25:39 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
using System.Collections.Specialized;
|
2014-07-28 17:18:18 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2014-08-12 13:33:13 +00:00
|
|
|
|
using RainmeterStudio.Core.Model;
|
2014-09-07 11:25:39 +00:00
|
|
|
|
using RainmeterStudio.Core.Utils;
|
2014-07-28 17:18:18 +00:00
|
|
|
|
|
|
|
|
|
namespace RainmeterStudio.UI.ViewModel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains the view model of a reference
|
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
public class ReferenceViewModel : INotifyPropertyChanged, INotifyCollectionChanged
|
2014-07-28 17:18:18 +00:00
|
|
|
|
{
|
2014-09-07 11:25:39 +00:00
|
|
|
|
private ObservableCollection<ReferenceViewModel> _children = new ObservableCollection<ReferenceViewModel>();
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
2014-07-28 17:18:18 +00:00
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the linked reference
|
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
public Reference Reference { get; private set; }
|
2014-07-28 17:18:18 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the name
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
return Reference.Name;
|
2014-07-28 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Path
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
return Reference.StoragePath;
|
2014-07-28 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an enumerable of this object's children
|
|
|
|
|
/// </summary>
|
2014-09-07 11:25:39 +00:00
|
|
|
|
public ObservableCollection<ReferenceViewModel> Children
|
2014-08-31 11:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _children;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-28 17:18:18 +00:00
|
|
|
|
|
|
|
|
|
private bool _isExpanded = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a property indicating if the tree view item is expanded
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsExpanded
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isExpanded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isExpanded = value;
|
|
|
|
|
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("IsExpanded"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _isSelected;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a property indicating if the tree view item is selected
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsSelected
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isSelected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isSelected = value;
|
|
|
|
|
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event triggered when a property is changed
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of reference view model
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reference">Reference</param>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
public ReferenceViewModel(Reference reference)
|
2014-07-28 17:18:18 +00:00
|
|
|
|
{
|
|
|
|
|
Reference = reference;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
Reference.CollectionChanged += Reference_CollectionChanged;
|
2014-09-07 11:25:39 +00:00
|
|
|
|
RefreshChildren();
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-07 11:25:39 +00:00
|
|
|
|
private void Reference_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2014-08-31 11:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
List<ReferenceViewModel> newItems = new List<ReferenceViewModel>();
|
|
|
|
|
List<ReferenceViewModel> oldItems = new List<ReferenceViewModel>();
|
|
|
|
|
|
|
|
|
|
// Update collection
|
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
|
|
|
newItems = e.NewItems.Cast<Reference>().Select(x => new ReferenceViewModel(x)).ToList();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
|
oldItems = _children.Where(x => e.OldItems.Contains(x.Reference)).ToList();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Replace:
|
|
|
|
|
newItems = e.NewItems.Cast<Reference>().Select(x => new ReferenceViewModel(x)).ToList();
|
|
|
|
|
oldItems = _children.Where(x => e.OldItems.Contains(x.Reference)).ToList();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2014-09-07 11:25:39 +00:00
|
|
|
|
RefreshChildren();
|
2014-08-31 11:41:24 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-07 11:25:39 +00:00
|
|
|
|
oldItems.ForEach(x => _children.Remove(x));
|
|
|
|
|
newItems.ForEach(_children.Add);
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
// Pass event
|
|
|
|
|
if (CollectionChanged != null)
|
|
|
|
|
CollectionChanged(this, new NotifyCollectionChangedEventArgs(e.Action, newItems, oldItems));
|
2014-07-28 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
#region Operations
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the 'IsExpanded' property for the entire subtree
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Value to set</param>
|
|
|
|
|
public void TreeExpand(bool value)
|
|
|
|
|
{
|
|
|
|
|
IsExpanded = value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-07 11:25:39 +00:00
|
|
|
|
private void RefreshChildren()
|
2014-08-31 11:41:24 +00:00
|
|
|
|
{
|
2014-09-07 11:25:39 +00:00
|
|
|
|
_children.Clear();
|
|
|
|
|
Reference.Children.Select(x => new ReferenceViewModel(x)).ForEach(_children.Add);
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when the linked reference collection changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
2014-07-28 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|