b991771a570d8f1232e6ceab7edd1acc9edad1d5
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / BindingBase.cs
1 using System;
2 using System.Collections;
3 using System.Runtime.CompilerServices;
4 using System.ComponentModel;
5
6 namespace Tizen.NUI.Binding
7 {
8     /// <summary>
9     /// An abstract class that provides a BindingMode and a formatting option.
10     /// </summary>
11     [EditorBrowsable(EditorBrowsableState.Never)]
12         public abstract class BindingBase
13         {
14                 static readonly ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext> SynchronizedCollections = new ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext>();
15
16                 BindingMode _mode = BindingMode.Default;
17                 string _stringFormat;
18
19                 internal BindingBase()
20                 {
21                 }
22
23         /// <summary>
24         /// Gets or sets the mode for this binding.
25         /// </summary>
26                 public BindingMode Mode
27                 {
28                         get { return _mode; }
29                         set
30                         {
31                                 if (   value != BindingMode.Default
32                                     && value != BindingMode.OneWay
33                                     && value != BindingMode.OneWayToSource
34                                     && value != BindingMode.TwoWay
35                                     && value != BindingMode.OneTime)
36                                         throw new ArgumentException("mode is not a valid BindingMode", "mode");
37
38                                 ThrowIfApplied();
39
40                                 _mode = value;
41                         }
42                 }
43
44         /// <summary>
45         /// Gets or sets the string format for this binding.
46         /// </summary>
47                 public string StringFormat
48                 {
49                         get { return _stringFormat; }
50                         set
51                         {
52                                 ThrowIfApplied();
53
54                                 _stringFormat = value;
55                         }
56                 }
57
58                 internal bool AllowChaining { get; set; }
59
60                 internal object Context { get; set; }
61
62                 internal bool IsApplied { get; private set; }
63
64         /// <summary>
65         /// Stops synchronization on the collection.
66         /// </summary>
67         /// <param name="collection">The collection on which to stop synchronization.</param>
68                 public static void DisableCollectionSynchronization(IEnumerable collection)
69                 {
70                         if (collection == null)
71                                 throw new ArgumentNullException(nameof(collection));
72
73                         SynchronizedCollections.Remove(collection);
74                 }
75
76                 internal static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
77                 {
78                         if (collection == null)
79                                 throw new ArgumentNullException(nameof(collection));
80                         if (callback == null)
81                                 throw new ArgumentNullException(nameof(callback));
82
83                         SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
84                 }
85
86         /// <summary>
87         /// Throws an InvalidOperationException if the binding has been applied.
88         /// </summary>
89                 protected void ThrowIfApplied()
90                 {
91                         if (IsApplied)
92                                 throw new InvalidOperationException("Can not change a binding while it's applied");
93                 }
94
95                 internal virtual void Apply(bool fromTarget)
96                 {
97                         IsApplied = true;
98                 }
99
100                 internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
101                 {
102                         IsApplied = true;
103                 }
104
105                 internal abstract BindingBase Clone();
106
107                 internal virtual object GetSourceValue(object value, Type targetPropertyType)
108                 {
109                         if (StringFormat != null)
110                                 return string.Format(StringFormat, value);
111
112                         return value;
113                 }
114
115                 internal virtual object GetTargetValue(object value, Type sourcePropertyType)
116                 {
117                         return value;
118                 }
119
120                 internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
121                 {
122                         if (collection == null)
123                                 throw new ArgumentNullException(nameof(collection));
124
125                         return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
126                 }
127
128                 internal virtual void Unapply(bool fromBindingContextChanged = false)
129                 {
130                         IsApplied = false;
131                 }
132         }
133 }