port from perforce
This commit is contained in:
123
aiwaz/Aiwaz.Resources/Resource.cs
Normal file
123
aiwaz/Aiwaz.Resources/Resource.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Aiwaz.Contracts;
|
||||
using Aiwaz.Core;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Aiwaz.Resources
|
||||
{
|
||||
public abstract class Resource : IResource, INotifyPropertyChanged, IDisposable
|
||||
{
|
||||
#region IResource Members
|
||||
|
||||
public abstract ICreationParams CreationParams
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
protected string wellKnownName;
|
||||
public string WellKnownName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (wellKnownName == null)
|
||||
{
|
||||
string resourceName = null;
|
||||
foreach (var attribute in this.GetType().GetCustomAttributes(true))
|
||||
{
|
||||
if (attribute.GetType().Equals(typeof(AiwazResourceAttribute)))
|
||||
{
|
||||
resourceName = ((AiwazResourceAttribute)attribute).Name;
|
||||
}
|
||||
}
|
||||
|
||||
if (resourceName == null)
|
||||
resourceName = "Unknown resource";
|
||||
|
||||
int count = 1;
|
||||
do
|
||||
{
|
||||
wellKnownName = string.Format("{0} {1}", resourceName, count++);
|
||||
} while (Engine.ExistsName(wellKnownName));
|
||||
|
||||
Engine.RegisterNamedObject(wellKnownName, this);
|
||||
}
|
||||
return wellKnownName;
|
||||
}
|
||||
set
|
||||
{
|
||||
string newName = value;
|
||||
if (Engine.ExistsName(newName))
|
||||
{
|
||||
int count = 1;
|
||||
do
|
||||
{
|
||||
newName = string.Format("{0} {1}", value, count++);
|
||||
} while (Engine.ExistsName(newName));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(wellKnownName))
|
||||
Engine.UnregisterNamedObject(wellKnownName);
|
||||
wellKnownName = newName;
|
||||
if (!string.IsNullOrEmpty(wellKnownName))
|
||||
Engine.RegisterNamedObject(wellKnownName, this);
|
||||
}
|
||||
}
|
||||
|
||||
public IResource Parent
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public abstract ObservableCollection<IResource> Children
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region INotifyPropertyChanged Members
|
||||
|
||||
protected void NotifyPropertyChanged(string property)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(property));
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
private bool _disposed = false;
|
||||
|
||||
protected bool Disposed
|
||||
{
|
||||
get { return _disposed; }
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!Disposed)
|
||||
{
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~Resource() { Dispose(false); }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user