4200b3188f42c0b8918bc6f1a8a4502c34c6f559
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / ElementTemplate.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using Tizen.NUI.Internals;
5
6 namespace Tizen.NUI.Binding
7 {
8     /// <summary>
9     /// Base class for DataTemplate and ControlTemplate classes.
10     /// </summary>
11     [EditorBrowsable(EditorBrowsableState.Never)]
12     public class ElementTemplate : IElement, IDataTemplate
13         {
14                 List<Action<object, ResourcesChangedEventArgs>> _changeHandlers;
15                 Element _parent;
16                 bool _canRecycle; // aka IsDeclarative
17
18                 internal ElementTemplate()
19                 {
20                 }
21
22                 internal ElementTemplate(Type type) : this()
23                 {
24                         if (type == null)
25                                 throw new ArgumentNullException("type");
26
27                         _canRecycle = true;
28
29                         LoadTemplate = () => Activator.CreateInstance(type);
30                 }
31
32                 internal ElementTemplate(Func<object> loadTemplate) : this()
33                 {
34                         if (loadTemplate == null)
35                                 throw new ArgumentNullException("loadTemplate");
36
37                         LoadTemplate = loadTemplate;
38                 }
39
40                 Func<object> LoadTemplate { get; set; }
41
42 #pragma warning disable 0612
43                 Func<object> IDataTemplate.LoadTemplate
44                 {
45                         get { return LoadTemplate; }
46                         set { LoadTemplate = value; }
47                 }
48 #pragma warning restore 0612
49
50                 void IElement.AddResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
51                 {
52                         _changeHandlers = _changeHandlers ?? new List<Action<object, ResourcesChangedEventArgs>>(1);
53                         _changeHandlers.Add(onchanged);
54                 }
55
56                 internal bool CanRecycle => _canRecycle;
57                 Element IElement.Parent
58                 {
59                         get { return _parent; }
60                         set
61                         {
62                                 if (_parent == value)
63                                         return;
64                                 if (_parent != null)
65                                         ((IElement)_parent).RemoveResourcesChangedListener(OnResourcesChanged);
66                                 _parent = value;
67                                 if (_parent != null)
68                                         ((IElement)_parent).AddResourcesChangedListener(OnResourcesChanged);
69                         }
70                 }
71
72                 void IElement.RemoveResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
73                 {
74                         if (_changeHandlers == null)
75                                 return;
76                         _changeHandlers.Remove(onchanged);
77                 }
78
79         /// <summary>
80         /// Used by the XAML infrastructure to load data templates and set up the content of the resulting UI.
81         /// </summary>
82         /// <returns></returns>
83         public object CreateContent()
84                 {
85                         if (LoadTemplate == null)
86                                 throw new InvalidOperationException("LoadTemplate should not be null");
87                         if (this is DataTemplateSelector)
88                                 throw new InvalidOperationException("Cannot call CreateContent directly on a DataTemplateSelector");
89
90                         object item = LoadTemplate();
91                         SetupContent(item);
92
93                         return item;
94                 }
95
96                 internal virtual void SetupContent(object item)
97                 {
98                 }
99
100                 void OnResourcesChanged(object sender, ResourcesChangedEventArgs e)
101                 {
102                         if (_changeHandlers == null)
103                                 return;
104                         foreach (Action<object, ResourcesChangedEventArgs> handler in _changeHandlers)
105                                 handler(this, e);
106                 }
107         }
108 }