Follow formatting NUI
[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", nameof(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             {
71                 ThrowIfApplied();
72                 _targetNullValue = value;
73             }
74         }
75
76         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
77         [EditorBrowsable(EditorBrowsableState.Never)]
78         public object FallbackValue
79         {
80             get => _fallbackValue;
81             set
82             {
83                 ThrowIfApplied();
84                 _fallbackValue = value;
85             }
86         }
87
88         internal bool AllowChaining { get; set; }
89
90         internal object Context { get; set; }
91
92         internal bool IsApplied { get; private set; }
93
94         /// <summary>
95         /// Stops synchronization on the collection.
96         /// </summary>
97         /// <param name="collection">The collection on which to stop synchronization.</param>
98         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public static void DisableCollectionSynchronization(IEnumerable collection)
101         {
102             if (collection == null)
103                 throw new ArgumentNullException(nameof(collection));
104
105             SynchronizedCollections.Remove(collection);
106         }
107
108         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         public static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
111         {
112             if (collection == null)
113                 throw new ArgumentNullException(nameof(collection));
114             if (callback == null)
115                 throw new ArgumentNullException(nameof(callback));
116
117             SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
118         }
119
120         /// <summary>
121         /// Throws an InvalidOperationException if the binding has been applied.
122         /// </summary>
123         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
124         [EditorBrowsable(EditorBrowsableState.Never)]
125         protected void ThrowIfApplied()
126         {
127             if (IsApplied)
128                 throw new InvalidOperationException("Can not change a binding while it's applied");
129         }
130
131         internal virtual void Apply(bool fromTarget)
132         {
133             IsApplied = true;
134         }
135
136         internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
137         {
138             IsApplied = true;
139         }
140
141         internal abstract BindingBase Clone();
142
143         internal virtual object GetSourceValue(object value, Type targetPropertyType)
144         {
145             if (value == null && TargetNullValue != null)
146                 return TargetNullValue;
147             if (StringFormat != null)
148                 return string.Format(StringFormat, value);
149
150             return value;
151         }
152
153         internal virtual object GetTargetValue(object value, Type sourcePropertyType)
154         {
155             return value;
156         }
157
158         internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
159         {
160             if (collection == null)
161                 throw new ArgumentNullException(nameof(collection));
162
163             return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
164         }
165
166         internal virtual void Unapply(bool fromBindingContextChanged = false)
167         {
168             IsApplied = false;
169         }
170     }
171 }