port from perforce
This commit is contained in:
105
aiwaz/Aiwaz.Editor/ViewModels/ResourceViewModel.cs
Normal file
105
aiwaz/Aiwaz.Editor/ViewModels/ResourceViewModel.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.ObjectModel;
|
||||
using Aiwaz.Contracts;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Aiwaz.Editor.ViewModels
|
||||
{
|
||||
public class ResourceViewModel : ViewModelBase
|
||||
{
|
||||
private bool creating;
|
||||
|
||||
public IResource Resource;
|
||||
|
||||
public string DisplayName
|
||||
{
|
||||
get
|
||||
{
|
||||
return Resource.WellKnownName;
|
||||
}
|
||||
set
|
||||
{
|
||||
Resource.WellKnownName = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ImageSource DisplayIcon
|
||||
{
|
||||
get
|
||||
{
|
||||
var stream = File.OpenRead("Resources/icon.png");
|
||||
BitmapImage bitmap = new BitmapImage();
|
||||
bitmap.BeginInit();
|
||||
bitmap.StreamSource = stream;
|
||||
bitmap.EndInit();
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<ResourceViewModel> nodes;
|
||||
public ObservableCollection<ResourceViewModel> Nodes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (nodes == null)
|
||||
{
|
||||
nodes = new ObservableCollection<ResourceViewModel>();
|
||||
nodes.CollectionChanged += new NotifyCollectionChangedEventHandler(nodes_CollectionChanged);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (nodes != value)
|
||||
{
|
||||
nodes = value;
|
||||
OnPropertyChanged("Nodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void nodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
if (creating)
|
||||
return;
|
||||
|
||||
if (e.Action == NotifyCollectionChangedAction.Add)
|
||||
{
|
||||
foreach (ResourceViewModel newItem in e.NewItems)
|
||||
{
|
||||
Resource.Children.Insert(e.NewStartingIndex, newItem.Resource);
|
||||
}
|
||||
}
|
||||
else if (e.Action == NotifyCollectionChangedAction.Remove)
|
||||
{
|
||||
foreach (ResourceViewModel oldItem in e.OldItems)
|
||||
{
|
||||
Resource.Children.Remove(oldItem.Resource);
|
||||
}
|
||||
}
|
||||
OnPropertyChanged("Nodes");
|
||||
}
|
||||
|
||||
public ResourceViewModel(IResource resource)
|
||||
{
|
||||
this.Resource = resource;
|
||||
|
||||
creating = true;
|
||||
if (resource.Children != null)
|
||||
foreach (var child in resource.Children)
|
||||
{
|
||||
Nodes.Add(new ResourceViewModel(child));
|
||||
}
|
||||
creating = false;
|
||||
OnPropertyChanged("Nodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user