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