2021-03-31 16:42:58 -05:00
// Decompiled with JetBrains decompiler
// Type: Microsoft.Iris.ModelItem
// Assembly: UIX, Version=4.8.0.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217
// MVID: A56C6C9D-B7F6-46A9-8BDE-B3D9B8D60B11
// Assembly location: C:\Program Files\Zune\UIX.dll
using Microsoft.Iris.Library ;
using System ;
using System.Collections ;
using System.Collections.Specialized ;
using System.ComponentModel ;
using System.Threading ;
namespace Microsoft.Iris
{
public class ModelItem :
IModelItem ,
INotifyPropertyChanged ,
IModelItemOwner ,
IDisposable ,
IThreadSafeObject
{
private static readonly EventCookie s_propertyChangedEvent = EventCookie . ReserveSlot ();
private static readonly EventCookie s_focusRequestedEvent = EventCookie . ReserveSlot ();
private static readonly DataCookie s_descriptionProperty = DataCookie . ReserveSlot ();
private static readonly DataCookie s_uniqueIdProperty = DataCookie . ReserveSlot ();
private static readonly DataCookie s_selectedProperty = DataCookie . ReserveSlot ();
private static readonly DataCookie s_selectionPolicyProperty = DataCookie . ReserveSlot ();
private static readonly DataCookie s_ownedObjectsProperty = DataCookie . ReserveSlot ();
private static readonly DataCookie s_extraDataProperty = DataCookie . ReserveSlot ();
private IModelItemOwner _owner ;
private DynamicData _dataMap ;
private bool _isDisposed ;
private Thread _affinity ;
public ModelItem ( IModelItemOwner owner , string description )
{
2021-03-31 17:15:06 -05:00
ThreadSafety . InitializeObject ( this );
2021-03-31 16:42:58 -05:00
this . _dataMap = new DynamicData ();
this . _dataMap . Create ();
2021-03-31 17:20:20 -05:00
this . SetData ( s_descriptionProperty , description );
2021-03-31 16:42:58 -05:00
this . Owner = owner ;
}
public ModelItem ( IModelItemOwner owner )
2021-03-31 17:15:06 -05:00
: this ( owner , null )
2021-03-31 16:42:58 -05:00
{
}
public ModelItem ()
2021-03-31 17:15:06 -05:00
: this ( null )
2021-03-31 16:42:58 -05:00
{
}
~ ModelItem ()
{
string name = this . GetType (). Name ;
2021-03-31 17:20:20 -05:00
string data = ( string ) this . GetData ( s_descriptionProperty );
2021-03-31 16:42:58 -05:00
this . OnDispose ( false );
}
public void Dispose () => this . Dispose ( ModelItemDisposeMode . RemoveOwnerReference );
public void Dispose ( ModelItemDisposeMode disposeMode )
{
using ( this . ThreadValidator )
{
if ( this . _isDisposed )
return ;
if ( this . _owner != null )
{
if ( disposeMode == ModelItemDisposeMode . RemoveOwnerReference )
this . _owner . UnregisterObject ( this );
2021-03-31 17:15:06 -05:00
this . _owner = null ;
2021-03-31 16:42:58 -05:00
}
try
{
2021-03-31 17:15:06 -05:00
GC . SuppressFinalize ( this );
2021-03-31 16:42:58 -05:00
this . OnDispose ( true );
}
finally
{
try
{
this . DisposeOwnedObjects ();
}
finally
{
this . _isDisposed = true ;
}
}
}
}
protected virtual void OnDispose ( bool disposing )
{
}
2021-03-31 17:15:06 -05:00
protected ThreadSafetyBlock ThreadValidator => new ThreadSafetyBlock ( this );
2021-03-31 16:42:58 -05:00
Thread IThreadSafeObject . Affinity
{
get => this . _affinity ;
set => this . _affinity = value ;
}
public IModelItemOwner Owner
{
get
{
using ( this . ThreadValidator )
return this . _owner ;
}
set
{
using ( this . ThreadValidator )
{
if ( this . _owner == value )
return ;
IModelItemOwner owner = this . _owner ;
owner ?. UnregisterObject ( this );
this . _owner = value ;
if ( this . _owner != null )
this . _owner . RegisterObject ( this );
this . OnOwnerChanged ( this . _owner , owner );
}
}
}
public bool IsDisposed
{
get
{
using ( this . ThreadValidator )
return this . _isDisposed ;
}
}
public string Description
{
get
{
using ( this . ThreadValidator )
2021-03-31 17:20:20 -05:00
return ( string ) this . GetData ( s_descriptionProperty );
2021-03-31 16:42:58 -05:00
}
set
{
using ( this . ThreadValidator )
{
if (!( this . Description != value ))
return ;
2021-03-31 17:20:20 -05:00
this . SetData ( s_descriptionProperty , value );
2021-03-31 16:42:58 -05:00
this . FirePropertyChanged ( nameof ( Description ));
}
}
}
public Guid UniqueId
{
get
{
using ( this . ThreadValidator )
{
2021-03-31 17:20:20 -05:00
object data = this . GetData ( s_uniqueIdProperty );
2021-03-31 16:42:58 -05:00
return data == null ? Guid . Empty : ( Guid ) data ;
}
}
set
{
using ( this . ThreadValidator )
{
if (!( this . UniqueId != value ))
return ;
2021-03-31 17:20:20 -05:00
this . SetData ( s_uniqueIdProperty , value );
2021-03-31 16:42:58 -05:00
this . FirePropertyChanged ( nameof ( UniqueId ));
}
}
}
public IDictionary Data
{
get
{
using ( this . ThreadValidator )
{
2021-03-31 17:20:20 -05:00
IDictionary dictionary = ( IDictionary ) this . GetData ( s_extraDataProperty );
2021-03-31 16:42:58 -05:00
if ( dictionary == null )
{
2021-03-31 17:15:06 -05:00
dictionary = new HybridDictionary ();
2021-03-31 17:20:20 -05:00
this . SetData ( s_extraDataProperty , dictionary );
2021-03-31 16:42:58 -05:00
}
return dictionary ;
}
}
}
public event PropertyChangedEventHandler PropertyChanged
{
add
{
using ( this . ThreadValidator )
2021-03-31 17:20:20 -05:00
this . AddEventHandler ( s_propertyChangedEvent , value );
2021-03-31 16:42:58 -05:00
}
remove
{
using ( this . ThreadValidator )
2021-03-31 17:20:20 -05:00
this . RemoveEventHandler ( s_propertyChangedEvent , value );
2021-03-31 16:42:58 -05:00
}
}
protected void FirePropertyChanged ( string property )
{
using ( this . ThreadValidator )
{
if ( property == null )
throw new ArgumentNullException ( nameof ( property ));
this . OnPropertyChanged ( property );
2021-03-31 17:20:20 -05:00
if (!( this . GetEventHandler ( s_propertyChangedEvent ) is PropertyChangedEventHandler eventHandler ))
2021-03-31 16:42:58 -05:00
return ;
2021-03-31 17:15:06 -05:00
eventHandler ( this , new PropertyChangedEventArgs ( property ));
2021-03-31 16:42:58 -05:00
}
}
protected virtual void OnPropertyChanged ( string property )
{
}
protected virtual void OnOwnerChanged ( IModelItemOwner newOwner , IModelItemOwner oldOwner )
{
}
void IModelItemOwner . RegisterObject ( ModelItem item )
{
using ( this . ThreadValidator )
{
if ( item == null )
throw new ArgumentNullException ( nameof ( item ));
if ( item == this )
throw new ArgumentException ( "Cannot make a ModelItem the owner of itself" );
this . GetOwnedObjects ( true ). Add ( item );
}
}
void IModelItemOwner . UnregisterObject ( ModelItem item )
{
using ( this . ThreadValidator )
{
if ( item == null )
throw new ArgumentNullException ( nameof ( item ));
Vector < ModelItem > ownedObjects = this . GetOwnedObjects ( false );
if ( ownedObjects == null || ! ownedObjects . Contains ( item ))
2021-03-31 17:15:06 -05:00
throw new ArgumentException ( InvariantString . Format ( "Cannot unregister an object that was never registered. Owner \"{0}\" was unable to identify \"{1}\"." , this , item ));
2021-03-31 16:42:58 -05:00
ownedObjects . Remove ( item );
}
}
private void DisposeOwnedObjects ()
{
Vector < ModelItem > ownedObjects = this . GetOwnedObjects ( false );
if ( ownedObjects == null )
return ;
foreach ( ModelItem modelItem in ownedObjects )
modelItem . Dispose ( ModelItemDisposeMode . KeepOwnerReference );
2021-03-31 17:20:20 -05:00
this . SetData ( s_ownedObjectsProperty , null );
2021-03-31 16:42:58 -05:00
}
private Vector < ModelItem > GetOwnedObjects ( bool createIfNoneFlag )
{
2021-03-31 17:20:20 -05:00
Vector < ModelItem > vector = ( Vector < ModelItem >) this . GetData ( s_ownedObjectsProperty );
2021-03-31 16:42:58 -05:00
if ( vector == null && createIfNoneFlag )
{
vector = new Vector < ModelItem >();
2021-03-31 17:20:20 -05:00
this . SetData ( s_ownedObjectsProperty , vector );
2021-03-31 16:42:58 -05:00
}
return vector ;
}
public bool Selected
{
get
{
using ( this . ThreadValidator )
{
2021-03-31 17:20:20 -05:00
object data = this . GetData ( s_selectedProperty );
2021-03-31 16:42:58 -05:00
return data != null && ( bool ) data ;
}
}
set
{
using ( this . ThreadValidator )
{
if ( this . Selected == value )
return ;
2021-03-31 17:20:20 -05:00
this . SetData ( s_selectedProperty , value );
2021-03-31 16:42:58 -05:00
this . FirePropertyChanged ( nameof ( Selected ));
}
}
}
public override string ToString ()
{
using ( this . ThreadValidator )
{
string name = this . GetType (). Name ;
string description = this . Description ;
2021-03-31 17:15:06 -05:00
return description != null ? InvariantString . Format ( "{0}:\"{1}\"" , name , description ) : name ;
2021-03-31 16:42:58 -05:00
}
}
internal object GetData ( DataCookie cookie ) => this . _dataMap . GetData ( cookie );
internal void SetData ( DataCookie cookie , object value ) => this . _dataMap . SetData ( cookie , value );
internal Delegate GetEventHandler ( EventCookie cookie ) => this . _dataMap . GetEventHandler ( cookie );
internal void AddEventHandler ( EventCookie cookie , Delegate handlerToAdd ) => this . _dataMap . AddEventHandler ( cookie , handlerToAdd );
internal void RemoveEventHandler ( EventCookie cookie , Delegate handlerToRemove ) => this . _dataMap . RemoveEventHandler ( cookie , handlerToRemove );
internal void RemoveEventHandlers ( EventCookie cookie ) => this . _dataMap . RemoveEventHandlers ( cookie );
private static uint GetKey ( EventCookie cookie )
{
uint uint32 = EventCookie . ToUInt32 ( cookie );
return uint32 != 0 U ? uint32 : throw new ArgumentException ( "invalid event key" );
}
}
}