[NUI] Modify TabContent to help customizing class
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabContent.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 using System;
18 using System.Collections.Generic;
19 using Tizen.NUI.BaseComponents;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// TabContent is a class which contains a set of Views and has one of them selected.
26     /// </summary>
27     /// <since_tizen> 9 </since_tizen>
28     public class TabContent : Control
29     {
30         private IList<View> views;
31
32         private void Initialize()
33         {
34             SelectedIndex = -1;
35             views = new List<View>();
36         }
37
38         /// <summary>
39         /// Creates a new instance of TabContent.
40         /// </summary>
41         /// <since_tizen> 9 </since_tizen>
42         public TabContent()
43         {
44             Initialize();
45         }
46
47         /// <summary>
48         /// Creates a new instance of a TabContent with style.
49         /// </summary>
50         /// <param name="style">A style applied to the newly created TabContent.</param>
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public TabContent(ControlStyle style) : base(style)
53         {
54             Initialize();
55         }
56
57         /// <summary>
58         /// The index of the selected view.
59         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
60         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
61         /// </summary>
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         protected int SelectedIndex { get; set; }
64
65         /// <summary>
66         /// A list of content views.
67         /// </summary>
68         [EditorBrowsable(EditorBrowsableState.Never)]
69         protected IList<View> Views
70         {
71             get
72             {
73                 return views;
74             }
75         }
76
77         /// <summary>
78         /// Gets the count of views.
79         /// </summary>
80         /// <since_tizen> 9 </since_tizen>
81         public int ViewCount => Views.Count;
82
83         /// <inheritdoc/>
84         [EditorBrowsable(EditorBrowsableState.Never)]
85         public override void OnInitialize()
86         {
87             base.OnInitialize();
88
89             AccessibilityRole = Role.PageTabList;
90         }
91
92         /// <summary>
93         /// Adds a view to TabContent.
94         /// </summary>
95         /// <param name="view">A view to be added to TabContent.</param>
96         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         protected virtual void AddView(View view)
99         {
100             if (view == null)
101             {
102                 throw new ArgumentNullException(nameof(view), "view should not be null.");
103             }
104
105             Views.Add(view);
106
107             if (SelectedIndex == -1)
108             {
109                 SelectView(0);
110             }
111         }
112
113         /// <summary>
114         /// Adds a view to TabContent.
115         /// </summary>
116         /// <param name="view">A view to be added to TabContent.</param>
117         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
118         internal void AddContentView(View view)
119         {
120             AddView(view);
121         }
122
123         /// <summary>
124         /// Removes a view from TabContent.
125         /// </summary>
126         /// <param name="view">A view to be removed from TabContent.</param>
127         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
128         /// <exception cref="ArgumentException">Thrown when the argument view does not exist in TabContent.</exception>
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         protected virtual void RemoveView(View view)
131         {
132             if (view == null)
133             {
134                 throw new ArgumentNullException(nameof(view), "view should not be null.");
135             }
136
137             if (Views.Contains(view) == false)
138             {
139                 throw new ArgumentException("view does not exist in TabContent.", nameof(view));
140             }
141
142             int index = Views.IndexOf(view);
143
144             Views.Remove(view);
145
146             if (index == SelectedIndex)
147             {
148                 if (Views.Count == 0)
149                 {
150                     SelectView(-1);
151                 }
152                 else if (SelectedIndex == Views.Count)
153                 {
154                     SelectView(SelectedIndex - 1);
155                 }
156             }
157         }
158
159         /// <summary>
160         /// Removes a view from TabContent.
161         /// </summary>
162         /// <param name="view">A view to be removed from TabContent.</param>
163         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
164         /// <exception cref="ArgumentException">Thrown when the argument view does not exist in TabContent.</exception>
165         internal void RemoveContentView(View view)
166         {
167             RemoveView(view);
168         }
169
170         /// <summary>
171         /// Selects a view at the specified index of TabContent.
172         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
173         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
174         /// </summary>
175         /// <param name="index">The index of a view in TabContent where the view will be selected.</param>
176         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than -1, or greater than or equal to the number of views.</exception>
177         [EditorBrowsable(EditorBrowsableState.Never)]
178         protected virtual void SelectView(int index)
179         {
180             if ((index < -1) || (index >= Views.Count))
181             {
182                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to -1, and less than the number of views.");
183             }
184
185             if (SelectedIndex != -1)
186             {
187                 Remove(Views[SelectedIndex]);
188             }
189
190             if (index != -1)
191             {
192                 Add(Views[index]);
193             }
194
195             SelectedIndex = index;
196         }
197
198         /// <summary>
199         /// Selects a view at the specified index of TabContent.
200         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
201         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
202         /// </summary>
203         /// <param name="index">The index of a view in TabContent where the view will be selected.</param>
204         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than -1, or greater than or equal to the number of views.</exception>
205         internal void SelectContentView(int index)
206         {
207             SelectView(index);
208         }
209
210         /// <summary>
211         /// Gets the view at the specified index of TabContent.
212         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
213         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
214         /// </summary>
215         /// <param name="index">The index of a view in TabContent where the specified view exists.</param>
216         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of views.</exception>
217         /// <since_tizen> 9 </since_tizen>
218         public View GetView(int index)
219         {
220             if ((index < 0) || (index >= Views.Count))
221             {
222                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of views.");
223             }
224
225             return Views[index];
226         }
227
228         /// <inheritdoc/>
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         protected override void Dispose(DisposeTypes type)
231         {
232             if (disposed)
233             {
234                 return;
235             }
236
237             if (type == DisposeTypes.Explicit)
238             {
239                 if (Views != null)
240                 {
241                     foreach (View view in Views)
242                     {
243                         Utility.Dispose(view);
244                     }
245
246                     views = null;
247                 }
248             }
249
250             base.Dispose(type);
251         }
252     }
253 }