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