Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_TokenEditPresenter.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  * @file                FUiCtrl_TokenEditPresenter.cpp
19  * @brief               This is the implementation file for the _TokenEditPresenter class.
20  */
21 #include <FGrp_BitmapImpl.h>
22 #include <FUiAnimVisualElementAnimationGroup.h>
23 #include <FGrp_TextTextSimple.h>
24 #include <FGrp_FontImpl.h>
25 #include "FUiAnim_VisualElement.h"
26 #include "FUi_UiTouchEvent.h"
27 #include "FUiCtrl_TokenEditPresenter.h"
28 #include "FUiCtrl_Scroll.h"
29 #include "FUi_Math.h"
30 #include "FUi_CoordinateSystemUtils.h"
31 #include "FUi_AccessibilityContainer.h"
32 #include "FUi_AccessibilityElement.h"
33 #include "FUiAnim_VisualElementImpl.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Media;
38 using namespace Tizen::Ui::Animations;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Graphics::_Text;
41
42 namespace Tizen { namespace Ui { namespace Controls
43 {
44
45 float
46 SineTimingFunction::CalculateProgress(float timeProgress) const
47 {
48         const float segments[3][3] = {{0.0f, 0.01f, 0.37f}, {0.37f, 0.72f, 0.888f}, {0.888f, 0.9999f, 1.0f}};
49         float timeProgressValue = timeProgress;
50         int segmentsLength = 3; //Length of the segments array
51         int index = (int) floor(segmentsLength * timeProgressValue);
52         if (index >= segmentsLength)
53         {
54                 index = segmentsLength - 1;
55         }
56
57         float progressValue = (timeProgressValue - index * (1.0 / segmentsLength)) * segmentsLength;
58         float segmentAtIndex[3];
59         for (int i = 0; i < 3; i++)
60         {
61                 segmentAtIndex[i] = segments[index][i];
62         }
63         float ret = 0 + 1 * (segmentAtIndex[0] + progressValue * (2 * (1 - progressValue) * (segmentAtIndex[1] - segmentAtIndex[0]) + progressValue * (segmentAtIndex[2] - segmentAtIndex[0])));
64
65         return ret;
66 }
67
68 class _Token
69         : public Object
70 {
71 public:
72         _Token(void);
73         result Construct(const String& text, Font* pEditFont);
74         virtual ~_Token(void);
75         float GetTextPixelWidth(void) const;
76         wchar_t* GetText(void) const;
77         result ResetToken(const String& text);
78         result SetBounds(FloatRectangle bounds);
79         _VisualElement* GetVisualElement(void) const;
80         _Token(const _Token& src);
81         _Token& operator =(const _Token& value);
82
83 public:
84         FloatRectangle displayRect;
85         TextObject* pTextObject;
86         Font* pFont;
87         int currTokenLength;
88         bool isImplicitAnimation;
89         bool isTextCut;
90 private:
91         wchar_t* __pTextBuffer;
92         float __textPixelWidth;
93         float __textPixelHeight;
94         _VisualElement* __pVisualElement;
95 };  // _Token
96
97 _Token::_Token(void)
98         : pTextObject(null)
99         , pFont(null)
100         , currTokenLength(0)
101         , isImplicitAnimation(true)
102         , isTextCut(false)
103         , __pTextBuffer(null)
104         , __textPixelWidth(0.0f)
105         , __textPixelHeight(0.0f)
106         , __pVisualElement(null)
107 {
108 }
109
110 result
111 _Token::Construct(const String& text, Font* pEditFont)
112 {
113         result r = E_SUCCESS;
114
115         currTokenLength = text.GetLength();
116         float tokenFontSize = 0.0f;
117
118         pTextObject = new (std::nothrow) TextObject;
119         SysTryReturnResult(NID_UI_CTRL, pTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
120
121         // for default initialize.
122         r = pTextObject->Construct();
123         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
124
125         r = pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
126         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
127
128         r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
129         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
130
131         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, tokenFontSize);
132
133         pFont = _FontImpl::CloneN(*pEditFont);
134         r = GetLastResult();
135         SysTryCatch(NID_UI_CTRL, pFont != null, , r, "[%s] Propagating.", GetErrorMessage(r));
136
137         (_FontImpl::GetInstance(*pFont))->SetStyle(FONT_STYLE_PLAIN);
138         (_FontImpl::GetInstance(*pFont))->SetSize(tokenFontSize);
139
140         r = pTextObject->SetFont(pFont, 0, pTextObject->GetTextLength());
141         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
142
143         r = ResetToken(text);
144         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
145
146         __pVisualElement = new (std::nothrow) _VisualElement;
147         SysTryCatch(NID_UI_CTRL, __pVisualElement != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
148
149         r = __pVisualElement->Construct();
150         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct _VisualElement.", GetErrorMessage(r));
151
152         __pVisualElement->SetImplicitAnimationEnabled(false);
153         __pVisualElement->SetOpacity(0.0f);
154
155         return r;
156
157 CATCH:
158         delete pTextObject;
159         pTextObject = null;
160
161         if (__pVisualElement)
162         {
163                 __pVisualElement->Destroy();
164                 __pVisualElement = null;
165         }
166
167         delete pFont;
168         pFont = null;
169
170         return r;
171 }
172
173 result
174 _Token::ResetToken(const String& text)
175 {
176         result r = E_SUCCESS;
177         FloatDimension textSize;
178
179         if (__pTextBuffer)
180         {
181                 delete[] __pTextBuffer;
182                 __pTextBuffer = null;
183         }
184
185         int length = text.GetLength();
186         wchar_t* pTempString = const_cast< wchar_t* >(text.GetPointer());
187         SysTryReturnResult(NID_UI_CTRL, pTempString != null, E_SYSTEM, "A system error has occurred. Token text string is null.");
188
189         __pTextBuffer = new (std::nothrow) wchar_t[(length + 1) * (sizeof(wchar_t))];
190         SysTryReturnResult(NID_UI_CTRL, __pTextBuffer != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
191
192         for (int i = 0; i < length; i++)
193         {
194                 __pTextBuffer[i] = pTempString[i];
195         }
196         __pTextBuffer[length] = 0;
197
198         pTextObject->RemoveAll(true);
199
200         TextSimple* pSimpleText = new (std::nothrow) TextSimple(__pTextBuffer, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, pFont);
201         SysTryCatch(NID_UI_CTRL, pSimpleText != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
202
203         r = pTextObject->AppendElement(*pSimpleText);
204         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
205
206         textSize = pTextObject->GetTextExtentF(0, length);
207         __textPixelWidth = textSize.width;
208         __textPixelHeight = textSize.height;
209
210         r = pTextObject->Compose();
211
212         currTokenLength = length;
213
214         return E_SUCCESS;
215
216 CATCH:
217         delete[] __pTextBuffer;
218         __pTextBuffer = null;
219
220         delete pSimpleText;
221         pSimpleText = null;
222
223         return E_SYSTEM;
224 }
225
226 result
227 _Token::SetBounds(FloatRectangle bounds)
228 {
229         result r = E_SUCCESS;
230         displayRect = bounds;
231         if (__pVisualElement)
232         {
233                 __pVisualElement->SetBounds(bounds);
234         }
235         return r;
236 }
237
238 _VisualElement*
239 _Token::GetVisualElement(void) const
240 {
241         return __pVisualElement;
242 }
243
244 _Token::~_Token(void)
245 {
246         delete pTextObject;
247         pTextObject = null;
248
249         delete pFont;
250         pFont = null;
251
252         delete[] __pTextBuffer;
253         __pTextBuffer = null;
254
255         if (__pVisualElement)
256         {
257                 __pVisualElement->Destroy();
258                 __pVisualElement = null;
259         }
260 }
261
262 float
263 _Token::GetTextPixelWidth(void) const
264 {
265         return __textPixelWidth;
266 }
267
268 wchar_t*
269 _Token::GetText(void) const
270 {
271         return __pTextBuffer;
272 }
273
274 _TokenEditPresenter::_TokenEditPresenter(void)
275         : __pTokenEdit(null)
276         , __pTokenList(null)
277         , __pTokenBgBitmap(null)
278         , __pTokenBgNormalEffectBitmap(null)
279         , __pTokenBgPressedEffectBitmap(null)
280         , __pTokenBgReplacementFocusBitmap(null)
281         , __pTokenBgFocusEffectBitmap(null)
282         , __pressedTokenIndex(-1)
283         , __isEditingToken(false)
284         , __editingTokenIndex(-1)
285         , __clientRect(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
286         , __initTextRect(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
287         , __isEditModeEnabled(true)
288         , __pDescriptionTextVisualElement(null)
289         , __pDescriptionTextTextObject(null)
290         , __isTokenEditingFinished(false)
291         , __prevScrollValue(0.0f)
292         , __scrollValue(0.0f)
293         , __isTokenScrolling(false)
294         , __isNeedToScroll(false)
295         , __maxScrollValue(0.0f)
296         , __autoShrink(false)
297         , __descriptionText(String())
298         , __isPopupVisible(false)
299         , __isLongPressed(false)
300         , __lineSpacing(0.0f)
301         , __animatingIndex(-1)
302         , __lastTokenIndex(-1)
303         , __pTimingFunction(null)
304         , __descriptionTextRectForScroll(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
305         , __previousTitleWidth(-1.0f)
306         , __isTokenEditPresenterInitialized(false)
307         , __isFocus(false)
308         , __previousCursorPosition(0)
309         , __isScrollValueChanged(false)
310         , __lineAdded(0)
311         , __isScrollValueModified(false)
312         , __isTouchMoveInProgress(false)
313         , __isTitleSliding(false)
314         , __touchPressInfo(FloatPoint(-1.0f, -1.0f))
315         , __editContentFontSize(0.0f)
316         , __trackTokenIndex(-1)
317         , __isAnimationInProgress(false)
318         , __focusOutIndex(-1)
319         , __accessibilityElements()
320         , __focusedTokenIndex(-1)
321         , __drawFocusState(false)
322         , __accessibilityFocusIn(false)
323 {
324 }
325
326 result
327 _TokenEditPresenter::InitializeDescriptionText(void)
328 {
329         result r = E_SUCCESS;
330         float descriptionTextSize = 0.0f;
331         Font* pFont = null;
332         float editFontSize = 0.0f;
333
334         __pDescriptionTextTextObject = new (std::nothrow) TextObject();
335         SysTryReturnResult(NID_UI_CTRL, __pDescriptionTextTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
336
337         // for default initialize.
338         r = __pDescriptionTextTextObject->Construct();
339         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
340
341         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
342         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
343
344         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
345         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
346
347         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
348
349         pFont = GetFont();
350         SysTryCatch(NID_UI_CTRL, pFont != null, , r, "[%s] Propagating.", GetErrorMessage(r));
351
352         editFontSize = GetTextSize();
353         (_FontImpl::GetInstance(*pFont))->SetSize(descriptionTextSize);
354
355         r = __pDescriptionTextTextObject->SetFont(pFont, 0, __pDescriptionTextTextObject->GetTextLength());
356         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
357
358         (_FontImpl::GetInstance(*pFont))->SetSize(editFontSize);
359
360         return r;
361
362 CATCH:
363
364         delete __pDescriptionTextTextObject;
365         __pDescriptionTextTextObject = null;
366
367         return r;
368 }
369
370 _TokenEditPresenter::~_TokenEditPresenter(void)
371 {
372         DisposeTokenEditPresenter();
373 }
374
375 result
376 _TokenEditPresenter::DisposeTokenEditPresenter(void)
377 {
378         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
379         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
380
381         if (pEditVisualElement && pCursorVisualElement)
382         {
383                 VisualElement* pCursorParent = pCursorVisualElement->GetParent();
384                 if (pCursorParent != pEditVisualElement)
385                 {
386                         if (pCursorParent)
387                         {
388                                 pCursorParent->DetachChild(*pCursorVisualElement);
389                                 pEditVisualElement->AttachChild(*pCursorVisualElement);
390                         }
391                 }
392         }
393
394         if (__pTokenList)
395         {
396                 __pTokenList->RemoveAll(true);
397                 delete __pTokenList;
398                 __pTokenList = null;
399         }
400
401         delete __pTokenBgBitmap;
402         __pTokenBgBitmap = null;
403
404         delete __pTokenBgNormalEffectBitmap;
405         __pTokenBgNormalEffectBitmap = null;
406
407         delete __pTokenBgPressedEffectBitmap;
408         __pTokenBgPressedEffectBitmap = null;
409
410         delete __pTokenBgReplacementFocusBitmap;
411         __pTokenBgReplacementFocusBitmap = null;
412
413         delete __pTokenBgFocusEffectBitmap;
414         __pTokenBgFocusEffectBitmap = null;
415
416         if (__pDescriptionTextVisualElement)
417         {
418                 __pDescriptionTextVisualElement->Destroy();
419                 __pDescriptionTextVisualElement = null;
420         }
421
422         delete __pDescriptionTextTextObject;
423         __pDescriptionTextTextObject = null;
424
425         if (__pTimingFunction)
426         {
427                 delete __pTimingFunction;
428                 __pTimingFunction = null;
429         }
430
431         RemoveChildAccessibilityElements();
432
433         return E_SUCCESS;
434 }
435
436 _TokenEditPresenter*
437 _TokenEditPresenter::CreateTokenEditPresenterN(void)
438 {
439         _TokenEditPresenter* pPresenter = new (std::nothrow) _TokenEditPresenter();
440         SysTryReturn(NID_UI_CTRL, pPresenter != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
441
442         return pPresenter;
443 }
444
445 result
446 _TokenEditPresenter::Initialize(const _Control& control)
447 {
448         result r = E_SUCCESS;
449
450         r = _EditPresenter::Initialize(control);
451         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
452
453         r = InitializeDescriptionText();
454         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
455
456         TextObject* pTextObject = GetTextObject();
457         if (pTextObject)
458         {
459                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
460                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
461                 pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
462                 pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
463         }
464
465         __pTokenEdit = dynamic_cast< _TokenEdit* >(GetEditView());
466         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
467
468         _TokenEditModel* pTokenEditModel = new (std::nothrow) _TokenEditModel();
469         SysTryReturnResult(NID_UI_CTRL, pTokenEditModel != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
470
471         SetModel(*pTokenEditModel);
472
473         SetKeypadEnabled(true);
474
475         // set title text default colors
476         Color titleTextNormalColor;
477         GET_COLOR_CONFIG(TOKENEDIT::TITLE_TEXT_NORMAL, titleTextNormalColor);
478         __pTokenEdit->SetTitleTextColor(EDIT_STATUS_NORMAL, titleTextNormalColor);
479
480         Color titleTextPressedColor;
481         GET_COLOR_CONFIG(TOKENEDIT::TITLE_TEXT_PRESSED, titleTextPressedColor);
482         __pTokenEdit->SetTitleTextColor(EDIT_STATUS_PRESSED, titleTextPressedColor);
483
484         Color titleTextHighlightedColor;
485         GET_COLOR_CONFIG(TOKENEDIT::TITLE_TEXT_HIGHLIGHTED, titleTextHighlightedColor);
486         __pTokenEdit->SetTitleTextColor(EDIT_STATUS_HIGHLIGHTED, titleTextHighlightedColor);
487
488         Color titleTextDisabledColor;
489         GET_COLOR_CONFIG(TOKENEDIT::TITLE_TEXT_DISABLED, titleTextDisabledColor);
490         __pTokenEdit->SetTitleTextColor(EDIT_STATUS_DISABLED, titleTextDisabledColor);
491
492         Color tokenEditBgNormal;
493         GET_COLOR_CONFIG(TOKENEDIT::EDIT_BG_NORMAL, tokenEditBgNormal);
494         __pTokenEdit->SetColor(EDIT_STATUS_NORMAL, tokenEditBgNormal);
495
496         Color tokenEditBgPressed;
497         GET_COLOR_CONFIG(TOKENEDIT::EDIT_BG_PRESSED, tokenEditBgPressed);
498         __pTokenEdit->SetColor(EDIT_STATUS_PRESSED, tokenEditBgPressed);
499
500         Color tokenEditBgHighlighted;
501         GET_COLOR_CONFIG(TOKENEDIT::EDIT_BG_HIGHLIGHTED, tokenEditBgHighlighted);
502         __pTokenEdit->SetColor(EDIT_STATUS_HIGHLIGHTED, tokenEditBgHighlighted);
503
504         Color tokenEditBgDisabled;
505         GET_COLOR_CONFIG(TOKENEDIT::EDIT_BG_DISABLED, tokenEditBgDisabled);
506         __pTokenEdit->SetColor(EDIT_STATUS_DISABLED, tokenEditBgDisabled);
507
508         Color tokenEditTextNormal;
509         GET_COLOR_CONFIG(TOKENEDIT::EDIT_TEXT_NORMAL, tokenEditTextNormal);
510         __pTokenEdit->SetTextColor(EDIT_TEXT_COLOR_NORMAL, tokenEditTextNormal);
511
512         Color tokenEditTextHighlighted;
513         GET_COLOR_CONFIG(TOKENEDIT::EDIT_TEXT_HIGHLIGHTED, tokenEditTextHighlighted);
514         __pTokenEdit->SetTextColor(EDIT_TEXT_COLOR_HIGHLIGHTED, tokenEditTextHighlighted);
515
516         Color tokenEditTextDisabled;
517         GET_COLOR_CONFIG(TOKENEDIT::EDIT_TEXT_DISABLED, tokenEditTextDisabled);
518         __pTokenEdit->SetTextColor(EDIT_TEXT_COLOR_DISABLED, tokenEditTextDisabled);
519
520         Color tokenGuideTextNormal;
521         GET_COLOR_CONFIG(TOKENEDIT::GUIDE_TEXT_NORMAL, tokenGuideTextNormal);
522         __pTokenEdit->SetGuideTextColor(tokenGuideTextNormal);
523
524         float leftMargin = 0.0f;
525         float rightMargin = 0.0f;
526         float tokenTopMargin = 0.0f;
527         float tokenBottomMargin = 0.0f;
528         float tokenHeight = 0.0f;
529         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
530         Color focusTokenColor;
531         Bitmap* pTokenBgFocusBitmap = null;
532
533         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
534         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
535         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
536         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
537         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
538         GET_COLOR_CONFIG(TOKENEDIT::BG_HIGHLIGHTED, focusTokenColor);
539
540         // For drawing token in specific area
541         __clientRect.x = leftMargin;
542         __clientRect.y = tokenTopMargin;
543         __clientRect.width = __pTokenEdit->GetBoundsF().width - leftMargin - rightMargin;
544         __clientRect.height = __pTokenEdit->GetBoundsF().height - tokenTopMargin - tokenBottomMargin;
545
546         __initTextRect = GetTextBoundsF();
547
548         FloatRectangle tempDspRect(__initTextRect.x, __initTextRect.y, __clientRect.width, tokenHeight);
549         SetTextBounds(tempDspRect);
550
551         float textSize = 0.0f;
552         GET_SHAPE_CONFIG(TOKENEDIT::TEXT_SIZE, orientation, textSize);
553
554         __editContentFontSize = textSize;
555
556         _EditPresenter::SetTextSize(__editContentFontSize);
557
558         __pTokenList = new (std::nothrow) Collection::LinkedList();
559         SysTryCatch(NID_UI_CTRL, __pTokenList != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
560
561         __delimiter = L"\n";
562
563         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgBitmap);
564         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
565
566         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_NORMAL_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgNormalEffectBitmap);
567         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
568
569         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_PRESSED_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgPressedEffectBitmap);
570         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
571
572         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_FOCUS, BITMAP_PIXEL_FORMAT_ARGB8888, pTokenBgFocusBitmap);
573         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
574
575         __pTokenBgReplacementFocusBitmap = _BitmapImpl::GetColorReplacedBitmapN(
576                     *pTokenBgFocusBitmap,Color::GetColor(COLOR_ID_MAGENTA), focusTokenColor);
577         SysTryCatch(NID_UI_CTRL, __pTokenBgReplacementFocusBitmap != null, , r, "[%s] Propagating.", GetErrorMessage(r));
578
579         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_FOCUS_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgFocusEffectBitmap);
580         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
581
582         delete pTokenBgFocusBitmap;
583         pTokenBgFocusBitmap = null;
584
585
586         __isTokenEditPresenterInitialized = true;
587
588         __previousCursorPosition = GetCursorPosition();
589
590         return r;
591
592 CATCH:
593
594         delete pTokenEditModel;
595         pTokenEditModel = null;
596
597         delete __pTokenBgBitmap;
598         __pTokenBgBitmap = null;
599
600         delete __pTokenBgNormalEffectBitmap;
601         __pTokenBgNormalEffectBitmap = null;
602
603         delete __pTokenBgPressedEffectBitmap;
604         __pTokenBgPressedEffectBitmap = null;
605
606         delete pTokenBgFocusBitmap;
607         pTokenBgFocusBitmap = null;
608
609         delete __pTokenBgReplacementFocusBitmap;
610         __pTokenBgReplacementFocusBitmap = null;
611
612         return r;
613 }
614
615 void
616 _TokenEditPresenter::DrawText(void)
617 {
618         bool isCustomBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_NORMAL);
619         //Checking IsBlocked() is additional check for handler movement in token edit mode
620         if ((__isEditingToken) && (__editingTokenIndex >= 0))
621         {
622                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
623                 if (pToken)
624                 {
625                         _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
626                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element of token.");
627
628                         Canvas* pTokenCanvas = pTokenVisualElement->GetCanvasN();
629                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get canvas of the token.");
630
631                         FloatRectangle tokenRect(0.0f, 0.0f, pToken->displayRect.width, pToken->displayRect.height);
632                         pTokenCanvas->SetBackgroundColor(Color(0));
633                         pTokenCanvas->Clear();
634
635                         Color selectedTokenColor = GetTokenEditColor(TOKEN_EDIT_STATUS_SELECTED);
636                         Bitmap* pReplacementColorBackgroundBitmap = null;
637                         if (__pTokenBgBitmap)
638                         {
639                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), selectedTokenColor);
640
641                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
642                                 {
643                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
644                                 }
645                                 else
646                                 {
647                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
648                                 }
649                         }
650                         else
651                         {
652                                 pTokenCanvas->FillRectangle(selectedTokenColor, tokenRect);
653                         }
654
655                         if (__pTokenBgPressedEffectBitmap && (!isCustomBitmap))
656                         {
657                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgPressedEffectBitmap))
658                                 {
659                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
660                                 }
661                                 else
662                                 {
663                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
664                                 }
665                         }
666
667                         _EditPresenter::DrawText(*pTokenCanvas);
668
669                         delete pTokenCanvas;
670                         pTokenCanvas = null;
671
672                         delete pReplacementColorBackgroundBitmap;
673                 }
674         }
675         else
676         {
677                 _VisualElement* pTextVisualElement = null;
678                 pTextVisualElement = GetTextVisualElement();
679                 if (pTextVisualElement)
680                 {
681                         _EditPresenter::DrawText();
682                 }
683                 else
684                 {
685                         Canvas* pCanvas = __pTokenEdit->GetCanvasN();
686                         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element of Control.");
687
688                         _EditPresenter::DrawText(*pCanvas);
689
690                         delete pCanvas;
691                         pCanvas = null;
692                 }
693         }
694 }
695
696 result
697 _TokenEditPresenter::Draw(Canvas& canvas)
698 {
699         if (!IsInitialized())
700         {
701                 InitializeAtFirstDrawing();
702
703                 if (IsFocused() == true)
704                 {
705                         ShowKeypad(false);
706                 }
707         }
708
709         canvas.SetBackgroundColor(Color(0, 0, 0, 0));
710         canvas.Clear();
711
712         DrawBackground(canvas);
713
714         DrawScrollBar();
715
716         if (__pDescriptionTextTextObject->GetTextLength() != 0)
717         {
718                 DrawDescriptionText();
719         }
720
721         if (GetTokenCount() != 0)
722         {
723                 DrawToken();
724         }
725
726         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
727         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
728
729         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
730         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
731
732         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
733
734         if (pToken)
735         {
736                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
737                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get visual element of token.");
738
739                 if (__isEditingToken)
740                 {
741                         Canvas* pTokenCanvas = pTokenVisualElement->GetCanvasN();
742                         SysTryReturnResult(NID_UI_CTRL, pTokenCanvas != null, E_SYSTEM, "A system error has occurred. Failed to get canvas of the token.");
743
744                         _EditPresenter::DrawText(*pTokenCanvas);
745
746                         InitializeCursor();
747
748                         delete pTokenCanvas;
749                         pTokenCanvas = null;
750                 }
751                 else
752                 {
753                         DrawText();
754                         InitializeCursor();
755                 }
756         }
757         else
758         {
759                 SysTryReturnResult(NID_UI_CTRL, !__isEditingToken, E_SYSTEM, "An invalid argument is given.");
760
761                 DrawText();
762                 InitializeCursor();
763         }
764
765         if (__isTokenEditingFinished)
766         {
767                 __isEditingToken = false;
768                 __editingTokenIndex = -1;
769                 _EditPresenter::SetTextSize(__editContentFontSize);
770
771                 __isTokenEditingFinished = false;
772         }
773
774         return E_SUCCESS;
775 }
776
777 bool
778 _TokenEditPresenter::DrawToken(int count)
779 {
780         int drawStartIndex = 0;
781         int tokenCount = 0;
782         float tokenTextLeftMargin = 0.0f;
783         float tokenVerticalSpacing = 0.0f;
784         bool isCustomBitmap = false;
785         bool isCustomFocusBitmap = false;
786
787         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
788
789         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
790
791         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
792         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
793
794         isCustomBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_NORMAL);
795         isCustomFocusBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_FOCUS);
796
797
798         if (__drawFocusState)
799         {
800                 __pTokenEdit->RefreshFocusUi();
801         }
802
803         if (count == -1)
804         {
805                 tokenCount = __pTokenList->GetCount();
806         }
807         else
808         {
809                 tokenCount = count;
810         }
811
812         Color normalTokenColor;
813         Color selectedTokenColor;
814         Color disabledTokenColor;
815
816         for (int i = drawStartIndex; i < tokenCount; i++)
817         {
818                 Bitmap* pReplacementColorBackgroundBitmap = null;
819
820                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
821                 if (pToken == null || pToken->pTextObject == null)
822                 {
823                         SysLog(NID_UI_CTRL, "[E_SYSTEM] The _Token instance is null");
824                         break;
825                 }
826
827                 normalTokenColor = GetTokenEditColor(TOKEN_EDIT_STATUS_NORMAL);
828                 selectedTokenColor = GetTokenEditColor(TOKEN_EDIT_STATUS_SELECTED);
829                 disabledTokenColor = GetTokenEditColor(TOKEN_EDIT_STATUS_DISABLED);
830
831                 _VisualElement* pTokenElement = pToken->GetVisualElement();
832                 if (pTokenElement == null)
833                 {
834                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The _VisualElement instance is null");
835                         break;
836                 }
837
838                 bool isSelected = false;
839                 Canvas* pTokenCanvas = pTokenElement->GetCanvasN();
840                 if (pTokenCanvas == null)
841                 {
842                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The Canvas instance is null");
843                         break;
844                 }
845                 FloatRectangle tokenRect(0.0f, 0.0f, pToken->displayRect.width, pToken->displayRect.height);
846                 pTokenCanvas->SetBackgroundColor(Color(0));
847                 pTokenCanvas->Clear();
848
849                 if (__pressedTokenIndex == i && IsFocused())
850                 {
851                         if (__pTokenBgBitmap)
852                         {
853                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), selectedTokenColor);
854
855                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
856                                 {
857                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
858                                 }
859                                 else
860                                 {
861                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
862                                 }
863                         }
864                         else
865                         {
866                                 pTokenCanvas->FillRectangle(selectedTokenColor, tokenRect);
867                         }
868
869
870                         if (__pTokenBgPressedEffectBitmap && (!isCustomBitmap))
871                         {
872                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgPressedEffectBitmap))
873                                 {
874                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
875                                 }
876                                 else
877                                 {
878                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
879                                 }
880                         }
881
882                         isSelected = true;
883                 }
884                 else
885                 {
886                         Color tokenBgColor = normalTokenColor;
887                         if (GetCurrentStatus() == EDIT_STATUS_DISABLED)
888                         {
889                                 tokenBgColor = disabledTokenColor;
890                         }
891
892                         if (__pTokenBgBitmap)
893                         {
894                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), tokenBgColor);
895
896                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
897                                 {
898                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
899                                 }
900                                 else
901                                 {
902                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
903                                 }
904                         }
905                         else
906                         {
907                                 pTokenCanvas->FillRectangle(tokenBgColor, tokenRect);
908                         }
909
910
911                         if (__pTokenBgNormalEffectBitmap && (!isCustomBitmap))
912                         {
913                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgNormalEffectBitmap))
914                                 {
915                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgNormalEffectBitmap);
916                                 }
917                                 else
918                                 {
919                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgNormalEffectBitmap);
920                                 }
921                         }
922                 }
923
924                 if (__focusedTokenIndex == i && __drawFocusState && (!__isEditingToken))
925                 {
926                         if (__pTokenBgReplacementFocusBitmap)
927                         {
928                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgReplacementFocusBitmap))
929                                 {
930                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgReplacementFocusBitmap);
931                                 }
932                                 else
933                                 {
934                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgReplacementFocusBitmap);
935                                 }
936                         }
937
938                         if (__pTokenBgFocusEffectBitmap && (!isCustomFocusBitmap))
939                         {
940                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgFocusEffectBitmap))
941                                 {
942                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgFocusEffectBitmap);
943                                 }
944                                 else
945                                 {
946                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgFocusEffectBitmap);
947                                 }
948                         }
949
950                 }
951
952                 pTokenElement->SetAnimationProvider(null);
953                 if (pToken->isImplicitAnimation)
954                 {
955                         VisualElementAnimation* pAnimation = CreateAnimationN(*pTokenElement);
956                         pTokenElement->AddAnimation(*pAnimation);
957                         delete pAnimation;
958                         pToken->isImplicitAnimation = false;
959                 }
960
961                 if (!__isEditingToken || __pressedTokenIndex != i)
962                 {
963                         Color textColor;
964                         FloatRectangle textRect(0.0f, 0.0f, pToken->displayRect.width - ((tokenTextLeftMargin * 2.0f)), pToken->displayRect.height - tokenVerticalSpacing);
965
966                         textRect.x += tokenTextLeftMargin;
967                         textRect.y += (tokenVerticalSpacing / 2.0f);
968
969                         if (isSelected)
970                         {
971                                 textColor = GetTokenEditTextColor(TOKEN_EDIT_STATUS_SELECTED);
972                         }
973                         else
974                         {
975                                 textColor = GetTokenEditTextColor(TOKEN_EDIT_STATUS_NORMAL);
976                         }
977
978                         pToken->pTextObject->SetForegroundColor(textColor, 0, pToken->pTextObject->GetTextLength());
979                         pToken->pTextObject->SetBounds(textRect);
980                         pToken->pTextObject->Compose();
981                         pToken->pTextObject->Draw(*_CanvasImpl::GetInstance(*pTokenCanvas));
982                 }
983
984                 pTokenElement->SetFlushNeeded();
985                 delete pTokenCanvas;
986
987                 delete pReplacementColorBackgroundBitmap;
988                 pReplacementColorBackgroundBitmap = null;
989         }
990         return true;
991 }
992
993 Color
994 _TokenEditPresenter::GetTokenEditColor(const TokenEditStatus status) const
995 {
996         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
997
998         return __pTokenEdit->GetTokenColor(status);
999 }
1000
1001 Color
1002 _TokenEditPresenter::GetTokenEditTextColor(const TokenEditStatus status) const
1003 {
1004         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
1005
1006         Color color;
1007
1008         if (status == TOKEN_EDIT_STATUS_NORMAL)
1009         {
1010                 color = __pTokenEdit->GetTokenTextColor();
1011         }
1012         else
1013         {
1014                 color = __pTokenEdit->GetSelectedTokenTextColor();
1015         }
1016
1017         return color;
1018 }
1019
1020 result
1021 _TokenEditPresenter::MakeToken(const String& tokenString)
1022 {
1023         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_INVALID_STATE, "_TokenEdit is in an invalid state.");
1024         result r = E_SUCCESS;
1025
1026         int tokenCount = 0;
1027         int index = -1;
1028         int tokenLength = tokenString.GetLength();
1029         SysTryReturnResult(NID_UI_CTRL, tokenLength, E_INVALID_ARG, "Invalid argument is used. Token length is (%d)", tokenLength);
1030
1031         String inputTokenString = tokenString;
1032         String replacementString = inputTokenString;
1033         bool enable = false;
1034
1035         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
1036
1037         if (enable)
1038         {
1039                 inputTokenString = replacementString;
1040         }
1041
1042         if (inputTokenString.GetLength() <= 0)
1043         {
1044                 r = ClearText();
1045                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Failed to clear text object.", GetErrorMessage(r));
1046
1047                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
1048                 return E_INVALID_ARG;
1049         }
1050
1051         _Token* pToken = new (std::nothrow) _Token();
1052         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1053
1054         r = pToken->Construct(inputTokenString, GetFont());
1055         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct token.", GetErrorMessage(r));
1056
1057         r = __pTokenList->Add(static_cast< Object& >(*pToken));
1058         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1059
1060         tokenCount = __pTokenList->GetCount();
1061         index = tokenCount - 1;
1062
1063         r = CalculateTokenPositionFromIndex(index);
1064         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
1065
1066         r = TrimTokenAndAdjustEllipsisAt(index);
1067         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
1068
1069         r = InitializeTokenVisibilityAt(index);
1070         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
1071
1072         r = SetInitialBounds();
1073         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1074
1075         r = ClearText();
1076         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to clear text object.", GetErrorMessage(r));
1077
1078         r = AdjustFlexibleHeight();
1079         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
1080
1081         r = CheckTokenScrolling();
1082         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
1083
1084         AppendTokenAccessibilityElement();
1085         UpdateTokenAccessibilityBounds();
1086
1087         return r;
1088
1089 CATCH:
1090         __pTokenList->Remove(*pToken);
1091
1092         delete pToken;
1093         pToken = null;
1094
1095         return r;
1096 }
1097
1098 result
1099 _TokenEditPresenter::MakeToken(void)
1100 {
1101         result r = E_SUCCESS;
1102
1103         String tempToken(GetText());
1104         r = MakeToken(tempToken);
1105
1106         return r;
1107 }
1108
1109 result
1110 _TokenEditPresenter::AppendToken(const Tizen::Base::String& token)
1111 {
1112         result r = E_SUCCESS;
1113
1114         r = MakeToken(token);
1115         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1116
1117         if (__editingTokenIndex >= 0)
1118         {
1119                 SetEditingTokenTextBounds(__editingTokenIndex);
1120                 _EditPresenter::SetCursorPosition(__previousCursorPosition);
1121         }
1122
1123         return r;
1124 }
1125
1126 result
1127 _TokenEditPresenter::InsertTokenAt(int index, const String& token, bool isUser)
1128 {
1129         SysTryReturnResult(NID_UI_CTRL, index >= 0, E_INVALID_ARG, "Invalid argument is used. index is (%d).", index);
1130         SysTryReturnResult(NID_UI_CTRL, token.GetLength() > 0, E_INVALID_ARG, "Invalid argument is used. Token length is (%d).", token.GetLength());
1131
1132         result r = E_SUCCESS;
1133
1134         _Token* pToken = new (std::nothrow) _Token();
1135         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1136
1137         r = pToken->Construct(token, GetFont());
1138         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct token.", GetErrorMessage(r));
1139
1140         r = __pTokenList->InsertAt(*pToken, index);
1141         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to insert token.", GetErrorMessage(r));
1142
1143         r = ClearText();
1144
1145         r = CalculateTokenPositionFromIndex(index);
1146         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
1147
1148         r = TrimTokenAndAdjustEllipsisAt(index);
1149         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
1150
1151         r = InitializeTokenVisibilityAt(index);
1152         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
1153
1154         r = SetInitialBounds();
1155         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1156
1157         r = AdjustFlexibleHeight();
1158         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
1159
1160         r = CheckTokenScrolling();
1161         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
1162
1163         for (int i = 0; i < GetTokenCount(); i++)
1164         {
1165                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
1166                 SysTryCatch(NID_UI_CTRL, pToken != null, , r = E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1167
1168                 r = pToken->SetBounds(pToken->displayRect);
1169                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds", GetErrorMessage(r));
1170         }
1171
1172         InsertTokenAccessibilityElementAt(index);
1173         UpdateTokenAccessibilityBounds();
1174
1175         if (isUser)
1176         {
1177                 if (__editingTokenIndex >= 0)
1178                 {
1179                         if (index <= __editingTokenIndex)
1180                         {
1181                                 __editingTokenIndex++;
1182                         }
1183                         __pressedTokenIndex = __editingTokenIndex;
1184
1185                         SetEditingTokenTextBounds(__editingTokenIndex);
1186                         _EditPresenter::SetCursorPosition(__previousCursorPosition);
1187                 }
1188                 else if (__pressedTokenIndex >= index)
1189                 {
1190                         __pressedTokenIndex++;
1191                 }
1192                 else if ((__focusedTokenIndex >= index) && __drawFocusState)
1193                 {
1194                         __focusedTokenIndex++;
1195                 }
1196         }
1197
1198         return r;
1199
1200 CATCH:
1201         __pTokenList->Remove(*pToken);
1202
1203         delete pToken;
1204         pToken = null;
1205
1206         return r;
1207 }
1208
1209 String
1210 _TokenEditPresenter::GetTokenAt(int index) const
1211 {
1212         String tempString;
1213         SysTryReturn(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), tempString, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] index (%d) is out of range.", index);
1214
1215         _Token* pToken = null;
1216         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
1217         if (pToken)
1218         {
1219                 tempString = pToken->GetText();
1220         }
1221
1222         return tempString;
1223 }
1224
1225 int
1226 _TokenEditPresenter::GetTokenCount(bool isInvokedByApp) const
1227 {
1228         if (!isInvokedByApp)
1229         {
1230                 return __pTokenList->GetCount();
1231         }
1232         else
1233         {
1234                 if (__isAnimationInProgress)
1235                 {
1236                         return __pTokenList->GetCount() - 1;
1237                 }
1238                 else
1239                 {
1240                         return __pTokenList->GetCount();
1241                 }
1242         }
1243 }
1244
1245 int
1246 _TokenEditPresenter::GetSelectedTokenIndex(void) const
1247 {
1248         if (__drawFocusState)
1249         {
1250                 return __focusedTokenIndex;
1251         }
1252
1253         return __pressedTokenIndex;
1254 }
1255
1256 bool
1257 _TokenEditPresenter::IsTokenEditModeEnabled(void) const
1258 {
1259         return __isEditModeEnabled;
1260 }
1261
1262 result
1263 _TokenEditPresenter::RemoveTokenAt(int index, bool isClearText)
1264 {
1265         result r = E_SUCCESS;
1266         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.", index);
1267
1268         if (index == __editingTokenIndex && isClearText)
1269         {
1270                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
1271                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
1272
1273                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
1274                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
1275
1276                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
1277                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1278
1279                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
1280                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1281
1282                 __editingTokenIndex = -1;
1283                 __isEditingToken = false;
1284                 __pressedTokenIndex = -1;
1285                 __isTokenEditingFinished = true;
1286
1287                 r = ClearText();
1288                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Failed to clear text object.");
1289
1290                 _EditPresenter::SetTextSize(__editContentFontSize);
1291         }
1292
1293         r = __pTokenList->RemoveAt(index, true);
1294         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to remove token.");
1295
1296         r = CalculateTokenPositionFromIndex(index);
1297         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to calculate token position.");
1298
1299         r = SetInitialBounds();
1300         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
1301
1302         _Token* pToken = null;
1303
1304         for (int i = 0; i < GetTokenCount(); i++)
1305         {
1306                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
1307                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1308
1309                 r = pToken->SetBounds(pToken->displayRect);
1310                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds");
1311         }
1312
1313         r = AdjustFlexibleHeight();
1314         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process to resize.");
1315
1316         r = CheckTokenScrolling();
1317         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process scroll.");
1318
1319         RemoveTokenAccessibilityElementAt(index);
1320         UpdateTokenAccessibilityBounds();
1321
1322         if (isClearText)
1323         {
1324                 if (index > __editingTokenIndex)
1325                 {
1326                         SetEditingTokenTextBounds(__editingTokenIndex);
1327                         _EditPresenter::SetCursorPosition(__previousCursorPosition);
1328                 }
1329                 if (index < __editingTokenIndex)
1330                 {
1331                         if (__editingTokenIndex > 0)
1332                         {
1333                                 __editingTokenIndex--;
1334                                 __pressedTokenIndex = __editingTokenIndex;
1335
1336                                 SetEditingTokenTextBounds(__editingTokenIndex);
1337                                 _EditPresenter::SetCursorPosition(__previousCursorPosition);
1338                         }
1339                 }
1340                 else if (index == __pressedTokenIndex)
1341                 {
1342                         __pressedTokenIndex = -1;
1343                         StopCursorTimer();
1344                         SetCursorDisabled(false);
1345                         StartCursorTimer();
1346                 }
1347                 else if (index >= 0 && index < __pressedTokenIndex)
1348                 {
1349                         __pressedTokenIndex--;
1350                 }
1351
1352                 if (__drawFocusState)
1353                 {
1354                         if (index == __focusedTokenIndex)
1355                         {
1356                                 __focusedTokenIndex = -1;
1357                                 StopCursorTimer();
1358                                 SetCursorDisabled(false);
1359                                 StartCursorTimer();
1360                         }
1361                         else if(index >= 0 && index < __focusedTokenIndex)
1362                         {
1363                                 __focusedTokenIndex--;
1364                         }
1365                 }
1366         }
1367         else if (index == __pressedTokenIndex)
1368         {
1369                 __pressedTokenIndex = -1;
1370                 StopCursorTimer();
1371                 SetCursorDisabled(false);
1372                 StartCursorTimer();
1373         }
1374         else if ((index == __focusedTokenIndex) && __drawFocusState)
1375         {
1376                 __focusedTokenIndex = -1;
1377                 StopCursorTimer();
1378                 SetCursorDisabled(false);
1379                 StartCursorTimer();
1380         }
1381
1382         return r;
1383 }
1384
1385 result
1386 _TokenEditPresenter::SetTokenSelected(int index, bool selected)
1387 {
1388         result r = E_SUCCESS;
1389
1390         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.");
1391
1392         if (selected == false)
1393         {
1394                 __pressedTokenIndex = -1;
1395         }
1396         else
1397         {
1398                 __pressedTokenIndex = index;
1399         }
1400
1401         return r;
1402 }
1403
1404 result
1405 _TokenEditPresenter::SetTokenEditModeEnabled(bool enable)
1406 {
1407         result r = E_SUCCESS;
1408
1409         __isEditModeEnabled = enable;
1410
1411         return r;
1412 }
1413
1414 result
1415 _TokenEditPresenter::CalculateTokenPositionFromIndex(int startIndex, bool leftWard)
1416 {
1417         result r = E_SUCCESS;
1418
1419         int tokenCount = __pTokenList->GetCount();
1420
1421         float leftMargin = 0.0f;
1422         float rightMargin = 0.0f;
1423         float tokenTopMargin = 0.0f;
1424         float tokenBottomMargin = 0.0f;
1425         float tokenHeight = 0.0f;
1426         float tokenVerticalSpacing = 0.0f;
1427         float tokenHorizontalSpacing = 0.0f;
1428         float tokenTextLeftMargin = 0.0f;
1429         float tokenTextRightMargin = 0.0f;
1430         float descriptionTextRightMargin = 0.0f;
1431         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1432
1433         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1434         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
1435         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1436         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
1437         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1438         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1439         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1440         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1441         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1442         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_RIGHT_MARGIN, orientation, descriptionTextRightMargin);
1443
1444         SysTryReturn(NID_UI_CTRL, startIndex >= 0 && startIndex <= tokenCount, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. startIndex = (%d)", startIndex);
1445
1446         _Token* pToken = null;
1447         _Token* pPreviousToken = null;
1448
1449         int index = startIndex;
1450
1451         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
1452         String titleText = __pTokenEdit->GetTitleText();
1453
1454         if (!_FloatCompare(GetDescriptionTextRect().width, __previousTitleWidth))
1455         {
1456                 __descriptionTextRectForScroll = GetDescriptionTextRect();
1457                 __previousTitleWidth = GetDescriptionTextRect().width;
1458         }
1459
1460         bool findPrevTokenLoopFlag = true;
1461         for (; index < tokenCount; index++)
1462         {
1463                 pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
1464                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1465
1466                 if (index == 0)
1467                 {
1468                         // TODO : description text (title style inner)
1469                         if (titleText.GetLength())
1470                         {
1471                                 pToken->displayRect.x = __descriptionTextRectForScroll.x + __descriptionTextRectForScroll.width + descriptionTextRightMargin;
1472                                 pToken->displayRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1473                         }
1474                         else    // Set description text.
1475                         {
1476                                 pToken->displayRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1477                                 pToken->displayRect.y = tokenTopMargin + __scrollValue + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN) + __lineSpacing;
1478
1479                         }
1480                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1481
1482                 }
1483                 else
1484                 {
1485                         if (findPrevTokenLoopFlag)
1486                         {
1487                                 pPreviousToken = static_cast< _Token* >(__pTokenList->GetAt(index - 1));
1488                                 r = GetLastResult();
1489                                 SysTryReturnResult(NID_UI_CTRL, pPreviousToken != null, r, "Propagating.");
1490
1491                                 findPrevTokenLoopFlag = false;
1492                         }
1493
1494                         float tempTextWidth = tokenEditBounds.width - pPreviousToken->displayRect.x - pPreviousToken->displayRect.width - tokenHorizontalSpacing - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1495                         if (tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin > tempTextWidth)       // Line change
1496                         {
1497                                 pToken->displayRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1498                                 pToken->displayRect.y = pPreviousToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing;
1499                                 __lineAdded++;
1500                         }
1501                         else
1502                         {
1503                                 pToken->displayRect.x = pPreviousToken->displayRect.x + pPreviousToken->displayRect.width +
1504                                                                                 tokenHorizontalSpacing;
1505                                 pToken->displayRect.y = pPreviousToken->displayRect.y;
1506                         }
1507
1508                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1509                 }
1510
1511                 pToken->displayRect.height = tokenHeight;
1512
1513                 pPreviousToken = pToken;
1514         }
1515
1516         for (int i = 0; i < __pTokenList->GetCount(); i++)
1517         {
1518                 TrimTokenAndAdjustEllipsisAt(i);
1519                 InitializeTokenVisibilityAt(i);
1520         }
1521         return r;
1522 }
1523
1524 result
1525 _TokenEditPresenter::InitializeTokenVisibilityAt(int ndex)
1526 {
1527         result r = E_SUCCESS;
1528
1529         _Token* pToken = null;
1530         pToken = static_cast< _Token* >(__pTokenList->GetAt(ndex));
1531         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1532
1533         r = pToken->SetBounds(pToken->displayRect);
1534         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to set bounds", GetErrorMessage(r));
1535
1536         _VisualElement* pRootElement = __pTokenEdit->GetVisualElement();
1537         SysTryReturn(NID_UI_CTRL, pRootElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1538
1539         _VisualElement* pVisualElement = pToken->GetVisualElement();
1540         SysTryReturn(NID_UI_CTRL, pVisualElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element.");
1541
1542         pVisualElement->SetShowState(true);
1543
1544         r = pRootElement->AttachChild(*pVisualElement);
1545         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to add child", GetErrorMessage(r));
1546
1547         return r;
1548 }
1549
1550 float
1551 _TokenEditPresenter::GetMaxTextHeight(void)
1552 {
1553         Font* pFont = null;
1554         float maxHeight = __editContentFontSize;
1555
1556         pFont = GetFont();
1557         SysTryReturn(NID_UI_CTRL, pFont != null, maxHeight, E_SYSTEM, "[E_SYSTEM] Failed to get Font instance.");
1558
1559         maxHeight = pFont->GetMaxHeightF();
1560
1561         return maxHeight;
1562 }
1563
1564 void
1565 _TokenEditPresenter::ResetTextBounds(void)
1566 {
1567         if (!__isEditingToken)
1568         {
1569                 result r = SetInitialBounds();
1570                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has occured. Failed to set margin.");
1571         }
1572         return;
1573 }
1574
1575 result
1576 _TokenEditPresenter::SetInitialBounds(void)
1577 {
1578         result r = E_SUCCESS;
1579
1580         float leftMargin = 0.0f;
1581         float rightMargin = 0.0f;
1582         float tokenTopMargin = 0.0f;
1583         float tokenHeight = 0.0f;
1584         float tokenMinWidth = 0.0f;
1585         float tokenVerticalSpacing = 0.0f;
1586         float tokenHorizontalSpacing = 0.0f;
1587         float tokenTextLeftMargin = 0.0f;
1588         float tokenTextRightMargin = 0.0f;
1589         float textBoundsAlignValue = 0.0f;
1590         float descriptionTextRightMargin = 0.0f;
1591         _ControlOrientation orientation = GetEditView()->GetOrientation();
1592
1593         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1594         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
1595         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1596         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1597         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinWidth);
1598         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1599         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1600         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1601         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1602         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_RIGHT_MARGIN, orientation, descriptionTextRightMargin);
1603
1604         float textObjectMaxHeight = GetMaxTextHeight();
1605         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
1606
1607         if (__pTokenList)
1608         {
1609                 int tokenCount = __pTokenList->GetCount();
1610
1611                 FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
1612                 FloatRectangle tokenTextRect = tokenEditBounds;
1613
1614                 if (tokenCount == 0)
1615                 {
1616                         if (__pTokenEdit->GetTitleText().GetLength())
1617                         {
1618                                 FloatRectangle descriptionTextRect = GetDescriptionTextRect();
1619                                 tokenTextRect.x = descriptionTextRect.x + descriptionTextRect.width + descriptionTextRightMargin;
1620                         }
1621                         else
1622                         {
1623                                 tokenTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1624                         }
1625
1626                         tokenTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN) + textBoundsAlignValue;
1627                         tokenTextRect.width -= tokenTextRect.x + rightMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1628                         tokenTextRect.height = textObjectMaxHeight;
1629
1630                         SetTextBounds(tokenTextRect);
1631                         //set cursor bounds with tokenTextRect
1632                         __pTokenEdit->SetCursorAccessibilityBounds(tokenTextRect);
1633                         return r;
1634                 }
1635
1636                 _Token* pToken = null;
1637                 // SetTextBounds from last token
1638                 pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
1639                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1640
1641                 float tempTextRectWidth = 0.0f;
1642                 tempTextRectWidth = tokenEditBounds.width - pToken->displayRect.x - pToken->displayRect.width - tokenHorizontalSpacing - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1643                 if (tokenMinWidth > tempTextRectWidth)        // Line change
1644                 {
1645                         tokenTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1646                         tokenTextRect.y = pToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing + textBoundsAlignValue;
1647                 }
1648                 else
1649                 {
1650                         tokenTextRect.x = pToken->displayRect.x + pToken->displayRect.width + tokenHorizontalSpacing;
1651                         tokenTextRect.y = pToken->displayRect.y + textBoundsAlignValue;
1652                 }
1653
1654                 tokenTextRect.width -= tokenTextRect.x + rightMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1655                 tokenTextRect.height = textObjectMaxHeight;
1656
1657                 SetTextBounds(tokenTextRect);
1658                 if (__pressedTokenIndex < 0) // Set cursor as global focused element if no token is selected
1659                 {
1660                         __pTokenEdit->SetCursorAccessibilityBounds(tokenTextRect);
1661                 }
1662         }
1663         else
1664         {
1665                 r = _EditPresenter::SetInitialBounds();
1666         }
1667
1668         return r;
1669 }
1670
1671 result
1672 _TokenEditPresenter::SetDescriptionTextRect(const FloatRectangle& rect)
1673 {
1674         result r = E_SUCCESS;
1675
1676         __descriptionTextRect = rect;
1677
1678         return r;
1679 }
1680
1681 FloatRectangle
1682 _TokenEditPresenter::GetDescriptionTextRect() const
1683 {
1684         return __descriptionTextRect;
1685 }
1686
1687 Rectangle
1688 _TokenEditPresenter::GetTextBounds(void) const
1689 {
1690         if ((__isPopupVisible == true || __isLongPressed == true) && __pressedTokenIndex >= 0)
1691         {
1692                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
1693                 SysTryReturn(NID_UI_CTRL, pToken, Rectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Unable to get valid token.");
1694
1695                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1696                 float tokenTextLeftMargin = 0.0f;
1697                 float tokenTextVerticalMargin = 0.0f;
1698                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1699                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenTextVerticalMargin);
1700
1701                 FloatRectangle textBoundsF(pToken->displayRect.x + tokenTextLeftMargin, pToken->displayRect.y + (tokenTextVerticalMargin / 2.0f), pToken->displayRect.width - (tokenTextLeftMargin * 2.0f), pToken->displayRect.height - tokenTextVerticalMargin);
1702                 Rectangle textBounds = _CoordinateSystemUtils::ConvertToInteger(textBoundsF);
1703
1704                 return textBounds;
1705         }
1706         else
1707         {
1708                 return _EditPresenter::GetTextBounds();
1709         }
1710 }
1711
1712 FloatRectangle
1713 _TokenEditPresenter::GetTextBoundsF(void) const
1714 {
1715         if ((__isPopupVisible == true || __isLongPressed == true) && __pressedTokenIndex >= 0)
1716         {
1717                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
1718                 SysTryReturn(NID_UI_CTRL, pToken, FloatRectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Unable to get valid token.");
1719
1720                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1721                 int tokenTextLeftMargin = 0;
1722                 int tokenTextVerticalMargin = 0;
1723                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1724                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenTextVerticalMargin);
1725
1726                 FloatRectangle textBounds(pToken->displayRect.x + tokenTextLeftMargin, pToken->displayRect.y + (tokenTextVerticalMargin / 2.0f), pToken->displayRect.width - (tokenTextLeftMargin * 2.0f), pToken->displayRect.height - tokenTextVerticalMargin);
1727
1728                 return textBounds;
1729         }
1730         else
1731         {
1732                 return _EditPresenter::GetTextBoundsF();
1733         }
1734 }
1735
1736 result
1737 _TokenEditPresenter::CutText(void)
1738 {
1739         if (__isEditingToken)
1740         {
1741                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
1742                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1743
1744                 pToken->isTextCut = true;
1745         }
1746         return _EditPresenter::CutText();
1747 }
1748
1749 result
1750 _TokenEditPresenter::SetLineSpacing(int linePixelGap)
1751 {
1752         __lineSpacing = linePixelGap;
1753
1754         return E_SUCCESS;
1755 }
1756
1757 result
1758 _TokenEditPresenter::SetLineSpacing(float linePixelGap)
1759 {
1760         __lineSpacing = linePixelGap;
1761
1762         return E_SUCCESS;
1763 }
1764
1765 int
1766 _TokenEditPresenter::GetLineSpacing(void) const
1767 {
1768         int lineSpacing = _CoordinateSystemUtils::ConvertToInteger(__lineSpacing);
1769         return lineSpacing;
1770 }
1771
1772 float
1773 _TokenEditPresenter::GetLineSpacingF(void) const
1774 {
1775         return __lineSpacing;
1776 }
1777
1778 VisualElementAnimation*
1779 _TokenEditPresenter::CreateAnimationN(VisualElement& source)
1780 {
1781         VisualElementAnimation* pAnimation = null;
1782         VisualElementAnimationGroup* pAnimationGroup = new (std::nothrow) VisualElementAnimationGroup();
1783         SysTryReturn(NID_UI_CTRL, pAnimationGroup, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1784
1785         pAnimationGroup->SetDuration(ANIMATION_DURATION_BOUNDS);
1786         if (__pTimingFunction == null)
1787         {
1788                 __pTimingFunction = new (std::nothrow) SineTimingFunction();
1789                 SysTryReturn(NID_UI_CTRL, __pTimingFunction, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1790         }
1791
1792         if (pAnimationGroup != null)
1793         {
1794                 VisualElementPropertyAnimation* pOpacityAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1795                 SysTryReturn(NID_UI_CTRL, pOpacityAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1796
1797                 pOpacityAnimation->SetPropertyName("opacity");
1798
1799                 pOpacityAnimation->SetStartValue(Variant(0.0f));
1800                 pOpacityAnimation->SetEndValue(Variant(1.0f));
1801
1802                 pOpacityAnimation->SetDuration(ANIMATION_DURATION_OPACITY);
1803                 pOpacityAnimation->SetTimingFunction(__pTimingFunction);
1804                 pAnimationGroup->AddAnimation(*pOpacityAnimation);
1805                 delete pOpacityAnimation;
1806
1807                 VisualElementPropertyAnimation* pBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1808                 SysTryReturn(NID_UI_CTRL, pBoundsAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1809
1810                 pBoundsAnimation->SetPropertyName("bounds");
1811                 FloatRectangle startValue = source.GetBounds();
1812                 startValue.x = startValue.x + startValue.width * 0.05;
1813                 startValue.y = startValue.y + startValue.height * 0.05;
1814                 startValue.width = startValue.width * 0.9;
1815                 startValue.height = startValue.height * 0.9;
1816
1817                 pBoundsAnimation->SetStartValue(Variant(startValue));
1818                 pBoundsAnimation->SetEndValue(Variant(source.GetBounds()));
1819
1820                 pBoundsAnimation->SetDuration(ANIMATION_DURATION_BOUNDS);
1821                 pBoundsAnimation->SetTimingFunction(__pTimingFunction);
1822                 pAnimationGroup->AddAnimation(*pBoundsAnimation);
1823
1824                 delete pBoundsAnimation;
1825
1826                 pAnimation = pAnimationGroup;
1827         }
1828         return pAnimation;
1829 }
1830
1831 result
1832 _TokenEditPresenter::CalculateDescriptionTextRect(const String& descriptionText)
1833 {
1834         result r = E_SUCCESS;
1835
1836         TextSimple* pSimpleText = null;
1837         float leftMargin = 0.0f;
1838         float tokenTopMargin = 0.0f;
1839         float tokenHeight = 0.0f;
1840         float tokenVerticalSpacing = 0.0f;
1841         float tokenTextLeftMargin = 0.0f;
1842         float tokenTextRightMargin = 0.0f;
1843         float tokenTitleWidth = 0.0f;
1844         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1845
1846         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1847         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1848         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1849         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1850         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1851         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1852         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TITLE_RECT_WIDTH, orientation, tokenTitleWidth);
1853
1854         int length = descriptionText.GetLength();
1855         FloatDimension textSize;
1856
1857         wchar_t* pTempString = const_cast< wchar_t* >(descriptionText.GetPointer());
1858
1859         SysAssertf(__pDescriptionTextTextObject != null, "The TextObject instance is null.");
1860
1861         __pDescriptionTextTextObject->RemoveAll(true);
1862         pSimpleText = new (std::nothrow) TextSimple(pTempString, length, TEXT_ELEMENT_SOURCE_TYPE_INTERNAL);
1863         __pDescriptionTextTextObject->AppendElement(*pSimpleText);
1864
1865         textSize = __pDescriptionTextTextObject->GetTextExtentF(0, length);
1866         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
1867         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
1868
1869         __descriptionTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1870         __descriptionTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN);
1871         if (textSize.width > tokenTitleWidth)
1872         {
1873                 textSize.width = tokenTitleWidth;
1874                 __pDescriptionTextTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
1875         }
1876
1877         __descriptionTextRect.width = tokenTextLeftMargin + textSize.width + tokenTextRightMargin;
1878         __descriptionTextRect.height = tokenHeight;
1879         __pDescriptionTextTextObject->SetBounds(__descriptionTextRect);
1880
1881         if (__pDescriptionTextTextObject->IsChanged())
1882         {
1883                 _EditPresenter::StopTitleSlidingTimer();
1884                 __isTitleSliding = false;
1885         }
1886
1887         r = __pDescriptionTextTextObject->Compose();
1888
1889         return r;
1890 }
1891
1892 bool
1893 _TokenEditPresenter::IsGuideTextActivated(void) const
1894 {
1895         bool isGuideTextActivated = _EditPresenter::IsGuideTextActivated();
1896
1897         if (__isTokenEditPresenterInitialized)
1898         {
1899                 if (GetTokenCount())
1900                 {
1901                         return false;
1902                 }
1903         }
1904
1905         return isGuideTextActivated;
1906 }
1907
1908 bool
1909 _TokenEditPresenter::DrawDescriptionText(void)
1910 {
1911         result r = E_SUCCESS;
1912         FloatRectangle tempDescriptionTextRect;
1913         FloatRectangle descriptionTextRect(__descriptionTextRect);
1914         Canvas* pDescriptionTextCanvas = null;
1915
1916         Font* pDescriptionFont = null;
1917         TextObjectActionType titleAction;
1918         _VisualElement* pRootElement = null;
1919
1920         if (__pDescriptionTextTextObject->GetFont(0)->GetFaceName() != GetTitleFontFaceName())
1921         {
1922                 float descriptionTextSize = 0.0f;
1923                 GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
1924
1925                 pDescriptionFont = GetFont();
1926                 if (pDescriptionFont)
1927                 {
1928                         float editFontSize = GetTextSize();
1929                         (_FontImpl::GetInstance(*pDescriptionFont))->SetSize(descriptionTextSize);
1930
1931                         r = __pDescriptionTextTextObject->SetFont(pDescriptionFont, 0, __pDescriptionTextTextObject->GetTextLength());
1932                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. SetFont failed");
1933
1934                         (_FontImpl::GetInstance(*pDescriptionFont))->SetSize(editFontSize);
1935                 }
1936         }
1937
1938         pRootElement = __pTokenEdit->GetVisualElement();
1939         SysTryCatch(NID_UI_CTRL, pRootElement, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1940
1941         if (!__pDescriptionTextVisualElement)
1942         {
1943                 __pDescriptionTextVisualElement = new (std::nothrow) _VisualElement();
1944                 SysTryCatch(NID_UI_CTRL, __pDescriptionTextVisualElement, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1945                 r = __pDescriptionTextVisualElement->Construct();
1946                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[E_SYSTEM] A system error has occurred. Failed to construct _VisualElement");
1947                 __pDescriptionTextVisualElement->SetImplicitAnimationEnabled(false);
1948                 __pDescriptionTextVisualElement->SetShowState(true);
1949
1950                 r = pRootElement->AttachChild(*__pDescriptionTextVisualElement);
1951                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1952         }
1953
1954         descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1955         __pDescriptionTextVisualElement->SetBounds(descriptionTextRect);
1956         UpdateTitleAccessibilityBounds(descriptionTextRect); // Update title accessibility bounds to same as DescriptionTextVisualElement bounds
1957
1958         pDescriptionTextCanvas = __pDescriptionTextVisualElement->GetCanvasN();
1959         if (pDescriptionTextCanvas == null)
1960         {
1961                 SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. Failed to get canvas of an instance of _VisualElement.");
1962                 return true;
1963         }
1964
1965         tempDescriptionTextRect = __descriptionTextRect;
1966         tempDescriptionTextRect.x = 0.0f;
1967         tempDescriptionTextRect.y = 0.0f;
1968
1969         titleAction = __pDescriptionTextTextObject->GetAction();
1970
1971         pDescriptionTextCanvas->SetBackgroundColor(Color(0));
1972         pDescriptionTextCanvas->Clear();
1973         __pDescriptionTextTextObject->SetForegroundColor(__pTokenEdit->GetTitleTextColor(GetCurrentStatus()), 0, __pDescriptionTextTextObject->GetTextLength());
1974
1975         if (IsFocused() == true)
1976         {
1977                 if (__pDescriptionTextTextObject->GetTextLengthAt(0) < __pDescriptionTextTextObject->GetTextLength())
1978                 {
1979                         if (titleAction != TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT)
1980                         {
1981                                 __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT);
1982                                 __pDescriptionTextTextObject->Compose();
1983                         }
1984                 }
1985                 else
1986                 {
1987                         if (titleAction != TEXT_OBJECT_ACTION_TYPE_NONE)
1988                         {
1989                                 __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
1990                                 __pDescriptionTextTextObject->Compose();
1991                         }
1992                 }
1993
1994                 if (!__isTitleSliding)
1995                 {
1996                         _EditPresenter::StopTitleSlidingTimer();
1997                         if (__pDescriptionTextTextObject->IsActionOn() == true)
1998                         {
1999                                 _EditPresenter::StartTitleSlidingTimer();
2000                                 __isTitleSliding = true;
2001                         }
2002                 }
2003         }
2004         else
2005         {
2006                 if (titleAction != TEXT_OBJECT_ACTION_TYPE_ABBREV)
2007                 {
2008                         __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
2009                         __pDescriptionTextTextObject->Compose();
2010                 }
2011         }
2012
2013         // Draw title text
2014         __pDescriptionTextTextObject->SetBounds(tempDescriptionTextRect);
2015         __pDescriptionTextTextObject->Draw(*_CanvasImpl::GetInstance(*pDescriptionTextCanvas));
2016
2017         delete pDescriptionTextCanvas;
2018
2019         return true;
2020
2021 CATCH:
2022         if (__pDescriptionTextVisualElement != null)
2023         {
2024                 __pDescriptionTextVisualElement->Destroy();
2025                 __pDescriptionTextVisualElement = null;
2026         }
2027
2028         return false;
2029 }
2030
2031 result
2032 _TokenEditPresenter::TrimTokenAndAdjustEllipsisAt(int index)
2033 {
2034         result r = E_SUCCESS;
2035
2036         float rightMargin = 0.0f;
2037         float tokenMinimumSize = 0.0f;
2038
2039         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2040         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
2041         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinimumSize);
2042
2043         _Token* pToken = null;
2044         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2045         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
2046
2047         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
2048         FloatRectangle tokenRect = pToken->displayRect;
2049
2050         float remainWidth = tokenEditBounds.width - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
2051
2052         float dspTokenWidth = tokenRect.x + tokenRect.width;
2053         float dspTokenGap = dspTokenWidth - remainWidth;
2054
2055         if (dspTokenGap > 0.0f)
2056         {
2057                 if (pToken->displayRect.width >= tokenMinimumSize)
2058                 {
2059                         if ((pToken->displayRect.width - dspTokenGap) < tokenMinimumSize)
2060                         {
2061                                 pToken->displayRect.width = tokenMinimumSize;
2062                         }
2063                         else
2064                         {
2065                                 pToken->displayRect.width -= dspTokenGap;
2066                         }
2067                 }
2068
2069                 // Adjust ellipsis
2070                 pToken->pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
2071                 pToken->pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
2072         }
2073
2074         return r;
2075 }
2076
2077 int
2078 _TokenEditPresenter::GetTokenIndexFromCoordinate(const FloatPoint point) const
2079 {
2080         int tokenIndex = -1;
2081
2082         int tokenCount = __pTokenList->GetCount();
2083         for (int i = 0; i < tokenCount; i++)
2084         {
2085                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2086                 if (pToken)
2087                 {
2088                         FloatRectangle tokenRect = pToken->displayRect;
2089                         if (tokenRect.Contains(point))
2090                         {
2091                                 tokenIndex = i;
2092                                 break;
2093                         }
2094                 }
2095         }
2096         return tokenIndex;
2097 }
2098
2099 result
2100 _TokenEditPresenter::SetEditingTokenTextBounds(int index, bool isSetText)
2101 {
2102         result r = E_SUCCESS;
2103
2104         float tokenHeight = 0.0f;
2105         float tokenVerticalSpacing = 0.0f;
2106         float tokenTextLeftMargin = 0.0f;
2107         float tokenFontSize = 0.0f;
2108         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2109
2110         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2111         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2112         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
2113         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_SIZE, orientation, tokenFontSize);
2114
2115         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2116         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
2117
2118         FloatRectangle tempTextDspRect;
2119         tempTextDspRect.x += tokenTextLeftMargin;
2120         tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
2121         tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
2122         tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
2123
2124         if (isSetText)
2125         {
2126                 if (!pToken->isTextCut)
2127                 {
2128                         SetText(pToken->GetText());
2129                 }
2130                 pToken->pTextObject->RemoveAll(true);
2131         }
2132
2133         _EditPresenter::SetTextSize(tokenFontSize);
2134
2135         SetTextBounds(tempTextDspRect);
2136
2137         return r;
2138 }
2139
2140 result
2141 _TokenEditPresenter::RecalculateTokenBounds(float position)
2142 {
2143         result r = E_SUCCESS;
2144
2145         __scrollValue = -position;
2146
2147         int tokenCount = GetTokenCount();
2148         CalculateTokenPositionFromIndex(0);
2149
2150         for (int i = 0; i < tokenCount; i++)
2151         {
2152                 _Token* pToken = null;
2153                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2154
2155                 if (pToken)
2156                 {
2157                         pToken->SetBounds(pToken->displayRect);
2158                 }
2159                 TrimTokenAndAdjustEllipsisAt(i);
2160                 InitializeTokenVisibilityAt(i);
2161         }
2162
2163         if (__pDescriptionTextTextObject->GetTextLength() != 0)
2164         {
2165                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2166         }
2167
2168         __pTokenEdit->Invalidate();
2169
2170         r = SetInitialBounds();
2171         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
2172
2173         return r;
2174 }
2175
2176 result
2177 _TokenEditPresenter::SetTokenBoundsByTouchInfo(const _TouchInfo& touchinfo)
2178 {
2179         result r = E_SUCCESS;
2180         int currentYPosition = _CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).y;
2181
2182         if (_FloatCompare(__prevScrollValue, 0.0f))
2183         {
2184                 __prevScrollValue = currentYPosition;
2185         }
2186         else    // Adjust moved y position to all tokens.
2187         {
2188                 if (__isNeedToScroll)       // Need to scroll
2189                 {
2190                         float tempDefference = __prevScrollValue - currentYPosition;
2191
2192                         __prevScrollValue = currentYPosition;
2193                         __scrollValue -= tempDefference;
2194
2195                         if (__scrollValue < -__maxScrollValue)
2196                         {
2197                                 __scrollValue = -__maxScrollValue;
2198
2199                                 return E_SUCCESS;
2200                         }
2201
2202                         if (__scrollValue > 0.0f)
2203                         {
2204                                 __scrollValue = 0.0f;
2205
2206                                 return E_SUCCESS;
2207                         }
2208
2209                         int tokenCount = GetTokenCount();
2210                         CalculateTokenPositionFromIndex(0);
2211                         for (int i = 0; i < tokenCount; i++)
2212                         {
2213                                 _Token* pToken = null;
2214                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2215
2216                                 if (pToken)
2217                                 {
2218                                         pToken->SetBounds(pToken->displayRect);
2219                                 }
2220                                 TrimTokenAndAdjustEllipsisAt(i);
2221                                 InitializeTokenVisibilityAt(i);
2222                         }
2223
2224                         if (__pDescriptionTextTextObject->GetTextLength() != 0)
2225                         {
2226                                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2227                         }
2228
2229                         __pTokenEdit->Invalidate();
2230                 }
2231                 else
2232                 {
2233                         __prevScrollValue = 0.0f;
2234                         __scrollValue = 0.0f;
2235                 }
2236         }
2237
2238         r = SetInitialBounds();
2239         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
2240
2241         return r;
2242 }
2243
2244 result
2245 _TokenEditPresenter::ProcessTokeningByTouchEvent(const _Control& source, const _TouchInfo& touchinfo)
2246 {
2247         int tokenIndex = GetTokenIndexFromCoordinate(touchinfo.GetCurrentPosition());
2248
2249         //Reset "longPressed" when Touch released on a different token after long gesture on a token
2250         if (__trackTokenIndex != tokenIndex)
2251         {
2252                 __isLongPressed = false;
2253         }
2254
2255         int prevPressedTokenIndex = __pressedTokenIndex;
2256         int prevEditedTokenIndex = __editingTokenIndex;
2257
2258         result r = E_SUCCESS;
2259         if (IsFocused())
2260         {
2261                 if (__isEditModeEnabled && __pressedTokenIndex != -1 && __pressedTokenIndex == tokenIndex)
2262                 {
2263                         //Comment below to Block Copy & Paste functionality in Token Edit mode
2264                         __editingTokenIndex = __pressedTokenIndex;
2265                         __isEditingToken = true;
2266                         __isTokenEditingFinished = false;
2267                         if (prevEditedTokenIndex != __editingTokenIndex)
2268                         {
2269                                 SetEditingTokenTextBounds(__editingTokenIndex);
2270                         }
2271                         SetCursorDisabled(false);
2272                 }
2273         }
2274
2275         if (tokenIndex != -1)
2276         {
2277                 if (__isEditingToken && (prevPressedTokenIndex != tokenIndex))   // Selected another token while editing.
2278                 {
2279                         __isPopupVisible = false;
2280                         __isLongPressed = false;
2281
2282                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2283                         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
2284
2285                         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2286                         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
2287
2288                         _Token* pToken = null;
2289                         _VisualElement* pTokenVisualElement = null;
2290
2291                         pToken = static_cast< _Token* >(__pTokenList->GetAt(prevPressedTokenIndex));
2292
2293                         bool isParentChanged = false;
2294                         if (pToken)
2295                         {
2296                                 pTokenVisualElement = pToken->GetVisualElement();
2297                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
2298
2299                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
2300                                 {
2301                                         isParentChanged = true;
2302                                         result r = E_SUCCESS;
2303                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
2304                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2305
2306                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2307                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2308                                 }
2309                         }
2310
2311                         String inputTokenString = GetText();
2312                         String replacementString = inputTokenString;
2313                         bool enable = false;
2314
2315                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
2316                         if (enable)
2317                         {
2318                                 inputTokenString = replacementString;
2319                         }
2320
2321                         __pressedTokenIndex = tokenIndex;
2322
2323                         r = RemoveTokenAt(prevPressedTokenIndex);
2324                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2325
2326                         if (inputTokenString.GetLength() > 0)
2327                         {
2328                                 r = InsertTokenAt(prevPressedTokenIndex, inputTokenString);
2329                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2330
2331                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(prevPressedTokenIndex));
2332                                 if (pToken)
2333                                 {
2334                                         pToken->currTokenLength = inputTokenString.GetLength();
2335                                 }
2336                         }
2337
2338                         ClearText();
2339                         //Flex height adjusted since token can move to another line
2340                         AdjustFlexibleHeight();
2341                         __editingTokenIndex = -1;
2342                         __isTokenEditingFinished = true;
2343                         __isEditingToken = false;
2344                         _EditPresenter::SetTextSize(__editContentFontSize);
2345
2346                         SetCursorDisabled(true);
2347                 }
2348                 else
2349                 {
2350                         __pressedTokenIndex = tokenIndex;
2351                         if (GetText().GetLength() > 0 && __pressedTokenIndex != prevPressedTokenIndex)
2352                         {
2353                                 r = MakeToken();
2354                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2355                         }
2356
2357                         if ((__isEditingToken == true) && (__pressedTokenIndex != -1))
2358                         {
2359                                 r = AttachCursorToToken();
2360                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2361                         }
2362
2363                         if (__isEditingToken == false)
2364                         {
2365                                 SetCursorDisabled(true);
2366                         }
2367                 }
2368         }
2369         else
2370         {
2371                 __isPopupVisible = false;
2372                 __isLongPressed = false;
2373
2374                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2375                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
2376
2377                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2378                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
2379
2380                 if (__isEditingToken)
2381                 {
2382                         _Token* pToken = null;
2383                         _VisualElement* pTokenVisualElement = null;
2384
2385                         pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2386
2387                         bool isParentChanged = false;
2388                         if (pToken)
2389                         {
2390                                 pTokenVisualElement = pToken->GetVisualElement();
2391                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
2392
2393                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
2394                                 {
2395                                         isParentChanged = true;
2396                                         result r = E_SUCCESS;
2397                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
2398                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2399
2400                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2401                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2402                                 }
2403                         }
2404
2405                         String inputTokenString = GetText();
2406                         String replacementString = inputTokenString;
2407                         bool enable = false;
2408
2409                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
2410                         if (enable)
2411                         {
2412                                 inputTokenString = replacementString;
2413                         }
2414                         __pressedTokenIndex = tokenIndex;
2415
2416                         RemoveTokenAt(__editingTokenIndex);
2417
2418                         if (inputTokenString.GetLength() > 0)
2419                         {
2420                                 InsertTokenAt(__editingTokenIndex, inputTokenString);
2421
2422                                 if (isParentChanged)
2423                                 {
2424                                         pToken->currTokenLength = inputTokenString.GetLength();
2425                                 }
2426                         }
2427
2428                         __isEditingToken = false;
2429                         __editingTokenIndex = -1;
2430                         _EditPresenter::SetTextSize(__editContentFontSize);
2431                         __isTokenEditingFinished = false;
2432
2433                         ClearText();
2434
2435                         //Flex height adjusted since token can move to another line
2436                         AdjustFlexibleHeight();
2437
2438                         SysTryReturnResult(NID_UI_CTRL, (inputTokenString.GetLength() > 0), E_INVALID_ARG, "Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
2439
2440                         DrawText();
2441                 }
2442                 else
2443                 {
2444                         __pressedTokenIndex = tokenIndex;
2445                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
2446                         {
2447                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
2448                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2449
2450                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2451                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2452                         }
2453                 }
2454
2455                 SetCursorDisabled(false);
2456         }
2457
2458         for (int i = 0; i < __pTokenList->GetCount(); i++)
2459         {
2460                 TrimTokenAndAdjustEllipsisAt(i);
2461                 InitializeTokenVisibilityAt(i);
2462         }
2463
2464         return E_SUCCESS;
2465 }
2466
2467 result
2468 _TokenEditPresenter::CheckTokenScrolling(bool scrollToCursorPosition)
2469 {
2470         bool needToScroll = false;
2471         float tokenTopMargin = 0.0f;
2472         float tokenBottomMargin = 0.0f;
2473         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2474
2475         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2476         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2477
2478         int tokenCount = GetTokenCount();
2479         if (tokenCount == 0)        // There is no token to scroll
2480         {
2481                 __isNeedToScroll = false;
2482                 __maxScrollValue = 0.0f;
2483                 __isTokenScrolling = false;
2484
2485                 return E_SUCCESS;
2486         }
2487
2488         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
2489         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2490
2491         float newScrollValue = 0.0f;
2492
2493         if (scrollToCursorPosition)
2494         {
2495                 FloatRectangle cursorBounds;
2496                 GetCursorBounds(false, cursorBounds);
2497                 newScrollValue = cursorBounds.y + cursorBounds.height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBoundsF().height;
2498                 __isScrollValueModified = true;
2499         }
2500         else
2501         {
2502                 newScrollValue = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBoundsF().height;
2503                 __isScrollValueModified = true;
2504         }
2505
2506         needToScroll = (newScrollValue > 0.0f) ? (true) : (false);
2507
2508         __isNeedToScroll = needToScroll;
2509
2510         if (__isNeedToScroll)
2511         {
2512                 __maxScrollValue = newScrollValue;
2513                 __isTokenScrolling = true;
2514
2515                 RecalculateTokenBounds(__maxScrollValue);
2516         }
2517         else
2518         {
2519                 if (!_FloatCompare(__scrollValue, 0.0f))
2520                 {
2521                         __scrollValue = 0.0f;
2522                         __maxScrollValue = 0.0f;        // To prevent unnecessary token scrolling.
2523                         RecalculateTokenBounds(__scrollValue);
2524                         __isTokenScrolling = false;
2525                 }
2526         }
2527
2528         return E_SUCCESS;
2529 }
2530
2531 result
2532 _TokenEditPresenter::SetTokenVisualElementBounds(int index, const FloatRectangle& bounds)
2533 {
2534         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2535         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
2536
2537         return pToken->SetBounds(pToken->displayRect);
2538 }
2539
2540 result
2541 _TokenEditPresenter::AdjustFlexibleHeight(void)
2542 {
2543         result r = E_SUCCESS;
2544
2545         FloatRectangle editRect = __pTokenEdit->GetBoundsF();
2546         FloatRectangle displayScrollBounds = GetDisplayScrollBoundsF();
2547         float calcHeight = CalculateFlexibleHeightF();
2548
2549         if (!_FloatCompare(editRect.height, calcHeight))
2550         {
2551                 displayScrollBounds.height = calcHeight;
2552                 SetScrollBarBounds(displayScrollBounds);
2553
2554                 editRect.height = calcHeight;
2555                 if (!__isEditingToken)
2556                 {
2557                         FloatRectangle editRectBounds = CoordinateSystem::AlignToDevice(editRect);
2558                         r = SetFlexBounds(editRectBounds);
2559                 }
2560         }
2561
2562         return r;
2563 }
2564
2565 float
2566 _TokenEditPresenter::CalculateFlexibleHeightF(void)
2567 {
2568         float tokenHeight = 0.0f;
2569         float tokenVerticalSpacing = 0.0f;
2570         float tokenTopMargin = 0.0f;
2571         float tokenBottomMargin = 0.0f;
2572         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2573
2574         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2575         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2576         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2577         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2578
2579         float height = 0.0f;
2580         int maxLinecount = GetMaxLineCount();
2581         float expectedEditHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
2582         float initialHeight = GetInitialBoundsF().height;
2583         float maximumLineHeight = tokenTopMargin + maxLinecount * tokenHeight + tokenVerticalSpacing * (maxLinecount - 1) + tokenBottomMargin;
2584
2585         if (initialHeight < expectedEditHeight)
2586         {
2587                 if (expectedEditHeight > maximumLineHeight)
2588                 {
2589                         height = maximumLineHeight;
2590                 }
2591                 else
2592                 {
2593                         height = expectedEditHeight;
2594                 }
2595         }
2596         else
2597         {
2598                 height = initialHeight;
2599         }
2600
2601         FloatDimension flexHeightDim = CoordinateSystem::AlignToDevice(FloatDimension(0, height));
2602         return flexHeightDim.height;
2603 }
2604
2605 result
2606 _TokenEditPresenter::DrawScrollBar(void)
2607 {
2608         result r = E_SUCCESS;
2609
2610         _Scroll* pScroll = GetScrollBar();
2611         if (pScroll == null)
2612         {
2613                 return E_SYSTEM;
2614         }
2615
2616         if (IsFocused() == false)
2617         {
2618                 if (__isTouchMoveInProgress == false)
2619                 {
2620                         pScroll->SetScrollVisibility(false);
2621                         return E_SUCCESS;
2622                 }
2623         }
2624
2625         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
2626         if (pToken == null)
2627         {
2628                 return E_SUCCESS;
2629         }
2630
2631         float tokenBottomMargin = 0.0f;
2632         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
2633
2634         float totalHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
2635         float dspHeight = __pTokenEdit->GetBoundsF().height;
2636         float firstDspY = -__scrollValue;
2637
2638         if (totalHeight <= dspHeight)
2639         {
2640                 pScroll->SetScrollVisibility(false);
2641                 pScroll->SetScrollRange(1, 1);
2642                 pScroll->SetScrollPosition(0.0f);
2643
2644                 SetScrollBarVisible(false);
2645                 SetPreviousScrollBarPosition(0.0f);
2646                 SetMaximumPreviousScrollBarPosition(0.0f);
2647         }
2648         else
2649         {
2650                 if (firstDspY > 0.0f)
2651                 {
2652                         pScroll->SetScrollRange(dspHeight, totalHeight);
2653                         pScroll->SetScrollPosition(firstDspY);
2654                         SetMaximumPreviousScrollBarPosition(totalHeight - dspHeight);
2655                 }
2656                 else
2657                 {
2658                         pScroll->SetScrollRange(dspHeight, totalHeight);
2659                         pScroll->SetScrollPosition(0.0f);
2660                         SetMaximumPreviousScrollBarPosition(0.0f);
2661                 }
2662
2663                 if (pScroll->GetScrollVisibility())
2664                 {
2665                         SetScrollBarVisible(true);
2666                 }
2667
2668                 SetPreviousScrollBarPosition(firstDspY);
2669         }
2670
2671         return r;
2672 }
2673
2674 void
2675 _TokenEditPresenter::SetAutoShrinkModeEnabled(bool enable)
2676 {
2677         __autoShrink = enable;
2678 }
2679
2680 bool
2681 _TokenEditPresenter::IsAutoShrinkModeEnabled(void) const
2682 {
2683         return __autoShrink;
2684 }
2685
2686 void
2687 _TokenEditPresenter::SetDescriptionText(String descriptionText)
2688 {
2689         __descriptionText = descriptionText;
2690 }
2691
2692 String
2693 _TokenEditPresenter::GetDescriptionText(void) const
2694 {
2695         return __descriptionText;
2696 }
2697
2698 int
2699 _TokenEditPresenter::CalculateVisibleTokenCount(void)
2700 {
2701         int visibleTokenCount = 0;
2702
2703         FloatRectangle intialBounds = GetInitialBoundsF();
2704         FloatRectangle tempInitialBounds = intialBounds;
2705         FloatRectangle hiddenTokenDisplayBounds;
2706         int tokenCount = GetTokenCount();
2707
2708         for (int i = 0; i < tokenCount; i++)
2709         {
2710                 tempInitialBounds = intialBounds;
2711                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2712                 SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2713
2714                 FloatRectangle displayBounds = pToken->displayRect;
2715                 if ((tempInitialBounds.y + displayBounds.y) > (tempInitialBounds.y + tempInitialBounds.height))
2716                 {
2717                         break;
2718                 }
2719                 visibleTokenCount++;
2720         }
2721         return visibleTokenCount;
2722 }
2723
2724 bool
2725 _TokenEditPresenter::OnFocusGained(void)
2726 {
2727         __isTitleSliding = false;
2728         if (__autoShrink)
2729         {
2730                 float tempHeight = CalculateFlexibleHeightF();
2731                 FloatRectangle tempRect = GetInitialBoundsF();
2732                 tempRect.height = tempHeight;
2733
2734                 _Token* pToken = null;
2735                 int tokenCount = GetTokenCount();
2736
2737                 for (int i = 0; i < tokenCount; i++)
2738                 {
2739                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2740
2741                         if (pToken)
2742                         {
2743                                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2744                                 if (pTokenVisualElement)
2745                                 {
2746                                         pTokenVisualElement->SetShowState(true);
2747                                 }
2748                         }
2749                 }
2750
2751                 SetFlexBounds(tempRect);
2752
2753                 SetEditingTokenTextBounds(__editingTokenIndex, false);
2754
2755                 if (!__isFocus)
2756                 {
2757                         __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2758                         __isFocus = true;
2759                 }
2760
2761                 if (__isScrollValueChanged && !__isScrollValueModified)
2762                 {
2763                         __maxScrollValue = __maxScrollValue - (__pTokenEdit->GetBoundsF().height - GetInitialBoundsF().height);
2764                         __isScrollValueChanged = false;
2765                 }
2766         }
2767
2768         if (GetTokenCount())
2769         {
2770                 CheckTokenScrolling();
2771         }
2772
2773         TextObject* pTextObject = GetTextObject();
2774         if (pTextObject)
2775         {
2776                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
2777                 pTextObject->Compose();
2778         }
2779
2780         if (__pressedTokenIndex < 0)
2781         {
2782                 StopCursorTimer();
2783                 SetCursorDisabled(false);
2784                 StartCursorTimer();
2785         }
2786
2787         RefreshAccessibilityElements();
2788
2789         return _EditPresenter::OnFocusGained();
2790 }
2791
2792 bool
2793 _TokenEditPresenter::OnFocusLost(void)
2794 {
2795         result r = E_SUCCESS;
2796         __isFocus = false;
2797
2798         _EditPresenter::StopTitleSlidingTimer();
2799         __isTitleSliding = false;
2800
2801         if (__editingTokenIndex >= 0)
2802         {
2803                 _Token* pToken = null;
2804                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2805                 if (pToken)
2806                 {
2807                         if (GetText().GetLength() > 0)
2808                         {
2809                                 OnTextCommitted(L"\n");
2810                         }
2811                         else
2812                         {
2813                                 RemoveTokenAt(__editingTokenIndex, true);
2814                         }
2815                 }
2816         }
2817         else
2818         {
2819                 //Remove pressed state on focus lost
2820                 __pressedTokenIndex = -1;
2821
2822                 if (GetText().GetLength() > 0)
2823                 {
2824                         OnTextCommitted(L"\n");
2825                 }
2826         }
2827
2828         if (__autoShrink)
2829         {
2830                 _Scroll* pScroll = GetScrollBar();
2831                 if (pScroll)
2832                 {
2833                         pScroll->SetScrollVisibility(false);
2834                 }
2835                 __scrollValue = 0.0f;
2836
2837                 int tokenCount = GetTokenCount();
2838                 _Token* pToken = null;
2839                 for (int i = 0; i < tokenCount; i++)
2840                 {
2841                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2842                         if (pToken)
2843                         {
2844                                 pToken->SetBounds(pToken->displayRect);
2845                         }
2846                 }
2847
2848                 r = SetInitialBounds();
2849                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating", GetErrorMessage(r));
2850
2851                 UpdateTokenAccessibilityBounds();
2852
2853                 __scrollValue = 0.0f;
2854                 r = CalculateTokenPositionFromIndex(0);
2855                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
2856
2857                 int visibleTokenCount = CalculateVisibleTokenCount();
2858
2859                 for (int i = visibleTokenCount; i < tokenCount; i++)
2860                 {
2861                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2862                         if (pToken)
2863                         {
2864                                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2865                                 if (pTokenVisualElement)
2866                                 {
2867                                         pTokenVisualElement->SetShowState(false);
2868                                 }
2869                         }
2870                 }
2871
2872                 float totalScrollValue = __maxScrollValue + (__pTokenEdit->GetBoundsF().height - GetInitialBoundsF().height);
2873
2874                 if (totalScrollValue > 0)
2875                 {
2876                         __isNeedToScroll = true;
2877                         __maxScrollValue = totalScrollValue;
2878                         __isTokenScrolling = true;
2879                         if (__lineAdded > 0)
2880                         {
2881                                 __isScrollValueChanged = true;
2882                                 __isScrollValueModified = false;
2883                         }
2884                 }
2885
2886                 FloatRectangle intialWindowBounds = GetInitialBoundsF();
2887                 SetFlexBounds(intialWindowBounds);
2888
2889                 SetEditingTokenTextBounds(__editingTokenIndex, false);
2890
2891                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2892         }
2893
2894         TextObject* pTextObject = GetTextObject();
2895         if (pTextObject)
2896         {
2897                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
2898                 pTextObject->Compose();
2899         }
2900
2901         RemoveChildAccessibilityElements();
2902         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
2903         SetDrawFocusState(false);
2904
2905         return _EditPresenter::OnFocusLost();
2906 }
2907
2908 result
2909 _TokenEditPresenter::SetFlexBounds(const FloatRectangle& bounds)
2910 {
2911         if (__pTokenEdit->GetBoundsF().height > bounds.height)
2912         {
2913                 if (__lineAdded > 0)
2914                 {
2915                         __lineAdded--;
2916                 }
2917         }
2918         return _EditPresenter::SetFlexBounds(bounds);
2919 }
2920
2921 result
2922 _TokenEditPresenter::SetTextSize(const int size)
2923 {
2924         result r = E_SUCCESS;
2925         __editContentFontSize = size;
2926
2927         if (!__isEditingToken)
2928         {
2929                 r = _EditPresenter::SetTextSize(size);
2930                 r = SetInitialBounds();
2931                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2932         }
2933
2934         return r;
2935 }
2936
2937 result
2938 _TokenEditPresenter::SetTextSize(const float size)
2939 {
2940         result r = E_SUCCESS;
2941         __editContentFontSize = size;
2942
2943         if (!__isEditingToken)
2944         {
2945                 r = _EditPresenter::SetTextSize(size);
2946                 r = SetInitialBounds();
2947                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2948         }
2949
2950         return r;
2951 }
2952
2953 bool
2954 _TokenEditPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
2955 {
2956         //Remove token focus on touch press
2957         __focusedTokenIndex = -1;
2958
2959         int tokenIndex = GetTokenIndexFromCoordinate(touchinfo.GetCurrentPosition());
2960         __trackTokenIndex = tokenIndex;
2961
2962         _TouchInfo TouchInfo(touchinfo);
2963         if (tokenIndex != -1)
2964         {
2965                 if (tokenIndex == __editingTokenIndex)
2966                 {
2967                         __touchPressInfo.x = touchinfo.GetCurrentPosition().x;
2968                         __touchPressInfo.y = touchinfo.GetCurrentPosition().y;
2969
2970                         _Token* pToken = null;
2971                         pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2972                         if (pToken)
2973                         {
2974                                 float tokenX = pToken->displayRect.x;
2975                                 float tokenY = pToken->displayRect.y;
2976                                 FloatPoint point(__touchPressInfo.x - tokenX, __touchPressInfo.y - tokenY);
2977                                 TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
2978                         }
2979                 }
2980         }
2981
2982         return _EditPresenter::OnTouchPressed(source, TouchInfo);
2983 }
2984
2985 bool
2986 _TokenEditPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
2987 {
2988         __touchPressInfo = FloatPoint(-1.0f, -1.0f);
2989
2990         ProcessTokeningByTouchEvent(source, touchinfo);
2991         if (GetTokenCount())
2992         {
2993                 //Set token bounds appropriately On Fast flick of scroll bar
2994                 if (!(__isEditingToken || __editingTokenIndex >= 0))
2995                 {
2996                         SetTokenBoundsByTouchInfo(touchinfo);
2997                 }
2998         }
2999
3000         _Scroll* pScroll = GetScrollBar();
3001         if (pScroll)
3002         {
3003                 pScroll->SetScrollVisibility(false);
3004         }
3005
3006         if (__prevScrollValue)
3007         {
3008                 __prevScrollValue = 0.0f;
3009                 __isTokenScrolling = false;
3010         }
3011
3012         _TouchInfo TouchInfo(touchinfo);
3013         _Token* pToken = null;
3014         if (__editingTokenIndex >= 0)
3015         {
3016                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3017                 if (pToken)
3018                 {
3019                         int tokenX = _CoordinateSystemUtils::ConvertToInteger(pToken->displayRect.x);
3020                         int tokenY = _CoordinateSystemUtils::ConvertToInteger(pToken->displayRect.y);
3021                         Point point(_CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).x - tokenX, _CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).y - tokenY);
3022                         TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
3023                 }
3024                 _EditPresenter::OnTouchReleased(source, TouchInfo);
3025                 __previousCursorPosition = GetCursorPosition();
3026         }
3027         else
3028         {
3029                 _EditPresenter::OnTouchReleased(source, touchinfo);
3030         }
3031
3032         __isTouchMoveInProgress = false;
3033         __trackTokenIndex = -1;
3034
3035         return false;
3036 }
3037
3038 void
3039 _TokenEditPresenter::OnInputConnectionTextCommitted(InputConnection& source, const String& committedText)
3040 {
3041         OnTextCommitted(committedText);
3042 }
3043
3044 void
3045 _TokenEditPresenter::OnTextCommitted(const String& commitText)
3046 {
3047         char enterText[2] = {'\n', };
3048         String enterTextComma(",");
3049         String enterTextSemiColon(";");
3050
3051         //OnTextCommitted blocked for these cases
3052         //1. Tab text not to be handled
3053         //2. Token is focused
3054         char tapText[2] = {'\t', };
3055         if (commitText == tapText)
3056         {
3057                 return;
3058         }
3059
3060         if ((commitText == enterText) || (commitText == enterTextComma) || (commitText == enterTextSemiColon))
3061         {
3062                 CoreKeypadAction keypadaction = GetKeypadAction();
3063                 __pTokenEdit->SendKeypadEvent(keypadaction, CORE_KEYPAD_EVENT_STATUS_ENTERACTION);
3064
3065                 if (__editingTokenIndex != -1)
3066                 {
3067                         ExitTokenEditingMode();
3068                 }
3069
3070                 if (GetText().GetLength() > 0)
3071                 {
3072                         MakeToken();
3073                 }
3074
3075                 for (int i = 0; i < __pTokenList->GetCount(); i++)
3076                 {
3077                         TrimTokenAndAdjustEllipsisAt(i);
3078                         InitializeTokenVisibilityAt(i);
3079                 }
3080
3081                 if (__pressedTokenIndex < 0 && __focusedTokenIndex < 0)
3082                 {
3083                         SetCursorDisabled(true);
3084                         __pTokenEdit->Draw();
3085                         SetCursorDisabled(false);
3086                         StartCursorTimer();
3087                 }
3088                  // when pressed enter while editing a token, set cursor as global focused element.
3089                 _AccessibilityElement* pCursorAccessibilityElement = __pTokenEdit->GetCursorAccessibilityElement();
3090                 if (pCursorAccessibilityElement && __accessibilityFocusIn)
3091                 {
3092                         _AccessibilityManager* pAccessibilityManager = _AccessibilityManager::GetInstance();
3093                         pAccessibilityManager->SetGlobalFocusedElement(*pCursorAccessibilityElement);
3094                         pAccessibilityManager->RequestToDrawFocusUi();
3095                 }
3096
3097                 return;
3098         }
3099
3100         _EditPresenter::OnTextCommitted(commitText);
3101         __previousCursorPosition = GetCursorPosition();
3102
3103         _Token* pToken = null;
3104         if (__editingTokenIndex >= 0)
3105         {
3106                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3107                 SysTryReturnVoidResult(NID_UI_CTRL, pToken, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
3108
3109                 pToken->ResetToken(GetText());
3110                 TrimTokenAndAdjustEllipsisAt(__editingTokenIndex);
3111
3112                 float tokenHeight = 0.0f;
3113                 float tokenVerticalSpacing = 0.0f;
3114                 float tokenTextLeftMargin = 0.0f;
3115
3116                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3117                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
3118                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
3119                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3120
3121                 FloatRectangle tempTextDspRect;
3122                 tempTextDspRect.x += tokenTextLeftMargin;
3123                 tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
3124                 tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
3125                 tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
3126
3127                 SetTextBounds(tempTextDspRect);
3128
3129                 if (GetCursorPosition() > pToken->currTokenLength)
3130                 {
3131                         _EditPresenter::SetCursorPosition(pToken->currTokenLength);
3132                 }
3133                 else
3134                 {
3135                         _EditPresenter::SetCursorPosition(GetCursorPosition());
3136                 }
3137         }
3138         else
3139         {
3140                 if (__isEditingToken == false)
3141                 {
3142                         __pressedTokenIndex = -1;
3143                         __focusedTokenIndex = -1;
3144                         SetCursorDisabled(false);
3145                 }
3146
3147                 float tokenTopMargin = 0.0f;
3148                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3149                 GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
3150
3151                 FloatRectangle textBounds = GetTextBoundsF();
3152                 textBounds.height = GetMaxTextHeight();
3153                 SetTextBounds(textBounds);
3154         }
3155
3156         if (__editingTokenIndex < 0)
3157         {
3158                 CheckTokenScrolling();
3159         }
3160         __pTokenEdit->Invalidate();
3161
3162         return;
3163 }
3164
3165 void
3166 _TokenEditPresenter::DeleteSurroundingText(InputConnection& source, int offset, int charCount)
3167 {
3168         OnSurroundingTextDeleted(offset, charCount);
3169 }
3170
3171 void
3172 _TokenEditPresenter::OnSurroundingTextDeleted(int offset, int charCount)
3173 {
3174         __lastTokenIndex = GetTokenCount() - 1;
3175         int cursorPosition = 0;
3176         int start = 0;
3177         int end = 0;
3178
3179         if ((offset == -1) && (charCount == 1))
3180         {
3181                 if (GetTextLength() == 0 && GetTokenCount())        // There is no candidate token.
3182                 {
3183                         if (__isEditingToken == true)
3184                         {
3185                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3186                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3187
3188                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3189                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3190
3191                                 _Token* pToken = null;
3192                                 _VisualElement* pTokenVisualElement = null;
3193
3194                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3195
3196                                 if (pToken)
3197                                 {
3198                                         pTokenVisualElement = pToken->GetVisualElement();
3199                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3200
3201                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3202                                         {
3203                                                 result r = E_SUCCESS;
3204                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3205                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3206
3207                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3208                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3209                                         }
3210                                 }
3211
3212                                 RemoveTokenAt(__editingTokenIndex);
3213
3214                                 CalculateTokenPositionFromIndex(__editingTokenIndex);
3215                                 for (int i = __editingTokenIndex; i < __lastTokenIndex + 1; i++)
3216                                 {
3217                                         _Token* pToken = null;
3218                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3219                                         if (pToken)
3220                                         {
3221                                                 pToken->SetBounds(pToken->displayRect);
3222                                         }
3223                                 }
3224
3225                                 __pressedTokenIndex = -1;
3226                                 __editingTokenIndex = -1;
3227                                 __isEditingToken = false;
3228                                 _EditPresenter::SetTextSize(__editContentFontSize);
3229                                 __isTokenEditingFinished = false;
3230
3231                                 AdjustFlexibleHeight();
3232                                 CheckTokenScrolling();
3233                         }
3234                         else if (__pressedTokenIndex != -1)
3235                         {
3236                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3237                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3238
3239                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3240                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3241
3242                                 _Token* pToken = null;
3243                                 _VisualElement* pTokenVisualElement = null;
3244
3245                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
3246
3247                                 if (pToken)
3248                                 {
3249                                         pTokenVisualElement = pToken->GetVisualElement();
3250                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3251
3252                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3253                                         {
3254                                                 result r = E_SUCCESS;
3255                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3256                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3257
3258                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3259                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3260                                         }
3261                                 }
3262
3263                                 RemoveTokenAt(__pressedTokenIndex);
3264
3265                                 CalculateTokenPositionFromIndex(__pressedTokenIndex);
3266                                 for (int i = __pressedTokenIndex; i < __lastTokenIndex + 1; i++)
3267                                 {
3268                                         _Token* pToken = null;
3269                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3270
3271                                         if (pToken)
3272                                         {
3273                                                 pToken->SetBounds(pToken->displayRect);
3274                                         }
3275                                 }
3276
3277                         }
3278                         else if (__focusedTokenIndex != -1)
3279                         {
3280                                 RemoveTokenAt(__focusedTokenIndex);
3281                         }
3282                         else
3283                         {
3284                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3285                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3286
3287                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3288                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3289
3290                                 _Token* pToken = null;
3291                                 _VisualElement* pTokenVisualElement = null;
3292
3293                                 if (__animatingIndex == (GetTokenCount() - 1))
3294                                 {
3295                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
3296                                         if (pToken)
3297                                         {
3298                                                 pTokenVisualElement = pToken->GetVisualElement();
3299                                                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3300                                                 pTokenVisualElement->RemoveAllAnimations();
3301                                         }
3302                                 }
3303
3304                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
3305                                 if (pToken)
3306                                 {
3307                                         pTokenVisualElement = pToken->GetVisualElement();
3308                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3309
3310                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3311                                         {
3312                                                 result r = E_SUCCESS;
3313                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3314                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3315
3316                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3317                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3318                                         }
3319                                 }
3320
3321                                 __animatingIndex = GetTokenCount() - 1;
3322
3323                                 if (pTokenVisualElement && __animatingIndex >= 0)
3324                                 {
3325                                         __isAnimationInProgress = true;
3326                                         PerformRemoveTokenAnimation(*pTokenVisualElement);
3327                                 }
3328                         }
3329
3330                         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
3331
3332                         DrawText();
3333                         __pTokenEdit->Invalidate();
3334                         return;
3335                 }
3336         }
3337
3338         if (__pressedTokenIndex >= 0 && __editingTokenIndex < 0 && !__isEditingToken)
3339         {
3340                 RemoveTokenAt(__pressedTokenIndex);
3341                 __pTokenEdit->Invalidate();
3342                 return;
3343         }
3344         else if (__focusedTokenIndex >= 0 && __editingTokenIndex < 0 && !__isEditingToken)
3345         {
3346                 RemoveTokenAt(__focusedTokenIndex);
3347                 __pTokenEdit->Invalidate();
3348                 return;
3349         }
3350
3351         //Backspace on Blocked text, delete full block
3352         if (IsBlocked() == true)
3353         {
3354                 GetBlockRange(start, end);
3355         }
3356         else
3357         {
3358                 cursorPosition = GetCursorPosition();
3359                 start = cursorPosition + offset;
3360                 if (start < 0)
3361                 {
3362                         return;
3363                 }
3364                 end = start + charCount;
3365                 if (end > GetTextLength())
3366                 {
3367                         return;
3368                 }
3369         }
3370
3371         DeleteText(start, end);
3372         __previousCursorPosition = start;
3373
3374         if (IsCopyPasteManagerExist())
3375         {
3376                 InitializeCopyPasteManager();
3377         }
3378         if (IsBlocked() == true)
3379         {
3380                 ReleaseTextBlock();
3381         }
3382         if (__isEditingToken != true)
3383         {
3384                 DrawText();
3385         }
3386
3387         __pTokenEdit->SendTextEvent(CORE_TEXT_EVENT_CHANGED);
3388
3389 #if 0 //ORIGINAL
3390         cursorPosition = GetCursorPosition();
3391         start = cursorPosition + offset;
3392         if (start < 0)
3393         {
3394                 return;
3395         }
3396         end = start + charCount;
3397         if (end > GetTextLength())
3398         {
3399                 return;
3400         }
3401
3402         Rectangle currBounds = __pTokenEdit->GetBounds();
3403         DeleteText(start, end);
3404         __previousCursorPosition = start;
3405         if (__isEditingToken != true)
3406         {
3407                 DrawText();
3408         }
3409 #endif
3410
3411         _Token* pToken = null;
3412
3413         if (__editingTokenIndex >= 0 && __isEditingToken)
3414         {
3415                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3416
3417                 if (pToken)
3418                 {
3419                         float tokenHeight = 0.0f;
3420                         float tokenVerticalSpacing = 0.0f;
3421                         float tokenTextLeftMargin = 0.0f;
3422
3423                         SetCursorPosition(__previousCursorPosition);
3424
3425                         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3426                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
3427                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
3428                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3429
3430                         FloatRectangle tempTextDspRect;
3431                         tempTextDspRect.x += tokenTextLeftMargin;
3432                         tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
3433                         tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
3434                         tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
3435
3436                         SetTextBounds(tempTextDspRect);
3437                         _EditPresenter::SetCursorPosition(start);
3438
3439                         pToken->ResetToken(GetText());
3440                         TrimTokenAndAdjustEllipsisAt(__editingTokenIndex);
3441                 }
3442         }
3443
3444         __pTokenEdit->Draw();
3445 }
3446
3447 bool
3448 _TokenEditPresenter::OnTapGestureDetected(void)
3449 {
3450         if (__editingTokenIndex >= 0)
3451         {
3452                 __isPopupVisible = true;
3453         }
3454
3455         //Uncomment below to Block Copy & Paste functionality in Token Edit mode
3456         if (__pressedTokenIndex != -1)
3457         {
3458                 return true;
3459         }
3460         return _EditPresenter::OnTapGestureDetected();
3461 }
3462
3463 bool
3464 _TokenEditPresenter::CheckCopyPastePopupShowStatus(void)
3465 {
3466         if (__editingTokenIndex < 0)
3467         {
3468                 float controlHeight = __pTokenEdit->GetBoundsF().height;
3469                 FloatRectangle cursorBounds;
3470                 GetCursorBounds(false, cursorBounds);
3471                 if (cursorBounds.y > controlHeight)
3472                 {
3473                         return true;
3474                 }
3475         }
3476
3477         return false;
3478 }
3479
3480 bool
3481 _TokenEditPresenter::OnLongPressGestureDetected(void)
3482 {
3483         if (CheckCopyPastePopupShowStatus())
3484         {
3485                 //TextBounds bigger than control height. Dont show Copy paste popup
3486                 return true;
3487         }
3488
3489         //Discard all long press that is detected after Touch press on a  token
3490         if (__trackTokenIndex >= 0)
3491         {
3492                 return true;
3493         }
3494
3495         __isLongPressed = true;
3496
3497         //Uncomment below to Block Copy & Paste functionality in Token Edit mode
3498         if (__pressedTokenIndex != -1)
3499         {
3500                 return true;
3501         }
3502         return _EditPresenter::OnLongPressGestureDetected();
3503 }
3504
3505 void
3506 _TokenEditPresenter::OnCursorTimerExpired(void)
3507 {
3508         if (__editingTokenIndex != -1)
3509         {
3510                 if (!IsFocused())
3511                 {
3512                         StopCursorTimer();
3513                         return;
3514                 }
3515
3516                 FloatRectangle cursorRect;
3517
3518                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3519                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
3520
3521                 //Set Editing token bounds for text scroll and cursor position calculation (SetTextBounds should have been done prior to this)
3522                 FloatRectangle cursorDspRect = _EditPresenter::GetTextBoundsF();
3523
3524                 if (CalculateCursorBounds(cursorDspRect, cursorRect) != E_SUCCESS)
3525                 {
3526                         StartCursorTimer();
3527                         return;
3528                 }
3529
3530                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3531                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3532
3533                 Canvas* pCursorCanvas = pCursorVisualElement->GetCanvasN();
3534                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get canvas of cursor visual element.");
3535
3536                 bool cursorEnable = IsCursorEnabled();
3537
3538                 pCursorVisualElement->SetBounds(cursorRect);
3539                 DrawCursor(*pCursorCanvas, cursorRect, cursorEnable);
3540
3541                 if (cursorEnable)
3542                 {
3543                         cursorEnable = false;
3544                 }
3545                 else
3546                 {
3547                         cursorEnable = true;
3548                 }
3549                 SetCursorEnabled(cursorEnable);
3550
3551                 pCursorCanvas->Show();
3552                 delete pCursorCanvas;
3553
3554                 StartCursorTimer();
3555         }
3556         else
3557         {
3558                 _EditPresenter::OnCursorTimerExpired();
3559         }
3560 }
3561
3562 bool
3563 _TokenEditPresenter::IsTextBlockedInTokenEdit(void) const
3564 {
3565         if ((IsBlocked() == true) && (__isEditingToken) && (__editingTokenIndex >= 0))
3566         {
3567                 return true;
3568         }
3569         return false;
3570 }
3571
3572 bool
3573 _TokenEditPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
3574 {
3575         __isTouchMoveInProgress = true;
3576
3577         if (GetTokenCount())
3578         {
3579                 //Scrolling is blocked when a popup is visible or when a text is blocked
3580                 if (IsBlocked() == true || __isLongPressed == true)
3581                 {
3582                         return false;
3583                 }
3584
3585                 //Allow touch move only in horizontal direction when editing token
3586                 _TouchInfo TouchInfo(touchinfo);
3587                 _Token* pToken = null;
3588                 if (__editingTokenIndex >= 0)
3589                 {
3590                         if (__touchPressInfo.y > 0.0f)
3591                         {
3592                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3593                                 if (pToken)
3594                                 {
3595                                         float tokenX = pToken->displayRect.x;
3596                                         float tokenY = pToken->displayRect.y;
3597                                         FloatPoint point(touchinfo.GetCurrentPosition().x - tokenX, __touchPressInfo.y - tokenY);
3598                                         TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
3599                                 }
3600
3601                                 bool retValue = _EditPresenter::OnTouchMoved(source, TouchInfo);
3602                                 __previousCursorPosition = GetCursorPosition();
3603
3604                                 return retValue;
3605                         }
3606                         else
3607                         {
3608                                 return false;
3609                         }
3610                 }
3611
3612                 SetTokenBoundsByTouchInfo(touchinfo);
3613
3614                 if (!(__pTokenEdit->GetEditStyle() & EDIT_STYLE_NOSCROLL))
3615                 {
3616                         _Scroll* pScroll = GetScrollBar();
3617                         if (pScroll)
3618                         {
3619                                 float tokenBottomMargin = 0.0f;
3620                                 GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
3621
3622                                 float totalHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
3623                                 float controlHeight = __pTokenEdit->GetBoundsF().height;
3624
3625                                 if (totalHeight > controlHeight)
3626                                 {
3627                                         pScroll->SetScrollVisibility(true);
3628                                 }
3629                                 else
3630                                 {
3631                                         pScroll->SetScrollVisibility(false);
3632                                 }
3633                         }
3634                 }
3635
3636                 return _EditPresenter::OnTouchMoved(source, touchinfo);
3637         }
3638         else
3639         {
3640                 return _EditPresenter::OnTouchMoved(source, touchinfo);
3641         }
3642
3643 }
3644
3645 void
3646 _TokenEditPresenter::OnVisualElementAnimationFinished(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, bool completedNormally)
3647 {
3648         __isAnimationInProgress = false;
3649         RemoveTokenAt(GetTokenCount() - 1);
3650         CalculateTokenPositionFromIndex(GetTokenCount() - 1);
3651
3652         for (int i = GetTokenCount() - 1; i < GetTokenCount() - 1 + 1; i++)
3653         {
3654                 _Token* pToken = null;
3655                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3656
3657                 if (pToken)
3658                 {
3659                         pToken->SetBounds(pToken->displayRect);
3660                 }
3661         }
3662
3663         if (__lastTokenIndex == __pressedTokenIndex)
3664         {
3665                 __pressedTokenIndex--;
3666         }
3667
3668         if (GetTokenCount() == 0 && __animatingIndex == 0)
3669         {
3670                 DrawText();
3671         }
3672         __animatingIndex = -1;
3673         InitializeCursor();
3674
3675         DrawToken();
3676
3677         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
3678
3679         return;
3680 }
3681
3682 void
3683 _TokenEditPresenter::OnTickOccurred(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, const Tizen::Ui::Variant& currentValue)
3684 {
3685         VisualElementValueAnimation* pAnimation = dynamic_cast< VisualElementValueAnimation* >(const_cast< VisualElementAnimation* >(&animation));
3686         VisualElement* pPresentation = const_cast< VisualElement* >(target.AcquirePresentationInstance());
3687
3688         if (pPresentation && pAnimation)
3689         {
3690                 float diffX = 0.0f;
3691                 float diffY = 0.0f;
3692
3693                 diffX = pPresentation->GetBounds().x;
3694                 diffY = pPresentation->GetBounds().y;
3695
3696                 Variant newBounds(FloatRectangle(diffX, diffY, currentValue.ToFloatRectangle().width, currentValue.ToFloatRectangle().height));
3697                 _VisualElementImpl::GetInstance(*pPresentation)->SetProperty(VePropBounds, newBounds);
3698         }
3699         else
3700         {
3701                 SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get presentation instance of visual element.");
3702         }
3703
3704         target.ReleasePresentationInstance();
3705 }
3706
3707 void
3708 _TokenEditPresenter::OnTimerExpired(Timer& timer)
3709 {
3710         Timer* onTimer = &timer;
3711         Canvas* pDescriptionTextCanvas = null;
3712
3713         if (onTimer == _EditPresenter::__pTitleSlidingTimer)
3714         {
3715                 if (!IsFocused())
3716                 {
3717                         _EditPresenter::StopTitleSlidingTimer();
3718                         __isTitleSliding = false;
3719                         return;
3720                 }
3721
3722                 FloatRectangle tempDescriptionTextRect = __descriptionTextRect;
3723                 tempDescriptionTextRect.x = 0.0f;
3724                 tempDescriptionTextRect.y = 0.0f;
3725
3726                 pDescriptionTextCanvas = __pDescriptionTextVisualElement->GetCanvasN();
3727                 SysTryReturnVoidResult(NID_UI_CTRL, pDescriptionTextCanvas, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pDescriptionTextCanvas is invalid!");
3728
3729                 pDescriptionTextCanvas->SetBackgroundColor(Color(0));
3730                 pDescriptionTextCanvas->Clear();
3731                 __pDescriptionTextTextObject->SetBounds(tempDescriptionTextRect);
3732                 __pDescriptionTextTextObject->DrawWithOffset(*_CanvasImpl::GetInstance(*pDescriptionTextCanvas));
3733                 Rectangle descriptionTextRect = _CoordinateSystemUtils::ConvertToInteger(tempDescriptionTextRect);
3734                 pDescriptionTextCanvas->Show(descriptionTextRect);
3735
3736                 delete pDescriptionTextCanvas;
3737
3738                 _EditPresenter::StartTitleSlidingTimer();
3739                 __isTitleSliding = true;
3740         }
3741         else
3742         {
3743                 _EditPresenter::OnTimerExpired(timer);
3744         }
3745 }
3746
3747 result
3748 _TokenEditPresenter::ChangeInternalLayout(_ControlOrientation orientation)
3749 {
3750         result r = E_SUCCESS;
3751
3752         __scrollValue = 0.0f;
3753         __maxScrollValue = 0.0f;
3754         float tokenBottomMargin = 0.0f;
3755         float newScrollValue = 0.0f;
3756
3757         FloatRectangle windowBounds = GetInitialBoundsF();
3758
3759         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
3760
3761         r = CalculateTokenPositionFromIndex(0);
3762         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3763
3764         r = SetInitialBounds();
3765         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3766
3767         if (__isEditingToken)
3768         {
3769                 if (__editingTokenIndex >= 0 && __editingTokenIndex < GetTokenCount())
3770                 {
3771                         String inputTokenString = GetText();
3772                         String replacementString = inputTokenString;
3773                         bool enable = false;
3774
3775                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
3776                         if (enable)
3777                         {
3778                                 inputTokenString = replacementString;
3779                         }
3780
3781                         if (inputTokenString.GetLength() > 0)
3782                         {
3783                                 int index = __editingTokenIndex;
3784                                 RemoveTokenAt(__editingTokenIndex, true);
3785                                 InsertTokenAt(index, inputTokenString);
3786                         }
3787                 }
3788         }
3789
3790         if (__pTokenEdit->IsAttachedToMainTree() && IsFocused())
3791         {
3792                 r = AdjustFlexibleHeight();
3793                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3794
3795                 r = CheckTokenScrolling();
3796                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3797         }
3798         else
3799         {
3800                 if (!__autoShrink)
3801                 {
3802                         r = AdjustFlexibleHeight();
3803                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3804
3805                         windowBounds = __pTokenEdit->GetBoundsF();
3806                 }
3807
3808                 newScrollValue = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin - windowBounds.height;
3809                 if (newScrollValue > 0.0f)
3810                 {
3811                         __maxScrollValue = newScrollValue;
3812                         __isNeedToScroll = true;
3813                 }
3814         }
3815
3816         return r;
3817 }
3818
3819 result
3820 _TokenEditPresenter::ChangeLayout(_ControlOrientation orientation)
3821 {
3822         result r = E_SUCCESS;
3823
3824         r = _EditPresenter::ChangeLayout(orientation);
3825         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3826
3827         r = ChangeInternalLayout(orientation);
3828
3829         return r;
3830 }
3831
3832 void
3833 _TokenEditPresenter::OnBoundsChanged(void)
3834 {
3835         if (!__isTokenEditPresenterInitialized)
3836         {
3837                 return;
3838         }
3839
3840         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3841         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
3842
3843         if (IsUpdateInitialBounds())
3844         {
3845                 SetControlInitialBounds(tokenEditBounds);
3846         }
3847         ChangeInternalLayout(orientation);
3848
3849         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
3850
3851         return;
3852 }
3853
3854 result
3855 _TokenEditPresenter::AttachCursorToToken(void)
3856 {
3857         result r = E_SUCCESS;
3858
3859         if (__pressedTokenIndex != -1)
3860         {
3861                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3862                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
3863
3864                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3865                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
3866
3867                 _Token* pToken = null;
3868                 _VisualElement* pTokenVisualElement = null;
3869
3870                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
3871                 if (pToken)
3872                 {
3873                         pTokenVisualElement = pToken->GetVisualElement();
3874                         SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
3875
3876                         if (pCursorVisualElement->GetParent() != pTokenVisualElement)
3877                         {
3878                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
3879                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3880
3881                                 r = pTokenVisualElement->AttachChild(*pCursorVisualElement);
3882                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3883                         }
3884                 }
3885         }
3886
3887         return r;
3888 }
3889
3890 result
3891 _TokenEditPresenter::DetachCursorFromToken(void)
3892 {
3893         result r = E_SUCCESS;
3894
3895         if (__pressedTokenIndex != -1)
3896         {
3897                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3898                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3899
3900                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3901                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3902
3903                 _Token* pToken = null;
3904                 _VisualElement* pTokenVisualElement = null;
3905
3906                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
3907
3908                 if (pToken)
3909                 {
3910                         pTokenVisualElement = pToken->GetVisualElement();
3911                         SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3912
3913                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3914                         {
3915                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3916                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3917
3918                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3919                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3920                         }
3921                 }
3922         }
3923         return r;
3924 }
3925
3926 void
3927 _TokenEditPresenter::ExitTokenEditingMode(void)
3928 {
3929         result r = E_SUCCESS;
3930
3931         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3932         SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3933
3934         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3935         SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3936
3937         _Token* pToken = null;
3938         _VisualElement* pTokenVisualElement = null;
3939
3940         pToken = static_cast <_Token*>(__pTokenList->GetAt(__editingTokenIndex));
3941
3942         if (pToken)
3943         {
3944                 pTokenVisualElement = pToken->GetVisualElement();
3945                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3946
3947                 if (pCursorVisualElement->GetParent() != pEditVisualElement)
3948                 {
3949                         r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3950                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3951
3952                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3953                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3954                 }
3955         }
3956
3957         String inputTokenString = GetText();
3958         String replacementString = inputTokenString;
3959         bool enable = false;
3960
3961         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
3962         if (enable)
3963         {
3964                 inputTokenString = replacementString;
3965         }
3966
3967         r = RemoveTokenAt(__editingTokenIndex);
3968
3969         _EditPresenter::SetTextSize(__editContentFontSize);
3970         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
3971
3972         if (inputTokenString.GetLength() > 0)
3973         {
3974                 r = InsertTokenAt(__editingTokenIndex, inputTokenString);
3975                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
3976
3977                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__editingTokenIndex));
3978                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3979
3980                 pToken->currTokenLength = inputTokenString.GetLength();
3981         }
3982
3983         CalculateTokenPositionFromIndex(0);
3984         int lastTokenIndex = GetTokenCount() - 1;
3985         for (int i = 0; i < lastTokenIndex + 1; i++)
3986         {
3987                 _Token* pToken = null;
3988                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3989
3990                 if (pToken)
3991                 {
3992                         pToken->SetBounds(pToken->displayRect);
3993                 }
3994         }
3995
3996         AdjustFlexibleHeight();
3997         __pressedTokenIndex = -1;
3998         __isTokenEditingFinished = true;
3999         __editingTokenIndex = -1;
4000         __isEditingToken = false;
4001
4002         CheckTokenScrolling();
4003         SetCursorDisabled(false);
4004
4005         if (inputTokenString.GetLength() <= 0)
4006         {
4007                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
4008         }
4009
4010         return;
4011 }
4012
4013 bool
4014 _TokenEditPresenter::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
4015 {
4016         _KeyCode keyCode = keyInfo.GetKeyCode();
4017         bool focusChanged = false;
4018         int tokenCount = GetTokenCount();
4019         int lastTokenIndex = tokenCount - 1;
4020
4021         if (IsUsbKeyboardConnected() && (keyInfo.GetKeyModifier() & _KEY_MODIFIER_CTRL))
4022         {
4023                 switch (keyCode)
4024                 {
4025                 case _KEY_A:
4026                 case _KEY_C:
4027                 case _KEY_X:
4028                 case _KEY_V:
4029                         if (__isEditingToken)
4030                         {
4031                                 return true;
4032                         }
4033                         break;
4034
4035                 default:
4036                         break;
4037                 }
4038         }
4039
4040         if ((keyCode == _KEY_NUM_LEFT) || (keyCode == _KEY_LEFT))
4041         {
4042                 if (__drawFocusState && (!__isEditingToken) && (tokenCount > 0))
4043                 {
4044                         if (__focusedTokenIndex == -1)
4045                         {
4046                                 if (GetCursorPosition() == 0)
4047                                 {
4048                                         __focusedTokenIndex = lastTokenIndex;
4049                                         if (GetTextLength() > 0)
4050                                         {
4051                                                 MakeToken();
4052                                                 StopCursorTimer();
4053                                                 SetCursorDisabled(true);
4054                                                 __pTokenEdit->Invalidate();
4055
4056                                                 StartCursorTimer();
4057
4058                                                 return _EditPresenter::OnKeyPressed(source, keyInfo);
4059                                         }
4060                                         focusChanged = true;
4061                                 }
4062                         }
4063                         else
4064                         {
4065                                 if (__focusedTokenIndex > 0)
4066                                 {
4067                                         __focusedTokenIndex--;
4068                                         focusChanged = true;
4069                                 }
4070                         }
4071                 }
4072         }
4073
4074         if ((keyCode == _KEY_NUM_RIGHT) || (keyCode == _KEY_RIGHT))
4075         {
4076                 if (__drawFocusState && (!__isEditingToken) && (tokenCount > 0))
4077                 {
4078                         if (__focusedTokenIndex != -1)
4079                         {
4080                                 if (__focusedTokenIndex == lastTokenIndex)
4081                                 {
4082                                         __focusedTokenIndex = -1;
4083                                         focusChanged = true;
4084                                 }
4085                                 else
4086                                 {
4087                                         __focusedTokenIndex++;
4088                                         focusChanged = true;
4089                                 }
4090                         }
4091                 }
4092         }
4093
4094         if (focusChanged)
4095         {
4096                 StopCursorTimer();
4097
4098                 if (__focusedTokenIndex != -1)
4099                 {
4100                         SetCursorDisabled(true);
4101                         ScrollToFocusedToken();
4102                 }
4103                 else
4104                 {
4105                         SetCursorDisabled(false);
4106                         CheckTokenScrolling();
4107                 }
4108
4109                 StartCursorTimer();
4110         }
4111
4112         return _EditPresenter::OnKeyPressed(source, keyInfo);
4113 }
4114
4115 String
4116 _TokenEditPresenter::GetTextAccessibilityElementText(void) const
4117 {
4118         String tokenText;
4119         String spaceText = " ";
4120         _Token* pToken = null;
4121         int tokenCount = GetTokenCount();
4122         static const int readTokenCount = 2;
4123         if (tokenCount > 0)
4124         {
4125                 for (int index = 0; index < readTokenCount; index++)
4126                 {
4127                         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4128                         SysTryReturn(NID_UI_CTRL, pToken, tokenText, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4129
4130                         if (index < (readTokenCount - 1))
4131                         {
4132                                 tokenText += pToken->GetText() + spaceText;
4133                         }
4134                         else
4135                         {
4136                                 String moreTokenText;
4137                                 int moreTokenCount = tokenCount - readTokenCount;
4138                                 if (moreTokenCount > 0)
4139                                 {
4140                                         moreTokenText.Format(15, L"and %d more", moreTokenCount);
4141                                 }
4142                                 tokenText += pToken->GetText() + spaceText + moreTokenText;
4143                         }
4144                 }
4145         }
4146         return tokenText;
4147 }
4148
4149 void
4150 _TokenEditPresenter::RefreshAccessibilityElements(void)
4151 {
4152         RemoveChildAccessibilityElements();
4153         AddChildAccessibilityElements();
4154
4155         return;
4156 }
4157
4158 result
4159 _TokenEditPresenter::AddChildAccessibilityElements(void)
4160 {
4161         //Accessibility Elements added to the container upon focus gained
4162         //1.Title
4163         //2.Token(s)
4164         //3. __accessibilityElements 0 - title 1 - token 0
4165
4166         result r = E_SUCCESS;
4167         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4168
4169         __pTokenEdit->AddTitleAccessibilityElement();
4170         int tokenCount = GetTokenCount();
4171
4172         for (int index = 0; index < tokenCount; index++)
4173         {
4174                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4175                 SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4176
4177                 if (pContainer)
4178                 {
4179                         _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4180                         SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4181
4182                         String labelText = pToken->GetText();
4183                         pAccessibilityElement->SetBounds(pToken->displayRect);
4184                         pAccessibilityElement->SetLabel(labelText);
4185                         pContainer->AddElement(*pAccessibilityElement);
4186                         __accessibilityElements.Add(pAccessibilityElement);
4187                 }
4188         }
4189
4190         __pTokenEdit->AddCursorAccessibilityElement();
4191
4192         return r;
4193 }
4194
4195 void
4196 _TokenEditPresenter::RemoveChildAccessibilityElements(void)
4197 {
4198         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4199         _AccessibilityElement* pAccessibilityElement = null;
4200
4201         __pTokenEdit->RemoveTitleAccessibilityElement();
4202
4203         while (__accessibilityElements.GetCount() > 0)
4204         {
4205                 if ((__accessibilityElements.GetAt(0, pAccessibilityElement)) == E_SUCCESS)
4206                 {
4207                         __accessibilityElements.RemoveAt(0);
4208                         pContainer->RemoveElement(*pAccessibilityElement);
4209                 }
4210         }
4211
4212         __pTokenEdit->RemoveCursorAccessibilityElement();
4213
4214         return;
4215 }
4216
4217 result
4218 _TokenEditPresenter::AppendTokenAccessibilityElement(void)
4219 {
4220         result r = E_SUCCESS;
4221         int tokenCount = GetTokenCount();
4222
4223         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4224
4225         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
4226         SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4227
4228         if (pContainer)
4229         {
4230                 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4231                 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4232
4233                 String labelText = pToken->GetText();
4234                 pAccessibilityElement->SetBounds(pToken->displayRect);
4235                 pAccessibilityElement->SetLabel(labelText);
4236                 pAccessibilityElement->SetHint(L"double tap to edit");
4237                 pContainer->AddElement(*pAccessibilityElement);
4238                 __accessibilityElements.Add(pAccessibilityElement);
4239         }
4240
4241         return r;
4242 }
4243
4244 result
4245 _TokenEditPresenter::InsertTokenAccessibilityElementAt(int index)
4246 {
4247         result r = E_SUCCESS;
4248
4249         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4250         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4251         SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4252
4253         if (pContainer)
4254         {
4255                 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4256                 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4257
4258                 String labelText = pToken->GetText();
4259                 pAccessibilityElement->SetBounds(pToken->displayRect);
4260                 pAccessibilityElement->SetLabel(labelText);
4261                 pContainer->InsertElement(*pAccessibilityElement, index);
4262                 __accessibilityElements.InsertAt(pAccessibilityElement, index);
4263         }
4264
4265         return r;
4266 }
4267
4268 void
4269 _TokenEditPresenter::RemoveTokenAccessibilityElementAt(int index)
4270 {
4271         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4272         _AccessibilityElement* pAccessibilityElement = null;
4273
4274         if (pContainer)
4275         {
4276                 if (__accessibilityElements.GetCount() > 0)
4277                 {
4278                         if ((__accessibilityElements.GetAt(index, pAccessibilityElement)) == E_SUCCESS)
4279                         {
4280                                 __accessibilityElements.RemoveAt(index);
4281                                 pContainer->RemoveElement(*pAccessibilityElement);
4282                         }
4283                 }
4284         }
4285         return;
4286 }
4287
4288 result
4289 _TokenEditPresenter::UpdateTokenAccessibilityBounds(void)
4290 {
4291         result r = E_SUCCESS;
4292         int tokenCount = GetTokenCount();
4293         _AccessibilityElement* pAccessibilityElement = null;
4294
4295         for (int index = 0; index < tokenCount; index++)
4296         {
4297                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4298                 SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4299
4300                 if ((__accessibilityElements.GetAt(index, pAccessibilityElement)) == E_SUCCESS)
4301                 {
4302                         if (pAccessibilityElement)
4303                         {
4304                                 pAccessibilityElement->SetBounds(pToken->displayRect);
4305                         }
4306                 }
4307         }
4308         return r;
4309 }
4310
4311 result
4312 _TokenEditPresenter::UpdateTitleAccessibilityBounds(const FloatRectangle& titleBounds)
4313 {
4314         _AccessibilityElement* pTitleAccessibilityElement = __pTokenEdit->GetTitleTextAccessibilityElement();
4315
4316         if (!pTitleAccessibilityElement)
4317         {
4318                 return E_SUCCESS;
4319         }
4320
4321         pTitleAccessibilityElement->SetBounds(titleBounds);
4322
4323         return E_SUCCESS;
4324 }
4325
4326 result
4327 _TokenEditPresenter::ScrollToFocusedTokenAccessibilityElement(const _AccessibilityElement& element)
4328 {
4329         result r = E_SUCCESS;
4330         int focusedTokenIndex = -1;
4331         int tokenCount = GetTokenCount();
4332         FloatRectangle focusedTokenRectangle;
4333         float newScrollValue = 0.0f;
4334         float tokenTopMargin = 0.0f;
4335         float tokenBottomMargin = 0.0f;
4336         float tokenHeight = 0.0f;
4337         float textBoundsAlignValue = 0.0f;
4338         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
4339         _AccessibilityElement* pAccessibilityElement = null;
4340
4341         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
4342         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
4343         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
4344
4345         FloatRectangle tokenEditRect = __pTokenEdit->GetBoundsF();
4346         float textObjectMaxHeight = GetMaxTextHeight();
4347         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
4348
4349         pAccessibilityElement = const_cast< _AccessibilityElement* >(&element);
4350         r = __accessibilityElements.IndexOf(pAccessibilityElement, focusedTokenIndex);
4351         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4352
4353         if ((focusedTokenIndex > -1) && (focusedTokenIndex < tokenCount))
4354         {
4355                 _Token* pToken = null;
4356                 pToken = static_cast< _Token* >(__pTokenList->GetAt(focusedTokenIndex));
4357                 r = GetLastResult();
4358                 SysTryReturnResult(NID_UI_CTRL, pToken != null, r, "Propagating.");
4359
4360                 focusedTokenRectangle = pToken->displayRect;
4361
4362                 float focusedTokenPosition = focusedTokenRectangle.y + focusedTokenRectangle.height;
4363
4364                 if ((focusedTokenRectangle.y > 0) && (focusedTokenPosition < tokenEditRect.height))
4365                 {
4366                         DrawToken();
4367                 }
4368                 else
4369                 {
4370                         if (focusedTokenRectangle.y < 0)
4371                         {
4372                                 newScrollValue = focusedTokenRectangle.y - tokenTopMargin - __scrollValue;
4373                         }
4374                         else
4375                         {
4376                                 newScrollValue = focusedTokenPosition - textBoundsAlignValue - tokenEditRect.height + tokenBottomMargin - __scrollValue;
4377                         }
4378
4379                         r = RecalculateTokenBounds(newScrollValue);
4380                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4381                 }
4382
4383                 r = __accessibilityElements.GetAt(focusedTokenIndex, pAccessibilityElement);
4384                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4385
4386                 _AccessibilityManager* pAccessibilityManager = _AccessibilityManager::GetInstance();
4387                 pAccessibilityManager->SetGlobalFocusedElement(*pAccessibilityElement);
4388                 pAccessibilityManager->RequestToDrawFocusUi();
4389         }
4390
4391         return r;
4392 }
4393
4394 result
4395 _TokenEditPresenter::ScrollToTitleAccessibilityElement(void)
4396 {
4397         _AccessibilityElement* pTitleAccessibilityElement = __pTokenEdit->GetTitleTextAccessibilityElement();
4398         if (pTitleAccessibilityElement)
4399         {
4400                 FloatRectangle titleTextBounds = pTitleAccessibilityElement->GetBounds();
4401                 if (titleTextBounds.y < 0)
4402                 {
4403                         float tokenTopMargin = 0.0f;
4404                         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
4405                         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
4406                         float newScrollValue = titleTextBounds.y - tokenTopMargin - __scrollValue;
4407                         result r = RecalculateTokenBounds(newScrollValue);
4408                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4409                 }
4410         }
4411
4412         return E_SUCCESS;
4413 }
4414
4415 bool
4416 _TokenEditPresenter::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
4417 {
4418         result r = E_SUCCESS;
4419         _AccessibilityElement* pTitleAccessibilityElement = __pTokenEdit->GetTitleTextAccessibilityElement();
4420
4421         __accessibilityFocusIn = true;
4422
4423         if (__focusOutIndex < 0) // Not a token accessibility element
4424         {
4425                 if (pTitleAccessibilityElement != &element)
4426                 {
4427                         return false;
4428                 }
4429         }
4430
4431         _Token* pToken = null;
4432         _AccessibilityElement* pCurrentElement = const_cast< _AccessibilityElement* >(&element);
4433         _AccessibilityElement* pPreviousAccessibilityElement = null;
4434         _AccessibilityElement* pNextAccessibilityElement = null;
4435         if (pTitleAccessibilityElement == &element)
4436         {
4437                 ScrollToTitleAccessibilityElement();
4438                 UpdateTokenAccessibilityBounds();
4439                 return false;
4440         }
4441         r = __accessibilityElements.GetAt(__focusOutIndex, pPreviousAccessibilityElement);
4442         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
4443
4444         if (pPreviousAccessibilityElement && pPreviousAccessibilityElement->GetAbsoluteBounds().y > element.GetAbsoluteBounds().y)  //Left flick
4445         {
4446                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__focusOutIndex - 1));
4447                 if (pToken)
4448                 {
4449                         if (pToken->displayRect.y < 0.0f && __focusOutIndex > 0)
4450                         {
4451                                 r = __accessibilityElements.GetAt(__focusOutIndex - 1, pNextAccessibilityElement);
4452                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
4453
4454                                 ScrollToFocusedTokenAccessibilityElement(*pNextAccessibilityElement);
4455                         }
4456                 }
4457         }
4458
4459         if (pPreviousAccessibilityElement && pPreviousAccessibilityElement->GetAbsoluteBounds().y < element.GetAbsoluteBounds().y)  //Right flick
4460         {
4461                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__focusOutIndex + 1));
4462                 if (pToken)
4463                 {
4464                         if (pToken->displayRect.y + pToken->displayRect.height > __pTokenEdit->GetBoundsF().height)
4465                         {
4466                                 r = __accessibilityElements.GetAt(__focusOutIndex + 1, pNextAccessibilityElement);
4467                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
4468
4469                                 ScrollToFocusedTokenAccessibilityElement(*pNextAccessibilityElement);
4470                         }
4471                 }
4472         }
4473
4474         if (pToken == null) //Draw Cursor Accessibility Element
4475         {
4476                 _AccessibilityElement* pCursorAccessibilityElement = __pTokenEdit->GetCursorAccessibilityElement();
4477                 if (pCursorAccessibilityElement && pPreviousAccessibilityElement && pCursorAccessibilityElement != pPreviousAccessibilityElement)
4478                 {
4479                         if (GetTextBoundsF().y - pPreviousAccessibilityElement->GetBounds().y > pPreviousAccessibilityElement->GetBounds().height)  //check for different lines
4480                         {
4481                                 float height = GetTextBoundsF().height + pPreviousAccessibilityElement->GetBounds().y + pPreviousAccessibilityElement->GetBounds().height;
4482                                 if (pCurrentElement == pCursorAccessibilityElement && height > __pTokenEdit->GetBoundsF().height)
4483                                 {
4484                                         float tokenBottomMargin = 0.0f;
4485                                         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
4486                                         float newScrollValue = GetTextBoundsF().height + tokenBottomMargin - __scrollValue;
4487                                         RecalculateTokenBounds(newScrollValue);
4488                                         _AccessibilityManager::GetInstance()->SetGlobalFocusedElement(*(__pTokenEdit->GetCursorAccessibilityElement()));
4489                                         _AccessibilityManager::GetInstance()->RequestToDrawFocusUi();
4490                                 }
4491                         }
4492                 }
4493         }
4494
4495         __focusOutIndex = -1;
4496         UpdateTokenAccessibilityBounds();
4497
4498         return false;
4499 }
4500
4501 bool
4502 _TokenEditPresenter::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
4503 {
4504         _AccessibilityElement* pAccessibilityElement = null;
4505         pAccessibilityElement = const_cast< _AccessibilityElement* >(&element);
4506         __accessibilityElements.IndexOf(pAccessibilityElement, __focusOutIndex);
4507         __accessibilityFocusIn = false;
4508
4509         return false;
4510 }
4511
4512 bool
4513 _TokenEditPresenter::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
4514 {
4515         _AccessibilityElement* pTextAccessibilityElement = __pTokenEdit->GetTextAccessibilityElement();
4516         if (pTextAccessibilityElement != null)
4517         {
4518                 if (pTextAccessibilityElement == &element)
4519                 {
4520                         if (__pTokenEdit->IsInternalFocused()) // Set cursor as global focus accessibility element only in focused mode.
4521                         {
4522                                 _AccessibilityElement* pCursorAccessibilityElement = __pTokenEdit->GetCursorAccessibilityElement();
4523                                 if (pCursorAccessibilityElement)
4524                                 {
4525                                         _AccessibilityManager::GetInstance()->SetGlobalFocusedElement(*pCursorAccessibilityElement);
4526                                         _AccessibilityManager::GetInstance()->RequestToDrawFocusUi();
4527                                 }
4528                         }
4529                 }
4530         }
4531
4532         return false;
4533 }
4534
4535 result
4536 _TokenEditPresenter::ScrollToFocusedToken(void)
4537 {
4538         result r = E_SUCCESS;
4539         FloatRectangle focussedTokenRectangle;
4540         float newScrollValue = 0.0f;
4541         float tokenTopMargin = 0.0f;
4542         float tokenBottomMargin = 0.0f;
4543         float tokenHeight = 0.0f;
4544         float textBoundsAlignValue = 0.0f;
4545         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
4546
4547
4548         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
4549         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
4550         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
4551
4552         FloatRectangle tokenEditRect = __pTokenEdit->GetBoundsF();
4553         float textObjectMaxHeight = GetMaxTextHeight();
4554         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
4555
4556         if (__focusedTokenIndex == -1)
4557         {
4558                 //Focus bitmap to be reset when no token is focused.
4559                 DrawToken();
4560         }
4561         else
4562         {
4563                 _Token* pToken = null;
4564                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__focusedTokenIndex));
4565                 r = GetLastResult();
4566                 SysTryReturnResult(NID_UI_CTRL, pToken != null, r, "Propagating.");
4567
4568                 focussedTokenRectangle = pToken->displayRect;
4569
4570                 float focussedTokenPosition= focussedTokenRectangle.y + focussedTokenRectangle.height ;
4571
4572                 if ((focussedTokenRectangle.y > 0) && (focussedTokenPosition < tokenEditRect.height))
4573                 {
4574                         //Focused token is within the tokenEdit boundary
4575                         DrawToken();
4576                 }
4577                 else
4578                 {
4579                         if (focussedTokenRectangle.y < 0)
4580                         {
4581                                 //Focused token is above the upper boundary
4582                                 newScrollValue = focussedTokenRectangle.y - tokenTopMargin - __scrollValue;
4583                         }
4584                         else
4585                         {
4586                                 //Focused token is below the lower boundary
4587                                 newScrollValue = focussedTokenPosition - textBoundsAlignValue - tokenEditRect.height + tokenBottomMargin - __scrollValue;
4588                         }
4589
4590                         r = RecalculateTokenBounds(newScrollValue);
4591                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4592                 }
4593         }
4594
4595         return r;
4596 }
4597
4598 void
4599 _TokenEditPresenter::SetDrawFocusState(bool focusState)
4600 {
4601         if (!focusState)
4602         {
4603                 __focusedTokenIndex = -1;
4604         }
4605         __drawFocusState = focusState;
4606
4607         return;
4608 }
4609
4610 int
4611 _TokenEditPresenter::GetFocusedTokenIndex(void) const
4612 {
4613         return __focusedTokenIndex;
4614 }
4615
4616 void
4617 _TokenEditPresenter::PrepareFocusUiMode(void)
4618 {
4619         if (__editingTokenIndex != -1)
4620         {
4621                 ExitTokenEditingMode();
4622         }
4623         else if (__pressedTokenIndex != -1)
4624         {
4625                 __pressedTokenIndex = -1;
4626                 DetachCursorFromToken();
4627                 StopCursorTimer();
4628                 SetCursorDisabled(false);
4629                 __pTokenEdit->Invalidate();
4630                 StartCursorTimer();
4631         }
4632
4633         return;
4634 }
4635
4636 void
4637 _TokenEditPresenter::PerformRemoveTokenAnimation(VisualElement& source)
4638 {
4639         if (__pTimingFunction == null)
4640         {
4641                 __pTimingFunction = new (std::nothrow) SineTimingFunction();
4642                 SysTryReturnVoidResult(NID_UI_CTRL, __pTimingFunction, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4643         }
4644
4645         AnimationTransaction::Begin();
4646
4647         VisualElementPropertyAnimation* pOpacityAnimation = new (std::nothrow) VisualElementPropertyAnimation();
4648         SysTryReturnVoidResult(NID_UI_CTRL, pOpacityAnimation, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4649
4650         pOpacityAnimation->SetPropertyName("opacity");
4651         pOpacityAnimation->SetStartValue(Variant(1.0f));
4652         pOpacityAnimation->SetEndValue(Variant(0.0f));
4653
4654         pOpacityAnimation->SetDuration(ANIMATION_DURATION_OPACITY);
4655         pOpacityAnimation->SetTimingFunction(__pTimingFunction);
4656
4657         source.AddAnimation("OpacityAnimation", *pOpacityAnimation);
4658         delete pOpacityAnimation;
4659
4660         VisualElementValueAnimation* pBoundsAnimation = new (std::nothrow) VisualElementValueAnimation();               //deletion will happen in catch/ in Destroy animation
4661         SysTryReturnVoidResult(NID_UI_CTRL, pBoundsAnimation, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4662
4663         pBoundsAnimation->SetTimingFunction(__pTimingFunction);
4664         FloatRectangle startValue = source.GetBounds();
4665         startValue.x = startValue.x + startValue.width * 0.05;
4666         startValue.y = startValue.y + startValue.height * 0.05;
4667         startValue.width = startValue.width * 0.9;
4668         startValue.height = startValue.height * 0.9;
4669
4670         pBoundsAnimation->SetStartValue(Variant(source.GetBounds()));
4671         pBoundsAnimation->SetEndValue(Variant(startValue));
4672         pBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
4673         pBoundsAnimation->SetVisualElementAnimationTickEventListener(this);
4674         pBoundsAnimation->SetDuration(ANIMATION_DURATION_BOUNDS);
4675         source.AddAnimation(L"BoundsAnimation", *pBoundsAnimation);
4676         delete pBoundsAnimation;
4677
4678         AnimationTransaction::Commit();
4679
4680         return;
4681 }
4682
4683 }}} //Tizen::Ui::Controls