add patch
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_TableViewPresenter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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 /**
19  * @file        FUiCtrl_TableViewPresenter.cpp
20  * @brief       This is the header file for the _TableViewPresenter class.
21  *
22  * This file contains the declarations of the _TableViewPresenter class.
23  */
24
25 #include <FGrpRectangle.h>
26 #include <FSysSystemTime.h>
27 #include "FUi_ResourceManager.h"
28 #include "FUi_UiTouchEvent.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUiAnim_VisualElement.h"
31 #include "FUiCtrl_FastScroll.h"
32 #include "FUiCtrl_Scroll.h"
33 #include "FUiCtrl_TableView.h"
34 #include "FUiCtrl_TableViewItem.h"
35 #include "FUiCtrl_TableViewPresenter.h"
36 #include "FUiCtrl_TableViewItemProviderAdaptor.h"
37 #include "FUi_CoordinateSystemUtils.h"
38 #include "FUi_FocusManagerImpl.h"
39 #include "FUi_Math.h"
40 #include "FUi_Window.h"
41
42 using namespace Tizen::Ui::Animations;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::Ui::Controls;
47
48 namespace Tizen { namespace Ui { namespace Controls
49 {
50 const float _TableViewPresenter::REORDER_SCROLL_ANIMATION_DISTANCE = 10.0f;
51 const float _TableViewPresenter::TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE = 12.3456f;
52
53 _TableViewPresenter::_TableViewPresenter()
54         : __pTableView(null)
55         , __pListModel(null)
56         , __pProviderAdaptor(null)
57         , __topMargin(0.0f)
58         , __bottomMargin(0.0f)
59         , __leftMargin(0.0f)
60         , __modelInitialized(false)
61         , __firstDrawnFlag(true)
62         , __statusChangedFlag(true)
63         , __scrolling(true)
64         , __pItemDrawingProperty(null)
65         , __pBaseVisualElement(null)
66         , __sweepOccured(false)
67         , __sweptItemPosition()
68         , __reservedScrollItemAlignment(TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP)
69         , __firstTouchMoved(false)
70         , __pReorderScrollTimer(null)
71         , __reorderInfo()
72         , __itemTotalHeight(0.0f)
73         , __pCapturedItemVisualElement(null)
74         , __pFastScrollTimer(null)
75         , __isFastScrollTimerEnabled(false)
76         , __scrollHeightOnFlickStarted(0)
77         , __scrollPositionOnFlickStarted(0)
78         , __isAnimationCallbackBlocked(false)
79         , __lockLoadItemWithScroll(false)
80         , __isRestoreFocus(false)
81         , __isAnnexFocused(false)
82         , __isTableViewFocused(false)
83         , __itemTouchReleasedEventState(TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_NORMAL)
84         , __scrollToItemTag()
85 {
86         __sweptItemTag.itemIndex = -1;
87         __sweptItemTag.groupIndex = -1;
88
89         __reservedScrollItemIndex.itemIndex = -1;
90         __reservedScrollItemIndex.groupIndex = -1;
91
92         __expandableItemTag.groupIndex = -1;
93         __expandableItemTag.itemIndex = -1;
94
95         __focusItemTag.groupIndex = -1;
96         __focusItemTag.itemIndex = -1;
97 }
98
99 _TableViewPresenter::~_TableViewPresenter()
100 {
101         Dispose();
102 }
103
104 _TableView*
105 _TableViewPresenter::GetView() const
106 {
107         return __pTableView;
108 }
109
110 _ListViewModel*
111 _TableViewPresenter::GetModel(void) const
112 {
113         return __pListModel;
114 }
115
116 bool
117 _TableViewPresenter::Initialize(_TableView* pTableView)
118 {
119         _ScrollPanelPresenter::Initialize(*pTableView);
120
121         __pTableView = pTableView;
122         __pBaseVisualElement = __pTableView->GetVisualElement();
123         __pBaseVisualElement->SetClipChildrenEnabled(true);
124         __pBaseVisualElement->SetSurfaceOpaque(false);
125
126         result r = __itemHeightList.Construct();
127         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, E_OUT_OF_MEMORY, ("[E_OUT_OF_MEMORY] The memory is insufficient."));
128
129         r = __sectionAlignmentList.Construct();
130         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, E_OUT_OF_MEMORY, ("[E_OUT_OF_MEMORY] The memory is insufficient."));
131
132         // Create Model
133         __pListModel = new (std::nothrow) _ListViewModel();
134         SysTryReturn(NID_UI_CTRL, __pListModel != null, false, E_OUT_OF_MEMORY, ("[E_OUT_OF_MEMORY] The memory is insufficient."));
135         __pListModel->SetListViewModelDelegate(this);
136
137         __pItemDrawingProperty = new (std::nothrow) _ItemDrawingProperty();
138         SysTryReturn(NID_UI_CTRL, __pItemDrawingProperty != null, false, E_OUT_OF_MEMORY, ("[E_OUT_OF_MEMORY] The memory is insufficient."));
139
140         // Timer
141         __pFastScrollTimer = new (std::nothrow) Timer();
142         SysTryCatch(NID_UI_CTRL, __pFastScrollTimer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
143
144         r = __pFastScrollTimer->Construct(*this);
145         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
146
147         return true;
148
149 CATCH:
150         if (__isFastScrollTimerEnabled)
151         {
152                 __pFastScrollTimer->Cancel();
153         }
154
155         delete __pFastScrollTimer;
156         __pFastScrollTimer = null;
157
158         return false;
159 }
160
161 result
162 _TableViewPresenter::Construct(_TableView* pTableView)
163 {
164         result r = E_SUCCESS;
165         bool init = false;
166
167         // Create Model
168         init = Initialize(pTableView);
169         SysTryCatch(NID_UI_CTRL, init == true, , r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
170
171         return r;
172
173 CATCH:
174         Dispose();
175         return r;
176 }
177
178 void
179 _TableViewPresenter::Dispose(void)
180 {
181         DeleteItemHeightList();
182         DeleteSectionAlignmentList();
183         DetachContextItem(__sweptItemTag);
184
185         delete __pListModel;
186         __pListModel = null;
187
188         delete __pProviderAdaptor;
189         __pProviderAdaptor = null;
190
191         delete __pItemDrawingProperty;
192         __pItemDrawingProperty = null;
193
194         delete __pReorderScrollTimer;
195         __pReorderScrollTimer = null;
196
197         if (__isFastScrollTimerEnabled)
198         {
199                 __pFastScrollTimer->Cancel();
200         }
201
202         delete __pFastScrollTimer;
203         __pFastScrollTimer = null;
204
205         if (__pCapturedItemVisualElement)
206         {
207                 GetView()->GetVisualElement()->DetachChild(*__pCapturedItemVisualElement);
208                 __pCapturedItemVisualElement->Destroy();
209         }
210         __pCapturedItemVisualElement = null;
211 }
212
213 result
214 _TableViewPresenter::SetItemProvider(const _TableViewItemProvider* pProvider)
215 {
216         result r = E_SUCCESS;
217
218         _TableViewItemProviderAdaptor* pProviderAdaptor = static_cast <_TableViewItemProviderAdaptor*>(__pListModel->GetItemProviderAdaptor());
219         if(pProviderAdaptor != null)
220         {
221                 _TableViewItemProvider* provider = pProviderAdaptor->GetItemProvider();
222                 if(provider != null && provider == pProvider)
223                 {
224                         return r;
225                 }
226         }
227
228         if (pProviderAdaptor == null)
229         {
230                 pProviderAdaptor = new (std::nothrow) _TableViewItemProviderAdaptor();
231                 SysTryReturn(NID_UI_CTRL, pProviderAdaptor != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, ("[E_OUT_OF_MEMORY] The memory is insufficient."));
232
233                 SetTableViewItemProviderAdaptor(pProviderAdaptor);
234         }
235
236         _TableViewItemProvider* pNewProvider = null;
237
238         if (pProvider != null)
239         {
240                 pNewProvider = const_cast <_TableViewItemProvider*>(pProvider);
241         }
242         else
243         {
244                 __pListModel->RemoveAllItem(false);
245         }
246
247         pProviderAdaptor->SetItemProvider(pNewProvider);
248         pProviderAdaptor->SetListWidth(__pTableView->GetBoundsF().width - (GetLeftMargin() * 2));
249         pProviderAdaptor->SetTableViewStyle(__pTableView->GetTableViewStyle());
250
251         return r;
252 }
253
254 result
255 _TableViewPresenter::GetItemFromPosition(const Tizen::Graphics::FloatPoint& position, TableViewItemTag& itemPos) const
256 {
257         _TableViewItem* pItem = null;
258         TableViewItemTag lastItemPos = {-1, -1};
259
260         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
261         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
262
263         do
264         {
265                 pItem = static_cast <_TableViewItem*>(__pListModel->LoadItem(itemPos.groupIndex, itemPos.itemIndex));
266                 SysTryCatch(NID_UI_CTRL, pItem != null, , E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
267
268                 FloatRectangle itemBounds = pItem->GetBoundsF();
269                 if (itemBounds.Contains(position))
270                 {
271                         return E_SUCCESS;
272                 }
273
274                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
275                 {
276                         break;
277                 }
278
279         } while (GetNextItemPosition(itemPos, itemPos));
280
281 CATCH:
282         itemPos.itemIndex = -1;
283         itemPos.groupIndex = -1;
284
285         return E_OUT_OF_RANGE;
286 }
287
288 result
289 _TableViewPresenter::GetTopDrawnItemIndex(int& groupIndex, int& itemIndex) const
290 {
291         result r = E_SUCCESS;
292
293         TableViewItemTag itemPos;
294         r = GetTopDrawnItem(itemPos);
295
296         groupIndex = itemPos.groupIndex;
297         itemIndex = itemPos.itemIndex;
298
299         return r;
300 }
301
302 result
303 _TableViewPresenter::SetTopDrawnItemIndex(int groupIndex, int itemIndex)
304 {
305         if (IsEmpty())
306         {
307                 return E_INVALID_ARG;
308         }
309
310         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
311         {
312                 return E_OUT_OF_RANGE;
313         }
314
315         ScrollToItem(groupIndex, itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
316
317         return E_SUCCESS;
318 }
319
320 result
321 _TableViewPresenter::GetBottomDrawnItemIndex(int& groupIndex, int& itemIndex) const
322 {
323         result r = E_SUCCESS;
324
325         TableViewItemTag itemPos;
326         r = GetBottomDrawnItem(itemPos);
327
328         groupIndex = itemPos.groupIndex;
329         itemIndex = itemPos.itemIndex;
330
331         return r;
332 }
333
334 result
335 _TableViewPresenter::SetBottomDrawnItemIndex(int groupIndex, int itemIndex)
336 {
337         if (IsEmpty())
338         {
339                 return E_INVALID_ARG;
340         }
341
342         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
343         {
344                 return E_OUT_OF_RANGE;
345         }
346
347         ScrollToItem(groupIndex, itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
348
349         return E_SUCCESS;
350 }
351
352 int
353 _TableViewPresenter::GetGroupCount(void) const
354 {
355         return __pListModel->GetAllGroupCount();
356 }
357
358
359 int
360 _TableViewPresenter::GetItemCountAt(int groupIndex) const
361 {
362         return __pListModel->GetItemCountInGroup(groupIndex);
363 }
364
365 bool
366 _TableViewPresenter::HasSectionHeader(int groupIndex) const
367 {
368         if (__pProviderAdaptor == null || __pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SECTION)
369         {
370                 return false;
371         }
372
373         return __pProviderAdaptor->HasSectionHeader(groupIndex);
374 }
375
376 bool
377 _TableViewPresenter::HasSectionFooter(int groupIndex) const
378 {
379         if (__pProviderAdaptor == null || __pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SECTION)
380         {
381                 return false;
382         }
383
384         return __pProviderAdaptor->HasSectionFooter(groupIndex);
385 }
386
387 int
388 _TableViewPresenter::GetItemCount(void) const
389 {
390         return __pListModel->GetAllItemCount();
391 }
392
393 result
394 _TableViewPresenter::RefreshTableView(int groupIndex, int itemIndex, TableViewRefreshType type, bool animation)
395 {
396         _TableViewItemProviderAdaptor* pProviderAdaptor = __pProviderAdaptor;
397
398         SysTryReturn(NID_UI_CTRL, pProviderAdaptor != null, E_INVALID_STATE, E_INVALID_STATE,
399                                 ("[E_INVALID_STATE] This instance isn't constructed."));
400
401         if ((groupIndex < 0) || (groupIndex > GetGroupCount()) || (itemIndex < -1) || (itemIndex > GetItemCountAt(groupIndex)))
402         {
403                 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
404                 return E_OUT_OF_RANGE;
405         }
406
407         if (groupIndex == GetGroupCount() && itemIndex != -1 && type != TABLE_VIEW_REFRESH_TYPE_ITEM_ADD)
408         {
409                 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
410                 return E_OUT_OF_RANGE;
411         }
412
413         TableViewItemTag topDrawnItemPos = {-1, -1};
414         TableViewItemTag refreshItemPos = {groupIndex, itemIndex};
415         float prevScrollAreaHeight = GetScrollAreaBounds().height;
416         float prevScrollPosition = GetScrollPosition();
417
418         GetTopDrawnItem(topDrawnItemPos);
419
420         ResetSweepItem();
421
422         switch (type)
423         {
424         case TABLE_VIEW_REFRESH_TYPE_ITEM_ADD:
425                 if (itemIndex == -1)
426                 {
427                         bool emptyState = IsEmpty();
428
429                         int itemCount = pProviderAdaptor->GetItemCount(groupIndex);
430                         if (__pListModel->InsertGroup(groupIndex, itemCount, false) != E_SUCCESS)
431                         {
432                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to add Group item.");
433                                 return E_SYSTEM;
434                         }
435
436                         if (emptyState)
437                         {
438                                 GetFirstItem(topDrawnItemPos);
439                         }
440                         else if (groupIndex <= topDrawnItemPos.groupIndex)
441                         {
442                                 topDrawnItemPos.groupIndex++;
443                         }
444                 }
445                 else
446                 {
447                         bool emptyState = IsEmpty();
448
449                         if (__pListModel->InsertItemToGroup(null, groupIndex, itemIndex) != E_SUCCESS)
450                         {
451                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to add item.");
452                                 return E_SYSTEM;
453                         }
454
455                         if (emptyState)
456                         {
457                                 GetFirstItem(topDrawnItemPos);
458                         }
459                 }
460
461                 RefreshItemHeightList(groupIndex, itemIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_ADD);
462                 RefreshSectionAlignmentList(groupIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_ADD);
463
464                 if (__pListModel->GetAllItemCount() == 1)
465                 {
466                         SetStatusChanged(true);
467                 }
468
469                 break;
470
471         case TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE:
472                 if (itemIndex == -1)
473                 {
474                         if (groupIndex == GetGroupCount())
475                         {
476                                 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
477                                 return E_OUT_OF_RANGE;
478                         }
479
480                         RefreshItemHeightList(groupIndex, itemIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE);
481                         RefreshSectionAlignmentList(groupIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE);
482
483                         if (__pListModel->RemoveGroup(groupIndex) != E_SUCCESS)
484                         {
485                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to remove group.");
486                                 return E_SYSTEM;
487                         }
488
489                         if (groupIndex == topDrawnItemPos.groupIndex)
490                         {
491                                 topDrawnItemPos.itemIndex = -1;
492                         }
493
494                         if (groupIndex < topDrawnItemPos.groupIndex)
495                         {
496                                 topDrawnItemPos.groupIndex--;
497                         }
498
499                         if ((groupIndex == GetGroupCount()) && (GetGroupCount() > 0))
500                         {
501                                 topDrawnItemPos.groupIndex--;
502                                 topDrawnItemPos.itemIndex = GetItemCountAt(GetGroupCount() - 1) - 1;
503                         }
504                 }
505                 else
506                 {
507                         if (itemIndex == GetItemCountAt(groupIndex))
508                         {
509                                 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
510                                 return E_OUT_OF_RANGE;
511                         }
512
513                         if (__pListModel->RemoveItemAt(groupIndex, itemIndex) != E_SUCCESS)
514                         {
515                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to remove item.");
516                                 return E_SYSTEM;
517                         }
518
519                         if ((groupIndex == topDrawnItemPos.groupIndex) && (itemIndex < topDrawnItemPos.itemIndex))
520                         {
521                                 GetPreviousItemPosition(topDrawnItemPos, topDrawnItemPos);
522                         }
523
524                         RefreshItemHeightList(groupIndex, itemIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE);
525                 }
526
527                 if (GetModel()->IsValidItem(topDrawnItemPos.groupIndex, topDrawnItemPos.itemIndex) == false)
528                 {
529                         GetPreviousItemPosition(topDrawnItemPos, topDrawnItemPos);
530                 }
531
532                 if (__pListModel->GetAllItemCount() == 0)
533                 {
534                         SetStatusChanged(true);
535                 }
536
537                 break;
538
539         case TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY:
540                 {
541                         TableViewItemTag itemTag = {groupIndex, itemIndex};
542                         _TableViewItem* pTableViewItem = FindItem(itemTag);
543
544                         if (pTableViewItem == null)
545                         {
546                                 return E_SUCCESS;
547                         }
548
549                         if (pProviderAdaptor->UpdateItem(pTableViewItem, itemTag.groupIndex, itemTag.itemIndex))
550                         {
551                                 FloatRectangle itemBounds = pTableViewItem->GetBoundsF();
552                                 FloatDimension itemDim = Tizen::Graphics::CoordinateSystem::AlignToDevice(FloatDimension(itemBounds.width, itemBounds.height));
553                                 itemBounds.height = itemDim.height;
554                                 pTableViewItem->SetBounds(itemBounds);
555
556                                 CheckItemHeightAndRefreshLayout(itemTag, true);
557
558                                 if (__sweptItemTag.groupIndex != itemTag.groupIndex || __sweptItemTag.itemIndex != itemTag.itemIndex)
559                                 {
560                                         ResetSweptItem();
561                                 }
562
563                                 pTableViewItem->SetPublicLabelUpdate(false);
564                                 pTableViewItem->SetItemChanged(true);
565                                 pTableViewItem->Invalidate();
566                                 pTableViewItem->AdjustContextItemBounds();
567
568                                 return E_SUCCESS;
569                         }
570                 }
571                 break;
572
573         default:
574                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to refresh list.");
575                 return E_SYSTEM;
576                 break;
577         }
578
579         __lockLoadItemWithScroll = true;
580
581         AdjustClientAreaBounds(true);
582
583         __lockLoadItemWithScroll = false;
584
585         if ((topDrawnItemPos.groupIndex > refreshItemPos.groupIndex)
586                 ||((topDrawnItemPos.groupIndex == refreshItemPos.groupIndex)&&(topDrawnItemPos.itemIndex > refreshItemPos.itemIndex)))
587         {
588                 RefreshItemLayout(topDrawnItemPos, refreshItemPos, type, false);
589
590                 float newScrollPosition = prevScrollPosition - (prevScrollAreaHeight - GetScrollAreaBounds().height);
591
592                 __lockLoadItemWithScroll = true;
593
594                 SetScrollPosition(newScrollPosition, false);
595
596                 __lockLoadItemWithScroll = false;
597         }
598         else
599         {
600                 RefreshItemLayout(topDrawnItemPos, refreshItemPos, type, animation);
601         }
602
603         return E_SUCCESS;
604 }
605
606 result
607 _TableViewPresenter::RefreshAllItems(void)
608 {
609         result r = E_SUCCESS;
610         TableViewItemTag itemTag = {-1, -1};
611         TableViewItemTag lastLoadedItemTag = {-1, -1};
612
613         __pListModel->GetLastLoadedItemIndex(lastLoadedItemTag.groupIndex, lastLoadedItemTag.itemIndex);
614         __pListModel->GetFirstLoadedItemIndex(itemTag.groupIndex, itemTag.itemIndex);
615
616         do
617         {
618                 _TableViewItem* pItem = FindItem(itemTag);
619
620                 if (pItem == null)
621                 {
622                         continue;
623                 }
624
625                 r = RefreshTableView(itemTag.groupIndex, itemTag.itemIndex, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY, false);
626                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
627
628                 if ((itemTag.itemIndex == lastLoadedItemTag.itemIndex) && (itemTag.groupIndex == lastLoadedItemTag.groupIndex))
629                 {
630                         break;
631                 }
632         } while (GetNextItemPosition(itemTag, itemTag));
633
634         return r;
635 }
636
637 result
638 _TableViewPresenter::UpdateTableView(void)
639 {
640         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
641
642         if (pVisualElement != null)
643         {
644                 __pTableView->GetVisualElement()->RemoveAllAnimations();
645         }
646
647
648         if (__modelInitialized == false)
649         {
650                 SetItemDrawingProperty();
651
652                 if(PreloadItem() == false)
653                 {
654                         return E_SUCCESS;
655                 }
656         }
657         else
658         {
659                 if (IsEmpty())
660                 {
661                         __pListModel->RemoveAllItem(false, false);
662
663                         PreloadItem();
664                 }
665                 else
666                 {
667                         TableViewItemTag topDrawnTag = {-1, -1};
668                         float shiftingDistance = 0;
669
670                         ResetSweepItem();
671
672                         GetTopDrawnItem(topDrawnTag);
673
674                         if (__pListModel->IsValidItem(topDrawnTag.groupIndex, topDrawnTag.itemIndex))
675                         {
676                                 _TableViewItem* pItem = FindItem(topDrawnTag);
677
678                                 if (pItem != null)
679                                 {
680                                         shiftingDistance = GetScrollPosition() - pItem->GetBoundsF().y;
681                                 }
682                         }
683
684                         __pListModel->RemoveAllItem(false, true);
685
686                         PreloadItem(topDrawnTag.groupIndex, topDrawnTag.itemIndex, shiftingDistance);
687
688                         if (__isRestoreFocus)
689                         {
690                                 __pTableView->SetFocused(true);
691                                 __pTableView->DrawFocus();
692                                 __isRestoreFocus = false;
693                         }
694
695                         __statusChangedFlag = true;
696
697                         if (__itemTouchReleasedEventState == TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_FIRE)
698                         {
699                                 __itemTouchReleasedEventState = TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_UPDATE_TABLE_VIEW;
700                         }
701                 }
702         }
703
704         return E_SUCCESS;
705 }
706
707 void
708 _TableViewPresenter::AdjustLayoutItems(float scrollPosition)
709 {
710         TableViewItemTag lastLoadedItemPos = {-1, -1};
711         TableViewItemTag firstLoadedItemPos = {-1, -1};
712         TableViewItemTag currentItemPos = {-1, -1};
713         _TableViewItem* pItem = null;
714
715         __pListModel->GetFirstLoadedItemIndex(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex);
716         __pListModel->GetLastLoadedItemIndex(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex);
717
718         pItem = FindItem(firstLoadedItemPos);
719
720         if (pItem != null)
721         {
722                 float positionY = pItem->GetPositionF().y;
723
724                 currentItemPos = firstLoadedItemPos;
725
726                 while (!_FloatCompareLE(positionY, scrollPosition) && GetPreviousItemPosition(currentItemPos, currentItemPos))
727                 {
728                         pItem = LoadItem(currentItemPos.groupIndex, currentItemPos.itemIndex);
729
730                         if (pItem == null)
731                         {
732                                 break;
733                         }
734
735                         positionY = pItem->GetPositionF().y;
736                 }
737         }
738
739         pItem = FindItem(lastLoadedItemPos);
740
741         if (pItem != null)
742         {
743                 float positionY = pItem->GetPositionF().y;
744                 float itemHeight = pItem->GetBoundsF().height;
745                 float limitScreenArea = scrollPosition + __pTableView->GetBoundsF().height;
746                 currentItemPos = lastLoadedItemPos;
747
748                 while ((positionY + itemHeight < limitScreenArea) && GetNextItemPosition(currentItemPos, currentItemPos))
749                 {
750                         pItem = LoadItem(currentItemPos.groupIndex, currentItemPos.itemIndex);
751
752                         if (pItem == null)
753                         {
754                                 break;
755                         }
756
757                         positionY = pItem->GetPositionF().y;
758                         itemHeight = pItem->GetBoundsF().height;
759                 }
760         }
761 }
762
763 void
764 _TableViewPresenter::ResetItemLayout(TableViewItemTag& topDrawnItemTag, float shiftingDistance)
765 {
766         if (__pListModel->IsValidItem(topDrawnItemTag.groupIndex, topDrawnItemTag.itemIndex) == false)
767         {
768                 return;
769         }
770
771         DetachAllItem(true);
772
773         _TableViewItem* pItem = LoadItem(topDrawnItemTag.groupIndex, topDrawnItemTag.itemIndex);
774
775         if (pItem == null)
776         {
777                 return;
778         }
779
780         TableViewItemTag currentItem = topDrawnItemTag;
781         TableViewItemTag itemPos = topDrawnItemTag;
782         float screenAreaHeight = __pTableView->GetBoundsF().height + shiftingDistance;
783         float loadedItemTotalHeight = 0.0f;
784         FloatRectangle itemBounds = pItem->GetBoundsF();
785
786         itemBounds.y = CalculateItemPositionY(itemPos.groupIndex, itemPos.itemIndex);
787         pItem->SetBounds(itemBounds);
788         CheckItemHeightAndRefreshLayout(itemPos, true);
789
790         loadedItemTotalHeight += itemBounds.height;
791
792         while (loadedItemTotalHeight < screenAreaHeight && GetNextItemPosition(currentItem, itemPos))
793         {
794                 pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
795
796                 if (pItem == null)
797                 {
798                         break;
799                 }
800
801                 currentItem = itemPos;
802                 loadedItemTotalHeight += pItem->GetBoundsF().height;
803         }
804
805         if (loadedItemTotalHeight < screenAreaHeight)
806         {
807                 __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
808                 currentItem = itemPos;
809
810                 while (loadedItemTotalHeight < screenAreaHeight && GetPreviousItemPosition(currentItem, itemPos))
811                 {
812                         pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
813
814                         if (pItem == null)
815                         {
816                                 break;
817                         }
818
819                         currentItem = itemPos;
820                         loadedItemTotalHeight += pItem->GetBoundsF().height;
821                 }
822         }
823 }
824
825 bool
826 _TableViewPresenter::IsCachingItemsTotalHeightLessThanViewHeight(void)
827 {
828         int currentCachingsize = GetMaxItemCachingSize();
829         int loadedItemCount = 0;
830         float itemTotalHeight = 0.0f;
831         float viewHeight = __pTableView->GetBoundsF().height;
832         float lastItemHeight = 0.0f;
833
834         TableViewItemTag lastLoadedItemPos = {-1, -1};
835         TableViewItemTag firstLoadedItemPos = {-1, -1};
836
837         __pListModel->GetFirstLoadedItemIndex(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex);
838         __pListModel->GetLastLoadedItemIndex(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex);
839
840         do
841         {
842                 _TableViewItem* pItem = FindItem(firstLoadedItemPos);
843
844                 if (pItem != null)
845                 {
846                         itemTotalHeight = itemTotalHeight + pItem->GetSizeF().height;
847                         lastItemHeight = pItem->GetSizeF().height;
848                         loadedItemCount++;
849                 }
850
851                 if ((firstLoadedItemPos.itemIndex == lastLoadedItemPos.itemIndex) && (firstLoadedItemPos.groupIndex == lastLoadedItemPos.groupIndex))
852                 {
853                         break;
854                 }
855         } while (GetNextItemPosition(firstLoadedItemPos, firstLoadedItemPos));
856
857         if (loadedItemCount < currentCachingsize || viewHeight < (itemTotalHeight - lastItemHeight))
858         {
859                 return false;
860         }
861
862         return true;
863 }
864
865 void
866 _TableViewPresenter::GetFirstLoadedItemIndex(int& groupIndex, int& itemIndex) const
867 {
868         __pListModel->GetFirstLoadedItemIndex(groupIndex, itemIndex);
869 }
870
871 void
872 _TableViewPresenter::GetLastLoadedItemIndex(int& groupIndex, int& itemIndex) const
873 {
874         __pListModel->GetLastLoadedItemIndex(groupIndex, itemIndex);
875 }
876
877 void
878 _TableViewPresenter::RefreshItemLayout(int groupIndex, int itemIndex)
879 {
880         TableViewItemTag itemTag = {groupIndex, itemIndex};
881
882         ResetSweepItem();
883
884         RefreshItemLayout(itemTag, itemTag, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY, false);
885 }
886
887 void
888 _TableViewPresenter::RefreshItemLayout(TableViewItemTag& topDrawnItemTag, TableViewItemTag& refreshItemTag, TableViewRefreshType type, bool animation)
889 {
890         if (!__pListModel->IsValidItem(topDrawnItemTag.groupIndex, topDrawnItemTag.itemIndex))
891         {
892                 return;
893         }
894
895         if (refreshItemTag.itemIndex == -1)
896         {
897                 animation = false;
898         }
899
900         _TableViewItem* pItem = null;
901
902         TableViewItemTag itemPos = {-1, -1};
903         TableViewItemTag currentItem = {-1, -1};
904
905         int loadItemCount = 0;
906         int maxLoadItemCount = GetMaxItemCachingSize();
907         FloatRectangle itemBounds(0.0f, 0.0f, 0.0f, 0.0f);
908
909         StopAllItemAnimation();
910
911         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
912
913         if (itemPos.groupIndex == -1 && itemPos.itemIndex == -1)
914         {
915                 itemPos = topDrawnItemTag;
916         }
917
918         currentItem = itemPos;
919
920         float itemPositionY = CalculateItemPositionY(itemPos.groupIndex, itemPos.itemIndex);
921
922         do
923         {
924                 pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
925
926                 if (pItem == null)
927                 {
928                         break;
929                 }
930
931                 if (pItem->HasParent())
932                 {
933                         pItem->SetDrawingProperty(__pItemDrawingProperty);
934                         SetItemType(pItem, itemPos);
935                 }
936
937                 itemBounds = pItem->GetBoundsF();
938                 itemBounds.y = itemPositionY;
939
940                 if (animation)
941                 {
942                         if (type == TABLE_VIEW_REFRESH_TYPE_ITEM_ADD)
943                         {
944                                 if (itemPos.groupIndex == refreshItemTag.groupIndex && itemPos.itemIndex == refreshItemTag.itemIndex)
945                                 {
946                                         pItem->SetBounds(itemBounds);
947                                         pItem->FadeInOutItem(false, ADD_ITEM_ANIMATION_DURATION, REFRESH_ITEM_ANIMATION_DELAY);
948                                         pItem->ZoomInOutItem(false, ADD_ITEM_ANIMATION_DURATION, REFRESH_ITEM_ANIMATION_DELAY);
949                                 }
950                                 else
951                                 {
952                                         if (!pItem->MoveItem(FloatPoint(itemBounds.x, itemBounds.y), ADD_ITEM_ANIMATION_DURATION, 0))
953                                         {
954                                                 pItem->SetBounds(itemBounds);
955                                         }
956                                 }
957                         }
958                         else
959                         {
960                                 if (!pItem->MoveItem(FloatPoint(itemBounds.x, itemBounds.y), REMOVE_ITEM_MOVE_ANIMATION_DURATION, REFRESH_ITEM_ANIMATION_DELAY))
961                                 {
962                                         pItem->SetBounds(itemBounds);
963                                 }
964                         }
965                 }
966                 else
967                 {
968                         pItem->SetBounds(itemBounds);
969                 }
970
971                 currentItem = itemPos;
972                 itemPositionY = itemBounds.y + itemBounds.height;
973
974                 loadItemCount++;
975
976         } while (loadItemCount < maxLoadItemCount && GetNextItemPosition(currentItem, itemPos));
977
978         if (loadItemCount < maxLoadItemCount)
979         {
980                 __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
981                 currentItem = itemPos;
982
983                 while (loadItemCount < maxLoadItemCount && GetPreviousItemPosition(currentItem, itemPos))
984                 {
985                         pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
986
987                         if (pItem == null)
988                         {
989                                 break;
990                         }
991
992                         currentItem = itemPos;
993
994                         loadItemCount++;
995                 }
996         }
997
998         if (refreshItemTag.itemIndex == -1)
999         {
1000                 ScrollToItem(topDrawnItemTag.groupIndex, topDrawnItemTag.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
1001         }
1002 }
1003
1004 void
1005 _TableViewPresenter::RefreshItemBounds(_TableViewItem* pItem, TableViewItemTag& itemPos)
1006 {
1007         FloatRectangle itemBounds;
1008
1009         if (pItem == null)
1010         {
1011                 return;
1012         }
1013
1014         itemBounds = pItem->GetBoundsF();
1015
1016         itemBounds.y = CalculateItemPositionY(itemPos.groupIndex, itemPos.itemIndex);
1017
1018         if (itemPos.itemIndex != -1)
1019         {
1020                 float itemMargin = GetLeftMargin();
1021                 itemBounds.x = itemMargin;
1022                 itemBounds.width = __pItemDrawingProperty->width - itemBounds.x - itemMargin;
1023         }
1024         else
1025         {
1026                 itemBounds.x = 0.0f;
1027                 itemBounds.width = __pItemDrawingProperty->width;
1028         }
1029
1030         pItem->SetBounds(itemBounds);
1031 }
1032
1033 result
1034 _TableViewPresenter::GetItemIndexFromPosition(const FloatPoint& position, int& groupIndex, int& itemIndex) const
1035 {
1036         result r = E_SUCCESS;
1037         TableViewItemTag itemPos;
1038
1039         FloatPoint targetPosition = position;
1040         float scrollPosition = GetScrollPosition();
1041         targetPosition.y += scrollPosition;
1042
1043         r = GetItemFromPosition(targetPosition, itemPos);
1044
1045         if (r != E_SUCCESS)
1046         {
1047                 groupIndex = -1;
1048                 itemIndex = -1;
1049                 return r;
1050         }
1051
1052         groupIndex = itemPos.groupIndex;
1053         itemIndex = itemPos.itemIndex;
1054
1055         return r;
1056 }
1057
1058
1059 bool
1060 _TableViewPresenter::PreloadItem(void)
1061 {
1062         if (GetModel() == null || __pProviderAdaptor == null)
1063         {
1064                 return false;
1065         }
1066
1067         __modelInitialized = true;
1068
1069         if (__pListModel->GetAllGroupCount() == 0 && (__pProviderAdaptor != null))
1070         {
1071                 int itemCount = 0;
1072                 int groupCount = __pProviderAdaptor->GetGroupCount();
1073
1074                 for (int i = 0; i < groupCount; i++)
1075                 {
1076                         itemCount = __pProviderAdaptor->GetItemCount(i);
1077                         __pListModel->AddGroup(itemCount, false);
1078                 }
1079         }
1080
1081         __pListModel->RestoreItemStatus();
1082
1083         CreateItemHeightList(TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE, TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE);
1084         CreateSectionAlignmentList();
1085
1086         int cachingSize = 0;
1087
1088         if (GetItemCount() > TABLEVIEW_MAX_ITEM_COUNT)
1089         {
1090                 cachingSize = TABLEVIEW_MAX_ITEM_COUNT;
1091         }
1092         else if (__pListModel->GetMaxCachingSize() < TABLEVIEW_MAX_ITEM_COUNT)
1093         {
1094                 cachingSize = __pListModel->GetMaxCachingSize();
1095         }
1096         else
1097         {
1098                 cachingSize = GetItemCount();
1099         }
1100
1101         TableViewItemTag itemPos = {-1, -1};
1102         GetFirstItem(itemPos);
1103
1104         _TableViewItem* pItem = null;
1105         for (int i = 0; i < cachingSize; i++)
1106         {
1107                 pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
1108
1109                 if (pItem == null)
1110                 {
1111                         break;
1112                 }
1113
1114                 if (GetNextItemPosition(itemPos, itemPos) == false)
1115                 {
1116                         break;
1117                 }
1118         }
1119
1120         if (IsCachingItemsTotalHeightLessThanViewHeight())
1121         {
1122                 AdjustLayoutItems(0.0f);
1123         }
1124
1125         PresetItemHeightList();
1126         AdjustClientAreaBounds(true);
1127
1128         TableViewScrollBarStyle scrollStyle = __pTableView->GetScrollStyle();
1129
1130         if (scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL
1131                         || scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL_FIXED)
1132         {
1133                 _FastScroll* pFastScroll = __pTableView->GetFastScrollBar();
1134                 if (pFastScroll != null)
1135                 {
1136                         pFastScroll->UpdateIndex();
1137                         pFastScroll->SetScrollVisibility(IsScrollable());
1138                 }
1139         }
1140         else if (scrollStyle != TABLE_VIEW_SCROLL_BAR_STYLE_NONE)
1141         {
1142                 if (IsScrollable())
1143                 {
1144                         FadeInScrollBar();
1145                 }
1146         }
1147
1148         return true;
1149 }
1150
1151 bool
1152 _TableViewPresenter::PreloadItem(int topDrawnGroupIndex, int topDrawnItemIndex, float shiftingDistance)
1153 {
1154         if (GetModel() == null || __pProviderAdaptor == null)
1155         {
1156                 return false;
1157         }
1158
1159         __modelInitialized = true;
1160
1161         if (__pListModel->GetAllGroupCount() == 0 && (__pProviderAdaptor != null))
1162         {
1163                 int itemCount = 0;
1164                 int groupCount = __pProviderAdaptor->GetGroupCount();
1165
1166                 for (int i = 0; i < groupCount; i++)
1167                 {
1168                         itemCount = __pProviderAdaptor->GetItemCount(i);
1169                         __pListModel->AddGroup(itemCount, false);
1170                 }
1171         }
1172
1173         float defaultGroupHeight = __pProviderAdaptor->GetDefaultGroupItemHeight();
1174         float defaultItemHeight = __pProviderAdaptor->GetDefaultItemHeight();
1175
1176         if (_FloatCompareLE(defaultGroupHeight, 0.0f))
1177         {
1178                 defaultGroupHeight = TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE;
1179         }
1180
1181         if (_FloatCompareLE(defaultItemHeight, 0.0f))
1182         {
1183                 defaultItemHeight = TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE;
1184         }
1185
1186         __pListModel->RestoreItemStatus();
1187
1188         DeleteItemHeightList();
1189         CreateItemHeightList(defaultGroupHeight, defaultItemHeight);
1190
1191         DeleteSectionAlignmentList();
1192         CreateSectionAlignmentList();
1193
1194         if (!__pListModel->IsValidItem(topDrawnGroupIndex, topDrawnItemIndex))
1195         {
1196                 TableViewItemTag firstItemTag = {-1, -1};
1197
1198                 GetFirstItem(firstItemTag);
1199
1200                 topDrawnGroupIndex = firstItemTag.groupIndex;
1201                 topDrawnItemIndex = firstItemTag.itemIndex;
1202
1203                 shiftingDistance = 0.0f;
1204         }
1205
1206         AdjustClientAreaBounds(true);
1207
1208         ScrollToItem(topDrawnGroupIndex, topDrawnItemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP, shiftingDistance);
1209
1210         if (IsCachingItemsTotalHeightLessThanViewHeight())
1211         {
1212                 AdjustLayoutItems(GetScrollPosition());
1213         }
1214
1215         if (__pTableView->GetScrollStyle() == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL
1216                         || __pTableView->GetScrollStyle() == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL_FIXED)
1217         {
1218                 _FastScroll* pFastScroll = __pTableView->GetFastScrollBar();
1219                 if (pFastScroll != null)
1220                 {
1221                         pFastScroll->UpdateIndex();
1222                         pFastScroll->SetScrollVisibility(IsScrollable());
1223                 }
1224         }
1225
1226         return true;
1227 }
1228
1229 bool
1230 _TableViewPresenter::IsItemChecked(int groupIndex, int itemIndex) const
1231 {
1232         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
1233         {
1234                 return false;
1235         }
1236
1237         return __pListModel->IsItemChecked(groupIndex, itemIndex);
1238 }
1239
1240 result
1241 _TableViewPresenter::SetItemChecked(int groupIndex, int itemIndex, bool checked)
1242 {
1243         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
1244         {
1245                 return E_OUT_OF_RANGE;
1246         }
1247
1248         if (__modelInitialized == false)
1249         {
1250                 return E_INVALID_STATE;
1251         }
1252
1253         bool enabled = __pListModel->IsItemEnabled(groupIndex, itemIndex);
1254         SysTryReturn(NID_UI_CTRL, (enabled == true), E_INVALID_OPERATION, E_INVALID_OPERATION,
1255                         "[E_INVALID_OPERATION] The item is disabled.");
1256
1257         if (__pListModel->IsItemChecked(groupIndex, itemIndex) == checked)
1258         {
1259                 return E_SUCCESS;
1260         }
1261
1262         TableViewItemTag itemTag = {groupIndex, itemIndex};
1263         _TableViewItem *pItem = FindItem(itemTag);
1264
1265         TableViewItemTag topDrawnItemTag = {-1, -1};
1266         GetTopDrawnItem(topDrawnItemTag);
1267
1268         TableViewItemTag bottomDrawnItemTag = {-1, -1};
1269         GetBottomDrawnItem(bottomDrawnItemTag);
1270
1271         if (pItem != null)
1272         {
1273                 if (((itemTag.groupIndex == topDrawnItemTag.groupIndex && itemTag.itemIndex >= topDrawnItemTag.itemIndex)
1274                  || itemTag.groupIndex > topDrawnItemTag.groupIndex) &&
1275                 ((itemTag.groupIndex == bottomDrawnItemTag.groupIndex && itemTag.itemIndex <= bottomDrawnItemTag.itemIndex)
1276                  || itemTag.groupIndex < bottomDrawnItemTag.groupIndex))
1277                 {
1278                         pItem->SetCheckedAnimationEnabled(checked);
1279                 }
1280         }
1281
1282         result r = __pListModel->SetItemChecked(groupIndex, itemIndex, checked);
1283
1284         return r;
1285 }
1286
1287 bool
1288 _TableViewPresenter::IsItemEnabled(int groupIndex, int itemIndex) const
1289 {
1290         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
1291         {
1292                 return false;
1293         }
1294
1295         return __pListModel->IsItemEnabled(groupIndex, itemIndex);
1296 }
1297
1298 result
1299 _TableViewPresenter::SetItemEnabled(int groupIndex, int itemIndex, bool enabled)
1300 {
1301         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()) || (itemIndex < -1) || (itemIndex >= GetItemCountAt(groupIndex)))
1302         {
1303                 return E_OUT_OF_RANGE;
1304         }
1305
1306         if (__pListModel->IsItemEnabled(groupIndex, itemIndex) == enabled)
1307         {
1308                 return E_SUCCESS;
1309         }
1310
1311         result r = __pListModel->SetItemEnabled(groupIndex, itemIndex, enabled);
1312
1313         return r;
1314 }
1315
1316 result
1317 _TableViewPresenter::SetStatusChanged(bool changed)
1318 {
1319         __statusChangedFlag = changed;
1320
1321         if (__statusChangedFlag == true)
1322         {
1323                 SetItemDrawingProperty();
1324
1325                 if ((__pItemDrawingProperty != null) && (__pItemDrawingProperty->propertyChanged == true))
1326                 {
1327                         __pListModel->SetAllLoadedItemStateChanged(true);
1328                 }
1329         }
1330
1331         return E_SUCCESS;
1332 }
1333
1334 bool
1335 _TableViewPresenter::IsStatusChanged(void) const
1336 {
1337         return __statusChangedFlag;
1338 }
1339
1340 float
1341 _TableViewPresenter::GetTopMargin(void) const
1342 {
1343         return __topMargin;
1344 }
1345
1346 result
1347 _TableViewPresenter::SetTopMargin(float topMargin)
1348 {
1349         __topMargin = topMargin;
1350
1351         return E_SUCCESS;
1352 }
1353
1354 float
1355 _TableViewPresenter::GetBottomMargin(void) const
1356 {
1357         return __bottomMargin;
1358 }
1359
1360 result
1361 _TableViewPresenter::SetBottomMargin(float bottomMargin)
1362 {
1363         __bottomMargin = bottomMargin;
1364
1365         return E_SUCCESS;
1366 }
1367
1368 float
1369 _TableViewPresenter::GetLeftMargin(void) const
1370 {
1371         return __leftMargin;
1372 }
1373
1374 result
1375 _TableViewPresenter::SetLeftMargin(float leftMargin)
1376 {
1377         __leftMargin = leftMargin;
1378
1379         return E_SUCCESS;
1380 }
1381
1382 void
1383 _TableViewPresenter::RestoreFocus(bool isRestoreFocus)
1384 {
1385         __isRestoreFocus = isRestoreFocus;
1386 }
1387
1388 void
1389 _TableViewPresenter::SetAnnexFocused(bool isAnnexFocused)
1390 {
1391         __isAnnexFocused = isAnnexFocused;
1392 }
1393
1394 void
1395 _TableViewPresenter::SetTableViewFocused(bool focusStatus)
1396 {
1397         if (focusStatus == false)
1398         {
1399                 __focusItemTag.groupIndex = -1;
1400                 __focusItemTag.itemIndex = -1;
1401                 __isRestoreFocus = false;
1402                 __isAnnexFocused = false;
1403         }
1404         __isTableViewFocused = focusStatus;
1405 }
1406
1407 bool
1408 _TableViewPresenter::IsTableViewFocused(void)
1409 {
1410         return __isTableViewFocused;
1411 }
1412
1413 void
1414 _TableViewPresenter::SetItemType(_TableViewItem* pItem, TableViewItemTag itemPosition)
1415 {
1416         TableViewStyle style = __pTableView->GetTableViewStyle();
1417
1418         if (itemPosition.itemIndex == -1)
1419         {
1420                 if (style == TABLE_VIEW_STYLE_SECTION)
1421                 {
1422                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_HEADER);
1423                 }
1424                 else
1425                 {
1426                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_TITLE);
1427                 }
1428         }
1429         else
1430         {
1431                 int itemCount = GetItemCountAt(itemPosition.groupIndex);
1432                 if (style == TABLE_VIEW_STYLE_SIMPLE)
1433                 {
1434                         if (itemCount == 1 || (itemCount > 1 && (itemPosition.itemIndex == itemCount - 1)))
1435                         {
1436                                 pItem->SetSimpleLastItemEnabled(true);
1437                         }
1438                         else
1439                         {
1440                                 pItem->SetSimpleLastItemEnabled(false);
1441                         }
1442                 }
1443
1444                 if (itemPosition.itemIndex == 0)
1445                 {
1446                         if (HasSectionFooter(itemPosition.groupIndex))
1447                         {
1448                                 if (itemCount == 1)
1449                                 {
1450                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_FOOTER);
1451                                 }
1452                                 else if (itemCount == 2)
1453                                 {
1454                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_ONE);
1455                                 }
1456                                 else
1457                                 {
1458                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_TOP);
1459                                 }
1460                         }
1461                         else
1462                         {
1463                                 if (itemCount == 1)
1464                                 {
1465                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_ONE);
1466                                 }
1467                                 else
1468                                 {
1469                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_TOP);
1470                                 }
1471                         }
1472                 }
1473                 else
1474                 {
1475                         int lastItemIndex = itemCount - 1;
1476
1477                         if (HasSectionFooter(itemPosition.groupIndex))
1478                         {
1479                                 if (itemPosition.itemIndex == lastItemIndex)
1480                                 {
1481                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_FOOTER);
1482                                 }
1483                                 else if (itemPosition.itemIndex == lastItemIndex - 1)
1484                                 {
1485                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_BOTTOM);
1486                                 }
1487                                 else
1488                                 {
1489                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_MIDDLE);
1490                                 }
1491                         }
1492                         else
1493                         {
1494                                 if (itemPosition.itemIndex == lastItemIndex)
1495                                 {
1496                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_BOTTOM);
1497                                 }
1498                                 else
1499                                 {
1500                                         pItem->SetItemType(TABLE_VIEW_ITEM_TYPE_MIDDLE);
1501                                 }
1502                         }
1503                 }
1504         }
1505 }
1506
1507 void
1508 _TableViewPresenter::SetItemLayout(_TableViewItem* pItem, TableViewItemTag& itemPos)
1509 {
1510         _TableViewItem* pSeriesItem = null;
1511         TableViewItemTag seriesItemPos = {-1, -1};
1512         FloatRectangle seriesItemBounds;
1513         FloatRectangle itemBounds;
1514         bool validBounds = false;
1515         bool downScroll = true;
1516
1517         if (itemPos.itemIndex == -1 && __pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SECTION)
1518         {
1519                 itemBounds.x = 0;
1520                 itemBounds.width = __pItemDrawingProperty->width;
1521         }
1522         else
1523         {
1524                 float itemMargin = GetLeftMargin();
1525                 itemBounds.x = itemMargin;
1526                 itemBounds.width = __pItemDrawingProperty->width - itemBounds.x - itemMargin;
1527         }
1528
1529         FloatDimension itemDimension = Tizen::Graphics::CoordinateSystem::AlignToDevice(FloatDimension(pItem->GetSizeF().width, pItem->GetSizeF().height));
1530         itemBounds.height = itemDimension.height;
1531
1532         if (itemPos.groupIndex == 0 && itemPos.itemIndex == -1)
1533         {
1534                 itemBounds.y = GetTopMargin();
1535                 validBounds = true;
1536         }
1537         else
1538         {
1539                 if (GetPreviousItemPosition(itemPos, seriesItemPos))
1540                 {
1541                         if (__pListModel->IsLoadedItem(seriesItemPos.groupIndex, seriesItemPos.itemIndex))
1542                         {
1543                                 pSeriesItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(seriesItemPos.groupIndex, seriesItemPos.itemIndex));
1544                                 if ((pSeriesItem != null) && pSeriesItem->HasParent())
1545                                 {
1546                                         if (__pTableView->IsReorderModeEnabled() && (__reorderInfo.groupIndex == seriesItemPos.groupIndex && __reorderInfo.itemIndex == seriesItemPos.itemIndex))
1547                                         {
1548                                                 seriesItemBounds = __reorderInfo.itemBounds;
1549                                         }
1550                                         else
1551                                         {
1552                                                 seriesItemBounds = pSeriesItem->GetBoundsF();
1553                                         }
1554
1555                                         itemBounds.y = seriesItemBounds.y + seriesItemBounds.height;
1556
1557                                         validBounds = true;
1558                                 }
1559                         }
1560                 }
1561
1562                 if ((!validBounds) && GetNextItemPosition(itemPos, seriesItemPos))
1563                 {
1564                         if (__pListModel->IsLoadedItem(seriesItemPos.groupIndex, seriesItemPos.itemIndex))
1565                         {
1566                                 pSeriesItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(seriesItemPos.groupIndex, seriesItemPos.itemIndex));
1567                                 if ((pSeriesItem != null) && pSeriesItem->HasParent())
1568                                 {
1569                                         if (__pTableView->IsReorderModeEnabled() && (__reorderInfo.groupIndex == seriesItemPos.groupIndex && __reorderInfo.itemIndex == seriesItemPos.itemIndex))
1570                                         {
1571                                                 seriesItemBounds = __reorderInfo.itemBounds;
1572                                         }
1573                                         else
1574                                         {
1575                                                 seriesItemBounds = pSeriesItem->GetBoundsF();
1576                                         }
1577
1578                                         itemBounds.y = seriesItemBounds.y - itemBounds.height;
1579
1580                                         validBounds = true;
1581                                         downScroll = false;
1582                                 }
1583                         }
1584                 }
1585         }
1586
1587         if (validBounds)
1588         {
1589                 pItem->SetBounds(itemBounds);
1590
1591                 CheckItemHeightAndRefreshLayout(itemPos, downScroll);
1592         }
1593         else
1594         {
1595                 itemBounds.y = CalculateItemPositionY(itemPos.groupIndex, itemPos.itemIndex);
1596                 pItem->SetBounds(itemBounds);
1597
1598                 CheckItemHeightAndRefreshLayout(itemPos, true);
1599         }
1600
1601         if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
1602         {
1603                 TableViewItemTag reorderSelectedItemTag = {__reorderInfo.groupIndex, __reorderInfo.itemIndex};
1604                 _TableViewItem* pReorderSelectedItem = FindItem(reorderSelectedItemTag);
1605
1606                 if (pReorderSelectedItem != null)
1607                 {
1608                         pReorderSelectedItem->GetVisualElement()->SetZOrder(null, true);
1609                 }
1610         }
1611
1612         if (pItem->GetItemLayoutEnabled())
1613         {
1614                 pItem->PartialUpdateLayout();
1615         }
1616 }
1617
1618 _TableViewItem*
1619 _TableViewPresenter::LoadItem(int groupIndex, int itemIndex)
1620 {
1621         _TableViewItem* pItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(groupIndex, itemIndex));
1622
1623         if ((pItem != null) && !pItem->HasParent())
1624         {
1625                 __lockLoadItemWithScroll = true;
1626
1627                 __pTableView->AttachChild(*pItem);
1628
1629                 pItem->SetDrawingProperty(__pItemDrawingProperty);
1630
1631                 TableViewItemTag itemPosition = {groupIndex, itemIndex};
1632                 SetItemType(pItem, itemPosition);
1633                 SetItemLayout(pItem, itemPosition);
1634                 pItem->SetReorderMode(__pTableView->IsReorderModeEnabled());
1635
1636                 __lockLoadItemWithScroll = false;
1637         }
1638
1639         return pItem;
1640 }
1641
1642 _TableViewItem*
1643 _TableViewPresenter::FindItem(TableViewItemTag& itemTag)
1644 {
1645         if (__pListModel->IsLoadedItem(itemTag.groupIndex, itemTag.itemIndex))
1646         {
1647                 return static_cast<_TableViewItem*>(__pListModel->LoadItem(itemTag.groupIndex, itemTag.itemIndex));
1648         }
1649         return null;
1650 }
1651
1652 void
1653 _TableViewPresenter::UnloadItem(int groupIndex, int itemIndex)
1654 {
1655         __pListModel->UnloadItem(groupIndex, itemIndex);
1656 }
1657
1658 void
1659 _TableViewPresenter::UnloadItem(TableViewItemTag& itemTag)
1660 {
1661         __pListModel->UnloadItem(itemTag.groupIndex, itemTag.itemIndex);
1662
1663         return;
1664 }
1665
1666 void
1667 _TableViewPresenter::DetachItem(TableViewItemTag& itemTag)
1668 {
1669         _TableViewItem *pItem = FindItem(itemTag);
1670
1671         if (pItem != null && pItem->HasParent())
1672         {
1673                 pItem->GetParent()->DetachChild(*pItem);
1674         }
1675 }
1676
1677 void
1678 _TableViewPresenter::DetachContextItem(TableViewItemTag& itemTag)
1679 {
1680         _TableViewItem *pItem = FindItem(itemTag);
1681         if (pItem == null)
1682         {
1683                 return;
1684         }
1685
1686         _TableViewItem* contextItem = pItem->GetContextItem();
1687         if (contextItem != null && contextItem->HasParent())
1688         {
1689                 contextItem->GetParent()->DetachChild(*pItem);
1690         }
1691 }
1692
1693 void
1694 _TableViewPresenter::DetachAllItem(bool removeItem)
1695 {
1696         TableViewItemTag itemPos = {-1, -1};
1697         TableViewItemTag lastItemPos = {-1, -1};
1698
1699         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
1700         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
1701
1702         if (itemPos.groupIndex == -1 && itemPos.itemIndex == -1)
1703         {
1704                 return;
1705         }
1706
1707         do
1708         {
1709                 DetachItem(itemPos);
1710
1711                 if (removeItem)
1712                 {
1713                         UnloadItem(itemPos);
1714                 }
1715
1716                 if (itemPos.itemIndex == lastItemPos.itemIndex && itemPos.groupIndex == lastItemPos.groupIndex)
1717                 {
1718                         break;
1719                 }
1720         } while (GetNextItemPosition(itemPos, itemPos));
1721 }
1722
1723 void
1724 _TableViewPresenter::SetItemDrawingProperty(void)
1725 {
1726         if (__pItemDrawingProperty != null)
1727         {
1728                 __pItemDrawingProperty->propertyChanged = false;
1729
1730                 if (__pItemDrawingProperty->itemDividerEnabled != __pTableView->IsItemDividerEnabled())
1731                 {
1732                         __pItemDrawingProperty->itemDividerEnabled = __pTableView->IsItemDividerEnabled();
1733                         __pItemDrawingProperty->propertyChanged = true;
1734                 }
1735
1736                 if (__pItemDrawingProperty->dividerColor != __pTableView->GetItemDividerColor())
1737                 {
1738                         __pItemDrawingProperty->dividerColor = __pTableView->GetItemDividerColor();
1739                         __pItemDrawingProperty->propertyChanged = true;
1740                 }
1741
1742                 if (__pItemDrawingProperty->sectionStyleEnabled != (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION))
1743                 {
1744                         __pItemDrawingProperty->sectionStyleEnabled = (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION);
1745                         __pItemDrawingProperty->propertyChanged = true;
1746                 }
1747
1748                 if (__pItemDrawingProperty->sectionStyleBgColor != __pTableView->GetSectionColor())
1749                 {
1750                         __pItemDrawingProperty->sectionStyleBgColor = __pTableView->GetSectionColor();
1751                         __pItemDrawingProperty->propertyChanged = true;
1752                 }
1753
1754                 if (__pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SIMPLE)
1755                 {
1756                         if (__pItemDrawingProperty->groupedLookEnabled != __pTableView->IsGroupedLookEnabled())
1757                         {
1758                                 __pItemDrawingProperty->groupedLookEnabled = __pTableView->IsGroupedLookEnabled();
1759                                 __pItemDrawingProperty->propertyChanged = true;
1760                         }
1761                 }
1762
1763                 if (__pItemDrawingProperty->reorderMode != __pTableView->IsReorderModeEnabled())
1764                 {
1765                         __pItemDrawingProperty->reorderMode = __pTableView->IsReorderModeEnabled();
1766                         __pItemDrawingProperty->propertyChanged = true;
1767                 }
1768
1769                 if (!_FloatCompare(__pItemDrawingProperty->leftMargin, __leftMargin))
1770                 {
1771                         __pItemDrawingProperty->leftMargin = __leftMargin;
1772                         __pItemDrawingProperty->propertyChanged = true;
1773                 }
1774
1775                 if (__pTableView->GetScrollStyle() == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL_FIXED)
1776                 {
1777                         GET_SHAPE_CONFIG(TABLEVIEW::FASTSCROLL_INDEX_WIDTH, _CONTROL_ORIENTATION_PORTRAIT,
1778                                         __pItemDrawingProperty->scrollMargin);
1779                 }
1780                 else
1781                 {
1782                         __pItemDrawingProperty->scrollMargin = 0;
1783                 }
1784
1785                 if (!_FloatCompare(__pItemDrawingProperty->width, __pTableView->GetBoundsF().width))
1786                 {
1787                         __pItemDrawingProperty->width = __pTableView->GetBoundsF().width;
1788                         __pItemDrawingProperty->propertyChanged = true;
1789                 }
1790         }
1791 }
1792
1793 void
1794 _TableViewPresenter::OnBoundsChanged(void)
1795 {
1796         float listWidth = __pTableView->GetBoundsF().width - (GetLeftMargin() * 2);
1797
1798         if (__pProviderAdaptor != null && __modelInitialized)
1799         {
1800                 StopExpandCollapseAnimation();
1801
1802                 if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
1803                 {
1804                         ResetReorderItem(__reorderInfo.groupIndex, __reorderInfo.itemIndex);
1805                 }
1806
1807                 if (!_FloatCompare(listWidth, __pProviderAdaptor->GetListWidth()))
1808                 {
1809                         __pProviderAdaptor->SetListWidth(listWidth);
1810
1811                         SetItemDrawingProperty();
1812
1813                         AdjustLoadedItemWidth();
1814
1815                         __pTableView->UpdateLayout();
1816                 }
1817
1818                 SetClientAreaHeight(__itemTotalHeight);
1819                 ResetSweepItem();
1820
1821                 AdjustLayoutItems(GetScrollPosition());
1822         }
1823         else
1824         {
1825                 if (__pProviderAdaptor != null)
1826                 {
1827                         __pProviderAdaptor->SetListWidth(listWidth);
1828                 }
1829
1830                 SetClientAreaHeight(__pTableView->GetBoundsF().height);
1831                 _ScrollPanelPresenter::OnBoundsChanged();
1832         }
1833 }
1834
1835 result
1836 _TableViewPresenter::Draw(void)
1837 {
1838         result r = _ScrollPanelPresenter::Draw();
1839         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1840
1841         if (__modelInitialized == false)
1842         {
1843                 SetItemDrawingProperty();
1844
1845                 if(PreloadItem() == false)
1846                 {
1847                         __pTableView->SetFocusNavigateEnabled(false);
1848                         return E_SUCCESS;
1849                 }
1850
1851                 if (__reservedScrollItemIndex.groupIndex != -1 && __reservedScrollItemIndex.itemIndex != -1)
1852                 {
1853                         ScrollToItem(__reservedScrollItemIndex.groupIndex, __reservedScrollItemIndex.itemIndex, __reservedScrollItemAlignment);
1854
1855                         __reservedScrollItemIndex.groupIndex = -1;
1856                         __reservedScrollItemIndex.itemIndex = -1;
1857
1858                         return E_SUCCESS;
1859                 }
1860         }
1861
1862         __firstDrawnFlag = false;
1863
1864         // Draw empty list
1865         if (IsEmpty() == true)
1866         {
1867                 __pTableView->SetFocusNavigateEnabled(false);
1868                 return DrawEmptyTableView();
1869         }
1870         else
1871         {
1872                 __pTableView->SetFocusNavigateEnabled(true);
1873         }
1874
1875         return E_SUCCESS;
1876 }
1877
1878 _UiTouchEventDelivery
1879 _TableViewPresenter::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
1880 {
1881         _UiTouchEventDelivery response = _ScrollPanelPresenter::OnPreviewTouchPressed(source, touchInfo);
1882
1883         _TableViewItem* pItem = GetTableViewItemFromControl(source);
1884         if (pItem == null)
1885         {
1886                 return response;
1887         }
1888
1889         TableViewItemTag itemPos = {-1, -1};
1890
1891         pItem->GetItemIndex(itemPos.groupIndex, itemPos.itemIndex);
1892
1893         if (itemPos.groupIndex == -1 && itemPos.itemIndex == -1)
1894         {
1895                 return response;
1896         }
1897
1898         if (IsScrollAnimationRunning() && (GetScrollPosition() == GetScrollPositionInternal()))
1899         {
1900                 pItem->SetTouchPressOnScroll(true);
1901                 return response;
1902         }
1903
1904         if (__pTableView->IsReorderModeEnabled())
1905         {
1906                 __firstTouchMoved = true;
1907
1908                 if (SelectReorderItem(itemPos.groupIndex, itemPos.itemIndex, false))
1909                 {
1910                         __reorderInfo.touchPressedPositionY = touchInfo.GetCurrentPosition().y;
1911                 }
1912         }
1913
1914         return response;
1915 }
1916
1917 _UiTouchEventDelivery
1918 _TableViewPresenter::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
1919 {
1920         if (__pTableView->IsReorderModeEnabled())
1921         {
1922                 if (__reorderInfo.itemIndex != -1)
1923                 {
1924                         if (!DragSelectedItem(touchInfo.GetCurrentPosition().y - __reorderInfo.touchPressedPositionY, true))
1925                         {
1926                                 __firstTouchMoved = false;
1927
1928                                 ResetReorderItem(__reorderInfo.groupIndex, __reorderInfo.itemIndex);
1929                         }
1930
1931                         return _UI_TOUCH_EVENT_DELIVERY_NO;
1932                 }
1933         }
1934
1935         return _ScrollPanelPresenter::OnPreviewTouchMoved(source, touchInfo);
1936 }
1937
1938 _UiTouchEventDelivery
1939 _TableViewPresenter::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
1940 {
1941         if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
1942         {
1943                 ResetReorderItem(__reorderInfo.groupIndex, __reorderInfo.itemIndex);
1944
1945                 if (__reorderInfo.blockedTouchReleaseState)
1946                 {
1947                         __reorderInfo.blockedTouchReleaseState = false;
1948
1949                         return _UI_TOUCH_EVENT_DELIVERY_NO;
1950                 }
1951         }
1952
1953         return _ScrollPanelPresenter::OnPreviewTouchReleased(source, touchInfo);
1954 }
1955
1956 _UiTouchEventDelivery
1957 _TableViewPresenter::OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
1958 {
1959         return _ScrollPanelPresenter::OnPreviewTouchCanceled(source, touchInfo);
1960 }
1961
1962 bool
1963 _TableViewPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchInfo)
1964 {
1965         __firstTouchMoved = true;
1966
1967         _TableViewItem* pItem = GetTableViewItemFromControl(source);
1968
1969         if (pItem == null)
1970         {
1971                 StopExpandCollapseAnimation();
1972                 return _ScrollPanelPresenter::OnTouchPressed(source, touchInfo);
1973         }
1974
1975         TableViewItemTag itemPos = {-1, -1};
1976         pItem->GetItemIndex(itemPos.groupIndex, itemPos.itemIndex);
1977
1978         if (!(pItem->IsContextItem()
1979                         || pItem->IsItemEnabled() == false
1980                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_TITLE
1981                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_HEADER
1982                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_FOOTER)
1983                         && ((itemPos.groupIndex != __sweptItemTag.groupIndex)
1984                                         || (itemPos.itemIndex != __sweptItemTag.itemIndex)))
1985         {
1986                 ResetSweptItem();
1987         }
1988
1989         __sweepOccured = false;
1990
1991         StopExpandCollapseAnimation();
1992
1993         return _ScrollPanelPresenter::OnTouchPressed(source, touchInfo);
1994 }
1995
1996 bool
1997 _TableViewPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchInfo)
1998 {
1999         if (__pTableView->IsReorderModeEnabled())
2000         {
2001                 if (__reorderInfo.blockedScroll)
2002                 {
2003                         return true;
2004                 }
2005
2006                 return _ScrollPanelPresenter::OnTouchMoved(source, touchInfo);
2007         }
2008
2009         _TableViewItem* pItem = GetTableViewItemFromControl(source);
2010         if (pItem == null
2011                         || pItem->IsItemEnabled() == false
2012                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_TITLE
2013                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_HEADER
2014                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_FOOTER
2015                         || pItem->IsAnnexOnOffSliding())
2016         {
2017                 return _ScrollPanelPresenter::OnTouchMoved(source, touchInfo);;
2018         }
2019
2020         if (__firstTouchMoved)
2021         {
2022                 FloatPoint prevTouchPosition = GetPreviousTouchPosition();
2023                 FloatPoint currTouchPosition = GetCurrentTouchPosition();
2024                 float moveDistanceX = currTouchPosition.x - prevTouchPosition.x;
2025                 float moveDistanceY = currTouchPosition.y - prevTouchPosition.y;
2026
2027                 if ((pItem->GetContextItem() != null && abs(moveDistanceX) > abs(moveDistanceY * 2)) || pItem->IsContextItem())
2028                 {
2029                         if (!pItem->IsContextItem() && !pItem->IsAnimationPlaying())
2030                         {
2031                                 pItem->GetItemIndex(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex);
2032                                 __sweptItemPosition = pItem->GetPositionF();
2033
2034                                 pItem->AdjustContextItemBounds();
2035                         }
2036
2037                         __sweepOccured = true;
2038                 }
2039                 else if (pItem->GetContextItem() == null && __pTableView->IsSweepEnabled())
2040                 {
2041                         __sweepOccured = true;
2042                 }
2043                 else
2044                 {
2045                         ResetSweptItem();
2046                 }
2047         }
2048
2049         __firstTouchMoved = false;
2050
2051         if (__sweepOccured)
2052         {
2053                 FloatPoint prevTouchPosition = GetPreviousTouchPosition();
2054                 FloatPoint currTouchPosition = GetCurrentTouchPosition();
2055
2056                 SweepItem(currTouchPosition.x - prevTouchPosition.x);
2057
2058                 return true;
2059         }
2060
2061         return _ScrollPanelPresenter::OnTouchMoved(source, touchInfo);
2062 }
2063
2064 bool
2065 _TableViewPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchInfo)
2066 {
2067         _TableViewItem* pItem = GetTableViewItemFromControl(source);
2068
2069         if (pItem == null
2070                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_TITLE
2071                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_HEADER
2072                         || pItem->GetItemType() == TABLE_VIEW_ITEM_TYPE_FOOTER)
2073         {
2074                 return _ScrollPanelPresenter::OnTouchReleased(source, touchInfo);
2075         }
2076
2077         if(__sweepOccured)
2078         {
2079                 if (pItem->IsContextItem())
2080                 {
2081                         pItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex));
2082                 }
2083                 _TableViewItem* pContextItem = pItem->GetContextItem();
2084
2085                 // Swept event fire
2086                 if (pContextItem == null && __pTableView->IsSweepEnabled())
2087                 {
2088                         FloatPoint prevPos = _ScrollPanelPresenter::GetPreviousTouchPosition();
2089
2090                         if (prevPos.x > touchInfo.GetCurrentPosition().x)
2091                         {
2092                                 pItem->FireItemSweepEvent(TABLE_VIEW_SWEEP_DIRECTION_LEFT);
2093                         }
2094                         else if (prevPos.x < touchInfo.GetCurrentPosition().x)
2095                         {
2096                                 pItem->FireItemSweepEvent(TABLE_VIEW_SWEEP_DIRECTION_RIGHT);
2097                         }
2098
2099                         ResetSweptItem();
2100                 }
2101                 else if (pContextItem != null)
2102                 {
2103                         AdjustSweptItemPosition(true);
2104                 }
2105         }
2106
2107         return _ScrollPanelPresenter::OnTouchReleased(source, touchInfo);
2108 }
2109
2110 bool
2111 _TableViewPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchInfo)
2112 {
2113         if (__sweepOccured)
2114         {
2115                 ResetSweptItem();
2116         }
2117
2118         if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
2119         {
2120                 ResetReorderItem(__reorderInfo.groupIndex, __reorderInfo.itemIndex);
2121                 return true;
2122         }
2123
2124         return _ScrollPanelPresenter::OnTouchCanceled(source, touchInfo);
2125 }
2126
2127 bool
2128 _TableViewPresenter::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
2129 {
2130         if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
2131         {
2132                 return true;
2133         }
2134
2135         _FlickDirection flickDirection = gesture.GetDirection();
2136
2137         if (flickDirection != _FLICK_DIRECTION_RIGHT && flickDirection != _FLICK_DIRECTION_LEFT)
2138         {
2139                 return _ScrollPanelPresenter::OnFlickGestureDetected(gesture);
2140         }
2141
2142         _Control* gestureControl = gesture.GetControl();
2143
2144         if (gestureControl == null)
2145         {
2146                 return _ScrollPanelPresenter::OnFlickGestureDetected(gesture);
2147         }
2148
2149         _TableViewItem* pItem = GetTableViewItemFromControl(*gestureControl);
2150
2151         if (pItem == null)
2152         {
2153                 return _ScrollPanelPresenter::OnFlickGestureDetected(gesture);
2154         }
2155
2156         if (flickDirection == _FLICK_DIRECTION_RIGHT)
2157         {
2158                 // event fire
2159                 if (pItem->GetContextItem() == null && __pTableView->IsSweepEnabled())
2160                 {
2161                         pItem->FireItemSweepEvent(TABLE_VIEW_SWEEP_DIRECTION_RIGHT);
2162                 }
2163                 else
2164                 {
2165                         if (pItem->GetContextItem() != null)
2166                         {
2167                                 AdjustSweptItemPosition();
2168                         }
2169                 }
2170         }
2171         else if (flickDirection == _FLICK_DIRECTION_LEFT)
2172         {
2173                 // event fire
2174                 if (pItem->GetContextItem() == null && __pTableView->IsSweepEnabled())
2175                 {
2176                         pItem->FireItemSweepEvent(TABLE_VIEW_SWEEP_DIRECTION_LEFT);
2177                 }
2178                 else
2179                 {
2180                         if (pItem->GetContextItem() != null)
2181                         {
2182                                 AdjustSweptItemPosition();
2183                         }
2184                 }
2185         }
2186
2187         return true;
2188 }
2189
2190 bool
2191 _TableViewPresenter::OnFlickGestureCanceled(_TouchFlickGestureDetector& gesture)
2192 {
2193         return _ScrollPanelPresenter::OnFlickGestureCanceled(gesture);
2194 }
2195
2196 bool
2197 _TableViewPresenter::OnPreviewFlickGestureDetected(_TouchFlickGestureDetector& gesture)
2198 {
2199         if (__pTableView->IsReorderModeEnabled() && __reorderInfo.itemIndex != -1)
2200         {
2201                 return true;
2202         }
2203
2204         _FlickDirection flickDirection = gesture.GetDirection();
2205         if (flickDirection != _FLICK_DIRECTION_RIGHT && flickDirection != _FLICK_DIRECTION_LEFT)
2206         {
2207                 return _ScrollPanelPresenter::OnFlickGestureDetected(gesture);
2208         }
2209
2210         return true;
2211 }
2212
2213 void
2214 _TableViewPresenter::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
2215 {
2216         _ScrollPanelPresenter::OnTimerExpired(timer);
2217
2218         if (&timer == __pReorderScrollTimer)
2219         {
2220                 float distance = 0.0f;
2221
2222                 if (__reorderInfo.isScrollDirectionUp)
2223                 {
2224                         distance = -REORDER_SCROLL_ANIMATION_DISTANCE;
2225                 }
2226                 else
2227                 {
2228                         distance = REORDER_SCROLL_ANIMATION_DISTANCE;
2229                 }
2230
2231                 DragSelectedItem(distance, false);
2232
2233                 distance = ScrollTo(distance + GetScrollPosition());
2234
2235                 StartReorderScrollTimer();
2236         }
2237         else if (&timer == __pFastScrollTimer)
2238         {
2239                 __isFastScrollTimerEnabled = false;
2240
2241                 _FastScroll* pFastScroll = __pTableView->GetFastScrollBar();
2242
2243                 if (pFastScroll != null && !_ScrollPanelPresenter::IsScrollAnimationRunning())
2244                 {
2245                         pFastScroll->SetScrollVisibility(false);
2246                 }
2247         }
2248 }
2249
2250 result
2251 _TableViewPresenter::StartFastScrollTimer(void)
2252 {
2253         result r = E_SUCCESS;
2254
2255         SysTryReturn(NID_UI_CTRL,
2256                                 __pTableView->GetScrollStyle() == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL,
2257                                 E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView scroll is not TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL");
2258         SysTryReturn(NID_UI_CTRL, __pFastScrollTimer != null, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Timer is not created.");
2259
2260         r = __pFastScrollTimer->Start(FAST_SCROLL_FADE_OUT_DURATION);
2261         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
2262
2263         __isFastScrollTimerEnabled = true;
2264
2265         return r;
2266 }
2267
2268 void
2269 _TableViewPresenter::StopFastScrollTimer(void)
2270 {
2271         result r = E_SUCCESS;
2272
2273         SysTryReturnVoidResult(NID_UI_CTRL, __pFastScrollTimer != null, E_INVALID_STATE, "[E_INVALID_STATE] Timer is invalid.");
2274
2275         r = __pFastScrollTimer->Cancel();
2276         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
2277
2278         __isFastScrollTimerEnabled = false;
2279
2280         return;
2281 }
2282
2283 bool
2284 _TableViewPresenter::IsEmpty(void) const
2285 {
2286         if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE)
2287         {
2288                 if (GetItemCountAt(0) <= 0)
2289                 {
2290                         return true;
2291                 }
2292         }
2293         else
2294         {
2295                 if (GetGroupCount() <= 0 )
2296                 {
2297                         return true;
2298                 }
2299         }
2300
2301         return false;
2302 }
2303
2304 result
2305 _TableViewPresenter::DrawEmptyTableView(void)
2306 {
2307         return E_SUCCESS;
2308 }
2309
2310 bool
2311 _TableViewPresenter::GetFirstItem(TableViewItemTag& firstItem) const
2312 {
2313         if (IsEmpty())
2314         {
2315                 firstItem.groupIndex = -1;
2316                 firstItem.itemIndex = -1;
2317         }
2318         else
2319         {
2320                 firstItem.groupIndex = 0;
2321                 firstItem.itemIndex = -1;
2322         }
2323
2324         return true;
2325 }
2326
2327 bool
2328 _TableViewPresenter::GetLastItem(TableViewItemTag& lastItem) const
2329 {
2330         if (IsEmpty())
2331         {
2332                 lastItem.groupIndex = -1;
2333                 lastItem.itemIndex = -1;
2334         }
2335         else
2336         {
2337                 lastItem.groupIndex = GetGroupCount() - 1;
2338                 lastItem.itemIndex = GetItemCountAt(lastItem.groupIndex) - 1;
2339         }
2340
2341         return true;
2342 }
2343
2344 result
2345 _TableViewPresenter::GetTopDrawnItem(TableViewItemTag& itemPos) const
2346 {
2347         _TableViewItem* pItem = null;
2348         TableViewItemTag lastItemPos = {-1, -1};
2349         float scrollPosition = 0.0f;
2350
2351         scrollPosition = GetScrollPosition();
2352
2353         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
2354         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
2355
2356         do
2357         {
2358                 pItem = static_cast <_TableViewItem*>(__pListModel->LoadItem(itemPos.groupIndex, itemPos.itemIndex));
2359                 if (pItem == null)
2360                 {
2361                         break;
2362                 }
2363
2364                 FloatRectangle itemBounds = pItem->GetBoundsF();
2365
2366                 if (scrollPosition < (itemBounds.y + itemBounds.height))
2367                 {
2368                         return E_SUCCESS;
2369                 }
2370
2371                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
2372                 {
2373                         break;
2374                 }
2375
2376         } while (GetNextItemPosition(itemPos, itemPos) == true);
2377
2378
2379         itemPos.itemIndex = -1;
2380         itemPos.groupIndex = -1;
2381
2382         return E_OUT_OF_RANGE;
2383 }
2384
2385 result
2386 _TableViewPresenter::GetBottomDrawnItem(TableViewItemTag& itemPos) const
2387 {
2388         _TableViewItem* pItem = null;
2389         TableViewItemTag lastItemPos = {-1, -1};
2390
2391         if (IsEmpty())
2392         {
2393                 itemPos.itemIndex = -1;
2394                 itemPos.groupIndex = -1;
2395
2396                 return E_INVALID_STATE;
2397         }
2398
2399         float scrollPosition = GetScrollPosition() + __pTableView->GetBoundsF().height;
2400
2401         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
2402         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
2403
2404         do
2405         {
2406                 static bool onProcessingByProvider = true;
2407                 pItem = static_cast <_TableViewItem*>(__pListModel->LoadItem(itemPos.groupIndex, itemPos.itemIndex));
2408                 onProcessingByProvider = false;
2409                 if (pItem == null)
2410                 {
2411                         break;
2412                 }
2413
2414                 FloatRectangle itemBounds = pItem->GetBoundsF();
2415
2416                 if (itemBounds.y <= scrollPosition && scrollPosition <= (itemBounds.y + itemBounds.height))
2417                 {
2418                         break;
2419                 }
2420
2421                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
2422                 {
2423                         break;
2424                 }
2425
2426         } while (GetNextItemPosition(itemPos, itemPos) == true);
2427
2428         return E_SUCCESS;
2429 }
2430
2431 bool
2432 _TableViewPresenter::GetPreviousItemPosition(const TableViewItemTag& currentItemPos, TableViewItemTag& prevItem) const
2433 {
2434         if (IsEmpty())
2435         {
2436                 return false;
2437         }
2438
2439         // check validation of group index
2440         if ((currentItemPos.groupIndex < 0) || (currentItemPos.groupIndex >= __pListModel->GetAllGroupCount()))
2441         {
2442                 return false;
2443         }
2444
2445         // if the current item is the first item
2446         if (currentItemPos.groupIndex == 0 && currentItemPos.itemIndex == -1)
2447         {
2448                 return false;
2449         }
2450
2451         if (currentItemPos.itemIndex == -1)
2452         {
2453                 if (__pListModel->IsGroupExpanded(currentItemPos.groupIndex - 1) == false)
2454                 {
2455                         prevItem.groupIndex = currentItemPos.groupIndex - 1;
2456                         prevItem.itemIndex = -1;
2457
2458                         return true;
2459                 }
2460         }
2461
2462         if (__pListModel->IsGroupExpanded(currentItemPos.groupIndex) == false)
2463         {
2464                 if (currentItemPos.groupIndex == 0 && currentItemPos.itemIndex == -1)
2465                 {
2466                         return false;
2467                 }
2468
2469                 prevItem.groupIndex = currentItemPos.groupIndex - 1;
2470                 prevItem.itemIndex = __pListModel->GetItemCountInGroup(prevItem.groupIndex) - 1;
2471
2472                 return true;
2473         }
2474
2475         if (currentItemPos.itemIndex == -1) // group title item
2476         {
2477                 prevItem.groupIndex = currentItemPos.groupIndex - 1;
2478                 prevItem.itemIndex = __pListModel->GetItemCountInGroup(prevItem.groupIndex) - 1;
2479         }
2480         else
2481         {
2482                 prevItem.groupIndex = currentItemPos.groupIndex;
2483                 prevItem.itemIndex = currentItemPos.itemIndex - 1;
2484         }
2485
2486         return true;
2487 }
2488
2489 bool
2490 _TableViewPresenter::GetNextItemPosition(const TableViewItemTag& currentItem, TableViewItemTag& nextItem) const
2491 {
2492         if (IsEmpty())
2493         {
2494                 return false;
2495         }
2496
2497         int lastGroup = __pListModel->GetAllGroupCount() - 1;
2498         int lastItemInCurrentGroup = __pListModel->GetItemCountInGroup(currentItem.groupIndex) - 1;
2499         int lastItem = __pListModel->GetItemCountInGroup(lastGroup) - 1;
2500
2501         // check validation of group index
2502         if ((currentItem.groupIndex < 0) || (currentItem.groupIndex > lastGroup))
2503         {
2504                 return false;
2505         }
2506
2507         // if the current item is the last item.
2508         if ((currentItem.groupIndex == lastGroup) && (currentItem.itemIndex == lastItem))
2509         {
2510                 return false;
2511         }
2512
2513         if (__pListModel->IsGroupExpanded(currentItem.groupIndex) == false)
2514         {
2515                 if (currentItem.groupIndex == lastGroup)
2516                 {
2517                         return false;
2518                 }
2519
2520                 nextItem.groupIndex = currentItem.groupIndex + 1;
2521                 nextItem.itemIndex = -1;
2522
2523                 return true;
2524         }
2525
2526         if (lastItemInCurrentGroup == currentItem.itemIndex)
2527         {
2528                 nextItem.groupIndex = currentItem.groupIndex + 1;
2529                 nextItem.itemIndex = -1;
2530         }
2531         else
2532         {
2533                 nextItem.groupIndex = currentItem.groupIndex;
2534                 nextItem.itemIndex = currentItem.itemIndex + 1;
2535         }
2536
2537         return true;
2538 }
2539
2540 float
2541 _TableViewPresenter::GetHeightOfAllItems(void) const
2542 {
2543         int groupCount = GetGroupCount();
2544         float totalHeight = 0;
2545
2546         for (int i = 0; i < groupCount; i++)
2547         {
2548                 TableViewItemTag itemTag = {i, -1};
2549
2550                 totalHeight += GetItemHeight(itemTag);
2551
2552                 if (!IsGroupExpanded(i))
2553                 {
2554                         continue;
2555                 }
2556
2557                 int itemCount = GetItemCountAt(i);
2558
2559                 for (int j = 0; j < itemCount; j++)
2560                 {
2561                         itemTag.itemIndex = j;
2562
2563                         totalHeight += GetItemHeight(itemTag);
2564                 }
2565         }
2566
2567         return totalHeight;
2568 }
2569
2570 void
2571 _TableViewPresenter::SetTableViewItemProviderAdaptor(_TableViewItemProviderAdaptor* pProviderAdaptor)
2572 {
2573         __pListModel->RegisterItemProviderAdaptor(pProviderAdaptor);
2574
2575         __pProviderAdaptor = pProviderAdaptor;
2576
2577         if (__pProviderAdaptor != null)
2578         {
2579                 __pProviderAdaptor->SetTableViewPresenter(this);
2580         }
2581 }
2582
2583 _TableViewItemProviderAdaptor*
2584 _TableViewPresenter::GetTableViewItemProviderAdaptor(void) const
2585 {
2586         return __pProviderAdaptor;
2587 }
2588
2589 result
2590 _TableViewPresenter::ExpandGroup(int groupIndex, bool withAnimation)
2591 {
2592         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()))
2593         {
2594                 return E_OUT_OF_RANGE;
2595         }
2596
2597         if (__modelInitialized == false)
2598         {
2599                 return E_INVALID_STATE;
2600         }
2601
2602         if (IsGroupExpanded(groupIndex) == true)
2603         {
2604                 return E_SUCCESS;
2605         }
2606
2607         TableViewItemTag topTag;
2608         GetTopDrawnItem(topTag);
2609
2610         TableViewItemTag bottomTag;
2611         GetBottomDrawnItem(bottomTag);
2612
2613         float screenPosition = GetScrollPosition();
2614
2615         __pListModel->SetGroupExpandState(groupIndex, true);
2616
2617         ResetSweepItem();
2618
2619         _TableViewItem* pItem = null;
2620         TableViewItemTag itemTag = {-1, -1};
2621         float groupTotalHeight = 0.0f;
2622         int itemCount = GetItemCountAt(groupIndex);
2623         for (int i = 0; i < itemCount; i++)
2624         {
2625                 pItem = static_cast<_TableViewItem*>(__pListModel->GetItemFromTemporaryBuffer(groupIndex, i));
2626                 groupTotalHeight += pItem->GetItemHeight();
2627                 itemTag.groupIndex = groupIndex;
2628                 itemTag.itemIndex = i;
2629                 SetItemHeight(itemTag, pItem->GetItemHeight());
2630         }
2631         AdjustClientAreaBounds(false, groupTotalHeight);
2632
2633         if (groupIndex < topTag.groupIndex || groupIndex > bottomTag.groupIndex)
2634         {
2635                 _TableViewItem* pItem = FindItem(topTag);
2636                 if (pItem != null)
2637                 {
2638                         FloatRectangle itemBounds = pItem->GetBoundsF();
2639                         itemBounds.y = CalculateItemPositionY(topTag.groupIndex, topTag.itemIndex);
2640                         pItem->SetBounds(itemBounds);
2641                         AttachNextItemsToBottom(topTag);
2642                         if (groupIndex < topTag.groupIndex)
2643                         {
2644                                 ScrollTo(screenPosition + groupTotalHeight);
2645                         }
2646                 }
2647
2648                 int firstLoadedGroupIndex = -1;
2649                 int firstLoadedItemIndex = -1;
2650                 __pListModel->GetFirstLoadedItemIndex(firstLoadedGroupIndex, firstLoadedItemIndex);
2651
2652                 for (int j = firstLoadedGroupIndex; j <= topTag.groupIndex; j++)
2653                 {
2654                         int itemCount = GetItemCountAt(j);
2655
2656                         for (int i = -1; i < itemCount; i++)
2657                         {
2658                                 if (i >= topTag.itemIndex && j == topTag.groupIndex)
2659                                 {
2660                                         break;
2661                                 }
2662
2663                                 UnloadItem(j, i);
2664                         }
2665                 }
2666                 return E_SUCCESS;
2667         }
2668
2669         itemTag.groupIndex = groupIndex;
2670         itemTag.itemIndex = -1;
2671
2672         __expandableItemTag.groupIndex = groupIndex;
2673         __expandableItemTag.itemIndex = -1;
2674
2675         if (withAnimation)
2676         {
2677                 _TableViewItem *pItem = FindItem(itemTag);
2678                 if (pItem == null)
2679                 {
2680                         return E_SUCCESS;
2681                 }
2682
2683                 FloatRectangle itemBounds = pItem->GetBoundsF();
2684                 float startPosition = itemBounds.y + itemBounds.height;
2685
2686                 float endPosition = startPosition + groupTotalHeight;
2687                 if (endPosition > screenPosition + __pTableView->GetBoundsF().height)
2688                 {
2689                         endPosition = screenPosition + __pTableView->GetBoundsF().height;
2690                 }
2691
2692                 _VisualElement* pVisualElement = __pTableView->GetVisualElement();
2693                 String animationName(L"EXPAND_GROUP_ANIMATION");
2694
2695                 VisualElementValueAnimation* pAnimation = __pTableView->GetVisualElementValueAnimation(animationName);
2696                 pAnimation->SetStartValue(Variant(startPosition));
2697                 pAnimation->SetEndValue(Variant(endPosition));
2698                 pAnimation->SetDuration(EXPAND_GROUP_ANIMATION_DURATION);
2699                 pVisualElement->AddAnimation(animationName, *pAnimation);
2700         }
2701         else
2702         {
2703                 ExpandGroupAnimationFinished(false);
2704         }
2705
2706         return E_SUCCESS;
2707 }
2708
2709 result
2710 _TableViewPresenter::CollapseGroup(int groupIndex, bool withAnimation)
2711 {
2712         if ((groupIndex < 0) || (groupIndex >= GetGroupCount()))
2713         {
2714                 return E_OUT_OF_RANGE;
2715         }
2716
2717         if (__modelInitialized == false)
2718         {
2719                 return E_INVALID_STATE;
2720         }
2721
2722         if (IsGroupExpanded(groupIndex) == false)
2723         {
2724                 return E_SUCCESS;
2725         }
2726
2727         TableViewItemTag itemTag;
2728         GetTopDrawnItem(itemTag);
2729
2730         TableViewItemTag bottomTag;
2731         GetBottomDrawnItem(bottomTag);
2732         float screenPosition = GetScrollPosition();
2733
2734         __pListModel->SetGroupExpandState(groupIndex, false);
2735
2736         ResetSweepItem();
2737
2738         if (groupIndex < itemTag.groupIndex || groupIndex > bottomTag.groupIndex)
2739         {
2740                 _TableViewItem* pItem = null;
2741                 TableViewItemTag itemTag = {-1, -1};
2742                 float groupTotalHeight = 0;
2743                 int itemCount = GetItemCountAt(groupIndex);
2744                 for (int i = 0; i < itemCount; i++)
2745                 {
2746                         _TableViewItem* pItem = static_cast<_TableViewItem*>(__pListModel->GetItemFromTemporaryBuffer(groupIndex, i));
2747                         groupTotalHeight += pItem->GetItemHeight();
2748                         itemTag.groupIndex = groupIndex;
2749                         itemTag.itemIndex = i;
2750                         SetItemHeight(itemTag, pItem->GetItemHeight());
2751                 }
2752
2753                 TableViewItemTag firstLoadedItemTag;
2754                 __pListModel->GetFirstLoadedItemIndex(firstLoadedItemTag.groupIndex, firstLoadedItemTag.itemIndex);
2755
2756                 if (firstLoadedItemTag.groupIndex == groupIndex)
2757                 {
2758                         SetLoadedItemsVisibleInGroup(groupIndex, false);
2759                         firstLoadedItemTag.groupIndex = groupIndex + 1;
2760                         firstLoadedItemTag.itemIndex = -1;
2761                 }
2762
2763                 for (int i = 0; i < itemCount; i++)
2764                 {
2765                         UnloadItem(groupIndex, i);
2766                 }
2767
2768                 __pListModel->GetFirstLoadedItemIndex(firstLoadedItemTag.groupIndex, firstLoadedItemTag.itemIndex);
2769                 pItem = FindItem(firstLoadedItemTag);
2770                 if (pItem != null)
2771                 {
2772                         FloatRectangle itemBounds = pItem->GetBoundsF();
2773                         itemBounds.y = CalculateItemPositionY(firstLoadedItemTag.groupIndex, firstLoadedItemTag.itemIndex);
2774                         pItem->SetBounds(itemBounds);
2775                         AttachNextItemsToBottom(firstLoadedItemTag);
2776                         if (groupIndex < itemTag.groupIndex)
2777                         {
2778                                 ScrollTo(screenPosition - groupTotalHeight);
2779                         }
2780                 }
2781
2782                 AdjustClientAreaBounds(false, -groupTotalHeight);
2783
2784                 return E_SUCCESS;
2785         }
2786
2787         if (itemTag.groupIndex < groupIndex)
2788         {
2789                 itemTag.groupIndex = groupIndex;
2790                 itemTag.itemIndex = -1;
2791         }
2792         __expandableItemTag.groupIndex = itemTag.groupIndex;
2793         __expandableItemTag.itemIndex = itemTag.itemIndex;
2794
2795         if(__firstDrawnFlag)
2796         {
2797                 withAnimation = false;
2798         }
2799
2800         if (withAnimation)
2801         {
2802                 _TableViewItem *pItem = FindItem(itemTag);
2803                 if (pItem == null)
2804                 {
2805                         return E_SUCCESS;
2806                 }
2807
2808                 FloatRectangle itemBounds = pItem->GetBoundsF();
2809                 float startPosition = 0;
2810                 if (itemTag.itemIndex == -1)
2811                 {
2812                         startPosition = itemBounds.y + itemBounds.height;
2813                 }
2814                 else
2815                 {
2816                         startPosition = GetScrollPosition();
2817                 }
2818
2819                 if (bottomTag.groupIndex > groupIndex)
2820                 {
2821                         bottomTag.groupIndex = groupIndex+1;
2822                         bottomTag.itemIndex = -1;
2823                 }
2824
2825                 pItem = FindItem(bottomTag);
2826                 if (pItem == null)
2827                 {
2828                         return E_SUCCESS;
2829                 }
2830
2831                 itemBounds = pItem->GetBoundsF();
2832                 float endPosition = itemBounds.y;
2833                 if (bottomTag.groupIndex != groupIndex +1)
2834                 {
2835                         endPosition += itemBounds.height;
2836                 }
2837
2838                 if (endPosition > screenPosition + __pTableView->GetBoundsF().height)
2839                 {
2840                         endPosition = screenPosition + __pTableView->GetBoundsF().height;
2841                 }
2842
2843                 _VisualElement* pVisualElement = __pTableView->GetVisualElement();
2844                 String animationName(L"COLLAPSE_GROUP_ANIMATION");
2845
2846                 VisualElementValueAnimation* pAnimation = __pTableView->GetVisualElementValueAnimation(animationName);
2847                 pAnimation->SetStartValue(Variant(endPosition));
2848                 pAnimation->SetEndValue(Variant(startPosition));
2849                 pAnimation->SetDuration(COLLAPSE_GROUP_ANIMATION_DURATION);
2850                 pVisualElement->AddAnimation(animationName, *pAnimation);
2851         }
2852         else
2853         {
2854                 CollapseGroupAnimationFinished(false);
2855         }
2856
2857         return E_SUCCESS;
2858 }
2859
2860 bool
2861 _TableViewPresenter::IsGroupExpanded(int groupIndex) const
2862 {
2863         return __pListModel->IsGroupExpanded(groupIndex);
2864 }
2865
2866 void
2867 _TableViewPresenter::StopExpandCollapseAnimation(void)
2868 {
2869         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
2870         String animationName(L"EXPAND_GROUP_ANIMATION");
2871         VisualElementValueAnimation* pExpandGroupAnimation = dynamic_cast<VisualElementValueAnimation*>(pVisualElement->GetAnimationN(animationName));
2872         if (pExpandGroupAnimation != null)
2873         {
2874                 pVisualElement->RemoveAnimation(animationName);
2875                 delete pExpandGroupAnimation;
2876         }
2877
2878         animationName = L"COLLAPSE_GROUP_ANIMATION";
2879         VisualElementValueAnimation* pCollapseGroupAnimation = dynamic_cast<VisualElementValueAnimation*>(pVisualElement->GetAnimationN(animationName));
2880         if (pCollapseGroupAnimation != null)
2881         {
2882                 pVisualElement->RemoveAnimation(animationName);
2883                 delete pCollapseGroupAnimation;
2884         }
2885 }
2886
2887 int
2888 _TableViewPresenter::LoadAllItemsInGroup(int groupIndex, bool downward)
2889 {
2890         int itemCountInGroup = __pListModel->GetItemCountInGroup(groupIndex);
2891         _TableViewItem* pItem = null;
2892
2893         if (downward)
2894         {
2895                 for (int i = 0; i < itemCountInGroup; i++)
2896                 {
2897                         pItem = LoadItem(groupIndex, i);
2898
2899                         if (pItem == null)
2900                         {
2901                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to load item.");
2902                         }
2903                 }
2904         }
2905         else
2906         {
2907                 for (int i = itemCountInGroup-1; i >= 0; i--)
2908                 {
2909                         pItem = LoadItem(groupIndex, i);
2910
2911                         if (pItem == null)
2912                         {
2913                                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] Unable to load item.");
2914                         }
2915                 }
2916         }
2917
2918         return itemCountInGroup;
2919 }
2920
2921 void
2922 _TableViewPresenter::SetLoadedItemsVisibleInGroup(int groupIndex, bool visible)
2923 {
2924         int itemCountInGroup = __pListModel->GetItemCountInGroup(groupIndex);
2925         _TableViewItem* pItem = null;
2926         TableViewItemTag itemTag;
2927         itemTag.groupIndex = groupIndex;
2928
2929         int firstLoadedGroupIndex = -1;
2930         int firstLoadedItemIndex = -1;
2931         __pListModel->GetFirstLoadedItemIndex(firstLoadedGroupIndex, firstLoadedItemIndex);
2932
2933         if (firstLoadedGroupIndex > groupIndex)
2934         {
2935                 return;
2936         }
2937
2938         int startIndex = 0;
2939         if (groupIndex == firstLoadedGroupIndex && firstLoadedItemIndex > 0)
2940         {
2941                 startIndex = firstLoadedItemIndex;
2942         }
2943
2944         for (int i = startIndex; i < itemCountInGroup; i++)
2945         {
2946                 itemTag.itemIndex = i;
2947                 pItem = FindItem(itemTag);
2948                 if (pItem == null)
2949                 {
2950                         continue;
2951                 }
2952
2953                 pItem->SetVisibleState(visible);
2954         }
2955 }
2956
2957 TableViewItemTag
2958 _TableViewPresenter::LoadItemsToBeVisible(const TableViewItemTag& from)
2959 {
2960         TableViewItemTag bottomTag = from;
2961         TableViewItemTag current;
2962         TableViewItemTag next;
2963
2964         _TableViewItem *pItem = LoadItem(from.groupIndex, from.itemIndex);
2965
2966         if (pItem == null)
2967         {
2968                 return from;
2969         }
2970
2971         FloatRectangle itemBounds = pItem->GetBoundsF();
2972         float viewHeight = __pTableView->GetBoundsF().height;
2973         float scrollPosition = GetScrollPosition();
2974         float itemPosition = itemBounds.y + itemBounds.height - scrollPosition;
2975
2976         while (viewHeight >= itemPosition)
2977         {
2978                 current.groupIndex = bottomTag.groupIndex;
2979                 current.itemIndex = bottomTag.itemIndex;
2980                 if (!GetNextItemPosition(current, next))
2981                 {
2982                         break;
2983                 }
2984
2985                 pItem = LoadItem(next.groupIndex, next.itemIndex);
2986                 bottomTag.groupIndex = next.groupIndex;
2987                 bottomTag.itemIndex = next.itemIndex;
2988                 itemPosition += pItem->GetBoundsF().height;
2989         }
2990
2991         return bottomTag;
2992 }
2993
2994 void
2995 _TableViewPresenter::AttachNextItemsToBottom(const TableViewItemTag& anchor)
2996 {
2997         TableViewItemTag itemTag = anchor;
2998         TableViewItemTag current;
2999         TableViewItemTag next;
3000
3001         _TableViewItem *pItem = FindItem(itemTag);
3002         if (pItem == null)
3003         {
3004                 return;
3005         }
3006
3007         FloatRectangle itemBounds = pItem->GetBoundsF();
3008         float itemPosition = itemBounds.y + itemBounds.height;
3009
3010         current.groupIndex = itemTag.groupIndex;
3011         current.itemIndex = itemTag.itemIndex;
3012         while (GetNextItemPosition(current, next))
3013         {
3014                 pItem = FindItem(next);
3015                 if (pItem == null)
3016                 {
3017                         current = next;
3018                         continue;
3019                 }
3020
3021                 itemBounds = pItem->GetBoundsF();
3022                 itemBounds.y = itemPosition;
3023                 pItem->SetBounds(itemBounds);
3024                 itemPosition += itemBounds.height;
3025                 current = next;
3026         }
3027 }
3028
3029 bool
3030 _TableViewPresenter::IsAnyItemInGroupLoaded(int groupIndex) const
3031 {
3032         int startGroupIndex = 0;
3033         int endGroupIndex = 0;
3034         int index = 0;
3035
3036         __pListModel->GetFirstLoadedItemIndex(startGroupIndex, index);
3037         __pListModel->GetLastLoadedItemIndex(endGroupIndex, index);
3038
3039         return groupIndex >= startGroupIndex && groupIndex <= endGroupIndex;
3040 }
3041
3042 void
3043 _TableViewPresenter::ScrollToHideNonClientArea(TableViewItemTag& bottomTag)
3044 {
3045         _TableViewItem* pItem = FindItem(bottomTag);
3046         if (pItem == null)
3047         {
3048                 return ;
3049         }
3050
3051         FloatRectangle itemBounds = pItem->GetBoundsF();
3052         float viewHeight = __pTableView->GetBoundsF().height;
3053         float scrollPosition = GetScrollPosition();
3054         float itemBottom = itemBounds.y + itemBounds.height - scrollPosition;
3055
3056         if (itemBottom < viewHeight)
3057         {
3058                 scrollPosition -= viewHeight - itemBottom;
3059                 if (scrollPosition < 0)
3060                 {
3061                         scrollPosition = 0;
3062                 }
3063
3064                 SetScrollPosition(scrollPosition, false);
3065         }
3066 }
3067
3068 void
3069 _TableViewPresenter::AdjustClientAreaBounds(bool reset, float dist)
3070 {
3071         float clientHeight = 0;
3072
3073         if (reset)
3074         {
3075                 __itemTotalHeight = GetHeightOfAllItems();
3076                 clientHeight = __itemTotalHeight;
3077         }
3078         else
3079         {
3080                 __itemTotalHeight = __itemTotalHeight + dist;
3081                 clientHeight = __itemTotalHeight;
3082         }
3083
3084         SetClientAreaHeight(clientHeight);
3085 }
3086
3087 void
3088 _TableViewPresenter::SetClientAreaHeight(float height)
3089 {
3090         FloatRectangle screenBounds = __pTableView->GetBoundsF();
3091         FloatRectangle clientBounds = screenBounds;
3092
3093         clientBounds.height = height;
3094
3095         if (clientBounds.height < screenBounds.height)
3096         {
3097             clientBounds.height = screenBounds.height;
3098         }
3099
3100         SetScrollAreaBounds(clientBounds);
3101
3102         _Scroll* pScroll = __pTableView->GetScrollBar();
3103         if (pScroll != null)
3104         {
3105                 pScroll->SetScrollRange(__pTableView->GetBoundsF().height, clientBounds.height);
3106         }
3107 }
3108
3109 void
3110 _TableViewPresenter::UnloadInvisibleItems(void)
3111 {
3112         float scrollPosition = GetScrollPosition();
3113         TableViewItemTag itemTag;
3114         __pListModel->GetFirstLoadedItemIndex(itemTag.groupIndex, itemTag.itemIndex);
3115
3116         _TableViewItem* pItem = null;
3117         FloatRectangle bounds;
3118
3119         while ((pItem = FindItem(itemTag)) != null)
3120         {
3121                 if (itemTag.itemIndex != -1)
3122                 {
3123                         bounds = pItem->GetBoundsF();
3124                         if (bounds.y + bounds.height < scrollPosition)
3125                         {
3126                                 UnloadItem(itemTag);
3127                         }
3128                         else
3129                         {
3130                                 break;
3131                         }
3132                 }
3133                 __pListModel->GetFirstLoadedItemIndex(itemTag.groupIndex, itemTag.itemIndex);
3134         }
3135
3136         __pListModel->GetLastLoadedItemIndex(itemTag.groupIndex, itemTag.itemIndex);
3137         scrollPosition += __pTableView->GetBoundsF().height;
3138
3139         while ((pItem = FindItem(itemTag)) != null)
3140         {
3141                 if (itemTag.itemIndex != -1)
3142                 {
3143                         bounds = pItem->GetBoundsF();
3144                         if (bounds.y > scrollPosition)
3145                         {
3146                                 UnloadItem(itemTag);
3147                         }
3148                         else
3149                         {
3150                                 break;
3151                         }
3152                 }
3153                 __pListModel->GetLastLoadedItemIndex(itemTag.groupIndex, itemTag.itemIndex);
3154         }
3155
3156 }
3157
3158 float
3159 _TableViewPresenter::ScrollToInternal(float newPosition)
3160 {
3161         if (!IsScrollEnabled())
3162         {
3163                 return 0.0f;
3164         }
3165
3166         if (IsScrollAnimationRunning())
3167         {
3168                 if (__scrollPositionOnFlickStarted > newPosition)
3169                 {
3170                         newPosition += GetScrollAreaBounds().height - __scrollHeightOnFlickStarted;
3171                 }
3172         }
3173
3174         float currentScrollPosition = GetScrollPosition();
3175         float currentscrollAreaHeight = GetScrollAreaBounds().height;
3176         float newScrollPosition = (newPosition < 0) ? 0 : (newPosition > currentscrollAreaHeight ? currentscrollAreaHeight : newPosition);
3177         float newScrollAreaHeight = 0;
3178
3179         if (IsModelUpdating())
3180         {
3181                 currentScrollPosition = GetScrollPositionInternal();
3182         }
3183
3184         if (!__lockLoadItemWithScroll)
3185         {
3186                 LoadItemWithScrollPosition(currentScrollPosition, newScrollPosition);
3187
3188                 if (currentScrollPosition > newScrollPosition)
3189                 {
3190                         newScrollAreaHeight = GetScrollAreaBounds().height;
3191                         newScrollPosition += newScrollAreaHeight - currentscrollAreaHeight;
3192                 }
3193         }
3194
3195         //scroll effect for _ScrollPanel
3196         if (newPosition < 0.0f || newPosition > currentscrollAreaHeight - __pTableView->GetBoundsF().height)
3197         {
3198                 newScrollPosition = newPosition;
3199         }
3200
3201         return _ScrollPanelPresenter::ScrollToInternal(newScrollPosition);
3202 }
3203
3204 void
3205 _TableViewPresenter::LoadItemWithScrollPosition(float previousScrollPosition, float currentScrollPos)
3206 {
3207         ClearLastResult();
3208
3209         TableViewItemTag lastLoadedItemPos = {-1, -1};
3210         TableViewItemTag firstLoadedItemPos = {-1, -1};
3211         __pListModel->GetLastLoadedItemIndex(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex);
3212         __pListModel->GetFirstLoadedItemIndex(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex);
3213
3214         if (currentScrollPos > previousScrollPosition && lastLoadedItemPos.groupIndex != -1)
3215         {
3216                 _TableViewItem* pLastItem = FindItem(lastLoadedItemPos);
3217                 if (pLastItem == null)
3218                 {
3219                         return;
3220                 }
3221
3222                 while (pLastItem && (pLastItem->GetBoundsF().y <= currentScrollPos + __pTableView->GetBoundsF().height))
3223                 {
3224                         TableViewItemTag nextItemPos = {-1, -1};
3225                         if (GetNextItemPosition(lastLoadedItemPos, nextItemPos))
3226                         {
3227                                 if (nextItemPos.itemIndex == -1)
3228                                 {
3229                                         LoadItem(nextItemPos.groupIndex, nextItemPos.itemIndex);
3230
3231                                         if (!GetNextItemPosition(nextItemPos, nextItemPos))
3232                                         {
3233                                                 break;
3234                                         }
3235                                 }
3236
3237                                 if (nextItemPos.groupIndex != -1)
3238                                 {
3239                                         _TableViewItem* pItem = LoadItem(nextItemPos.groupIndex, nextItemPos.itemIndex);
3240                                         if (pItem == null)
3241                                         {
3242                                                 return;
3243                                         }
3244
3245                                         FloatRectangle scrollAreaBounds = GetScrollAreaBounds();
3246                                         if (pItem->GetBoundsF().y + pItem->GetBoundsF().height >= scrollAreaBounds.height)
3247                                         {
3248                                                 AdjustClientAreaBounds(true);
3249                                         }
3250                                 }
3251                         }
3252                         else
3253                         {
3254                                 break;
3255                         }
3256
3257                         lastLoadedItemPos = nextItemPos;
3258                         pLastItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex));
3259                 }
3260         }
3261
3262         if (currentScrollPos < previousScrollPosition && firstLoadedItemPos.groupIndex != -1)
3263         {
3264                 _TableViewItem* pFirstItem = FindItem(firstLoadedItemPos);
3265                 if (pFirstItem == null)
3266                 {
3267                         return;
3268                 }
3269
3270                 float scrollOffsetFromBottom = GetScrollAreaBounds().height - currentScrollPos;
3271
3272                 while (pFirstItem)
3273                 {
3274                         FloatRectangle scrollAreaBounds = GetScrollAreaBounds();
3275                         float itemOffsetFromBottom = scrollAreaBounds.height - (pFirstItem->GetBoundsF().y + pFirstItem->GetBoundsF().height);
3276
3277                         if (scrollOffsetFromBottom < itemOffsetFromBottom)
3278                         {
3279                                 break;
3280                         }
3281
3282                         TableViewItemTag prevItemPos = {-1, -1};
3283                         if (GetPreviousItemPosition(firstLoadedItemPos, prevItemPos))
3284                         {
3285                                 if (prevItemPos.itemIndex == -1)
3286                                 {
3287                                         LoadItem(prevItemPos.groupIndex, prevItemPos.itemIndex);
3288
3289                                         if (!GetPreviousItemPosition(prevItemPos, prevItemPos))
3290                                         {
3291                                                 break;
3292                                         }
3293                                 }
3294
3295                                 if (prevItemPos.groupIndex != -1)
3296                                 {
3297                                         LoadItem(prevItemPos.groupIndex, prevItemPos.itemIndex);
3298                                 }
3299                         }
3300                         else
3301                         {
3302                                 break;
3303                         }
3304
3305                         firstLoadedItemPos = prevItemPos;
3306                         pFirstItem = FindItem(firstLoadedItemPos);
3307                 }
3308         }
3309 }
3310
3311 void
3312 _TableViewPresenter::ScrollToItem(int groupIndex, int itemIndex, TableViewScrollItemAlignment itemAlignment, float shiftingDistance)
3313 {
3314         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
3315         String animationName(L"EXPAND_GROUP_ANIMATION");
3316         VisualElementValueAnimation* pExpandGroupAnimation = dynamic_cast<VisualElementValueAnimation*>(pVisualElement->GetAnimationN(animationName));
3317         if (pExpandGroupAnimation != null)
3318         {
3319                 __scrollToItemTag.groupIndex = groupIndex;
3320                 __scrollToItemTag.itemIndex = itemIndex;
3321                 __scrollToItemTag.itemAlignment = itemAlignment;
3322                 __scrollToItemTag.shiftingDistance = shiftingDistance;
3323                 __scrollToItemTag.startedAnimation = true;
3324                 delete pExpandGroupAnimation;
3325                 return;
3326         }
3327
3328         animationName = L"COLLAPSE_GROUP_ANIMATION";
3329         VisualElementValueAnimation* pCollapseGroupAnimation = dynamic_cast<VisualElementValueAnimation*>(pVisualElement->GetAnimationN(animationName));
3330         if (pCollapseGroupAnimation != null)
3331         {
3332                 __scrollToItemTag.groupIndex = groupIndex;
3333                 __scrollToItemTag.itemIndex = itemIndex;
3334                 __scrollToItemTag.itemAlignment = itemAlignment;
3335                 __scrollToItemTag.shiftingDistance = shiftingDistance;
3336                 __scrollToItemTag.startedAnimation = true;
3337                 delete pCollapseGroupAnimation;
3338                 return;
3339         }
3340
3341         if (IsGroupExpanded(groupIndex) == false)
3342         {
3343                 itemIndex = -1;
3344         }
3345
3346         TableViewItemTag itemPos = {groupIndex, itemIndex};
3347
3348         StopAllItemAnimation();
3349         ResetSweepItem();
3350
3351         if (!__pListModel->IsLoadedItem(groupIndex, itemIndex))
3352         {
3353                 ResetItemLayout(itemPos, shiftingDistance);
3354         }
3355
3356         _TableViewItem* pItem = FindItem(itemPos);
3357
3358         if (pItem == null)
3359         {
3360                 return;
3361         }
3362
3363         float scrollPosition = pItem->GetBoundsF().y;
3364
3365         scrollPosition = scrollPosition + shiftingDistance;
3366
3367         SetScrollPosition(scrollPosition, false);
3368
3369         if (itemAlignment == TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM)
3370         {
3371                 SetScrollPosition(scrollPosition - (__pTableView->GetBoundsF().height - pItem->GetBoundsF().height), false);
3372         }
3373 }
3374
3375 result
3376 _TableViewPresenter::ScrollByPixel(float scrollDistance)
3377 {
3378         FadeInScrollBar();
3379         result r = GetLastResult();
3380         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3381
3382         ResetSweepItem();
3383
3384         ScrollTo(scrollDistance + GetScrollPosition());
3385         r = GetLastResult();
3386         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3387
3388         RollbackBouncing(true);
3389         r = GetLastResult();
3390         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3391
3392         FadeOutScrollBar();
3393         r = GetLastResult();
3394         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
3395
3396         return r;
3397 }
3398
3399 void
3400 _TableViewPresenter::SetScrollEnabled(bool enable)
3401 {
3402         __scrolling = enable;
3403 }
3404
3405 bool
3406 _TableViewPresenter::IsScrollEnabled(void) const
3407 {
3408         return __scrolling;
3409 }
3410
3411 bool
3412 _TableViewPresenter::GetFirstDrawnFlag(void) const
3413 {
3414         return __firstDrawnFlag;
3415 }
3416
3417 int
3418 _TableViewPresenter::GetMaxItemCachingSize(void) const
3419 {
3420         return __pListModel->GetMaxCachingSize();
3421 }
3422
3423 void
3424 _TableViewPresenter::ResetSweepItem(void)
3425 {
3426         if (__sweptItemTag.groupIndex != -1 && __sweptItemTag.itemIndex != -1)
3427         {
3428                 ResetSweptItem();
3429         }
3430 }
3431
3432 void
3433 _TableViewPresenter::GetSweepItemIndex(int& groupIndex, int& itemIndex) const
3434 {
3435         groupIndex = __sweptItemTag.groupIndex;
3436         itemIndex = __sweptItemTag.itemIndex;
3437 }
3438
3439 int
3440 _TableViewPresenter::GetPressedItemCount(void)
3441 {
3442         _TableViewItem* pItem = null;
3443         TableViewItemTag itemPos = {-1, -1};
3444         TableViewItemTag lastItemPos = {-1, -1};
3445         int count = 0;
3446
3447         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
3448         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
3449
3450         do
3451         {
3452                 pItem = static_cast <_TableViewItem*>(__pListModel->LoadItem(itemPos.groupIndex, itemPos.itemIndex));
3453                 if (pItem == null)
3454                 {
3455                         continue;
3456                 }
3457
3458                 if (pItem->GetDrawingStatus() == TABLE_VIEW_ITEM_DRAWING_STATUS_PRESSED)
3459                 {
3460                         count++;
3461                 }
3462
3463                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
3464                 {
3465                         break;
3466                 }
3467         } while (GetNextItemPosition(itemPos, itemPos) == true);
3468
3469         return count;
3470 }
3471
3472 void
3473 _TableViewPresenter::BlockAnimationCallback(bool blocked)
3474 {
3475         __isAnimationCallbackBlocked = blocked;
3476 }
3477
3478 float
3479 _TableViewPresenter::CalculateItemPositionY(int groupIndex, int itemIndex)
3480 {
3481         TableViewItemTag itemPos = {-1, -1};
3482         TableViewItemTag currentItemPos = {-1, -1};
3483         float positionY = 0.0f;
3484
3485         GetFirstItem(itemPos);
3486
3487         if (itemPos.groupIndex == -1 && itemPos.itemIndex == -1)
3488         {
3489                 return 0;
3490         }
3491
3492         if (__pProviderAdaptor == null)
3493         {
3494                 return -1;
3495         }
3496
3497         positionY = GetTopMargin();
3498
3499         while ((itemPos.groupIndex < groupIndex) || ((itemPos.groupIndex == groupIndex) && (itemPos.itemIndex < itemIndex)))
3500         {
3501                 currentItemPos = itemPos;
3502
3503                 float itemHeight = GetItemHeight(itemPos);
3504
3505                 positionY += itemHeight;
3506
3507                 if (!GetNextItemPosition(currentItemPos, itemPos))
3508                 {
3509                         break;
3510                 }
3511         }
3512
3513         return positionY;
3514 }
3515
3516 void
3517 _TableViewPresenter::FadeInScrollBar(void)
3518 {
3519         TableViewScrollBarStyle scrollStyle = __pTableView->GetScrollStyle();
3520
3521         if (scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_NONE)
3522         {
3523                 _Scroll* pScroll = __pTableView->GetScrollBar();
3524
3525                 if (pScroll != null)
3526                 {
3527                         pScroll->SetVisibleState(false);
3528                 }
3529         }
3530         else if (scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL)
3531         {
3532                 _Scroll* pScroll = __pTableView->GetScrollBar();
3533
3534                 if (pScroll != null)
3535                 {
3536                         pScroll->SetVisibleState(false);
3537                 }
3538
3539                 _FastScroll* pFastScroll = __pTableView->GetFastScrollBar();
3540
3541                 if (pFastScroll != null)
3542                 {
3543                         pFastScroll->SetScrollVisibility(true);
3544                 }
3545         }
3546         else
3547         {
3548                 _ScrollPanelPresenter::FadeInScrollBar();
3549         }
3550 }
3551
3552 void
3553 _TableViewPresenter::FadeOutScrollBar(void)
3554 {
3555         TableViewScrollBarStyle scrollStyle = __pTableView->GetScrollStyle();
3556         if (scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_FAST_SCROLL)
3557         {
3558                 StopFastScrollTimer();
3559
3560                 if (!_ScrollPanelPresenter::IsScrollAnimationRunning())
3561                 {
3562                         StartFastScrollTimer();
3563                 }
3564         }
3565         else if (scrollStyle == TABLE_VIEW_SCROLL_BAR_STYLE_FIXED)
3566         {
3567                 _Scroll* pScroll = __pTableView->GetScrollBar();
3568
3569                 if (pScroll != null)
3570                 {
3571                         pScroll->SetScrollingEffectVisibility(false);
3572                 }
3573         }
3574         else
3575         {
3576                 _ScrollPanelPresenter::FadeOutScrollBar();
3577         }
3578 }
3579
3580 void
3581 _TableViewPresenter::SweepItem(float x)
3582 {
3583         if (!_FloatCompare(x,  0.0f))
3584         {
3585                 _TableViewItem* pItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex));
3586
3587                 if (pItem != null)
3588                 {
3589                         _TableViewItem* pContextItem  = pItem->GetContextItem();
3590
3591                         if (pContextItem != null)
3592                         {
3593                                 float currentPosition = __sweptItemPosition.x + x;
3594                                 float sweepDistance = __pTableView->GetBoundsF().width;
3595                                 float sweepMargin = 0.0f;
3596                                 float contextItemWidth = pItem->GetBoundsF().width;
3597                                 bool activated = pItem->IsContextItemActivated();
3598                                 bool needToFireEvent = false;
3599
3600                                 GET_SHAPE_CONFIG(TABLEVIEW::CONTEXTITEM_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sweepMargin);
3601
3602                                 if (currentPosition >= (sweepDistance - sweepMargin))
3603                                 {
3604                                         currentPosition = (sweepDistance - sweepMargin);
3605                                         needToFireEvent = !activated;
3606                                 }
3607                                 else if (currentPosition <= __leftMargin)
3608                                 {
3609                                         currentPosition = __leftMargin;
3610                                         needToFireEvent = activated;
3611                                 }
3612
3613                                 __sweptItemPosition.x = currentPosition;
3614
3615                                 // Set TableViewItem bounds
3616                                 pItem->SetPosition(__sweptItemPosition);
3617
3618                                 // Set TableViewContextItem bounds
3619                                 if (!__pTableView->IsAncestorOf(*pContextItem))
3620                                 {
3621                                         pContextItem->SetDrawingProperty(__pItemDrawingProperty);
3622                                         __pTableView->InsertChildToBottom(*pContextItem);
3623                                         __pTableView->UpdateLayout();
3624                                 }
3625
3626                                 contextItemWidth = ((contextItemWidth >  (__sweptItemPosition.x - __leftMargin)) ? (__sweptItemPosition.x - __leftMargin) : contextItemWidth);
3627
3628                                 pContextItem->ExposeContextItem(FloatRectangle(__leftMargin, __sweptItemPosition.y, contextItemWidth, pContextItem->GetSizeF().height), pItem->GetSize().width);
3629
3630                                 if (needToFireEvent)
3631                                 {
3632                                         PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_SLIDER_SWEEP, pItem);
3633
3634                                         activated = !activated; // ContextItem Activation State Changed
3635
3636                                         __pTableView->FireTableViewContextItemActivationEvent(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex, pContextItem, activated);
3637                                         pItem->SetContextItemActivation(activated);
3638
3639                                         if (!activated)
3640                                         {
3641                                                 ResetSweepItem();
3642                                         }
3643                                 }
3644                         }
3645                 }
3646         }
3647 }
3648
3649 void
3650 _TableViewPresenter::ResetSweptItem(void)
3651 {
3652         _TableViewItem* pItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex));
3653
3654         if (pItem != null)
3655         {
3656                 _VisualElement* pVisualElement = __pTableView->GetVisualElement();
3657
3658                 if (pVisualElement != null)
3659                 {
3660                         String animationName(L"SWEEP_ITEM_ANIMATION");
3661                         VisualElementAnimation* pContextItemAnimation = pVisualElement->GetAnimationN(animationName);
3662                         if (pContextItemAnimation != null)
3663                         {
3664                                 pVisualElement->RemoveAnimation(animationName);
3665
3666                                 delete pContextItemAnimation;
3667                         }
3668                 }
3669
3670                 pItem->SetPosition(FloatPoint(__leftMargin, __sweptItemPosition.y));
3671
3672                 _TableViewItem* pContextItem = pItem->GetContextItem();
3673                 if (pContextItem != null)
3674                 {
3675                         if (__pTableView->IsAncestorOf(*pContextItem))
3676                         {
3677                                 __pTableView->DetachChild(*pContextItem);
3678                         }
3679
3680                         if (pItem->IsContextItemActivated())
3681                         {
3682                                 __pTableView->FireTableViewContextItemActivationEvent(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex, pContextItem, false);
3683                                 pItem->SetContextItemActivation(false);
3684                         }
3685                 }
3686         }
3687
3688         __sweptItemTag.itemIndex = -1;
3689         __sweptItemTag.groupIndex = -1;
3690
3691         __sweptItemPosition.SetPosition(-1.0f, -1.0f);
3692         __sweepOccured = false;
3693 }
3694
3695 void
3696 _TableViewPresenter::AdjustSweptItemPosition(bool withAnimation)
3697 {
3698         float itemWidth = __pTableView->GetBoundsF().width;
3699         float sweepDistance = 0.0f;
3700
3701         if (_FloatCompare(__sweptItemPosition.x, __leftMargin))
3702         {
3703                 ResetSweptItem();
3704                 return;
3705         }
3706
3707         float sweepMargin = 0.0f;
3708         GET_SHAPE_CONFIG(TABLEVIEW::CONTEXTITEM_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sweepMargin);
3709
3710         if (__sweptItemPosition.x > (itemWidth - sweepMargin) / 2)
3711         {
3712                 sweepDistance = itemWidth - __sweptItemPosition.x;
3713         }
3714         else
3715         {
3716                 sweepDistance = -__sweptItemPosition.x;
3717         }
3718
3719         if (!withAnimation)
3720         {
3721                 SweepItem(sweepDistance);
3722         }
3723         else
3724         {
3725                 _TableViewItem* pItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(__sweptItemTag.groupIndex, __sweptItemTag.itemIndex));
3726
3727                 if (pItem != null)
3728                 {
3729                         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
3730
3731                         if (pVisualElement != null)
3732                         {
3733                                 String animationName(L"SWEEP_ITEM_ANIMATION");
3734                                 float startValue = __sweptItemPosition.x;
3735                                 float endValue = 0.0f;
3736
3737                                 if(sweepDistance > 0)
3738                                 {
3739                                         endValue = itemWidth - sweepMargin;
3740                                 }
3741                                 else
3742                                 {
3743                                         if(_FloatCompare(itemWidth, __sweptItemPosition.x))
3744                                         {
3745                                                 endValue = itemWidth - sweepMargin;
3746                                         }
3747                                         else
3748                                         {
3749                                                 endValue = __leftMargin;
3750                                         }
3751                                 }
3752
3753                                 pVisualElement->SetImplicitAnimationEnabled(false);
3754
3755                                 VisualElementValueAnimation* pAnimation = __pTableView->GetVisualElementValueAnimation(animationName);
3756                                 pAnimation->SetStartValue(Variant(startValue));
3757                                 pAnimation->SetEndValue(Variant(endValue));
3758                                 pAnimation->SetDuration(SWEEP_ITEM_ANIMATION_DURATION);
3759                                 pVisualElement->AddAnimation(animationName, *pAnimation);
3760                         }
3761                 }
3762         }
3763 }
3764
3765 void
3766 _TableViewPresenter::AdjustLoadedItemWidth(void)
3767 {
3768         TableViewItemTag lastLoadedItemPos = {-1, -1};
3769         TableViewItemTag firstLoadedItemPos = {-1, -1};
3770         __pListModel->GetLastLoadedItemIndex(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex);
3771         __pListModel->GetFirstLoadedItemIndex(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex);
3772
3773         do
3774         {
3775                 _TableViewItem* pItem = FindItem(firstLoadedItemPos);
3776
3777                 if (pItem == null)
3778                 {
3779                         continue;
3780                 }
3781
3782                 FloatDimension itemSize = pItem->GetSizeF();
3783                 itemSize.width = __pProviderAdaptor->GetListWidth();
3784
3785                 pItem->SetItemChanged(true);
3786                 pItem->SetSize(itemSize);
3787
3788                 if ((firstLoadedItemPos.itemIndex == lastLoadedItemPos.itemIndex) && (firstLoadedItemPos.groupIndex == lastLoadedItemPos.groupIndex))
3789                 {
3790                         break;
3791                 }
3792         } while (GetNextItemPosition(firstLoadedItemPos, firstLoadedItemPos));
3793 }
3794
3795 result
3796 _TableViewPresenter::OpenContextItem(int groupIndex, int itemIndex)
3797 {
3798         SysTryReturn(NID_UI_CTRL, groupIndex >= 0 && groupIndex < GetGroupCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid group index.");
3799
3800         TableViewItemTag itemTag = {groupIndex, itemIndex};
3801         _TableViewItem* pItem = FindItem(itemTag);
3802
3803         SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Item was not loaded.");
3804         SysTryReturn(NID_UI_CTRL, pItem->GetContextItem() != null, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] ContextItem is not set to the item.");
3805         SysTryReturn(NID_UI_CTRL,__sweptItemTag.groupIndex != groupIndex || __sweptItemTag.itemIndex != itemIndex, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] ContextItem already opened.");
3806
3807         ResetSweepItem();
3808
3809         __sweptItemTag.groupIndex = groupIndex;
3810         __sweptItemTag.itemIndex = itemIndex;
3811         __sweptItemPosition = pItem->GetPositionF();
3812
3813         pItem->AdjustContextItemBounds();
3814         __sweepOccured = true;
3815
3816         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
3817
3818         if (pVisualElement != null)
3819         {
3820                 float itemWidth = __pTableView->GetBoundsF().width;
3821                 float sweepMargin = 0.0f;
3822                 GET_SHAPE_CONFIG(TABLEVIEW::CONTEXTITEM_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sweepMargin);
3823
3824                 String animationName(L"SWEEP_ITEM_ANIMATION");
3825                 float startValue = __sweptItemPosition.x;
3826                 float endValue = itemWidth - sweepMargin;
3827
3828                 pVisualElement->SetImplicitAnimationEnabled(false);
3829
3830                 VisualElementValueAnimation* pAnimation = __pTableView->GetVisualElementValueAnimation(animationName);
3831                 pAnimation->SetStartValue(Variant(startValue));
3832                 pAnimation->SetEndValue(Variant(endValue));
3833                 pAnimation->SetDuration(SWEEP_ITEM_ANIMATION_DURATION);
3834                 pVisualElement->AddAnimation(animationName, *pAnimation);
3835         }
3836
3837         return E_SUCCESS;
3838 }
3839
3840 result
3841 _TableViewPresenter::CloseContextItem(int groupIndex, int itemIndex)
3842 {
3843         SysTryReturn(NID_UI_CTRL, groupIndex >= 0 && groupIndex < GetGroupCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid group index.");
3844
3845         TableViewItemTag itemTag = {groupIndex, itemIndex};
3846         _TableViewItem* pItem = FindItem(itemTag);
3847
3848         SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Item was not loaded.");
3849         SysTryReturn(NID_UI_CTRL, pItem->GetContextItem() != null, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] ContextItem is not set to the item.");
3850         SysTryReturn(NID_UI_CTRL, __sweptItemTag.groupIndex == groupIndex && __sweptItemTag.itemIndex == itemIndex, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] ContextItem was not opened.");
3851
3852         ResetSweepItem();
3853
3854         return E_SUCCESS;
3855 }
3856
3857 bool
3858 _TableViewPresenter::IsContextItemOpened(int groupIndex, int itemIndex) const
3859 {
3860         SysTryReturn(NID_UI_CTRL, groupIndex >= 0 && groupIndex < GetGroupCount(), false, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid group index.");
3861
3862         if ( __sweptItemTag.groupIndex == -1 && __sweptItemTag.itemIndex == -1)
3863         {
3864                 return false;
3865         }
3866
3867         if (__sweptItemTag.groupIndex == groupIndex && __sweptItemTag.itemIndex == itemIndex)
3868         {
3869                 return true;
3870         }
3871
3872         return false;
3873 }
3874
3875 result
3876 _TableViewPresenter::SetSectionHeaderTextHorizontalAlignment(int sectionIndex, HorizontalAlignment alignment)
3877 {
3878         SysTryReturn(NID_UI_CTRL, __pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView is TABLE_VIEW_STYLE_SECTION");
3879         SysTryReturn(NID_UI_CTRL, HasSectionHeader(sectionIndex), E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Header is not set to the section.");
3880         SysTryReturn(NID_UI_CTRL, sectionIndex >= 0 && sectionIndex < GetGroupCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid section index.");
3881
3882         _TableViewSectionStringAlignment newAlignment;
3883
3884         __sectionAlignmentList.GetAt(sectionIndex, newAlignment);
3885         newAlignment.headerHorizontalAlignment = alignment;
3886         __sectionAlignmentList.SetAt(newAlignment, sectionIndex);
3887
3888         TableViewItemTag itemTag = {sectionIndex, -1};
3889         _TableViewItem* pItem = FindItem(itemTag);
3890
3891         if (pItem != null)
3892         {
3893                 pItem->SetSectionHeaderFooterAlignment(alignment);
3894         }
3895
3896         return E_SUCCESS;
3897 }
3898
3899 HorizontalAlignment
3900 _TableViewPresenter::GetSectionHeaderTextHorizontalAlignment(int sectionIndex) const
3901 {
3902         SysTryReturn(NID_UI_CTRL, __pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION, ALIGNMENT_LEFT, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView is TABLE_VIEW_STYLE_SECTION");
3903         SysTryReturn(NID_UI_CTRL, HasSectionHeader(sectionIndex), ALIGNMENT_LEFT, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Header is not set to the section.");
3904         SysTryReturn(NID_UI_CTRL, sectionIndex >= 0 && sectionIndex < GetGroupCount(), ALIGNMENT_LEFT, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid section index.");
3905
3906         _TableViewSectionStringAlignment alignment;
3907         __sectionAlignmentList.GetAt(sectionIndex, alignment);
3908
3909         return alignment.headerHorizontalAlignment;
3910 }
3911
3912 result
3913 _TableViewPresenter::SetSectionFooterTextHorizontalAlignment(int sectionIndex, HorizontalAlignment alignment)
3914 {
3915         SysTryReturn(NID_UI_CTRL, __pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView is TABLE_VIEW_STYLE_SECTION");
3916         SysTryReturn(NID_UI_CTRL, HasSectionFooter(sectionIndex), E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Footer is not set to the section.");
3917         SysTryReturn(NID_UI_CTRL, sectionIndex >= 0 && sectionIndex < GetGroupCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid section index.");
3918
3919         _TableViewSectionStringAlignment newAlignment;
3920
3921         __sectionAlignmentList.GetAt(sectionIndex, newAlignment);
3922         newAlignment.footerHorizontalAlignment= alignment;
3923         __sectionAlignmentList.SetAt(newAlignment, sectionIndex);
3924
3925         TableViewItemTag itemTag = {sectionIndex, GetItemCountAt(sectionIndex) - 1};
3926         _TableViewItem* pItem = FindItem(itemTag);
3927
3928         if (pItem != null)
3929         {
3930                 pItem->SetSectionHeaderFooterAlignment(alignment);
3931         }
3932
3933         return E_SUCCESS;
3934 }
3935
3936 HorizontalAlignment
3937 _TableViewPresenter::GetSectionFooterTextHorizontalAlignment(int sectionIndex) const
3938 {
3939         SysTryReturn(NID_UI_CTRL, __pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION, ALIGNMENT_RIGHT, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView is TABLE_VIEW_STYLE_SECTION");
3940         SysTryReturn(NID_UI_CTRL, HasSectionFooter(sectionIndex), ALIGNMENT_RIGHT, E_INVALID_OPERATION, "[E_INVALID_OPERATION] Footer is not set to the section.");
3941         SysTryReturn(NID_UI_CTRL, sectionIndex >= 0 && sectionIndex < GetGroupCount(), ALIGNMENT_RIGHT, E_OUT_OF_RANGE, "[E_INVALID_ARG] Invalid section index.");
3942
3943         _TableViewSectionStringAlignment alignment;
3944         __sectionAlignmentList.GetAt(sectionIndex, alignment);
3945
3946         return alignment.footerHorizontalAlignment;
3947 }
3948
3949 bool
3950 _TableViewPresenter::IsValidDrawnItem(int groupIndex, int itemIndex)
3951 {
3952         TableViewItemTag currentItemTag = {groupIndex, itemIndex};
3953         TableViewItemTag itemTag = {-1, -1};
3954
3955         if (GetNextItemPosition(currentItemTag, itemTag))
3956         {
3957                 if (__pListModel->IsLoadedItem(itemTag.groupIndex, itemTag.itemIndex))
3958                 {
3959                         return true;
3960                 }
3961         }
3962
3963         if (GetPreviousItemPosition(currentItemTag, itemTag))
3964         {
3965                 if (__pListModel->IsLoadedItem(itemTag.groupIndex, itemTag.itemIndex))
3966                 {
3967                         return true;
3968                 }
3969         }
3970
3971         return false;
3972 }
3973
3974 void
3975 _TableViewPresenter::StopAllItemAnimation(void)
3976 {
3977         TableViewItemTag currentItemTag = {-1, -1};
3978         TableViewItemTag lastItemTag = {-1, -1};
3979
3980         __pListModel->GetFirstLoadedItemIndex(currentItemTag.groupIndex, currentItemTag.itemIndex);
3981         __pListModel->GetLastLoadedItemIndex(lastItemTag.groupIndex, lastItemTag.itemIndex);
3982
3983         do
3984         {
3985                 _TableViewItem *pItem = FindItem(currentItemTag);
3986                 if (pItem != null)
3987                 {
3988                         pItem->StopAllAnimation();
3989                 }
3990
3991                 if (currentItemTag.groupIndex == lastItemTag.groupIndex && currentItemTag.itemIndex == lastItemTag.itemIndex)
3992                 {
3993                         break;
3994                 }
3995
3996         }while (GetNextItemPosition(currentItemTag, currentItemTag));
3997 }
3998
3999 bool
4000 _TableViewPresenter::GetTableViewItemPosition(_TableViewItem& item, TableViewItemTag& itemTag)
4001 {
4002         TableViewItemTag lastLoadedItemPos = {-1, -1};
4003         TableViewItemTag firstLoadedItemPos = {-1, -1};
4004         __pListModel->GetLastLoadedItemIndex(lastLoadedItemPos.groupIndex, lastLoadedItemPos.itemIndex);
4005         __pListModel->GetFirstLoadedItemIndex(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex);
4006
4007         _TableViewItem* pCurrentItem = null;
4008         do
4009         {
4010                 pCurrentItem = static_cast<_TableViewItem*>(__pListModel->LoadItem(firstLoadedItemPos.groupIndex, firstLoadedItemPos.itemIndex));
4011                 if (pCurrentItem == &item)
4012                 {
4013                         itemTag.groupIndex = firstLoadedItemPos.groupIndex;
4014                         itemTag.itemIndex = firstLoadedItemPos.itemIndex;
4015                         return true;
4016                 }
4017
4018                 if ((firstLoadedItemPos.itemIndex == lastLoadedItemPos.itemIndex) && (firstLoadedItemPos.groupIndex == lastLoadedItemPos.groupIndex))
4019                 {
4020                         break;
4021                 }
4022         } while (GetNextItemPosition(firstLoadedItemPos, firstLoadedItemPos));
4023
4024         return false;
4025 }
4026
4027 float
4028 _TableViewPresenter::GetItemHeight(TableViewItemTag itemTag) const
4029 {
4030         float itemHeight = 0.0f;
4031
4032         if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SECTION && itemTag.itemIndex != -1)
4033         {
4034                 if (HasSectionFooter(itemTag.groupIndex))
4035                 {
4036                         if (itemTag.itemIndex == GetItemCountAt(itemTag.groupIndex) - 1)
4037                         {
4038                                 GET_SHAPE_CONFIG(TABLEVIEW::GROUPITEM_DEFAULT_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, itemHeight);
4039                                 return itemHeight;
4040                         }
4041                 }
4042         }
4043
4044         if (__pProviderAdaptor == null)
4045         {
4046                 return -1;
4047         }
4048
4049         int startIndex = 0;
4050         for (int i = 0; i < itemTag.groupIndex; i++)
4051         {
4052                 int itemCount = GetItemCountAt(i);
4053
4054                 startIndex = startIndex + itemCount + 1;
4055         }
4056
4057         itemTag.itemIndex++;
4058         itemTag.itemIndex = startIndex + itemTag.itemIndex;
4059
4060         __itemHeightList.GetAt(itemTag.itemIndex, itemHeight);
4061
4062         return itemHeight;
4063 }
4064
4065 float
4066 _TableViewPresenter::SetItemHeight(TableViewItemTag itemTag, float height)
4067 {
4068         int startIndex = 0;
4069
4070         for (int i = 0; i < itemTag.groupIndex; i++)
4071         {
4072                 int itemCount = GetItemCountAt(i);
4073
4074                 startIndex = startIndex + itemCount + 1;
4075         }
4076
4077         itemTag.itemIndex++;
4078         itemTag.itemIndex = startIndex + itemTag.itemIndex;
4079
4080         float oldHeight = 0.0f;
4081         __itemHeightList.GetAt(itemTag.itemIndex, oldHeight);
4082         __itemHeightList.SetAt(height, itemTag.itemIndex);
4083
4084         return oldHeight;
4085 }
4086
4087
4088 result
4089 _TableViewPresenter::SetReorderMode(bool enabled)
4090 {
4091         ResetSweepItem();
4092
4093         __reorderInfo.itemIndex = -1;
4094         __reorderInfo.groupIndex = -1;
4095
4096         _TableViewItem* pItem = null;
4097         TableViewItemTag itemPos = {-1, -1};
4098         TableViewItemTag lastItemPos = {-1, -1};
4099
4100         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
4101         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
4102
4103         do
4104         {
4105                 pItem = FindItem(itemPos);
4106
4107                 if (pItem != null)
4108                 {
4109                         if(pItem->IsReorderMode() == enabled)
4110                         {
4111                                 break;
4112                         }
4113
4114                         pItem->SetReorderMode(enabled);
4115
4116                         if (enabled && itemPos.itemIndex != -1)
4117                         {
4118                                 if (pItem->GetDrawingStatus() == TABLE_VIEW_ITEM_DRAWING_STATUS_PRESSED)
4119                                 {
4120                                         if (SelectReorderItem(itemPos.groupIndex, itemPos.itemIndex, true))
4121                                         {
4122                                                 FloatPoint itemPosition = pItem->GetPositionF();
4123                                                 FloatPoint touchPosition = pItem->GetLastTouchPressedPositionF();
4124
4125                                                 __reorderInfo.touchPressedPositionY = itemPosition.y + touchPosition.y - GetScrollPosition();
4126                                                 __reorderInfo.blockedTouchReleaseState = true;
4127                                         }
4128                                 }
4129                         }
4130                 }
4131
4132                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
4133                 {
4134                         break;
4135                 }
4136
4137         } while (GetNextItemPosition(itemPos, itemPos) == true);
4138
4139         return E_SUCCESS;
4140 }
4141
4142 bool
4143 _TableViewPresenter::SelectReorderItem(int groupIndex, int itemIndex, bool directSelection)
4144 {
4145         if (itemIndex == -1 || (!directSelection && GetPressedItemCount() > 0))
4146         {
4147                 return false;
4148         }
4149
4150         TableViewItemTag itemPos = {groupIndex, itemIndex};
4151         _TableViewItem* pItem = FindItem(itemPos);
4152
4153         if (pItem == null)
4154         {
4155                 return false;
4156         }
4157
4158         pItem->SetDrawingStatus(TABLE_VIEW_ITEM_DRAWING_STATUS_PRESSED);
4159         pItem->SetItemChanged(true);
4160         pItem->Invalidate();
4161
4162         __reorderInfo.blockedScroll = false;
4163         __reorderInfo.itemBounds = pItem->GetBoundsF();
4164         __reorderInfo.itemIndex = itemIndex;
4165         __reorderInfo.groupIndex = groupIndex;
4166         __reorderInfo.originGroupIndex = groupIndex;
4167         __reorderInfo.originItemIndex = itemIndex;
4168         __reorderInfo.itemBasisPositionY = __reorderInfo.itemBounds.y;
4169
4170         Tizen::System::SystemTime::GetTicks(__reorderInfo.touchPressedTick);
4171
4172         pItem->GetVisualElement()->SetZOrder(null, true);
4173
4174         if (GetScrollAreaBounds().height < __pTableView->GetBoundsF().height)
4175         {
4176                 float itemHeight = pItem->GetBoundsF().height;
4177
4178                 __pListModel->GetLastLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
4179
4180                 pItem = FindItem(itemPos);
4181
4182                 if (pItem != null)
4183                 {
4184                         FloatRectangle itemBounds =  pItem->GetBoundsF();
4185                         __reorderInfo.nonScrollableLimitArea = itemBounds.y + itemBounds.height - itemHeight;
4186                 }
4187         }
4188         else
4189         {
4190                 __reorderInfo.nonScrollableLimitArea = 0;
4191         }
4192
4193         return true;
4194 }
4195
4196 bool
4197 _TableViewPresenter::ResetReorderItem(int groupIndex, int itemIndex)
4198 {
4199         TableViewItemTag itemPos = {groupIndex, itemIndex};
4200         _TableViewItem* pItem = FindItem(itemPos);
4201
4202         if (pItem == null)
4203         {
4204                 return false;
4205         }
4206
4207         pItem->SetDrawingStatus(TABLE_VIEW_ITEM_DRAWING_STATUS_NORMAL);
4208         pItem->SetItemChanged(true);
4209         pItem->Invalidate();
4210
4211         PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_LIST_REORDER, pItem);
4212
4213         FloatPoint position = FloatPoint(__reorderInfo.itemBounds.x, __reorderInfo.itemBounds.y);
4214         pItem->SetPosition(position);
4215
4216         if (__reorderInfo.originGroupIndex != __reorderInfo.groupIndex || __reorderInfo.originItemIndex != __reorderInfo.itemIndex)
4217         {
4218                 if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE)
4219                 {
4220                         __pTableView->FireTableViewItemReorderEvent(__reorderInfo.originItemIndex, __reorderInfo.itemIndex);
4221                 }
4222                 else if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_GROUPED)
4223                 {
4224                         __pTableView->FireTableViewItemReorderEvent(__reorderInfo.originGroupIndex, __reorderInfo.originItemIndex, __reorderInfo.groupIndex, __reorderInfo.itemIndex);
4225                 }
4226         }
4227
4228         __reorderInfo.groupIndex = -1;
4229         __reorderInfo.itemIndex = -1;
4230         __reorderInfo.originGroupIndex = -1;
4231         __reorderInfo.originItemIndex = -1;
4232         __reorderInfo.nonScrollableLimitArea = 0;
4233
4234         StopReorderScrollTimer();
4235
4236         return true;
4237 }
4238
4239 bool
4240 _TableViewPresenter::CheckReorderItemScrollAnimation(_TableViewItem *pItem)
4241 {
4242         float currentScrollPosition = GetScrollPosition();
4243         float limitTopPositionY = currentScrollPosition;
4244         float limitBottomPositionY = currentScrollPosition + __pTableView->GetBoundsF().height - __reorderInfo.itemBounds.height;
4245
4246         float itemPositionY = pItem->GetPositionF().y;
4247
4248         if (itemPositionY < limitTopPositionY)
4249         {
4250                 if (__pReorderScrollTimer == null)
4251                 {
4252                         __reorderInfo.isScrollDirectionUp = true;
4253
4254                         return true;
4255                 }
4256         }
4257         else if (itemPositionY > limitBottomPositionY)
4258         {
4259                 if (__pReorderScrollTimer == null)
4260                 {
4261                         __reorderInfo.isScrollDirectionUp = false;
4262
4263                         return true;
4264                 }
4265         }
4266
4267         return false;
4268 }
4269
4270 bool
4271 _TableViewPresenter::DragSelectedItem(float distance, bool relativeCoordinate)
4272 {
4273         TableViewItemTag itemPos = {__reorderInfo.groupIndex, __reorderInfo.itemIndex};
4274         _TableViewItem* pItem = FindItem(itemPos);
4275
4276         if (pItem == null)
4277         {
4278                 return false;
4279         }
4280
4281         if (__firstTouchMoved)
4282         {
4283                 long long currentTick = 0;
4284                 Tizen::System::SystemTime::GetTicks(currentTick);
4285
4286                 if (currentTick - __reorderInfo.touchPressedTick < REORDER_TOUCH_UIACTIVATE_DURATION)
4287                 {
4288                         return false;
4289                 }
4290
4291                 __firstTouchMoved = false;
4292         }
4293
4294         FloatPoint itemPosition = pItem->GetPositionF();
4295
4296         if (relativeCoordinate)
4297         {
4298                 itemPosition.y = __reorderInfo.itemBasisPositionY + distance;
4299         }
4300         else
4301         {
4302                 itemPosition.y += distance;
4303
4304                 __reorderInfo.itemBasisPositionY += distance;
4305         }
4306
4307         if (itemPosition.y < 0)
4308         {
4309                 itemPosition.y = 0;
4310         }
4311
4312         float scrollAreaHeight = GetScrollAreaBounds().height;
4313         float screenHeight = __pTableView->GetBoundsF().height;
4314         float limitBottomPositionY = 0.0f;
4315
4316         if (scrollAreaHeight < screenHeight)
4317         {
4318                 limitBottomPositionY = __reorderInfo.nonScrollableLimitArea;
4319         }
4320         else
4321         {
4322                 limitBottomPositionY = scrollAreaHeight - __reorderInfo.itemBounds.height;
4323         }
4324
4325         if (itemPosition.y > limitBottomPositionY)
4326         {
4327                 itemPosition.y = limitBottomPositionY;
4328         }
4329
4330         pItem->SetPosition(itemPosition);
4331
4332         // check scroll
4333         if (CheckReorderItemScrollAnimation(pItem))
4334         {
4335                 if (__pReorderScrollTimer == null)
4336                 {
4337                         StartReorderScrollTimer();
4338                 }
4339         }
4340         else
4341         {
4342                 if (__pReorderScrollTimer != null)
4343                 {
4344                         StopReorderScrollTimer();
4345                 }
4346         }
4347
4348         // check swap
4349         TableViewItemTag moveItemTag = {-1, -1};
4350
4351         if (CheckReorderItemPosition(pItem, moveItemTag))
4352         {
4353                 if (moveItemTag.itemIndex == -1)
4354                 {
4355                         int destGroupIndex = moveItemTag.groupIndex;
4356
4357                         if (destGroupIndex != 0 && destGroupIndex == __reorderInfo.groupIndex)
4358                         {
4359                                 destGroupIndex--;
4360                         }
4361
4362                         if (moveItemTag.groupIndex == 0 || !__pProviderAdaptor->IsReorderable(__reorderInfo.groupIndex, destGroupIndex))
4363                         {
4364                                 __reorderInfo.blockedScroll = true;
4365
4366                                 return false;
4367                         }
4368                 }
4369
4370                 ReorderItem(moveItemTag.groupIndex, moveItemTag.itemIndex);
4371         }
4372
4373         return true;
4374 }
4375
4376 bool
4377 _TableViewPresenter::CheckReorderItemPosition(_TableViewItem* pItem, TableViewItemTag& reorderItemTag)
4378 {
4379         TableViewItemTag currentItemTag = {__reorderInfo.groupIndex, __reorderInfo.itemIndex};
4380         TableViewItemTag previousItemTag = {-1, -1};
4381         TableViewItemTag nextItemTag = {-1, -1};
4382
4383         FloatRectangle itemBounds = pItem->GetBoundsF();
4384
4385         if (GetPreviousItemPosition(currentItemTag, previousItemTag))
4386         {
4387                 _TableViewItem* pPreviousItem = LoadItem(previousItemTag.groupIndex, previousItemTag.itemIndex);
4388
4389                 if (pPreviousItem != null)
4390                 {
4391                         FloatRectangle previousItemBounds = pPreviousItem->GetBoundsF();
4392
4393                         if (itemBounds.y < previousItemBounds.y + (previousItemBounds.height / 2))
4394                         {
4395                                 //return previousItemIndex;
4396                                 reorderItemTag.groupIndex = previousItemTag.groupIndex;
4397                                 reorderItemTag.itemIndex = previousItemTag.itemIndex;
4398
4399                                 return true;
4400                         }
4401                 }
4402         }
4403
4404         if (GetNextItemPosition(currentItemTag, nextItemTag))
4405         {
4406                 _TableViewItem* pNextItem = LoadItem(nextItemTag.groupIndex, nextItemTag.itemIndex);
4407
4408                 if (pNextItem != null)
4409                 {
4410                         FloatRectangle nextItemBounds = pNextItem->GetBoundsF();
4411
4412                         if (itemBounds.y + itemBounds.height > nextItemBounds.y + (nextItemBounds.height / 2))
4413                         {
4414                                 //return nextItemIndex;
4415                                 reorderItemTag.groupIndex = nextItemTag.groupIndex;
4416                                 reorderItemTag.itemIndex = nextItemTag.itemIndex;
4417
4418                                 return true;
4419                         }
4420                 }
4421         }
4422
4423         reorderItemTag.groupIndex = -1;
4424         reorderItemTag.itemIndex = -1;
4425
4426         return false;
4427 }
4428
4429 bool
4430 _TableViewPresenter::ReorderItem(int destinationGroupIndex, int destinationItemIndex)
4431 {
4432         TableViewItemTag itemPos = {destinationGroupIndex, destinationItemIndex};
4433
4434         _TableViewItem* pItem = FindItem(itemPos);
4435
4436         if (pItem == null)
4437         {
4438                 return false;
4439         }
4440
4441         if (pItem->IsAnimationPlaying())
4442         {
4443                 return false;
4444         }
4445
4446         FloatRectangle destinationItemBounds = pItem->GetBoundsF();
4447
4448         FloatPoint destinationItemPosition = FloatPoint(destinationItemBounds.x, 0.0f);
4449
4450         if ( __reorderInfo.itemIndex > destinationItemIndex && __reorderInfo.groupIndex == destinationGroupIndex)
4451         {
4452                 destinationItemPosition.y = __reorderInfo.itemBounds.y + __reorderInfo.itemBounds.height - destinationItemBounds.height;
4453                 __reorderInfo.itemBounds.y = destinationItemBounds.y;
4454
4455                 if (destinationItemIndex == -1)
4456                 {
4457                         destinationGroupIndex--;
4458                         int itemCount = GetItemCountAt(destinationGroupIndex);
4459                         destinationItemIndex = itemCount;
4460                 }
4461         }
4462         else
4463         {
4464                 destinationItemPosition.y = __reorderInfo.itemBounds.y;
4465                 __reorderInfo.itemBounds.y = destinationItemBounds.y + destinationItemBounds.height - __reorderInfo.itemBounds.height;
4466
4467                 if (destinationItemIndex == -1)
4468                 {
4469                         destinationItemIndex = 0;
4470                 }
4471         }
4472
4473         __pListModel->MoveItem(__reorderInfo.groupIndex, __reorderInfo.itemIndex, destinationGroupIndex, destinationItemIndex);
4474
4475         if (!pItem->MoveItem(destinationItemPosition, REORDER_ITEM_MOVE_ANIMATION_DURATION, 0))
4476         {
4477                 pItem->SetPosition(destinationItemPosition);
4478         }
4479
4480         __reorderInfo.itemIndex = destinationItemIndex;
4481         __reorderInfo.groupIndex = destinationGroupIndex;
4482
4483         return true;
4484 }
4485
4486 void
4487 _TableViewPresenter::StartReorderScrollTimer(void)
4488 {
4489         StopReorderScrollTimer();
4490
4491         __pReorderScrollTimer = new (std::nothrow) Tizen::Base::Runtime::Timer;
4492         SysTryReturnVoidResult(NID_UI_CTRL, __pReorderScrollTimer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
4493
4494         result r = __pReorderScrollTimer->Construct(*this);
4495         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4496
4497         r = __pReorderScrollTimer->Start(REORDER_SCROLL_ANIMATION_TIMER_DURATION);
4498         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4499
4500         return;
4501
4502 CATCH:
4503         StopReorderScrollTimer();
4504 }
4505
4506 void
4507 _TableViewPresenter::StopReorderScrollTimer(void)
4508 {
4509         delete __pReorderScrollTimer;
4510         __pReorderScrollTimer = null;
4511
4512 }
4513
4514 void
4515 _TableViewPresenter::UpdateLayout(void)
4516 {
4517         // nothing
4518 }
4519
4520 bool
4521 _TableViewPresenter::CheckItemHeightAndRefreshLayout(TableViewItemTag itemTag, bool downScroll)
4522 {
4523         _TableViewItem* pItem = FindItem(itemTag);
4524
4525         if (pItem == null)
4526         {
4527                 return false;
4528         }
4529
4530         if (pItem->IsAnimationPlaying())
4531         {
4532                 return false;
4533         }
4534
4535         FloatRectangle itemBounds = pItem->GetBoundsF();
4536
4537         if (_FloatCompare(GetItemHeight(itemTag), itemBounds.height))
4538         {
4539                 return false;
4540         }
4541
4542         float newHeight = itemBounds.height;
4543         float originHeight = SetItemHeight(itemTag, newHeight);
4544         float dist = newHeight - originHeight;
4545
4546         AdjustClientAreaBounds(false, dist);
4547
4548         if (!downScroll)
4549         {
4550                 itemBounds.y += dist;
4551                 pItem->SetBounds(itemBounds);
4552         }
4553
4554         AttachNextItemsToBottom(itemTag);
4555
4556         return true;
4557 }
4558
4559 bool
4560 _TableViewPresenter::CreateItemHeightList(float defaultGroupItemHeight, float defaultItemHeight)
4561 {
4562         int groupCount = GetGroupCount();
4563
4564         result r = __itemHeightList.SetCapacity(GetItemCount());
4565         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4566
4567         for (int i = 0; i < groupCount; i++)
4568         {
4569                 int itemCount = GetItemCountAt(i);
4570
4571                 if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE)
4572                 {
4573                         __itemHeightList.Add(0.0f);
4574                 }
4575                 else
4576                 {
4577                         __itemHeightList.Add(defaultGroupItemHeight);
4578                 }
4579
4580                 for (int j = 0; j < itemCount; j++)
4581                 {
4582                         __itemHeightList.Add(defaultItemHeight);
4583                 }
4584         }
4585
4586         return true;
4587
4588 CATCH:
4589         DeleteItemHeightList();
4590
4591         return false;
4592 }
4593
4594 void
4595 _TableViewPresenter::DeleteItemHeightList(void)
4596 {
4597         __itemHeightList.RemoveAll();
4598 }
4599
4600 bool
4601 _TableViewPresenter::RefreshItemHeightList(int groupIndex, int itemIndex, TableViewRefreshType refreshType)
4602 {
4603         float defaultItemHeight = __pProviderAdaptor->GetDefaultItemHeight();
4604         float defaultGroupItemHeight = __pProviderAdaptor->GetDefaultGroupItemHeight();
4605
4606         int startIndex = 0;
4607         for (int i = 0; i < groupIndex; i++)
4608         {
4609                 int itemCount = GetItemCountAt(i);
4610                 startIndex = startIndex + itemCount + 1;
4611         }
4612
4613         int targetIndex = startIndex + itemIndex + 1;
4614
4615         if (refreshType == TABLE_VIEW_REFRESH_TYPE_ITEM_ADD)
4616         {
4617                 if (itemIndex == -1)
4618                 {
4619                         result r = __itemHeightList.SetCapacity(GetItemCount());
4620                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
4621
4622                         int itemCount = GetItemCountAt(groupIndex);
4623                         for (int i = 0; i < itemCount; i++)
4624                         {
4625                                 __itemHeightList.InsertAt(defaultItemHeight, targetIndex);
4626                         }
4627
4628                         __itemHeightList.InsertAt(defaultGroupItemHeight, targetIndex);
4629
4630                 }
4631                 else
4632                 {
4633                         __itemHeightList.InsertAt(defaultItemHeight, targetIndex);
4634                 }
4635         }
4636         else if (refreshType == TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE)
4637         {
4638                 if (itemIndex == -1)
4639                 {
4640                         int itemCount = GetItemCountAt(groupIndex) + 1;
4641
4642                         for (int i = 0; i < itemCount; i++)
4643                         {
4644                                 __itemHeightList.RemoveAt(targetIndex);
4645                         }
4646                 }
4647                 else
4648                 {
4649                         __itemHeightList.RemoveAt(targetIndex);
4650                 }
4651         }
4652
4653         return true;
4654 }
4655
4656 bool
4657 _TableViewPresenter::CreateSectionAlignmentList(void)
4658 {
4659         if (__pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SECTION)
4660         {
4661                 return false;
4662         }
4663
4664         int groupCount = GetGroupCount();
4665
4666         result r = __sectionAlignmentList.SetCapacity(GetGroupCount());
4667         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
4668
4669         _TableViewSectionStringAlignment alignment;
4670         alignment.headerHorizontalAlignment = ALIGNMENT_LEFT;
4671         alignment.footerHorizontalAlignment = ALIGNMENT_RIGHT;
4672
4673         for (int i = 0; i < groupCount; i++)
4674         {
4675
4676                 __sectionAlignmentList.Add(alignment);
4677         }
4678
4679         return true;
4680 }
4681
4682 void
4683 _TableViewPresenter::DeleteSectionAlignmentList(void)
4684 {
4685         __sectionAlignmentList.RemoveAll();
4686 }
4687
4688 bool
4689 _TableViewPresenter::RefreshSectionAlignmentList(int groupIndex, TableViewRefreshType refreshType)
4690 {
4691         if (__pTableView->GetTableViewStyle() != TABLE_VIEW_STYLE_SECTION)
4692         {
4693                 return false;
4694         }
4695
4696         if (refreshType == TABLE_VIEW_REFRESH_TYPE_ITEM_ADD)
4697         {
4698                 _TableViewSectionStringAlignment alignment;
4699                 alignment.headerHorizontalAlignment = ALIGNMENT_LEFT;
4700                 alignment.footerHorizontalAlignment = ALIGNMENT_RIGHT;
4701
4702                 __sectionAlignmentList.InsertAt(alignment, groupIndex);
4703         }
4704         else if (refreshType == TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE)
4705         {
4706                 __sectionAlignmentList.RemoveAt(groupIndex);
4707         }
4708
4709         return true;
4710 }
4711
4712 void
4713 _TableViewPresenter::CaptureAndStartRemoveItemAnimation(int groupIndex, int itemIndex)
4714 {
4715         TableViewItemTag itemTag = {groupIndex, itemIndex};
4716         _TableViewItem* pItem = FindItem(itemTag);
4717
4718         if (pItem == null)
4719         {
4720                 return;
4721         }
4722
4723         FloatRectangle itemBounds = pItem->GetBoundsF();
4724         result r = E_SUCCESS;
4725
4726         _VisualElement* pVisualElement = null;
4727         String animationName(L"REMOVE_ITEM_ANIMATION");
4728         Canvas* pCanvas = null;
4729         VisualElementValueAnimation* pAnimation = __pTableView->GetVisualElementValueAnimation(animationName);
4730
4731         FloatDimension startValue(100.0f, itemBounds.height);
4732         FloatDimension endValue(0.0f, 0.0f);
4733
4734         Tizen::Graphics::Bitmap* pBitmap = pItem->GetCapturedBitmapN(true);
4735
4736         if (pBitmap == null)
4737         {
4738                 return;
4739         }
4740
4741         pCanvas = null;
4742         pVisualElement = new (std::nothrow) _VisualElement();
4743         SysTryCatch(NID_UI_CTRL, pVisualElement != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
4744
4745         r = pVisualElement->Construct();
4746         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4747
4748         pVisualElement->SetImplicitAnimationEnabled(false);
4749         pVisualElement->SetSurfaceOpaque(false);
4750         pVisualElement->SetBounds(FloatRectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height));
4751         pVisualElement->SetShowState(true);
4752
4753         pCanvas = pVisualElement->GetCanvasN();
4754         SysTryCatch(NID_UI_CTRL, pCanvas != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
4755
4756         pCanvas->Clear();
4757         pCanvas->DrawBitmap(FloatRectangle(0, 0, itemBounds.width, itemBounds.height), *pBitmap);
4758
4759         pAnimation->SetDelay(0);
4760         pAnimation->SetStartValue(Variant(startValue));
4761         pAnimation->SetEndValue(Variant(endValue));
4762         pAnimation->SetDuration(REMOVE_ITEM_MOVE_ANIMATION_DURATION);
4763
4764         r = pVisualElement->AddAnimation(animationName, *pAnimation);
4765         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4766
4767         delete pAnimation;
4768         delete pCanvas;
4769         delete pBitmap;
4770
4771         return;
4772
4773 CATCH:
4774         delete pCanvas;
4775         delete pBitmap;
4776         pVisualElement->Destroy();
4777 }
4778
4779 void
4780 _TableViewPresenter::PresetItemHeightList(void)
4781 {
4782         int groupCount = GetGroupCount();
4783         float defaultGroupItemHeight = __pProviderAdaptor->GetDefaultGroupItemHeight();
4784         float defaultItemHeight = __pProviderAdaptor->GetDefaultItemHeight();
4785
4786         for (int i = 0; i < groupCount; i++)
4787         {
4788                 TableViewItemTag itemTag = {i, -1};
4789                 int itemCount = GetItemCountAt(i);
4790
4791                 if (_FloatCompare(GetItemHeight(itemTag), TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE))
4792                 {
4793                         SetItemHeight(itemTag, defaultGroupItemHeight);
4794                 }
4795
4796                 for (int j = 0; j < itemCount; j++)
4797                 {
4798                         itemTag.itemIndex = j;
4799
4800                         if (_FloatCompare(GetItemHeight(itemTag), TABLEVIEW_DEFAULT_ITEM_HEIGHT_VALUE))
4801                         {
4802                                 SetItemHeight(itemTag, defaultItemHeight);
4803                         }
4804                 }
4805         }
4806 }
4807
4808 _TableViewItem*
4809 _TableViewPresenter::GetTableViewItemFromControl(const _Control& source)
4810 {
4811         _Control* currentControl = const_cast<_Control*>(&source);
4812         _TableViewItem* pItem = null;
4813
4814         while (__pTableView != currentControl)
4815         {
4816                 pItem = dynamic_cast<_TableViewItem*>(currentControl);
4817
4818                 if ((pItem != null) || (currentControl == null))
4819                 {
4820                         break;
4821                 }
4822
4823                 currentControl = currentControl->GetParent();
4824         }
4825
4826         return pItem;
4827 }
4828
4829 void
4830 _TableViewPresenter::ShowTableViewCapturedItem(int groupIndex, int itemIndex, float height)
4831 {
4832         TableViewItemTag itemTag = {groupIndex, itemIndex};
4833         _TableViewItem* pItem = FindItem(itemTag);
4834
4835         if ((pItem == null) || _FloatCompare(height, 0.0f))
4836         {
4837                 return;
4838         }
4839
4840         result r = E_SUCCESS;
4841         Canvas* pCanvas = null;
4842         FloatRectangle itemBounds = pItem->GetBoundsF();
4843
4844         bool visibleState = pItem->GetVisibleState();
4845         pItem->SetVisibleState(true);
4846         Bitmap* pBitmap = pItem->GetCapturedBitmapN(true);
4847         pItem->SetVisibleState(visibleState);
4848
4849         if (pBitmap == null)
4850         {
4851                 return;
4852         }
4853
4854         if (__pCapturedItemVisualElement == null)
4855         {
4856                 __pCapturedItemVisualElement = new (std::nothrow) _VisualElement();
4857                 SysTryCatch(NID_UI_CTRL, __pCapturedItemVisualElement != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
4858
4859                 r = __pCapturedItemVisualElement->Construct();
4860                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
4861
4862                 r = GetView()->GetVisualElement()->AttachChild(*__pCapturedItemVisualElement);
4863                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to attach child to the control VisualElement.", GetErrorMessage(r));
4864
4865                 __pCapturedItemVisualElement->SetSurfaceOpaque(false);
4866                 __pCapturedItemVisualElement->SetImplicitAnimationEnabled(false);
4867         }
4868
4869         __pCapturedItemVisualElement->SetShowState(true);
4870         __pCapturedItemVisualElement->SetBounds(FloatRectangle(itemBounds.x, itemBounds.y, itemBounds.width, height));
4871
4872         pCanvas = __pCapturedItemVisualElement->GetCanvasN();
4873         SysTryCatch(NID_UI_CTRL, pCanvas != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
4874
4875         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
4876         pCanvas->Clear();
4877         pCanvas->DrawBitmap(FloatPoint(0.0f, 0.0f), *pBitmap);
4878
4879         delete pCanvas;
4880         delete pBitmap;
4881
4882         return;
4883
4884 CATCH:
4885         delete pBitmap;
4886
4887         __pCapturedItemVisualElement->Destroy();
4888 }
4889
4890 void
4891 _TableViewPresenter::SetLoadedItemsVisibleFromPosition(float position, bool visible)
4892 {
4893         _TableViewItem* pItem;
4894         FloatRectangle itemBounds;
4895         TableViewItemTag current;
4896         current.groupIndex = __expandableItemTag.groupIndex;
4897         current.itemIndex = __expandableItemTag.itemIndex;
4898
4899         int itemCountInGroup = __pListModel->GetItemCountInGroup(__expandableItemTag.groupIndex);
4900
4901         for (int i = 0; i < itemCountInGroup; i++)
4902         {
4903                 current.itemIndex = i;
4904
4905                 if (visible)
4906                 {
4907                         pItem = LoadItem(current.groupIndex, current.itemIndex);
4908                         if (pItem == null)
4909                         {
4910                                 break;
4911                         }
4912                 }
4913                 else
4914                 {
4915                         pItem = FindItem(current);
4916                         if (pItem == null)
4917                         {
4918                                 continue;
4919                         }
4920                 }
4921
4922                 itemBounds = pItem->GetBoundsF();
4923
4924                 if (visible)
4925                 {
4926                         if ((position >= itemBounds.y) && (position < itemBounds.y + itemBounds.height))
4927                         {
4928                                 ShowTableViewCapturedItem(current.groupIndex, current.itemIndex, position - itemBounds.y);
4929                                 pItem->SetVisibleState(false);
4930                                 break;
4931                         }
4932                         else
4933                         {
4934                                 pItem->SetVisibleState(true);
4935                         }
4936                 }
4937                 else
4938                 {
4939                         if (position < itemBounds.y)
4940                         {
4941                                 UnloadItem(current);
4942                         }
4943                         else if ((position >= itemBounds.y) && (position < itemBounds.y + itemBounds.height))
4944                         {
4945                                 pItem->SetVisibleState(false);
4946                                 ShowTableViewCapturedItem(current.groupIndex, current.itemIndex, position - itemBounds.y);
4947                         }
4948                         else
4949                         {
4950                                 pItem->SetVisibleState(true);
4951                         }
4952                 }
4953         }
4954 }
4955
4956 void
4957 _TableViewPresenter::MoveLoadedItemsFromPosition(float position)
4958 {
4959         FloatRectangle itemBounds;
4960         float itemPosition = position;
4961
4962         TableViewItemTag current;
4963         TableViewItemTag next;
4964
4965         current.groupIndex = __expandableItemTag.groupIndex;
4966         current.itemIndex = __pListModel->GetItemCountInGroup(__expandableItemTag.groupIndex) -1;
4967
4968         float screenPosition = GetScrollPosition();
4969         FloatRectangle screenBounds = __pTableView->GetBoundsF();
4970         screenPosition += screenBounds.height;
4971
4972         _TableViewItem* pItem = null;
4973         while (GetNextItemPosition(current, next))
4974         {
4975                 if (screenPosition > itemPosition)
4976                 {
4977                         pItem = LoadItem(next.groupIndex, next.itemIndex);
4978                 }
4979                 else
4980                 {
4981                         pItem = FindItem(next);
4982                 }
4983
4984                 if (pItem == null)
4985                 {
4986                         break;
4987                 }
4988
4989                 itemBounds = pItem->GetBoundsF();
4990                 itemBounds.y = itemPosition;
4991                 pItem->SetBounds(itemBounds);
4992                 itemPosition += itemBounds.height;
4993
4994                 current = next;
4995         }
4996 }
4997
4998 void
4999 _TableViewPresenter::ExpandGroupAnimationFinished(bool completedNormally)
5000 {
5001         if (completedNormally == false)
5002         {
5003                 TableViewItemTag current;
5004                 TableViewItemTag next;
5005
5006                 current.groupIndex = __expandableItemTag.groupIndex;
5007                 current.itemIndex = -1;
5008
5009                 _TableViewItem* pNextItem = null;
5010                 _TableViewItem* pCurrentItem = null;
5011                 pCurrentItem = LoadItem(current.groupIndex, current.itemIndex);
5012
5013                 if (pCurrentItem == null)
5014                 {
5015                         return;
5016                 }
5017
5018                 FloatRectangle itemBounds;
5019                 FloatRectangle screenBounds = __pTableView->GetBoundsF();
5020                 float screenPosition = GetScrollPosition() + screenBounds.height;
5021                 float nextItemPositionY = pCurrentItem->GetBoundsF().y + pCurrentItem->GetBoundsF().height;
5022
5023                 while (GetNextItemPosition(current, next))
5024                 {
5025                         if (screenPosition >= nextItemPositionY)
5026                         {
5027                                 pCurrentItem = LoadItem(current.groupIndex, current.itemIndex);
5028                                 if (pCurrentItem == null)
5029                                 {
5030                                         break;
5031                                 }
5032                                 pNextItem = LoadItem(next.groupIndex, next.itemIndex);
5033                                 if (pNextItem == null)
5034                                 {
5035                                         break;
5036                                 }
5037                                 pNextItem->SetVisibleState(true);
5038                                 itemBounds = pNextItem->GetBoundsF();
5039                                 itemBounds.y = pCurrentItem->GetBoundsF().y + pCurrentItem->GetBoundsF().height;
5040                                 pNextItem->SetBounds(itemBounds);
5041                                 nextItemPositionY = itemBounds.y + itemBounds.height;
5042                         }
5043                         else
5044                         {
5045                                 UnloadItem(next.groupIndex, next.itemIndex);
5046                         }
5047
5048                         current = next;
5049                 }
5050         }
5051         else
5052         {
5053                 int currentGroupIndex = -1;
5054                 int currentItemIndex = -1;
5055                 GetBottomDrawnItemIndex(currentGroupIndex, currentItemIndex);
5056
5057                 int lastLoadedGroupIndex = -1;
5058                 int lastLoadedItemIndex = -1;
5059                 __pListModel->GetLastLoadedItemIndex(lastLoadedGroupIndex, lastLoadedItemIndex);
5060
5061                 TableViewItemTag current;
5062                 current.groupIndex = currentGroupIndex;
5063                 current.itemIndex = currentItemIndex;
5064
5065                 _TableViewItem* pCurrentItem;
5066                 pCurrentItem = FindItem(current);
5067                 if (pCurrentItem != null)
5068                 {
5069                         pCurrentItem->SetVisibleState(true);
5070                 }
5071
5072                 for (int j = currentGroupIndex; j <= lastLoadedGroupIndex; j++)
5073                 {
5074                         int itemCount = GetItemCountAt(j);
5075
5076                         for (int i = -1; i < itemCount; i++)
5077                         {
5078                                 if (i <= currentItemIndex && j == currentGroupIndex)
5079                                 {
5080                                         continue;
5081                                 }
5082
5083                                 UnloadItem(j, i);
5084                         }
5085                 }
5086         }
5087 }
5088
5089 void
5090 _TableViewPresenter::CollapseGroupAnimationFinished(bool completedNormally)
5091 {
5092         _TableViewItem* pItem = null;
5093         TableViewItemTag itemTag = {-1, -1};
5094         float groupTotalHeight = 0;
5095         int itemCountInGroup = __pListModel->GetItemCountInGroup(__expandableItemTag.groupIndex);
5096         for (int i = 0; i < itemCountInGroup; i++)
5097         {
5098                 pItem = static_cast<_TableViewItem*>(__pListModel->GetItemFromTemporaryBuffer(__expandableItemTag.groupIndex, i));
5099                 groupTotalHeight += pItem->GetItemHeight();
5100                 itemTag.groupIndex = __expandableItemTag.groupIndex;
5101                 itemTag.itemIndex = i;
5102                 SetItemHeight(itemTag, pItem->GetItemHeight());
5103         }
5104
5105         if (completedNormally == false)
5106         {
5107                 TableViewItemTag current;
5108                 TableViewItemTag next;
5109
5110                 current.groupIndex = __expandableItemTag.groupIndex;
5111                 current.itemIndex = -1;
5112
5113                 for (int i = 0; i < itemCountInGroup; i++)
5114                 {
5115                         UnloadItem(__expandableItemTag.groupIndex, i);
5116                 }
5117
5118                 _TableViewItem* pCurrentItem = LoadItem(current.groupIndex, current.itemIndex);
5119                 FloatRectangle itemBounds = pCurrentItem->GetBoundsF();
5120                 itemBounds.y = CalculateItemPositionY(current.groupIndex, current.itemIndex);
5121                 pCurrentItem->SetBounds(itemBounds);
5122
5123                 float nextItemPositionY = pCurrentItem->GetBoundsF().y + pCurrentItem->GetBoundsF().height;
5124
5125                 FloatRectangle screenBounds = __pTableView->GetBoundsF();
5126                 float screenPosition = GetScrollPosition() + screenBounds.height;
5127
5128                 while (GetNextItemPosition(current, next))
5129                 {
5130                         current = next;
5131                         if (screenPosition >= nextItemPositionY)
5132                         {
5133                                 pCurrentItem = LoadItem(current.groupIndex, current.itemIndex);
5134                                 if (pCurrentItem == null)
5135                                 {
5136                                         break;
5137                                 }
5138
5139                                 pCurrentItem->SetVisibleState(true);
5140                                 itemBounds = pCurrentItem->GetBoundsF();
5141                                 itemBounds.y = nextItemPositionY;
5142                                 pCurrentItem->SetBounds(itemBounds);
5143                                 nextItemPositionY = pCurrentItem->GetBoundsF().y + pCurrentItem->GetBoundsF().height;
5144                         }
5145                         else
5146                         {
5147                                 UnloadItem(current.groupIndex, current.itemIndex);
5148                         }
5149                 }
5150
5151                 int currentGroupIndex = -1;
5152                 int currentItemIndex = -1;
5153                 GetTopDrawnItemIndex(currentGroupIndex, currentItemIndex);
5154                 if (__expandableItemTag.groupIndex < currentGroupIndex)
5155                 {
5156                         SetScrollPosition(GetScrollPosition() - groupTotalHeight, false);
5157                 }
5158         }
5159         else
5160         {
5161                 int currentGroupIndex = -1;
5162                 int currentItemIndex = -1;
5163                 GetTopDrawnItemIndex(currentGroupIndex, currentItemIndex);
5164
5165                 int firstLoadedGroupIndex = -1;
5166                 int firstLoadedItemIndex = -1;
5167                 __pListModel->GetFirstLoadedItemIndex(firstLoadedGroupIndex, firstLoadedItemIndex);
5168
5169                 for (int j = firstLoadedGroupIndex; j <= currentGroupIndex; j++)
5170                 {
5171                         int itemCount = GetItemCountAt(j);
5172
5173                         for (int i = -1; i < itemCount; i++)
5174                         {
5175                                 if (i >= currentItemIndex && j == currentGroupIndex)
5176                                 {
5177                                         break;
5178                                 }
5179
5180                                 UnloadItem(j, i);
5181                         }
5182                 }
5183
5184                 for (int i = 0; i < itemCountInGroup; i++)
5185                 {
5186                         UnloadItem(__expandableItemTag.groupIndex, i);
5187                 }
5188
5189                 GetTopDrawnItemIndex(currentGroupIndex, currentItemIndex);
5190                 TableViewItemTag currentItemTag;
5191                 currentItemTag.groupIndex = currentGroupIndex;
5192                 currentItemTag.itemIndex = currentItemIndex;
5193
5194                 _TableViewItem* pItem = FindItem(currentItemTag);
5195                 if (pItem != null)
5196                 {
5197                         FloatRectangle itemBounds = pItem->GetBoundsF();
5198                         itemBounds.y = CalculateItemPositionY(currentGroupIndex, currentItemIndex);
5199                         pItem->SetBounds(itemBounds);
5200                         AttachNextItemsToBottom(currentItemTag);
5201                         if (__expandableItemTag.groupIndex < currentGroupIndex)
5202                         {
5203                                 SetScrollPosition(itemBounds.y, false);
5204                         }
5205                 }
5206         }
5207
5208         AdjustClientAreaBounds(false, -groupTotalHeight);
5209 }
5210
5211 void
5212 _TableViewPresenter::OnVisualElementAnimationStarted(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target)
5213 {
5214         if (keyName == L"REMOVE_ITEM_ANIMATION")
5215         {
5216                 return;
5217         }
5218
5219         if (keyName == L"SCROLL_PANEL_SCROLLING_ANIMATION")
5220         {
5221                 __scrollHeightOnFlickStarted = GetScrollAreaBounds().height;
5222                 __scrollPositionOnFlickStarted = GetScrollPosition();
5223         }
5224
5225         _ScrollPanelPresenter::OnVisualElementAnimationStarted(animation, keyName, target);
5226 }
5227
5228 void
5229 _TableViewPresenter::OnVisualElementAnimationRepeated(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, long currentRepeatCount)
5230 {
5231         if (keyName == L"REMOVE_ITEM_ANIMATION")
5232         {
5233                 return;
5234         }
5235         _ScrollPanelPresenter::OnVisualElementAnimationRepeated(animation, keyName, target, currentRepeatCount);
5236 }
5237
5238 void
5239 _TableViewPresenter::OnVisualElementAnimationFinished(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, bool completedNormally)
5240 {
5241         if (__isAnimationCallbackBlocked)
5242         {
5243                 return;
5244         }
5245
5246         if (keyName == L"REMOVE_ITEM_ANIMATION")
5247         {
5248                 _VisualElement* pVisualElement = GetView()->GetVisualElement();
5249
5250                 pVisualElement->DetachChild(target);
5251
5252                 target.Destroy();
5253                 return;
5254         }
5255         else if (keyName == L"EXPAND_GROUP_ANIMATION")
5256         {
5257                 if (__pCapturedItemVisualElement)
5258                 {
5259                         __pCapturedItemVisualElement->SetShowState(false);
5260                 }
5261                 ExpandGroupAnimationFinished(completedNormally);
5262
5263                 if (__scrollToItemTag.startedAnimation)
5264                 {
5265                         __scrollToItemTag.startedAnimation = false;
5266                         ScrollToItem(__scrollToItemTag.groupIndex, __scrollToItemTag.itemIndex, __scrollToItemTag.itemAlignment, __scrollToItemTag.shiftingDistance);
5267                 }
5268                 return;
5269         }
5270         else if (keyName == L"COLLAPSE_GROUP_ANIMATION")
5271         {
5272                 if (__pCapturedItemVisualElement)
5273                 {
5274                         __pCapturedItemVisualElement->SetShowState(false);
5275                 }
5276                 CollapseGroupAnimationFinished(completedNormally);
5277
5278                 if (__scrollToItemTag.startedAnimation)
5279                 {
5280                         __scrollToItemTag.startedAnimation = false;
5281                         ScrollToItem(__scrollToItemTag.groupIndex, __scrollToItemTag.itemIndex, __scrollToItemTag.itemAlignment, __scrollToItemTag.shiftingDistance);
5282                 }
5283                 return;
5284         }
5285
5286         _ScrollPanelPresenter::OnVisualElementAnimationFinished(animation, keyName, target, completedNormally);
5287 }
5288
5289 void
5290 _TableViewPresenter::OnTickOccurred(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, const Tizen::Ui::Variant& currentValue)
5291 {
5292         _VisualElement* pVisualElement = __pTableView->GetVisualElement();
5293
5294         if (&target != pVisualElement)
5295         {
5296                 return;
5297         }
5298
5299         if (keyName == L"REMOVE_ITEM_ANIMATION")
5300         {
5301                 Dimension value = currentValue.ToDimension();
5302                 target.SetOpacity(value.width);
5303
5304                 FloatRectangle bounds = target.GetBounds();
5305                 bounds.height = value.height;
5306                 target.SetBounds(bounds);
5307
5308                 target.SetFlushNeeded();
5309
5310                 return;
5311         }
5312         else if (keyName == L"EXPAND_GROUP_ANIMATION")
5313         {
5314                 float currentPosition = currentValue.ToFloat();
5315                 SetLoadedItemsVisibleFromPosition(currentPosition, true);
5316                 MoveLoadedItemsFromPosition(currentPosition);
5317
5318                 return;
5319         }
5320         else if (keyName == L"COLLAPSE_GROUP_ANIMATION")
5321         {
5322                 float currentPosition = currentValue.ToFloat();
5323                 SetLoadedItemsVisibleFromPosition(currentPosition, false);
5324                 MoveLoadedItemsFromPosition(currentPosition);
5325
5326                 return;
5327         }
5328         else if (keyName == L"SWEEP_ITEM_ANIMATION")
5329         {
5330                 float currentPosition = currentValue.ToFloat();
5331                 float moveDestance = currentPosition - __sweptItemPosition.x;
5332                 SweepItem(moveDestance);
5333
5334                 return;
5335         }
5336
5337         _ScrollPanelPresenter::OnTickOccurred(animation, keyName, target, currentValue);
5338 }
5339
5340 bool
5341 _TableViewPresenter::OnAccessibilityItemRefreshed(const _AccessibilityContainer& control, const _AccessibilityElement& element, _AccessibilityFocusDirection direction)
5342 {
5343         TableViewItemTag itemPos = {-1, -1};
5344
5345         if (direction == _ACCESSIBILITY_FOCUS_DIRECTION_NEXT)
5346         {
5347                 GetFirstItem(itemPos);
5348                 if (GetAccessibilityElementFocusedState() == false && FindItem(itemPos) == null)
5349                 {
5350                         ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
5351
5352                         return true;
5353                 }
5354         }
5355         else if (direction == _ACCESSIBILITY_FOCUS_DIRECTION_PREVIOUS)
5356         {
5357                 GetLastItem(itemPos);
5358
5359                 if (IsGroupExpanded(itemPos.groupIndex) == false)
5360                 {
5361                         itemPos.itemIndex = -1;
5362                 }
5363
5364                 if (GetAccessibilityElementFocusedState() == false && FindItem(itemPos) == null)
5365                 {
5366                         ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
5367
5368                         return true;
5369                 }
5370         }
5371
5372         return false;
5373 }
5374
5375 bool
5376 _TableViewPresenter::OnOcurredOverflowItems(const int currentCashSize, const int overflowCount)
5377 {
5378         return IsCachingItemsTotalHeightLessThanViewHeight();
5379 }
5380
5381 bool
5382 _TableViewPresenter::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
5383 {
5384         if (!__pTableView->IsFocusModeStateEnabled())
5385         {
5386                 return false;
5387         }
5388
5389         _KeyCode keyCode = keyInfo.GetKeyCode();
5390         _Control* pFocusedControl = null;
5391         _Window* pTop = source.GetRootWindow();
5392         if (pTop)
5393         {
5394                 pFocusedControl = pTop->GetCurrentFocusControl();
5395         }
5396
5397         _TableViewItem* pItem = null;
5398         TableViewItemTag itemPos = {-1, -1};
5399
5400         switch (keyCode)
5401         {
5402         case _KEY_UP:
5403                 if (pFocusedControl != null)
5404                 {
5405                         pItem = dynamic_cast<_TableViewItem*>(pFocusedControl);
5406                         if (pItem == null)
5407                         {
5408                                 _Control* pParentControl = pFocusedControl->GetParent();
5409                                 pItem = dynamic_cast<_TableViewItem*>(pParentControl);
5410                         }
5411
5412                         if (pItem != null)
5413                         {
5414                                 pItem->GetItemIndex(itemPos.groupIndex, itemPos.itemIndex);
5415                                 TableViewItemTag firstItemPos = {-1, -1};
5416                                 GetFirstItem(firstItemPos);
5417                                 if (itemPos.groupIndex == firstItemPos.groupIndex && itemPos.itemIndex == firstItemPos.itemIndex)
5418                                 {
5419                                         SetTableViewFocused(false);
5420                                         return false;
5421                                 }
5422
5423                                 while (GetPreviousItemPosition(itemPos, itemPos))
5424                                 {
5425                                         if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE)
5426                                         {
5427                                                 TableViewItemTag firstItemPos = {-1, -1};
5428                                                 GetFirstItem(firstItemPos);
5429                                                 if (itemPos.groupIndex == firstItemPos.groupIndex && itemPos.itemIndex == firstItemPos.itemIndex)
5430                                                 {
5431                                                         SetTableViewFocused(false);
5432                                                         return false;
5433                                                 }
5434                                         }
5435
5436                                         if (IsGroupExpanded(itemPos.groupIndex) == false)
5437                                         {
5438                                                 itemPos.itemIndex = -1;
5439                                         }
5440
5441                                         pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
5442                                         if (pItem == null)
5443                                         {
5444                                                 SetTableViewFocused(false);
5445                                                 return false;
5446                                         }
5447
5448                                         if (!pItem->GetVisibleState() || !pItem->GetEnableState()
5449                                                         || !pItem->IsFocusable())
5450                                         {
5451                                                 TableViewItemTag firstItemPos = {-1, -1};
5452                                                 GetFirstItem(firstItemPos);
5453                                                 if (itemPos.groupIndex == firstItemPos.groupIndex && itemPos.itemIndex == firstItemPos.itemIndex)
5454                                                 {
5455                                                         SetTableViewFocused(false);
5456                                                         return false;
5457                                                 }
5458
5459                                                 continue;
5460                                         }
5461
5462                                         if (pItem->GetBoundsF().y < GetScrollPosition())
5463                                         {
5464                                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
5465                                         }
5466
5467                                         if (pItem->GetEnableState() && pItem->GetVisibleState()
5468                                                         && pItem->IsFocusable())
5469                                         {
5470                                                 pItem->SetFocused(true);
5471                                                 __focusItemTag.groupIndex = itemPos.groupIndex;
5472                                                 __focusItemTag.itemIndex = itemPos.itemIndex;
5473                                                 __isAnnexFocused = false;
5474                                                 pItem->DrawFocus();
5475                                                 break;
5476                                         }
5477                                 }
5478                         }
5479                         else
5480                         {
5481                                 SetTableViewFocused(false);
5482                                 return false;
5483                         }
5484                 }
5485                 break;
5486
5487         case _KEY_DOWN:
5488                 if (pFocusedControl != null)
5489                 {
5490                         pItem = dynamic_cast<_TableViewItem*>(pFocusedControl);
5491                         if (pItem == null)
5492                         {
5493                                 _Control* pParentControl = pFocusedControl->GetParent();
5494                                 pItem = dynamic_cast<_TableViewItem*>(pParentControl);
5495                         }
5496
5497                         if (pItem != null)
5498                         {
5499                                 pItem->GetItemIndex(itemPos.groupIndex, itemPos.itemIndex);
5500                                 TableViewItemTag lastItemPos = {-1, -1};
5501                                 GetLastItem(lastItemPos);
5502                                 if (itemPos.groupIndex == lastItemPos.groupIndex && itemPos.itemIndex == lastItemPos.itemIndex)
5503                                 {
5504                                         SetTableViewFocused(false);
5505                                         return false;
5506                                 }
5507
5508                                 while (GetNextItemPosition(itemPos, itemPos))
5509                                 {
5510                                         if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE && itemPos.itemIndex == -1)
5511                                         {
5512                                                 itemPos.itemIndex = 0;
5513                                         }
5514
5515                                         pItem = LoadItem(itemPos.groupIndex, itemPos.itemIndex);
5516                                         if (pItem == null)
5517                                         {
5518                                                 SetTableViewFocused(false);
5519                                                 return false;
5520                                         }
5521
5522                                         if (!pItem->GetVisibleState() || !pItem->GetEnableState()
5523                                                         || !pItem->IsFocusable())
5524                                         {
5525                                                 TableViewItemTag lastItemPos = {-1, -1};
5526                                                 GetLastItem(lastItemPos);
5527
5528                                                 if (itemPos.groupIndex == lastItemPos.groupIndex && itemPos.itemIndex == lastItemPos.itemIndex)
5529                                                 {
5530                                                         SetTableViewFocused(false);
5531                                                         return false;
5532                                                 }
5533
5534                                                 continue;
5535                                         }
5536                                         if (pItem->GetBoundsF().y + pItem->GetBoundsF().height > GetScrollPosition() + __pTableView->GetBoundsF().height)
5537                                         {
5538                                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
5539                                         }
5540
5541                                         if (pItem->GetEnableState() && pItem->GetVisibleState()
5542                                                         && pItem->IsFocusable())
5543                                         {
5544                                                 pItem->SetFocused(true);
5545                                                 pItem->DrawFocus();
5546                                                 __focusItemTag.groupIndex = itemPos.groupIndex;
5547                                                 __focusItemTag.itemIndex = itemPos.itemIndex;
5548                                                 __isAnnexFocused = false;
5549                                                 break;
5550                                         }
5551                                 }
5552                         }
5553                         else
5554                         {
5555                                 SetTableViewFocused(false);
5556                                 return false;
5557                         }
5558                 }
5559                 break;
5560
5561         case _KEY_TAB:
5562                 SetTableViewFocused(false);
5563                 return false;
5564
5565         default:
5566                 return false;
5567         }
5568
5569         return true;
5570 }
5571
5572 bool
5573 _TableViewPresenter::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
5574 {
5575         if (!__pTableView->IsFocusModeStateEnabled())
5576         {
5577                 return false;
5578         }
5579
5580          _KeyCode keyCode = keyInfo.GetKeyCode();
5581
5582         if (keyCode == _KEY_UP || keyCode == _KEY_DOWN)
5583         {
5584                 return true;
5585         }
5586
5587         return false;
5588 }
5589
5590 void
5591 _TableViewPresenter::OnDrawFocus(void)
5592 {
5593         if (!__pTableView->IsFocusModeStateEnabled())
5594         {
5595                 return;
5596         }
5597
5598         _Control* pFocusedControl = null;
5599         _Window* pTop = __pTableView->GetRootWindow();
5600         if (pTop)
5601         {
5602                 pFocusedControl = pTop->GetCurrentFocusControl();
5603         }
5604
5605         _TableView* pTableView = null;
5606         _TableViewItem* pItem = null;
5607         TableViewItemTag itemPos = {-1, -1};
5608
5609         pItem = FindItem(__focusItemTag);
5610
5611         if (__isRestoreFocus && pItem != null)
5612         {
5613                 if (pItem->GetEnableState() && pItem->GetVisibleState() && pItem->IsFocusable())
5614                 {
5615                         if (__isAnnexFocused)
5616                         {
5617                                 pItem->DrawAnnexFocus();
5618                         }
5619                         else
5620                         {
5621                                 pItem->SetFocused(true);
5622                                 pItem->DrawFocus();
5623                         }
5624                 }
5625
5626                 __isRestoreFocus = false;
5627                 return;
5628         }
5629
5630         if (pFocusedControl != null)
5631         {
5632                 pTableView = dynamic_cast<_TableView*>(pFocusedControl);
5633                 if (pTableView != null)
5634                 {
5635                         if (_FocusManagerImpl::GetInstance()->IsForwardDirection() == true)
5636                         {
5637                                 GetFirstItem(itemPos);
5638                                 if (__pTableView->GetTableViewStyle() == TABLE_VIEW_STYLE_SIMPLE)
5639                                 {
5640                                         itemPos.itemIndex = 0;
5641                                 }
5642
5643                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
5644                                 pItem = FindItem(itemPos);
5645                                 if (pItem != null)
5646                                 {
5647                                         if (pItem->GetEnableState() && pItem->GetVisibleState()
5648                                                         && pItem->IsFocusable())
5649                                         {
5650                                                 __focusItemTag.groupIndex = itemPos.groupIndex;
5651                                                 __focusItemTag.itemIndex = itemPos.itemIndex;
5652                                                 __isAnnexFocused = false;
5653                                                 pItem->SetFocused(true);
5654                                                 pItem->DrawFocus();
5655                                         }
5656                                         else
5657                                         {
5658                                                 while (GetNextItemPosition(itemPos, itemPos))
5659                                                 {
5660                                                         TableViewItemTag topDrawnItemIndex = {-1, -1};
5661                                                         GetTopDrawnItem(topDrawnItemIndex);
5662                                                         if (topDrawnItemIndex.groupIndex <= itemPos.groupIndex && topDrawnItemIndex.itemIndex <= itemPos.itemIndex)
5663                                                         {
5664                                                                 TableViewItemTag bottomDrawnItemIndex = {-1, -1};
5665                                                                 GetBottomDrawnItem(bottomDrawnItemIndex);
5666                                                                 if (bottomDrawnItemIndex.groupIndex >= itemPos.groupIndex && bottomDrawnItemIndex.itemIndex >= itemPos.itemIndex)
5667                                                                 {
5668                                                                         pItem = FindItem(itemPos);
5669                                                                 }
5670                                                                 else
5671                                                                 {
5672                                                                         ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
5673                                                                         pItem = FindItem(itemPos);
5674                                                                 }
5675                                                         }
5676                                                         else
5677                                                         {
5678                                                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_TOP);
5679                                                                 pItem = FindItem(itemPos);
5680                                                         }
5681
5682                                                         if (pItem != null)
5683                                                         {
5684                                                                 if (!pItem->GetVisibleState() || !pItem->GetEnableState()
5685                                                                                 || !pItem->IsFocusable())
5686                                                                 {
5687                                                                         TableViewItemTag lastItemPos = {-1, -1};
5688                                                                         GetLastItem(lastItemPos);
5689
5690                                                                         if (itemPos.groupIndex == lastItemPos.groupIndex && itemPos.itemIndex == lastItemPos.itemIndex)
5691                                                                         {
5692                                                                                 return;
5693                                                                         }
5694
5695                                                                         continue;
5696                                                                 }
5697                                                                 else
5698                                                                 {
5699                                                                         __focusItemTag.groupIndex = itemPos.groupIndex;
5700                                                                         __focusItemTag.itemIndex = itemPos.itemIndex;
5701                                                                         __isAnnexFocused = false;
5702                                                                         pItem->SetFocused(true);
5703                                                                         pItem->DrawFocus();
5704                                                                         break;
5705                                                                 }
5706                                                         }
5707                                                 }
5708                                         }
5709                                 }
5710                         }
5711                         else
5712                         {
5713                                 GetLastItem(itemPos);
5714                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
5715                                 pItem = FindItem(itemPos);
5716
5717                                 if (pItem != null)
5718                                 {
5719                                         if (pItem->GetEnableState() && pItem->GetVisibleState()
5720                                                         && pItem->IsFocusable())
5721                                         {
5722                                                 __focusItemTag.groupIndex = itemPos.groupIndex;
5723                                                 __focusItemTag.itemIndex = itemPos.itemIndex;
5724                                                 __isAnnexFocused = false;
5725                                                 pItem->SetFocused(true);
5726                                                 pItem->DrawFocus();
5727                                         }
5728                                         else
5729                                         {
5730                                                 while (GetPreviousItemPosition(itemPos, itemPos))
5731                                                 {
5732                                                         TableViewItemTag bottomDrawnItemIndex = {-1, -1};
5733                                                         GetBottomDrawnItem(bottomDrawnItemIndex);
5734
5735                                                         if (bottomDrawnItemIndex.groupIndex >= itemPos.groupIndex && bottomDrawnItemIndex.itemIndex >= itemPos.itemIndex)
5736                                                         {
5737                                                                 TableViewItemTag topDrawnItemIndex = {-1, -1};
5738                                                                 GetTopDrawnItem(topDrawnItemIndex);
5739                                                                 if (topDrawnItemIndex.groupIndex <= itemPos.groupIndex && topDrawnItemIndex.itemIndex <= itemPos.itemIndex)
5740                                                                 {
5741                                                                         pItem = FindItem(itemPos);
5742                                                                 }
5743                                                                 else
5744                                                                 {
5745                                                                         ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
5746                                                                         pItem = FindItem(itemPos);
5747                                                                 }
5748                                                         }
5749                                                         else
5750                                                         {
5751                                                                 ScrollToItem(itemPos.groupIndex, itemPos.itemIndex, TABLE_VIEW_SCROLL_ITEM_ALIGNMENT_BOTTOM);
5752                                                                 pItem = FindItem(itemPos);
5753                                                         }
5754
5755                                                         if (pItem != null)
5756                                                         {
5757                                                                 if (!pItem->GetVisibleState() || !pItem->GetEnableState()
5758                                                                                 || !pItem->IsFocusable())
5759                                                                 {
5760                                                                         TableViewItemTag firstItemPos = {-1, -1};
5761                                                                         GetFirstItem(firstItemPos);
5762                                                                         if (itemPos.groupIndex == firstItemPos.groupIndex && itemPos.itemIndex == firstItemPos.itemIndex)
5763                                                                         {
5764                                                                                 return;
5765                                                                         }
5766
5767                                                                         continue;
5768                                                                 }
5769                                                                 else
5770                                                                 {
5771                                                                         __focusItemTag.groupIndex = itemPos.groupIndex;
5772                                                                         __focusItemTag.itemIndex = itemPos.itemIndex;
5773                                                                         __isAnnexFocused = false;
5774                                                                         pItem->SetFocused(true);
5775                                                                         pItem->DrawFocus();
5776                                                                         break;
5777                                                                 }
5778                                                         }
5779                                                 }
5780                                         }
5781                                 }
5782                         }
5783                 }
5784         }
5785 }
5786
5787 void
5788 _TableViewPresenter::OnFocusModeStateChanged(void)
5789 {
5790         __focusItemTag.groupIndex = -1;
5791         __focusItemTag.itemIndex = -1;
5792         __isRestoreFocus = false;
5793         __isAnnexFocused = false;
5794
5795         _Window* pTop = __pTableView->GetRootWindow();
5796
5797         if (pTop != null)
5798         {
5799                 _Control* pFocusedControl = pTop->GetCurrentFocusControl();
5800
5801                 if (pFocusedControl != null)
5802                 {
5803                         pFocusedControl->RemoveFocusRing();
5804                 }
5805         }
5806 }
5807
5808 void
5809 _TableViewPresenter::OnVisibleStateChanged(void)
5810 {
5811         //reset focus
5812         if (__pTableView->IsFocused())
5813         {
5814                 __pTableView->RemoveFocusRing();
5815         }
5816 }
5817
5818 void
5819 _TableViewPresenter::OnAncestorEnableStateChanged(const _Control& control)
5820 {
5821         //reset focus
5822         if (__pTableView->IsFocused())
5823         {
5824                 __pTableView->RemoveFocusRing();
5825         }
5826 }
5827
5828 void
5829 _TableViewPresenter::OnAncestorVisibleStateChanged(const _Control& control)
5830 {
5831         //reset focus
5832         if (__pTableView->IsFocused())
5833         {
5834                 __pTableView->RemoveFocusRing();
5835         }
5836 }
5837
5838 void
5839 _TableViewPresenter::OnFocusableStateChanged(bool focusableState)
5840 {
5841         if (!focusableState)
5842         {
5843                 //reset focus
5844                 if (__pTableView->IsFocused())
5845                 {
5846                         __pTableView->RemoveFocusRing();
5847                 }
5848         }
5849 }
5850
5851 bool
5852 _TableViewPresenter::GetAccessibilityElementFocusedState(void)
5853 {
5854         _TableViewItem* pItem = null;
5855         TableViewItemTag itemPos = {-1, -1};
5856         TableViewItemTag lastItemPos = {-1, -1};
5857
5858         __pListModel->GetFirstLoadedItemIndex(itemPos.groupIndex, itemPos.itemIndex);
5859         __pListModel->GetLastLoadedItemIndex(lastItemPos.groupIndex, lastItemPos.itemIndex);
5860
5861         do
5862         {
5863                 pItem = static_cast <_TableViewItem*>(__pListModel->LoadItem(itemPos.groupIndex, itemPos.itemIndex));
5864                 SysTryReturn(NID_UI_CTRL, pItem != null, false, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
5865
5866                 _AccessibilityContainer* pContainer = pItem->GetAccessibilityContainer();
5867                 _AccessibilityElement* pElement = null;
5868
5869                 if (pContainer != null)
5870                 {
5871                         pElement = pContainer->GetCurrentFocusedElement();
5872                         if (pElement != null)
5873                         {
5874                                 return true;
5875                         }
5876                 }
5877
5878                 if ((itemPos.itemIndex == lastItemPos.itemIndex) && (itemPos.groupIndex == lastItemPos.groupIndex))
5879                 {
5880                         break;
5881                 }
5882
5883         } while (GetNextItemPosition(itemPos, itemPos) == true);
5884
5885         return false;
5886 }
5887
5888 void
5889 _TableViewPresenter::FireItemTouchReleasedEventDuringPressing(int groupIndex, int itemIndex)
5890 {
5891         TableViewItemTag fireItemPos;
5892         fireItemPos.groupIndex = groupIndex;
5893         fireItemPos.itemIndex = itemIndex;
5894
5895         __itemTouchReleasedEventState = TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_FIRE;
5896         _TableViewItem* pItem = FindItem(fireItemPos);
5897         if (pItem != null)
5898         {
5899                 pItem->FireItemTouchReleased();
5900         }
5901
5902         // for new item after UpdateTableView.
5903         pItem = FindItem(fireItemPos);
5904         if (pItem != null)
5905         {
5906                 if (__itemTouchReleasedEventState == TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_UPDATE_TABLE_VIEW)
5907                 {
5908                         __itemTouchReleasedEventState = TABLE_VIEW_ITEM_TOUCH_RELEASED_EVENT_NORMAL;
5909                 }
5910                 else
5911                 {
5912                         pItem->SetSelectionState(true);
5913                         pItem->FireItemTouchPressed();
5914                 }
5915         }
5916 }
5917
5918 bool
5919 _TableViewSectionStringAlignment::operator== (const _TableViewSectionStringAlignment& rhs) const
5920 {
5921         if (headerHorizontalAlignment == rhs.headerHorizontalAlignment && footerHorizontalAlignment == rhs.footerHorizontalAlignment)
5922         {
5923                 return true;
5924         }
5925
5926         return false;
5927 }
5928
5929 bool
5930 _TableViewSectionStringAlignment::operator!= (const _TableViewSectionStringAlignment& rhs) const
5931 {
5932         if (headerHorizontalAlignment == rhs.headerHorizontalAlignment && footerHorizontalAlignment == rhs.footerHorizontalAlignment)
5933         {
5934                 return false;
5935         }
5936
5937         return true;
5938 }
5939
5940 }}} // Tizen::Ui::Controls