[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / AttachedProperty.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.Linq;
10     using System.Text;
11     using System.Activities.Presentation.Model;
12     using System.Runtime;
13     using System.Diagnostics.CodeAnalysis;
14
15     public abstract class AttachedProperty
16     {
17         Type ownerType = typeof(object);
18
19         [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "By design.")]
20         public abstract Type Type
21         {
22             get;
23         }
24
25         public abstract bool IsReadOnly
26         {
27             get;
28         }
29
30         public bool IsBrowsable
31         {
32             get;
33             set;
34         }
35
36         internal bool IsVisibleToModelItem
37         {
38             get;
39             set;
40         }
41
42         public string Name
43         { 
44             get; set; 
45         }
46
47         public Type OwnerType
48         {
49             get
50             {
51                 return this.ownerType;
52             }
53             set
54             {
55                 this.ownerType = value;
56             }
57         }
58
59         public abstract object GetValue(ModelItem modelItem);
60         public abstract void SetValue(ModelItem modelItem, object value);
61         public abstract void ResetValue(ModelItem modelItem);
62
63         public void NotifyPropertyChanged(ModelItem modelItem)
64         {
65             if (null != modelItem)
66             {
67                 ((IModelTreeItem)modelItem).OnPropertyChanged(this.Name);
68             }
69         }
70     }
71
72     
73     public class AttachedProperty<T> : AttachedProperty
74     {
75         [Fx.Tag.KnownXamlExternal]
76         public Func<ModelItem, T> Getter
77         { 
78             get; set; 
79         }
80
81         [Fx.Tag.KnownXamlExternal]
82         public Action<ModelItem, T> Setter
83         { 
84             get; set; 
85         }
86
87         public override Type Type
88         {
89             get { return typeof(T); }
90         }
91
92         public override bool IsReadOnly
93         {
94             get { return (this.Setter == null); }
95         }
96
97         public override object GetValue(ModelItem modelItem)
98         {
99             return this.Getter(modelItem);
100         }
101
102         public override void SetValue(ModelItem modelItem, object Value)
103         {
104             if (this.IsReadOnly)
105             {
106                 throw FxTrace.Exception.AsError(new InvalidOperationException(SR.PropertyIsReadOnly));
107             }
108             this.Setter(modelItem, (T)Value);
109             this.NotifyPropertyChanged(modelItem);
110         }
111
112         public override void ResetValue(ModelItem modelItem)
113         {
114             SetValue(modelItem, default(T));
115         }
116     }
117 }