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