Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ContextMenuModel.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FUiCtrl_ContextMenuModel.cpp
19  * @brief               This is the implementation file for the _ContextMenuModel class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUiCtrl_ContextMenuModel.h"
25
26 static const int DEFAULT_ITEM_COUNT = 32;
27
28 using namespace Tizen::Graphics;
29
30 namespace Tizen { namespace Ui { namespace Controls
31 {
32
33 _ContextMenuModel::_ContextMenuModel(void)
34 {
35
36 }
37
38 _ContextMenuModel::~_ContextMenuModel(void)
39 {
40         __items.RemoveAll(true);
41 }
42
43
44 result
45 _ContextMenuModel::Construct(void)
46 {
47
48         result r = __items.Construct(DEFAULT_ITEM_COUNT);
49         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to construct ArrayList.");
50
51         return r;
52 }
53
54 bool
55 _ContextMenuModel::IsValidItem(_ContextMenuItem *pItem)
56 {
57         if (pItem == null)
58                 return false;
59
60         int actionId = pItem->GetActionId();
61         int itemCount = __items.GetCount();
62
63         for (int i = 0; i < itemCount; i++)
64         {
65                 _ContextMenuItem* pTargetItem = GetItem(i);
66
67                 if (pTargetItem == null)
68                         return false;
69
70                 if (pTargetItem->GetActionId() == actionId)
71                 {
72                         return false;
73                 }
74         }
75
76         return true;
77 }
78
79 int
80 _ContextMenuModel::GetItemCount(void) const
81 {
82
83         return __items.GetCount();
84 }
85
86 result
87 _ContextMenuModel::AddItem(_ContextMenuItem* pItem)
88 {
89         if (IsValidItem(pItem) == false)
90         {
91                 return E_SYSTEM;
92         }
93
94         return __items.Add(*pItem);
95 }
96
97 result
98 _ContextMenuModel::InsertItem( _ContextMenuItem* pItem, int index)
99 {
100         if (IsValidItem(pItem) == false)
101         {
102                 return E_SYSTEM;
103         }
104
105         return __items.InsertAt(*pItem, index);
106 }
107
108 result
109 _ContextMenuModel::SetItem(_ContextMenuItem* pItem, int index)
110 {
111         if (IsValidItem(pItem) == false)
112         {
113                 _ContextMenuItem* pTargetItem = GetItem(index);
114                 if (pItem == null || pTargetItem == null || pItem->GetActionId() != pTargetItem->GetActionId())
115                 {
116                         return E_SYSTEM;
117                 }
118         }
119
120         return __items.SetAt(*pItem, index, true);
121 }
122
123 result
124 _ContextMenuModel::RemoveItem(int index)
125 {
126         return __items.RemoveAt(index, true);
127 }
128
129 result
130 _ContextMenuModel::RemoveAllItem(void)
131 {
132         __items.RemoveAll(true);
133
134         return E_SUCCESS;
135 }
136
137 void
138 _ContextMenuModel::ResetAllItem()
139 {
140         int count = __items.GetCount();
141
142         _ContextMenuItem* pItem;
143         for(int index = 0; index < count; index++)
144         {
145                 pItem = static_cast <_ContextMenuItem*>(__items.GetAt(index));
146                 pItem->Reset();
147         }
148 }
149
150 _ContextMenuItem*
151 _ContextMenuModel::GetItem(int index) const
152 {
153         int count = __items.GetCount();
154
155         if (index < 0 || index >= count)
156         {
157                 return null;
158         }
159
160         const _ContextMenuItem* pItem = static_cast <const _ContextMenuItem*>(__items.GetAt(index));
161         return const_cast<_ContextMenuItem*>(pItem);
162
163 //      return static_cast <_ContextMenuItem*>(__items.GetAt(index));
164 }
165
166
167 }}} // Tizen::Ui::Controls