Make CollectionView APIs public
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Template / DataTemplateSelector.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Collections.Generic;
21
22 namespace Tizen.NUI.Binding
23 {
24     /// <summary>
25     /// Selects DataTemplate objects by data type and container.
26     /// </summary>
27     /// <since_tizen> 9 </since_tizen>
28     public abstract class DataTemplateSelector : DataTemplate
29     {
30         Dictionary<Type, DataTemplate> _dataTemplates = new Dictionary<Type, DataTemplate>();
31
32         /// <summary>
33         /// Returns a DataTemplate for item by calling
34         /// OnSelectTemplate(Object, BindableObject) and verifying its result.
35         /// </summary>
36         /// <param name="item">The data for which to return a template.</param>
37         /// <param name="container">An optional container object in which
38         /// the developer may have opted to store DataTemplateSelector objects.</param>
39         /// <returns>A developer-defined DataTemplate that can be used to display item.</returns>
40         public DataTemplate SelectTemplate(object item, BindableObject container)
41         {
42             DataTemplate dataTemplate = null;
43             if (_dataTemplates.TryGetValue(item.GetType(), out dataTemplate))
44             {
45                 return dataTemplate;
46             }
47
48             dataTemplate = OnSelectTemplate(item, container);
49             if (dataTemplate is DataTemplateSelector)
50                 throw new NotSupportedException(
51                     "DataTemplateSelector.OnSelectTemplate must not return another DataTemplateSelector");
52
53             return dataTemplate;
54         }
55
56         /// <summary>
57         /// The developer overrides this method to return a valid data template for the specified item.
58         /// This method is called by SelectTemplate(Object, BindableObject).
59         /// </summary>
60         /// <param name="item">The data for which to return a template.</param>
61         /// <param name="container">An optional container object in which
62         /// the developer may have opted to store DataTemplateSelector objects.</param>
63         /// <returns>A developer-defined DataTemplate that can be used to display item.</returns>
64         /// <since_tizen> 9 </since_tizen>
65         protected abstract DataTemplate OnSelectTemplate(object item, BindableObject container);
66     }
67 }