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