[NUI] Adjust directory (#903)
[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         object _targetNullValue;
19         object _fallbackValue;
20
21         internal BindingBase()
22         {
23         }
24
25         /// <summary>
26         /// Gets or sets the mode for this binding.
27         /// </summary>
28         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
29         [EditorBrowsable(EditorBrowsableState.Never)]
30         public BindingMode Mode
31         {
32             get { return _mode; }
33             set
34             {
35                 if (   value != BindingMode.Default
36                     && value != BindingMode.OneWay
37                     && value != BindingMode.OneWayToSource
38                     && value != BindingMode.TwoWay
39                     && value != BindingMode.OneTime)
40                     throw new ArgumentException("mode is not a valid BindingMode", "mode");
41
42                 ThrowIfApplied();
43
44                 _mode = value;
45             }
46         }
47
48         /// <summary>
49         /// Gets or sets the string format for this binding.
50         /// </summary>
51         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
52         [EditorBrowsable(EditorBrowsableState.Never)]
53         public string StringFormat
54         {
55             get { return _stringFormat; }
56             set
57             {
58                 ThrowIfApplied();
59
60                 _stringFormat = value;
61             }
62         }
63
64         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public object TargetNullValue
67                 {
68                         get { return _targetNullValue; }
69                         set {
70                                 ThrowIfApplied();
71                                 _targetNullValue = value;
72                         }
73                 }
74
75         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public object FallbackValue {
78                         get => _fallbackValue;
79                         set {
80                                 ThrowIfApplied();
81                                 _fallbackValue = value;
82                         }
83                 }
84
85                 internal bool AllowChaining { get; set; }
86
87         internal object Context { get; set; }
88
89         internal bool IsApplied { get; private set; }
90
91         /// <summary>
92         /// Stops synchronization on the collection.
93         /// </summary>
94         /// <param name="collection">The collection on which to stop synchronization.</param>
95         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public static void DisableCollectionSynchronization(IEnumerable collection)
98         {
99             if (collection == null)
100                 throw new ArgumentNullException(nameof(collection));
101
102             SynchronizedCollections.Remove(collection);
103         }
104
105         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
106         [EditorBrowsable(EditorBrowsableState.Never)]
107         public static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
108         {
109             if (collection == null)
110                 throw new ArgumentNullException(nameof(collection));
111             if (callback == null)
112                 throw new ArgumentNullException(nameof(callback));
113
114             SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
115         }
116
117         /// <summary>
118         /// Throws an InvalidOperationException if the binding has been applied.
119         /// </summary>
120         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         protected void ThrowIfApplied()
123         {
124             if (IsApplied)
125                 throw new InvalidOperationException("Can not change a binding while it's applied");
126         }
127
128         internal virtual void Apply(bool fromTarget)
129         {
130             IsApplied = true;
131         }
132
133         internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
134         {
135             IsApplied = true;
136         }
137
138         internal abstract BindingBase Clone();
139
140         internal virtual object GetSourceValue(object value, Type targetPropertyType)
141         {
142             if (value == null && TargetNullValue != null)
143                 return TargetNullValue;
144             if (StringFormat != null)
145                 return string.Format(StringFormat, value);
146
147             return value;
148         }
149
150         internal virtual object GetTargetValue(object value, Type sourcePropertyType)
151         {
152             return value;
153         }
154
155         internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
156         {
157             if (collection == null)
158                 throw new ArgumentNullException(nameof(collection));
159
160             return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
161         }
162
163         internal virtual void Unapply(bool fromBindingContextChanged = false)
164         {
165             IsApplied = false;
166         }
167     }
168 }