d2e18389ce786be94b8777fe9d65d163c85d0cae
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabContent.cs
1 /*
2  * Copyright(c) 2020 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     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class TabContent : Control
29     {
30         private IList<View> views;
31
32         /// <summary>
33         /// Creates a new instance of TabContent.
34         /// </summary>
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public TabContent()
37         {
38             SelectedIndex = -1;
39             views = new List<View>();
40         }
41
42         /// <summary>
43         /// The index of the selected view.
44         /// </summary>
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         protected int SelectedIndex { get; set; }
47
48         /// <summary>
49         /// Gets the count of views.
50         /// </summary>
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public int ViewCount => views.Count;
53
54         /// <summary>
55         /// Adds a view to TabContent.
56         /// </summary>
57         /// <param name="view">A view to be added to TabContent.</param>
58         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         protected internal void AddView(View view)
61         {
62             if (view == null)
63             {
64                 throw new ArgumentNullException(nameof(view), "view should not be null.");
65             }
66
67             views.Add(view);
68
69             if (SelectedIndex == -1)
70             {
71                 Select(0);
72             }
73         }
74
75         /// <summary>
76         /// Removes a view from TabContent.
77         /// </summary>
78         /// <param name="view">A view to be removed from TabContent.</param>
79         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
80         /// <exception cref="ArgumentException">Thrown when the argument view does not exist in TabContent.</exception>
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         protected internal void RemoveView(View view)
83         {
84             if (view == null)
85             {
86                 throw new ArgumentNullException(nameof(view), "view should not be null.");
87             }
88
89             if (views.Contains(view) == false)
90             {
91                 throw new ArgumentException("view does not exist in TabContent.", nameof(view));
92             }
93
94             int index = views.IndexOf(view);
95
96             views.Remove(view);
97
98             if (index == SelectedIndex)
99             {
100                 if (views.Count == 0)
101                 {
102                     Select(-1);
103                 }
104                 else if (SelectedIndex == views.Count)
105                 {
106                     Select(SelectedIndex - 1);
107                 }
108             }
109         }
110
111         /// <summary>
112         /// Selects a view at the specified index of TabContent.
113         /// </summary>
114         /// <param name="index">The index of TabContent where a view will be selected.</param>
115         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than -1, or greater than or equal to the number of views.</exception>
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         protected internal void Select(int index)
118         {
119             if ((index < -1) || (index >= views.Count))
120             {
121                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to -1, and less than the number of views.");
122             }
123
124             if (SelectedIndex != -1)
125             {
126                 Remove(views[SelectedIndex]);
127             }
128
129             if (index != -1)
130             {
131                 Add(views[index]);
132             }
133
134             SelectedIndex = index;
135         }
136
137         /// <summary>
138         /// Gets the view at the specified index of TabContent.
139         /// </summary>
140         /// <param name="index">The index of TabContent where the specified view exists.</param>
141         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of views.</exception>
142         [EditorBrowsable(EditorBrowsableState.Never)]
143         public View GetView(int index)
144         {
145             if ((index < 0) || (index >= views.Count))
146             {
147                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of views.");
148             }
149
150             return views[index];
151         }
152
153         /// <summary>
154         /// Dispose TabContent and all children on it.
155         /// </summary>
156         /// <param name="type">Dispose type.</param>
157         [EditorBrowsable(EditorBrowsableState.Never)]
158         protected override void Dispose(DisposeTypes type)
159         {
160             if (disposed)
161             {
162                 return;
163             }
164
165             if (type == DisposeTypes.Explicit)
166             {
167                 if (views != null)
168                 {
169                     foreach (View view in views)
170                     {
171                         Utility.Dispose(view);
172                     }
173
174                     views = null;
175                 }
176             }
177
178             base.Dispose(type);
179         }
180     }
181 }