using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;
using CBR.Core.Helpers;
using CBR.Core.Models;
using CBR.Core.Services;
using System.Windows;
using CBR.Views;
namespace CBR.ViewModels
{
public class ExplorerViewModel : ViewModelBase
{
#region ----------------CONSTRUCTOR----------------
public ExplorerViewModel()
{
//register to the mediator for messages
Mediator.Instance.Register(
(Object o) =>
{
CatalogData = (Catalog)o;
},
ViewModelMessages.CatalogChanged);
Mediator.Instance.Register(
(Object o) =>
{
Sort( o as string );
},
ViewModelMessages.ExplorerSort);
Mediator.Instance.Register(
(Object o) =>
{
Group( o as string );
},
ViewModelMessages.ExplorerGroup);
Mediator.Instance.Register(
(Object o) =>
{
ChangeExplorerView(o as string);
},
ViewModelMessages.ExplorerView);
}
#endregion
#region ----------------PROPERTIES----------------
private Catalog _catalogData = new Catalog();
/// <summary>
/// owns a copy from MainViewModel
/// </summary>
public Catalog CatalogData
{
get { return _catalogData; }
set
{
if (_catalogData != value)
{
_catalogData = value;
RaisePropertyChanged("CatalogData");
RaisePropertyChanged("Books");
}
}
}
/// <summary>
/// the collection for list binding
/// </summary>
public ICollectionView Books
{
get
{
if (CatalogData != null)
{
return CollectionViewSource.GetDefaultView(CatalogData.Books);
}
else
return null;
}
}
private string _searchedText = string.Empty;
/// <summary>
/// filter string from ribbon
/// </summary>
public string SearchedText
{
get { return _searchedText; }
set
{
_searchedText = value;
Books.Filter = delegate(object obj)
{
if (String.IsNullOrEmpty(_searchedText))
return true;
Book bk = obj as Book;
if (bk == null)
return false;
return (bk.FileName.IndexOf(_searchedText, 0, StringComparison.InvariantCultureIgnoreCase) > -1);
};
}
}
private bool _IsExplorerViewThumb = true;
/// <summary>
/// explorer display mode as bool, false = grid view
/// </summary>
public bool IsExplorerViewThumb
{
get { return _IsExplorerViewThumb; }
set
{
if (_IsExplorerViewThumb != value)
{
_IsExplorerViewThumb = value;
RaisePropertyChanged("IsExplorerViewThumb");
}
}
}
/// <summary>
/// sort dropdown menu items
/// </summary>
public List<PropertyViewModel> SortProperties
{
get { return GetSortProperties(); }
}
/// <summary>
/// group dropdown menu items
/// </summary>
public List<PropertyViewModel> GroupProperties
{
get { return GetGroupProperties(); }
}
#endregion
#region ---------------- COMMANDS ----------------
#region forward command
private ICommand forwardCommand;
public ICommand ForwardCommand
{
get
{
if (forwardCommand == null)
forwardCommand = new DelegateCommand<string>(
delegate(string param)
{
Mediator.Instance.NotifyColleagues(ViewModelMessages.ContextCommand,
new CommandContext() { CommandName = param, CommandParameter = this.Books.CurrentItem } );
},
delegate(string param)
{
if (CatalogData != null && Books.CurrentItem != null)
{
return true;
}
return false;
});
return forwardCommand;
}
}
#endregion
/// <summary>
/// Command like function called from xaml with trigger
/// </summary>
public void NotifyColleagues()
{
if (Books != null)
Mediator.Instance.NotifyColleagues(ViewModelMessages.BookSelected, Books.CurrentItem as Book);
}
#endregion
#region ----------------INTERNALS----------------
private List<PropertyViewModel> _groupProperties = null;
private List<PropertyViewModel> GetGroupProperties()
{
if (_groupProperties == null)
{
_groupProperties = new List<PropertyViewModel>();
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Type", ToDisplay = "Type of" });
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Folder", ToDisplay = "Folder" });
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "FileExtension", ToDisplay = "File extension" });
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Rating", ToDisplay = "Rating" });
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "IsSecured", ToDisplay = "Secured" });
_groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "IsRead", ToDisplay = "Read" });
foreach (string dyn in WorkspaceService.Instance.Settings.Dynamics)
_groupProperties.Add(new PropertyViewModel() { Prefix = "Dynamics.", Name = dyn, ToDisplay = dyn });
}
return _groupProperties;
}
private List<PropertyViewModel> _sortProperties = null;
private List<PropertyViewModel> GetSortProperties()
{
if (_sortProperties == null)
{
_sortProperties = new List<PropertyViewModel>();
foreach (PropertyDescriptor inf in TypeDescriptor.GetProperties(new Book()))
{
if (inf.Attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes))
{
_sortProperties.Add(
new PropertyViewModel()
{
Prefix = "",
Name = inf.Name,
ToDisplay = (inf.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute).Description
});
}
}
foreach (string dyn in WorkspaceService.Instance.Settings.Dynamics)
_sortProperties.Add(new PropertyViewModel() { Prefix = "Dynamics.", Name = dyn, ToDisplay = dyn });
}
return _sortProperties;
}
internal void ChangeExplorerView(string modeAsParam)
{
if (modeAsParam.Equals("Details"))
IsExplorerViewThumb = false;
else
IsExplorerViewThumb = true;
}
internal void Sort(string propertyName)
{
PropertyViewModel prop = GetSortProperties().Find(p => p.Name == propertyName);
IEnumerable<SortDescription> result =
Books.SortDescriptions.Cast<SortDescription>().Where(p => p.PropertyName == prop.Prefix + prop.Name);
if (result != null && result.Count() == 1)
{
Books.SortDescriptions.Remove(result.First());
}
else
{
Books.SortDescriptions.Add(new SortDescription(prop.Prefix + prop.Name, ListSortDirection.Ascending));
}
RaisePropertyChanged("Books");
}
internal void Group(string propertyName)
{
PropertyViewModel prop = GetGroupProperties().Find(p => p.Name == propertyName);
IEnumerable<PropertyGroupDescription> result =
Books.GroupDescriptions.Cast<PropertyGroupDescription>().Where(p => p.PropertyName == prop.Prefix + prop.Name);
if (result != null && result.Count() == 1)
{
Books.GroupDescriptions.Remove(result.First());
}
else
{
Books.GroupDescriptions.Add(new PropertyGroupDescription(prop.Prefix + prop.Name));
}
Messenger.Default.Send<MessageBase>( new MessageBase(this) );
RaisePropertyChanged("Books");
}
#endregion
}
}