Tizen 2.1 base
[framework/osp/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 Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @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 "FUiAnim_VisualElement.h"
25 #include "FUi_UiTouchEvent.h"
26 #include "FUiCtrl_TokenEdit.h"
27 #include "FUiCtrl_TokenEditPresenter.h"
28 #include "FUiCtrl_Scroll.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Media;
32 using namespace Tizen::Ui::Animations;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Graphics::_Text;
35
36 namespace Tizen { namespace Ui { namespace Controls
37 {
38
39 float
40 SineTimingFunction::CalculateProgress(float timeProgress) const
41 {
42         const float segments[3][3] = {{0.0f, 0.01f, 0.37f}, {0.37f, 0.72f, 0.888f}, {0.888f, 0.9999f, 1.0f}};
43         float timeProgressValue = timeProgress;
44         int segmentsLength = 3; //Length of the segments array
45         int index = (int)floor(segmentsLength * timeProgressValue);
46         if (index >= segmentsLength)
47         {
48                 index = segmentsLength - 1;
49         }
50
51         float progressValue = (timeProgressValue - index * (1.0 / segmentsLength)) * segmentsLength;
52         float segmentAtIndex[3];
53         for(int i = 0; i < 3; i++)
54         {
55                 segmentAtIndex[i] = segments[index][i];
56         }
57         float ret = 0 + 1 * (segmentAtIndex[0] + progressValue * (2 * (1 - progressValue) * (segmentAtIndex[1] - segmentAtIndex[0]) + progressValue * (segmentAtIndex[2] - segmentAtIndex[0])));
58
59         return ret;
60 }
61
62 class _Token
63         : public Object
64 {
65 public:
66         _Token(const String& text, Font* pEditFont);
67         virtual ~_Token(void);
68         int GetTextPixelWidth(void) const;
69         wchar_t* GetText(void) const;
70         result ResetToken(const String& text);
71         result SetBounds(Rectangle bounds);
72         _VisualElement* GetVisualElement(void) const;
73         _Token(const _Token& src);
74         _Token& operator =(const _Token& value);
75
76 public:
77         Rectangle displayRect;
78         TextObject* pTextObject;
79         Font* pFont;
80         int currTokenLength;
81         bool isImplicitAnimation;
82         bool isTextCut;
83 private:
84         wchar_t* __pTextBuffer;
85         int __textPixelWidth;
86         int __textPixelHeight;
87         _VisualElement* __pVisualElement;
88 };      // _Token
89
90 _Token::_Token(const String& text, Font* pEditFont)
91         : pTextObject(null)
92         , pFont(null)
93         , currTokenLength(0)
94         , isImplicitAnimation(true)
95         , isTextCut(false)
96         , __pTextBuffer(null)
97         , __textPixelWidth(0)
98         , __textPixelHeight(0)
99         , __pVisualElement(null)
100 {
101         result r = E_SUCCESS;
102
103         currTokenLength = text.GetLength();
104         int tokenFontSize = 0;
105
106         pTextObject = new (std::nothrow) TextObject;
107         SysTryReturnVoidResult(NID_UI_CTRL, pTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
108
109         // for default initialize.
110         r = pTextObject->Construct();
111         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
112
113         r = pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
114         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
115
116         r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
117         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
118
119         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, tokenFontSize);
120
121         pFont = new (std::nothrow) Font;
122         SysTryCatch(NID_UI_CTRL, pFont != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
123
124         r = pFont->Construct(pEditFont->GetFaceName(), FONT_STYLE_PLAIN, tokenFontSize);
125         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
126
127         r = pTextObject->SetFont(pFont, 0, pTextObject->GetTextLength());
128         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
129
130         r = ResetToken(text);
131         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
132
133         __pVisualElement = new (std::nothrow) _VisualElement;
134         SysTryCatch(NID_UI_CTRL, __pVisualElement != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
135
136         r = __pVisualElement->Construct();
137         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct _VisualElement.", GetErrorMessage(r));
138
139         __pVisualElement->SetImplicitAnimationEnabled(false);
140
141         return;
142
143 CATCH:
144         delete pTextObject;
145         pTextObject = null;
146
147         if (__pVisualElement)
148         {
149                 __pVisualElement->Destroy();
150                 __pVisualElement = null;
151         }
152
153         delete pFont;
154         pFont = null;
155
156         return;
157 }
158
159 result
160 _Token::ResetToken(const String& text)
161 {
162         result r = E_SUCCESS;
163         Dimension textSize;
164
165         if (__pTextBuffer)
166         {
167                 delete[] __pTextBuffer;
168                 __pTextBuffer = null;
169         }
170
171         int length = text.GetLength();
172         wchar_t* pTempString = const_cast <wchar_t*>(text.GetPointer());
173         SysTryReturnResult(NID_UI_CTRL, pTempString != null, E_SYSTEM, "A system error has occurred. Token text string is null.");
174
175         __pTextBuffer = new (std::nothrow) wchar_t[(length + 1) * (sizeof(wchar_t))];
176         SysTryReturnResult(NID_UI_CTRL, __pTextBuffer != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
177
178         for (int i = 0; i < length; i++)
179         {
180                 __pTextBuffer[i] = pTempString[i];
181         }
182         __pTextBuffer[length] = 0;
183
184         pTextObject->RemoveAll();
185
186         TextSimple* pSimpleText = new (std::nothrow)TextSimple(__pTextBuffer, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, pFont);
187         SysTryCatch(NID_UI_CTRL, pSimpleText != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
188
189         r = pTextObject->AppendElement(*pSimpleText);
190         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
191
192         textSize = pTextObject->GetTextExtent(0, length);
193         __textPixelWidth = textSize.width;
194         __textPixelHeight = textSize.height;
195
196         r = pTextObject->Compose();
197
198         currTokenLength = length;
199
200         return E_SUCCESS;
201
202 CATCH:
203         delete[] __pTextBuffer;
204         __pTextBuffer = null;
205
206         delete pSimpleText;
207         pSimpleText = null;
208
209         return E_SYSTEM;
210 }
211
212 result
213 _Token::SetBounds(Rectangle bounds)
214 {
215         result r = E_SUCCESS;
216         displayRect = bounds;
217         if (__pVisualElement)
218         {
219                 __pVisualElement->SetBounds(FloatRectangle(bounds.x, bounds.y, bounds.width, bounds.height));
220         }
221         return r;
222 }
223
224 _VisualElement*
225 _Token::GetVisualElement(void) const
226 {
227         return __pVisualElement;
228 }
229
230 _Token::~_Token(void)
231 {
232         delete pTextObject;
233         pTextObject = null;
234
235         delete pFont;
236         pFont = null;
237
238         delete[] __pTextBuffer;
239         __pTextBuffer = null;
240
241         if (__pVisualElement)
242         {
243                 __pVisualElement->Destroy();
244                 __pVisualElement = null;
245         }
246 }
247
248 int
249 _Token::GetTextPixelWidth(void) const
250 {
251         return __textPixelWidth;
252 }
253
254 wchar_t*
255 _Token::GetText(void) const
256 {
257         return __pTextBuffer;
258 }
259
260 _TokenEditPresenter::_TokenEditPresenter(void)
261         : __pTokenEdit(null)
262         , __pTokenList(null)
263         , __pTokenBgBitmap(null)
264         , __pressedTokenIndex(-1)
265         , __isEditingToken(false)
266         , __edittingTokenIndex(-1)
267         , __clientRect(Rectangle(0, 0, 0, 0))
268         , __initTextRect(Rectangle(0, 0, 0, 0))
269         , __isEditModeEnabled(true)
270         , __pDescriptionTextVisualElement(null)
271         , __pDescriptionTextTextObject(null)
272         , __pDescriptionTextFont(null)
273         , __isTokenEditingFinished(false)
274         , __prevScrollValue(0)
275         , __scrollValue(0)
276         , __isTokenScrolling(false)
277         , __isNeedToScroll(false)
278         , __maxScrollValue(0)
279         , __autoShrink(false)
280         , __hiddenTokenDisplayCountBounds(Rectangle())
281         , __pHiddenTokenCountVisualElement(null)
282         , __autoShrinkAlignmentValue(0)
283         , __pHiddenTokenCountTextObject(null)
284         , __descriptionText(String())
285         , __isPopupVisible(false)
286         , __isLongPressed(false)
287         , __lineSpacing(0)
288         , __animatingIndex(-1)
289         , __lastTokenIndex(-1)
290         , __pTimingFunction(null)
291         , __descriptionTextRectForScroll(Rectangle(0, 0, 0, 0))
292         , __previousTitleWidth(-1)
293         , __previousEditBounds(Rectangle(0, 0, 0, 0))
294         , __isTokenEditPresenterInitialized(false)
295         , __isFocus(false)
296         , __previousCursorPosition(0)
297         , __isScrollValueChanged(false)
298         , __lineAdded(0)
299         , __isScrollValueModified(false)
300 {
301 }
302
303 result
304 _TokenEditPresenter::InitializeDescriptionText(void)
305 {
306         result r = E_SUCCESS;
307         int descriptionTextSize = 0;
308
309         __pDescriptionTextTextObject = new (std::nothrow) TextObject();
310         SysTryReturnResult(NID_UI_CTRL, __pDescriptionTextTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
311
312         // for default initialize.
313         r = __pDescriptionTextTextObject->Construct();
314         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
315
316         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
317         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
318
319         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
320         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
321
322         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
323
324         __pDescriptionTextFont = new (std::nothrow) Font();
325         SysTryCatch(NID_UI_CTRL, __pDescriptionTextFont != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
326
327         r = __pDescriptionTextFont->Construct(GetTitleFontFaceName(), FONT_STYLE_PLAIN, descriptionTextSize);
328         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
329         r = __pDescriptionTextTextObject->SetFont(__pDescriptionTextFont, 0, __pDescriptionTextTextObject->GetTextLength());
330         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
331
332         return r;
333
334 CATCH:
335
336         delete __pDescriptionTextTextObject;
337         __pDescriptionTextTextObject = null;
338
339         delete __pDescriptionTextFont;
340         __pDescriptionTextFont = null;
341
342         return r;
343 }
344
345 _TokenEditPresenter::~_TokenEditPresenter(void)
346 {
347         DisposeTokenEditPresenter();
348 }
349
350 result
351 _TokenEditPresenter::DisposeTokenEditPresenter(void)
352 {
353         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
354         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
355
356         if (pEditVisualElement && pCursorVisualElement)
357         {
358                 VisualElement* pCursorParent = pCursorVisualElement->GetParent();
359                 if (pCursorParent != pEditVisualElement)
360                 {
361                         if (pCursorParent)
362                         {
363                                 pCursorParent->DetachChild(*pCursorVisualElement);
364                                 pEditVisualElement->AttachChild(*pCursorVisualElement);
365                         }
366                 }
367         }
368
369         if (__pTokenList)
370         {
371                 __pTokenList->RemoveAll(true);
372                 delete __pTokenList;
373                 __pTokenList = null;
374         }
375
376         delete __pTokenBgBitmap;
377         __pTokenBgBitmap = null;
378
379         if (__pDescriptionTextVisualElement)
380         {
381                 __pDescriptionTextVisualElement->Destroy();
382                 __pDescriptionTextVisualElement = null;
383         }
384
385         delete __pDescriptionTextTextObject;
386         __pDescriptionTextTextObject = null;
387
388         if (__pDescriptionTextFont)
389         {
390                 delete __pDescriptionTextFont;
391                 __pDescriptionTextFont = null;
392         }
393
394         if (__pHiddenTokenCountVisualElement)
395         {
396                 __pHiddenTokenCountVisualElement->Destroy();
397                 __pHiddenTokenCountVisualElement = null;
398         }
399
400         delete __pHiddenTokenCountTextObject;
401         __pHiddenTokenCountTextObject = null;
402
403         if (__pTimingFunction)
404         {
405                 delete __pTimingFunction;
406                 __pTimingFunction = null;
407         }
408
409         return E_SUCCESS;
410 }
411
412 _TokenEditPresenter*
413 _TokenEditPresenter::CreateTokenEditPresenterN(void)
414 {
415         _TokenEditPresenter* pPresenter = new (std::nothrow) _TokenEditPresenter();
416         SysTryReturn(NID_UI_CTRL, pPresenter != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
417
418         return pPresenter;
419 }
420
421 result
422 _TokenEditPresenter::Initialize(const _Control& control)
423 {
424         result r = E_SUCCESS;
425
426         r = _EditPresenter::Initialize(control);
427         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
428
429         r = InitializeDescriptionText();
430         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
431
432         TextObject* pTextObject = GetTextObject();
433         if (pTextObject)
434         {
435                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
436                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
437                 pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
438         }
439
440         __pTokenEdit = dynamic_cast <_TokenEdit*>(GetEditView());
441         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
442
443         _TokenEditModel* pTokenEditModel = new (std::nothrow) _TokenEditModel();
444         SysTryReturnResult(NID_UI_CTRL, pTokenEditModel != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
445
446         SetModel(*pTokenEditModel);
447
448         SetKeypadEnabled(true);
449
450         int tokenLeftMargin = 0;
451         int tokenRightMargin = 0;
452         int tokenTopMargin = 0;
453         int tokenBottomMargin = 0;
454         int tokenHeight = 0;
455         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
456
457         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, tokenLeftMargin);
458         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, tokenRightMargin);
459         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
460         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
461         GET_SHAPE_CONFIG(TOKEN_HEIGHT, orientation, tokenHeight);
462
463         // For drawing token in specific area
464         __clientRect.x = tokenLeftMargin;
465         __clientRect.y = tokenTopMargin;
466         __clientRect.width = __pTokenEdit->GetBounds().width - tokenLeftMargin - tokenRightMargin;
467         __clientRect.height = __pTokenEdit->GetBounds().height - tokenTopMargin - tokenBottomMargin;
468
469         __initTextRect = GetTextBounds();
470
471         Rectangle tempDspRect(__initTextRect.x, __initTextRect.y, __clientRect.width, tokenHeight);
472         SetTextBounds(tempDspRect);
473
474         int textSize = 0;
475         GET_SHAPE_CONFIG(TOKENEDIT::TEXT_SIZE, orientation, textSize);
476
477         Font* pFont = new (std::nothrow) Font();
478         SysTryCatch(NID_UI_CTRL, pFont != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
479
480         r = pFont->Construct(FONT_STYLE_PLAIN, textSize);
481         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
482
483         SetFont(*pFont);
484
485         delete pFont;
486         pFont = null;
487
488         __pTokenList = new (std::nothrow) Collection::LinkedList();
489         SysTryCatch(NID_UI_CTRL, __pTokenList != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
490
491         __delimiter = L"\n";
492         GET_BITMAP_CONFIG_N(TOKENEDIT::TOKEN_BG, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgBitmap);
493
494         __previousEditBounds = __pTokenEdit->GetBounds();
495
496         __isTokenEditPresenterInitialized = true;
497
498         __previousCursorPosition = GetCursorPosition();
499
500         return r;
501
502 CATCH:
503
504         delete pTokenEditModel;
505         pTokenEditModel = null;
506
507         delete pFont;
508         pFont = null;
509
510         return r;
511 }
512
513 result
514 _TokenEditPresenter::Draw(Canvas& canvas)
515 {
516         if (!IsInitialized())
517         {
518                 InitializeAtFirstDrawing();
519
520                 if (IsFocused() == true)
521                 {
522                         ShowKeypad(false);
523                 }
524         }
525
526         canvas.SetBackgroundColor(Color(0, 0, 0, 0));
527         canvas.Clear();
528
529         DrawBackground(canvas);
530
531         DrawScrollBar();
532
533         if (__pDescriptionTextTextObject->GetTextLength() != 0)
534         {
535                 DrawDescriptionText();
536         }
537
538         if (GetTokenCount() != 0)
539         {
540                 DrawToken();
541         }
542
543         if (__autoShrink)
544         {
545                 DrawHiddenTokenCount();
546         }
547
548         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
549         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
550
551         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
552         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
553
554         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
555
556         if (pToken)
557         {
558                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
559                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get visual element of token.");
560
561                 if (__isEditingToken)
562                 {
563                         Canvas* pTokenCanvas = pTokenVisualElement->GetCanvasN();
564                         SysTryReturnResult(NID_UI_CTRL, pTokenCanvas != null, E_SYSTEM, "A system error has occurred. Failed to get canvas of the token.");
565
566                         _EditPresenter::SetFont(*(pToken->pFont));
567
568                         DrawText(*pTokenCanvas);
569                         InitializeCursor();
570
571                         delete pTokenCanvas;
572                         pTokenCanvas = null;
573                 }
574                 else
575                 {
576                         //DrawText(canvas);
577                         DrawText();
578                         InitializeCursor();
579                 }
580         }
581         else
582         {
583                 SysTryReturnResult(NID_UI_CTRL, !__isEditingToken, E_SYSTEM, "An invalid argument is given.");
584
585                 //DrawText(canvas);
586                 DrawText();
587                 InitializeCursor();
588         }
589
590         if (__isTokenEditingFinished)
591         {
592                 _EditPresenter::SetDefaultFont();
593
594                 __isEditingToken = false;
595                 __edittingTokenIndex = -1;
596                 __isTokenEditingFinished = false;
597         }
598
599         return E_SUCCESS;
600 }
601
602 bool
603 _TokenEditPresenter::DrawToken(int count)
604 {
605         int drawStartIndex = 0;
606         int tokenCount = 0;
607         int tokenTextLeftMargin = 0;
608
609         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
610
611         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
612
613         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
614
615         if (count == -1)
616         {
617                 tokenCount = __pTokenList->GetCount();
618         }
619         else
620         {
621                 tokenCount = count;
622         }
623
624         Color normalTokenColor;
625         Color selectedTokenColor;
626
627         for (int i = drawStartIndex; i < tokenCount; i++)
628         {
629                 Bitmap* pReplacementColorBackgroundBitmap = null;
630
631                 _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
632                 if (pToken == null || pToken->pTextObject == null)
633                 {
634                         SysLog(NID_UI_CTRL, "[E_SYSTEM] The _Token instance is null");
635                         break;
636                 }
637
638                 normalTokenColor = GetTokenEditColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
639                 selectedTokenColor = GetTokenEditColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
640
641                 _VisualElement* pTokenElement = pToken->GetVisualElement();
642                 if (pTokenElement == null)
643                 {
644                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The _VisualElement instance is null");
645                         break;
646                 }
647
648                 bool isSelected = false;
649                 Canvas* pTokenCanvas = pTokenElement->GetCanvasN();
650                 if (pTokenCanvas == null)
651                 {
652                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The Canvas instance is null");
653                         break;
654                 }
655                 Rectangle tokenRect(0, 0 , pToken->displayRect.width, pToken->displayRect.height);
656
657                 if (__pressedTokenIndex == i && IsFocused())
658                 {
659                         if (__pTokenBgBitmap)
660                         {
661                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), selectedTokenColor);
662
663                                 if (pReplacementColorBackgroundBitmap->IsNinePatchedBitmap())
664                                 {
665                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
666                                 }
667                                 else
668                                 {
669                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
670                                 }
671                         }
672                         else
673                         {
674                                 pTokenCanvas->FillRectangle(selectedTokenColor, tokenRect);
675                         }
676
677                         isSelected = true;
678                 }
679                 else
680                 {
681                         if (__pTokenBgBitmap)
682                         {
683                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), normalTokenColor);
684
685                                 if (pReplacementColorBackgroundBitmap->IsNinePatchedBitmap())
686                                 {
687                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
688                                 }
689                                 else
690                                 {
691                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
692                                 }
693                         }
694                         else
695                         {
696                                 pTokenCanvas->FillRectangle(normalTokenColor, tokenRect);
697                         }
698                 }
699
700                 pTokenElement->SetAnimationProvider(null);
701                 if (pToken->isImplicitAnimation)
702                 {
703                         VisualElementAnimation* pAnimation = CreateAnimationN(*pTokenElement, true);
704                         pTokenElement->AddAnimation(*pAnimation);
705                         delete pAnimation;
706                         pToken->isImplicitAnimation = false;
707                 }
708
709                 if (!__isEditingToken || __pressedTokenIndex != i)
710                 {
711                         Color textColor;
712                         Rectangle textRect(0, 0, pToken->displayRect.width - ((tokenTextLeftMargin * 2)), pToken->displayRect.height);
713
714                         textRect.x += tokenTextLeftMargin;
715                         if (pToken->displayRect.width > pToken->GetTextPixelWidth())
716                         {
717                                 textRect.width = pToken->GetTextPixelWidth() + 1;
718                         }
719
720                         if (isSelected)
721                         {
722                                 textColor = GetTokenEditTextColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
723                         }
724                         else
725                         {
726                                 textColor = GetTokenEditTextColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
727                         }
728
729                         pToken->pTextObject->SetForegroundColor(textColor, 0, pToken->pTextObject->GetTextLength());
730                         pToken->pTextObject->SetBounds(textRect);
731                         pToken->pTextObject->Compose();
732                         pToken->pTextObject->Draw(*_CanvasImpl::GetInstance(*pTokenCanvas));
733                 }
734
735                 pTokenElement->SetFlushNeeded();
736                 delete pTokenCanvas;
737
738                 delete pReplacementColorBackgroundBitmap;
739                 pReplacementColorBackgroundBitmap = null;
740         }
741         return true;
742 }
743
744 Color
745 _TokenEditPresenter::GetTokenEditColor(const ExpandableEditAreaTokenStatus status) const
746 {
747         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
748
749         Color color;
750
751         if (status == EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL)
752                 color = __pTokenEdit->GetTokenColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
753         else
754                 color = __pTokenEdit->GetTokenColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
755
756         return color;
757 }
758
759 Color
760 _TokenEditPresenter::GetTokenEditTextColor(const ExpandableEditAreaTokenStatus status) const
761 {
762         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
763
764         Color color;
765
766         if (status == EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL)
767                 color = __pTokenEdit->GetTokenTextColor();
768         else
769                 color = __pTokenEdit->GetSelectedTokenTextColor();
770
771         return color;
772 }
773
774 result
775 _TokenEditPresenter::MakeToken(const String& tokenString)
776 {
777         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_INVALID_STATE, "_TokenEdit is in an invalid state.");
778         result r = E_SUCCESS;
779
780         int tokenCount = 0;
781         int index = -1;
782         int tokenLength = tokenString.GetLength();
783         SysTryReturnResult(NID_UI_CTRL, tokenLength, E_INVALID_ARG, "Invalid argument is used. Token length is (%d)", tokenLength);
784
785         String inputTokenString = tokenString;
786         String replacementString = inputTokenString;
787         bool enable = false;
788
789         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
790
791         if (enable)
792         {
793                 inputTokenString = replacementString;
794         }
795
796         if (inputTokenString.GetLength() <= 0)
797         {
798                 r = ClearText();
799                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Failed to clear text object.", GetErrorMessage(r));
800
801                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
802                 return E_INVALID_ARG;
803         }
804
805         _Token* pToken = new (std::nothrow) _Token(inputTokenString, GetFont());
806         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
807
808         r = __pTokenList->Add(static_cast <Object&>(*pToken));
809         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
810
811         tokenCount = __pTokenList->GetCount();
812         index = tokenCount - 1;
813
814         r = CalculateTokenPositionFromIndex(index);
815         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
816
817         r = TrimTokenAndAdjustEllipsisAt(index);
818         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
819
820         r = InitializeTokenVisibilityAt(index);
821         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
822
823         r = SetInitialBounds();
824         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
825
826         r = ClearText();
827         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to clear text object.", GetErrorMessage(r));
828
829         r = AdjustFlexibleHeight();
830         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
831
832         r = CheckTokenScrolling();
833         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
834
835         return r;
836
837 CATCH:
838         delete pToken;
839         pToken = null;
840
841         return r;
842 }
843
844 result
845 _TokenEditPresenter::MakeToken(void)
846 {
847         result r = E_SUCCESS;
848
849         String tempToken(GetText());
850         r = MakeToken(tempToken);
851
852         return r;
853 }
854
855 result
856 _TokenEditPresenter::InsertTokenAt(int index, const String& token, bool isUser)
857 {
858         SysTryReturnResult(NID_UI_CTRL, index >= 0, E_INVALID_ARG, "Invalid argument is used. index is (%d).", index);
859         SysTryReturnResult(NID_UI_CTRL, token.GetLength() > 0, E_INVALID_ARG, "Invalid argument is used. Token length is (%d).", token.GetLength());
860
861         result r = E_SUCCESS;
862
863         _Token* pToken = new (std::nothrow) _Token(token, GetFont());
864         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
865
866         r = __pTokenList->InsertAt(*pToken, index);
867         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to insert token.", GetErrorMessage(r));
868
869         if (__isEditingToken)
870         {
871                 _EditPresenter::SetFont(*(pToken->pFont));
872         }
873
874         r = ClearText();
875
876         r = CalculateTokenPositionFromIndex(index);
877         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
878
879         r = TrimTokenAndAdjustEllipsisAt(index);
880         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
881
882         r = InitializeTokenVisibilityAt(index);
883         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
884
885         r = SetInitialBounds();
886         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
887
888         r = AdjustFlexibleHeight();
889         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
890
891         r = CheckTokenScrolling();
892         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
893
894         for (int i = 0; i < GetTokenCount(); i++)
895         {
896                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
897                 SysTryCatch(NID_UI_CTRL, pToken != null, ,r = E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
898
899                 r = pToken->SetBounds(pToken->displayRect);
900                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds", GetErrorMessage(r));
901         }
902
903         if (isUser)
904         {
905                 if (__edittingTokenIndex >= 0)
906                 {
907                         if (index <= __edittingTokenIndex)
908                         {
909                                 __edittingTokenIndex++;
910                         }
911                         __pressedTokenIndex = __edittingTokenIndex;
912
913                         SetEditingTokenTextBounds(__edittingTokenIndex);
914                         _EditPresenter::SetCursorPosition(__previousCursorPosition);
915                 }
916                 else if (__pressedTokenIndex >= index)
917                 {
918                         __pressedTokenIndex++;
919                 }
920         }
921
922         return r;
923
924 CATCH:
925
926         delete pToken;
927         pToken = null;
928
929         return r;
930 }
931
932 String
933 _TokenEditPresenter::GetTokenAt(int index) const
934 {
935         String tempString;
936         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);
937
938         _Token* pToken = null;
939         pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
940         if (pToken)
941         {
942                 tempString = pToken->GetText();
943         }
944
945         return tempString;
946 }
947
948 int
949 _TokenEditPresenter::GetTokenCount(void) const
950 {
951         return __pTokenList->GetCount();
952 }
953
954 int
955 _TokenEditPresenter::GetSelectedTokenIndex(void) const
956 {
957         return __pressedTokenIndex;
958 }
959
960 bool
961 _TokenEditPresenter::IsTokenEditModeEnabled(void) const
962 {
963         return __isEditModeEnabled;
964 }
965
966 result
967 _TokenEditPresenter::RemoveTokenAt(int index, bool isClearText)
968 {
969         result r = E_SUCCESS;
970         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.", index);
971
972         if (index == __edittingTokenIndex && isClearText)
973         {
974                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
975                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
976
977                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
978                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
979
980                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
981                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
982
983                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
984                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
985
986                 __edittingTokenIndex = -1;
987                 __isEditingToken = false;
988                 __pressedTokenIndex = -1;
989                 __isTokenEditingFinished = true;
990
991                 r = ClearText();
992                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Failed to clear text object.");
993
994                 _EditPresenter::SetDefaultFont();
995         }
996
997         r = __pTokenList->RemoveAt(index, true);
998         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to remove token.");
999
1000         r = CalculateTokenPositionFromIndex(index);
1001         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to calculate token position.");
1002
1003         r = SetInitialBounds();
1004         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
1005
1006         _Token* pToken = null;
1007
1008         for (int i = 0; i < GetTokenCount(); i++)
1009         {
1010                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
1011                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1012
1013                 r = pToken->SetBounds(pToken->displayRect);
1014                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds");
1015         }
1016
1017         r = AdjustFlexibleHeight();
1018         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process to resize.");
1019
1020         r = CheckTokenScrolling();
1021         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process scroll.");
1022
1023         if (isClearText)
1024         {
1025                 if (index < __edittingTokenIndex)
1026                 {
1027                         if (__edittingTokenIndex > 0)
1028                         {
1029                                 __edittingTokenIndex--;
1030                                 __pressedTokenIndex = __edittingTokenIndex;
1031
1032                                 SetEditingTokenTextBounds(__edittingTokenIndex);
1033                                 _EditPresenter::SetCursorPosition(__previousCursorPosition);
1034                         }
1035                 }
1036                 else if (index == __pressedTokenIndex)
1037                 {
1038                         __pressedTokenIndex = -1;
1039                 }
1040                 else if (index >= 0 && index < __pressedTokenIndex)
1041                 {
1042                         __pressedTokenIndex--;
1043                 }
1044         }
1045         else if (index == __pressedTokenIndex)
1046         {
1047                 __pressedTokenIndex = -1;
1048         }
1049
1050         return r;
1051 }
1052
1053 result
1054 _TokenEditPresenter::SetTokenSelected(int index, bool selected)
1055 {
1056         result r = E_SUCCESS;
1057
1058         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.");
1059
1060         if (selected == false)
1061         {
1062                 __pressedTokenIndex = -1;
1063         }
1064         else
1065         {
1066                 __pressedTokenIndex = index;
1067         }
1068
1069         return r;
1070 }
1071
1072 result
1073 _TokenEditPresenter::SetTokenEditModeEnabled(bool enable)
1074 {
1075         result r = E_SUCCESS;
1076
1077         __isEditModeEnabled = enable;
1078
1079         return r;
1080 }
1081
1082 result
1083 _TokenEditPresenter::CalculateTokenPositionFromIndex(int startIndex, bool leftWard)
1084 {
1085         result r = E_SUCCESS;
1086
1087         int tokenCount = __pTokenList->GetCount();
1088
1089         int tokenLeftMargin = 0;
1090         int tokenRighttMargin = 0;
1091         int tokenTopMargin = 0;
1092         int tokenBottomMargin = 0;
1093         int tokenHeight = 0;
1094         int tokenVerticalSpacing = 0;
1095         int tokenHorizontalSpacing = 0;
1096         int tokenTextLeftMargin = 0;
1097         int tokenTextRightMargin = 0;
1098         int descriptionTextRightMargin = 0;
1099         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1100
1101         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, tokenLeftMargin);
1102         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, tokenRighttMargin);
1103         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1104         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
1105         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1106         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1107         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1108         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1109         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1110         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_RIGHT_MARGIN, orientation, descriptionTextRightMargin);
1111
1112         SysTryReturn(NID_UI_CTRL, startIndex >= 0 && startIndex <= tokenCount, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. startIndex = (%d)", startIndex);
1113
1114         _Token* pToken = null;
1115         _Token* pPreviousToken = null;
1116
1117         int index = startIndex;
1118
1119         Rectangle tokenEditBounds = __pTokenEdit->GetBounds();
1120         String titleText = __pTokenEdit->GetTitleText();
1121
1122         if (GetDescriptionTextRect().width != __previousTitleWidth)
1123         {
1124                 __descriptionTextRectForScroll = GetDescriptionTextRect();
1125                 __previousTitleWidth = GetDescriptionTextRect().width;
1126         }
1127
1128         bool findPrevTokenLoopFlag = true;
1129         for (; index < tokenCount; index++)
1130         {
1131                 pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
1132                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1133
1134                 if (index == 0)
1135                 {
1136                         // TODO : description text (title style inner)
1137                         if (titleText.GetLength())
1138                         {
1139                                 pToken->displayRect.x = __descriptionTextRectForScroll.x + __descriptionTextRectForScroll.width + descriptionTextRightMargin;
1140                                 pToken->displayRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1141                         }
1142                         else        // Set description text.
1143                         {
1144                                 pToken->displayRect.x = tokenTextLeftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1145                                 pToken->displayRect.y = tokenTopMargin + __scrollValue + __pTokenEdit->GetVerticalMargin(EDIT_TEXT_TOP_MARGIN) + __lineSpacing;
1146
1147                         }
1148                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1149
1150                 }
1151                 else
1152                 {
1153                         if (findPrevTokenLoopFlag)
1154                         {
1155                                 pPreviousToken = static_cast <_Token*>(__pTokenList->GetAt(index - 1));
1156                                 findPrevTokenLoopFlag = false;
1157                         }
1158
1159                         int tempTextWidth = tokenEditBounds.width - pPreviousToken->displayRect.x - pPreviousToken->displayRect.width - tokenHorizontalSpacing - __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);
1160                         if (tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin > tempTextWidth)           // Line change
1161                         {
1162                                 pToken->displayRect.x = tokenTextLeftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1163                                 pToken->displayRect.y = pPreviousToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing;
1164                                 __lineAdded++;
1165                         }
1166                         else
1167                         {
1168                                 pToken->displayRect.x = pPreviousToken->displayRect.x + pPreviousToken->displayRect.width +
1169                                                 tokenHorizontalSpacing;
1170                                 pToken->displayRect.y = pPreviousToken->displayRect.y;
1171                         }
1172
1173                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1174                 }
1175
1176                 pToken->displayRect.height = tokenHeight;
1177
1178                 pPreviousToken = pToken;
1179         }
1180
1181         for (int i = 0; i < __pTokenList->GetCount(); i++)
1182         {
1183                 TrimTokenAndAdjustEllipsisAt(i);
1184                 InitializeTokenVisibilityAt(i);
1185         }
1186         return r;
1187 }
1188
1189 result
1190 _TokenEditPresenter::InitializeTokenVisibilityAt(int ndex)
1191 {
1192         result r = E_SUCCESS;
1193
1194         _Token* pToken = null;
1195         pToken = static_cast <_Token*>(__pTokenList->GetAt(ndex));
1196         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1197
1198         r = pToken->SetBounds(pToken->displayRect);
1199         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to set bounds", GetErrorMessage(r));
1200
1201         _VisualElement* pRootElement = __pTokenEdit->GetVisualElement();
1202         SysTryReturn(NID_UI_CTRL, pRootElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1203
1204         _VisualElement* pVisualElement = pToken->GetVisualElement();
1205         SysTryReturn(NID_UI_CTRL, pVisualElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element.");
1206
1207         pVisualElement->SetShowState(true);
1208
1209         r = pRootElement->AttachChild(*pVisualElement);
1210         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to add child", GetErrorMessage(r));
1211
1212         return r;
1213 }
1214
1215 result
1216 _TokenEditPresenter::SetInitialBounds(void)
1217 {
1218         result r = E_SUCCESS;
1219
1220         int tokenTopMargin = 0;
1221         int tokenHeight = 0;
1222         int tokenMinWidth = 0;
1223         int tokenVerticalSpacing = 0;
1224         int tokenHorizontalSpacing = 0;
1225         int tokenTextLeftMargin = 0;
1226         int tokenTextRightMargin = 0;
1227         _ControlOrientation orientation = GetEditView()->GetOrientation();
1228
1229         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1230         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1231         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinWidth);
1232         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1233         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1234         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1235         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1236
1237         if (__pTokenList)
1238         {
1239                 int tokenCount = __pTokenList->GetCount();
1240
1241                 Rectangle tokenEditBounds = __pTokenEdit->GetBounds();
1242                 Rectangle tokenTextRect = tokenEditBounds;
1243
1244                 if (tokenCount == 0)
1245                 {
1246                         if (__pTokenEdit->GetTitleText().GetLength())
1247                         {
1248                                 Rectangle descriptionTextRect = GetDescriptionTextRect();
1249
1250                                 tokenTextRect.x = descriptionTextRect.x + descriptionTextRect.width + tokenVerticalSpacing;
1251                                 tokenTextRect.width -= (tokenTextLeftMargin + tokenTextRightMargin + descriptionTextRect.width + 2 * __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN));
1252                         }
1253                         else
1254                         {
1255                                 tokenTextRect.x = tokenTextLeftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1256                                 tokenTextRect.width -= (tokenTextLeftMargin + tokenTextRightMargin + 2 * __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN));
1257                         }
1258
1259                         tokenTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMargin(EDIT_TEXT_TOP_MARGIN);
1260                         tokenTextRect.height = __pTokenEdit->GetTextSize() + (2 * tokenTopMargin);
1261
1262                         SetTextBounds(tokenTextRect);
1263
1264                         return r;
1265                 }
1266
1267                 _Token* pToken = null;
1268                 // SetTextBounds from last token
1269                 pToken = static_cast <_Token*>(__pTokenList->GetAt(tokenCount - 1));
1270                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1271
1272                 int tempTextRectWidth = 0;
1273                 tempTextRectWidth = tokenEditBounds.width - pToken->displayRect.x - pToken->displayRect.width - tokenHorizontalSpacing - __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);
1274                 if (tokenMinWidth > tempTextRectWidth)        // Line change
1275                 {
1276                         tokenTextRect.x = tokenTextLeftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1277                         tokenTextRect.y = pToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing;
1278                         tokenTextRect.width = tokenEditBounds.width - tokenTextLeftMargin - tokenTextRightMargin - 2 * __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);
1279                 }
1280                 else
1281                 {
1282                         tokenTextRect.x = pToken->displayRect.x + pToken->displayRect.width + tokenHorizontalSpacing;
1283                         tokenTextRect.y = pToken->displayRect.y;
1284                         tokenTextRect.width = tokenEditBounds.width - pToken->displayRect.x - pToken->displayRect.width -
1285                                         tokenTextRightMargin - __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN) - tokenTextLeftMargin;
1286                 }
1287
1288                 tokenTextRect.height = __pTokenEdit->GetTextSize() + (2 * tokenTopMargin);
1289
1290                 SetTextBounds(tokenTextRect);
1291         }
1292         else
1293         {
1294                 r = _EditPresenter::SetInitialBounds();
1295         }
1296
1297         return r;
1298 }
1299
1300 result
1301 _TokenEditPresenter::SetDescriptionTextRect(const Rectangle& rect)
1302 {
1303         result r = E_SUCCESS;
1304
1305         __descriptionTextRect = rect;
1306
1307         return r;
1308 }
1309
1310 Rectangle
1311 _TokenEditPresenter::GetDescriptionTextRect() const
1312 {
1313         return __descriptionTextRect;
1314 }
1315
1316 Rectangle
1317 _TokenEditPresenter::GetTextBounds(void) const
1318 {
1319         if ((__isPopupVisible == true || __isLongPressed == true) && __pressedTokenIndex >= 0)
1320         {
1321                 _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
1322                 SysTryReturn(NID_UI_CTRL, pToken, Rectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Unable to get valid token.");
1323
1324                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1325                 int tokenTextLeftMargin = 0;
1326                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1327
1328                 Rectangle textBounds(pToken->displayRect.x + tokenTextLeftMargin, pToken->displayRect.y, pToken->displayRect.width, pToken->displayRect.height);
1329                 return textBounds;
1330         }
1331         else
1332         {
1333                 return _EditPresenter::GetTextBounds();
1334         }
1335 }
1336
1337 result
1338 _TokenEditPresenter::CutText(void)
1339 {
1340         if (__isEditingToken)
1341         {
1342                 _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
1343                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1344
1345                 pToken->isTextCut = true;
1346         }
1347         return _EditPresenter::CutText();
1348 }
1349
1350 result
1351 _TokenEditPresenter::SetLineSpacing(int linePixelGap)
1352 {
1353         __lineSpacing = linePixelGap;
1354
1355         return E_SUCCESS;
1356 }
1357
1358 int
1359 _TokenEditPresenter::GetLineSpacing(void) const
1360 {
1361         return __lineSpacing;
1362 }
1363
1364 VisualElementAnimation*
1365 _TokenEditPresenter::CreateAnimationN(VisualElement& source, bool create)
1366 {
1367         VisualElementAnimation* pAnimation = null;
1368         VisualElementAnimationGroup* pAnimationGroup = new (std::nothrow) VisualElementAnimationGroup();
1369         SysTryReturn(NID_UI_CTRL, pAnimationGroup, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1370
1371         pAnimationGroup->SetDuration(ANIMATION_DURATION_BOUNDS);
1372         if (__pTimingFunction == null)
1373         {
1374                 __pTimingFunction = new (std::nothrow) SineTimingFunction();
1375                 SysTryReturn(NID_UI_CTRL, __pTimingFunction, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1376         }
1377
1378         if (pAnimationGroup != null)
1379         {
1380                 VisualElementPropertyAnimation* pOpacityAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1381                 SysTryReturn(NID_UI_CTRL, pOpacityAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1382
1383                 pOpacityAnimation->SetPropertyName("opacity");
1384                 if (!create)
1385                 {
1386                         pOpacityAnimation->SetStartValue(Variant(1.0f));
1387                         pOpacityAnimation->SetEndValue(Variant(0.0f));
1388                 }
1389                 else
1390                 {
1391                         pOpacityAnimation->SetStartValue(Variant(0.0f));
1392                         pOpacityAnimation->SetEndValue(Variant(1.0f));
1393                 }
1394
1395                 pOpacityAnimation->SetDuration(ANIMATION_DURATION_OPACITY);
1396                 pOpacityAnimation->SetTimingFunction(__pTimingFunction);
1397                 pAnimationGroup->AddAnimation(*pOpacityAnimation);
1398                 delete pOpacityAnimation;
1399
1400                 VisualElementPropertyAnimation* pBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1401                 SysTryReturn(NID_UI_CTRL, pBoundsAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1402
1403                 pBoundsAnimation->SetPropertyName("bounds");
1404                 FloatRectangle startValue = source.GetBounds();
1405                 startValue.x = startValue.x + startValue.width * 0.05;
1406                 startValue.y = startValue.y + startValue.height * 0.05;
1407                 startValue.width = startValue.width * 0.9;
1408                 startValue.height = startValue.height * 0.9;
1409
1410                 if (!create)
1411                 {
1412                         pBoundsAnimation->SetStartValue(Variant(source.GetBounds()));
1413                         pBoundsAnimation->SetEndValue(Variant(startValue));
1414                         pBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1415                 }
1416                 else
1417                 {
1418                         pBoundsAnimation->SetStartValue(Variant(startValue));
1419                         pBoundsAnimation->SetEndValue(Variant(source.GetBounds()));
1420                 }
1421
1422                 pBoundsAnimation->SetDuration(ANIMATION_DURATION_BOUNDS);
1423                 pBoundsAnimation->SetTimingFunction(__pTimingFunction);
1424                 pAnimationGroup->AddAnimation(*pBoundsAnimation);
1425
1426                 delete pBoundsAnimation;
1427
1428                 pAnimation = pAnimationGroup;
1429         }
1430         return pAnimation;
1431 }
1432
1433 result
1434 _TokenEditPresenter::CalculateDescriptionTextRect(const String& descriptionText)
1435 {
1436         result r = E_SUCCESS;
1437
1438         TextSimple* pSimpleText = null;
1439         int tokenTopMargin = 0;
1440         int tokenHeight = 0;
1441         int tokenVerticalSpacing = 0;
1442         int tokenTextLeftMargin = 0;
1443         int tokenTextRightMargin = 0;
1444         int tokenTitleWidth = 0;
1445         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1446
1447         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1448         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1449         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1450         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1451         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1452         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TITLE_RECT_WIDTH, orientation, tokenTitleWidth);
1453
1454         int length = descriptionText.GetLength();
1455         Dimension textSize;
1456
1457         wchar_t* pTempString = const_cast <wchar_t*>(descriptionText.GetPointer());
1458
1459         SysAssertf(__pDescriptionTextTextObject != null, "The TextObject instance is null.");
1460
1461         __pDescriptionTextTextObject->RemoveAll();
1462         pSimpleText = new (std::nothrow)TextSimple(pTempString, length, TEXT_ELEMENT_SOURCE_TYPE_INTERNAL);
1463         __pDescriptionTextTextObject->AppendElement(*pSimpleText);
1464
1465         textSize = __pDescriptionTextTextObject->GetTextExtent(0, length);
1466         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
1467         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
1468
1469         __descriptionTextRect.x = tokenTextLeftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);;
1470         __descriptionTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMargin(EDIT_TEXT_TOP_MARGIN);;
1471         if (textSize.width > tokenTitleWidth)
1472         {
1473                 textSize.width = tokenTitleWidth;
1474                 __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
1475                 __pDescriptionTextTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
1476         }
1477         __descriptionTextRect.width = tokenTextLeftMargin + textSize.width + tokenTextRightMargin;
1478         __descriptionTextRect.height = tokenHeight;
1479         __pDescriptionTextTextObject->SetBounds(__descriptionTextRect);
1480
1481         r = __pDescriptionTextTextObject->Compose();
1482
1483         return r;
1484 }
1485
1486 bool
1487 _TokenEditPresenter::DrawDescriptionText(void)
1488 {
1489         result r = E_SUCCESS;
1490         Rectangle tempDescriptionTextRect;
1491         Canvas* pDescriptionTextCanvas = null;
1492
1493         Font* pDescriptionFont = null;
1494
1495         if (__pDescriptionTextTextObject->GetFont(0)->GetFaceName() != GetTitleFontFaceName())
1496         {
1497                 int descriptionTextSize = 0;
1498                 GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
1499
1500                 pDescriptionFont = new (std::nothrow) Font();
1501                 SysTryReturn(NID_UI_CTRL, pDescriptionFont != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1502
1503                 r = pDescriptionFont->Construct(GetTitleFontFaceName(), FONT_STYLE_PLAIN, descriptionTextSize);
1504                 if (r != E_SUCCESS)
1505                 {
1506                         delete pDescriptionFont;
1507                         pDescriptionFont = null;
1508                 }
1509                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
1510
1511                 r = __pDescriptionTextTextObject->SetFont(pDescriptionFont, 0, __pDescriptionTextTextObject->GetTextLength());
1512                 if (r != E_SUCCESS)
1513                 {
1514                         delete pDescriptionFont;
1515                         pDescriptionFont = null;
1516                 }
1517                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
1518
1519                 delete __pDescriptionTextFont;
1520                 __pDescriptionTextFont = null;
1521
1522                 __pDescriptionTextFont = pDescriptionFont;
1523         }
1524
1525         _VisualElement* pRootElement = __pTokenEdit->GetVisualElement();
1526         SysTryReturn(NID_UI_CTRL, pRootElement, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1527
1528         if (__pDescriptionTextVisualElement)
1529         {
1530                 __pDescriptionTextVisualElement->Destroy();
1531                 __pDescriptionTextVisualElement = null;
1532         }
1533         __pDescriptionTextVisualElement = new (std::nothrow) _VisualElement();
1534         SysTryReturn(NID_UI_CTRL, __pDescriptionTextVisualElement, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1535         r = __pDescriptionTextVisualElement->Construct();
1536         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[E_SYSTEM] A system error has occurred. Failed to construct _VisualElement");
1537         __pDescriptionTextVisualElement->SetImplicitAnimationEnabled(false);
1538         __pDescriptionTextVisualElement->SetBounds(FloatRectangle(__descriptionTextRect.x, __descriptionTextRect.y, __descriptionTextRect.width, __descriptionTextRect.height));     // TBD.
1539         __pDescriptionTextVisualElement->SetShowState(true);
1540
1541         r = pRootElement->AttachChild(*__pDescriptionTextVisualElement);
1542         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1543
1544         pDescriptionTextCanvas = __pDescriptionTextVisualElement->GetCanvasN();
1545         if (pDescriptionTextCanvas == null)
1546         {
1547                 SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. Failed to get canvas of an instance of _VisualElement.");
1548                 return true;
1549         }
1550
1551         tempDescriptionTextRect = __descriptionTextRect;
1552         tempDescriptionTextRect.x = 0;
1553         tempDescriptionTextRect.y = 0;
1554
1555         __pDescriptionTextTextObject->SetForegroundColor(__pTokenEdit->GetTitleTextColor(GetCurrentStatus()), 0, __pDescriptionTextTextObject->GetTextLength());
1556
1557         pDescriptionTextCanvas->FillRectangle(__pTokenEdit->GetColor(GetCurrentStatus()), tempDescriptionTextRect);
1558         __pDescriptionTextTextObject->SetBounds(tempDescriptionTextRect);
1559         __pDescriptionTextTextObject->Compose();
1560         __pDescriptionTextTextObject->Draw(*_CanvasImpl::GetInstance(*pDescriptionTextCanvas));
1561         __pDescriptionTextVisualElement->SetFlushNeeded();
1562
1563         delete pDescriptionTextCanvas;
1564
1565         return true;
1566 CATCH:
1567         __pDescriptionTextVisualElement->Destroy();
1568         __pDescriptionTextVisualElement = null;
1569
1570         return false;
1571 }
1572
1573 result
1574 _TokenEditPresenter::TrimTokenAndAdjustEllipsisAt(int index)
1575 {
1576         result r = E_SUCCESS;
1577
1578         int tokenTextRightMargin = 0;
1579         int tokenMinimumSize = 0;
1580
1581         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1582         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1583         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinimumSize);
1584
1585         _Token* pToken = null;
1586         pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
1587         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1588
1589         Rectangle tokenEditBounds = __pTokenEdit->GetBounds();
1590         Rectangle tokenRect = pToken->displayRect;
1591
1592         int remainWidth = tokenEditBounds.width - tokenTextRightMargin - __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);;
1593         int dspTokenWidth = tokenRect.x + tokenRect.width;
1594         int dspTokenGap = dspTokenWidth - remainWidth;
1595
1596         if (dspTokenGap > 0)
1597         {
1598                 if (pToken->displayRect.width >= tokenMinimumSize)
1599                 {
1600                         if ((pToken->displayRect.width - dspTokenGap) < tokenMinimumSize)
1601                         {
1602                                 pToken->displayRect.width = tokenMinimumSize;
1603                         }
1604                         else
1605                         {
1606                                 pToken->displayRect.width -= dspTokenGap;
1607                         }
1608                 }
1609
1610                 // Adjust ellipsis
1611                 pToken->pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
1612                 pToken->pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
1613         }
1614
1615         return r;
1616 }
1617
1618 int
1619 _TokenEditPresenter::GetTokenIndexFromCoordinate(const Point point) const
1620 {
1621         int tokenIndex = -1;
1622
1623         int tokenCount = __pTokenList->GetCount();
1624         for (int i = 0; i < tokenCount; i++)
1625         {
1626                 _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
1627                 if (pToken)
1628                 {
1629                         Rectangle tokenRect = pToken->displayRect;
1630                         if (tokenRect.Contains(point))
1631                         {
1632                                 tokenIndex = i;
1633                                 break;
1634                         }
1635                 }
1636         }
1637         return tokenIndex;
1638 }
1639
1640 result
1641 _TokenEditPresenter::SetEditingTokenTextBounds(int index, bool isSetText)
1642 {
1643         result r = E_SUCCESS;
1644
1645         int tokenHeight = 0;
1646         int tokenVerticalSpacing = 0;
1647         int tokenTextLeftMargin = 0;
1648         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1649
1650         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1651         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1652         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1653
1654         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
1655         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1656
1657         Rectangle tempTextDspRect;
1658         tempTextDspRect.x += tokenTextLeftMargin;
1659         tempTextDspRect.y += tokenVerticalSpacing / 2;
1660         tempTextDspRect.width = pToken->GetTextPixelWidth();
1661         tempTextDspRect.height = tokenHeight;
1662
1663         if (isSetText)
1664         {
1665                 if (!pToken->isTextCut)
1666                 {
1667                         SetText(pToken->GetText());
1668                 }
1669                 pToken->pTextObject->RemoveAll();
1670         }
1671
1672         SetTextBounds(tempTextDspRect);
1673
1674         return r;
1675 }
1676
1677 result
1678 _TokenEditPresenter::RecalculateTokenBounds(int position)
1679 {
1680         result r = E_SUCCESS;
1681
1682         __scrollValue = -position;
1683
1684         int tokenCount = GetTokenCount();
1685         CalculateTokenPositionFromIndex(0);
1686
1687         for (int i = 0; i < tokenCount; i++)
1688         {
1689                 _Token* pToken = null;
1690                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
1691
1692                 if (pToken)
1693                 {
1694                         pToken->SetBounds(pToken->displayRect);
1695                 }
1696                 TrimTokenAndAdjustEllipsisAt(i);
1697                 InitializeTokenVisibilityAt(i);
1698         }
1699
1700         if (__pDescriptionTextTextObject->GetTextLength() != 0)
1701         {
1702                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1703         }
1704
1705         __pTokenEdit->Draw();
1706         __pTokenEdit->Show();
1707
1708         r = SetInitialBounds();
1709         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
1710
1711         return r;
1712 }
1713
1714 result
1715 _TokenEditPresenter::SetTokenBoundsByTouchInfo(const _TouchInfo& touchinfo)
1716 {
1717         result r = E_SUCCESS;
1718         int currentYPosition = touchinfo.GetCurrentPosition().y;
1719
1720         if (__prevScrollValue == 0)
1721         {
1722                 __prevScrollValue = currentYPosition;
1723         }
1724         else    // Adjust moved y position to all tokens.
1725         {
1726                 if (__isNeedToScroll)           // Need to scroll
1727                 {
1728                         int tempDefference = __prevScrollValue - currentYPosition;
1729
1730                         __prevScrollValue = currentYPosition;
1731                         __scrollValue -= tempDefference;
1732
1733                         if (__scrollValue < -__maxScrollValue)
1734                         {
1735                                 __scrollValue = -__maxScrollValue;
1736
1737                                 return E_SUCCESS;
1738                         }
1739
1740                         if (__scrollValue > 0)
1741                         {
1742                                 __scrollValue = 0;
1743
1744                                 return E_SUCCESS;
1745                         }
1746
1747                         int tokenCount = GetTokenCount();
1748                         CalculateTokenPositionFromIndex(0);
1749                         for (int i = 0; i < tokenCount; i++)
1750                         {
1751                                 _Token* pToken = null;
1752                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
1753
1754                                 if (pToken)
1755                                 {
1756                                         pToken->SetBounds(pToken->displayRect);
1757                                 }
1758                                 TrimTokenAndAdjustEllipsisAt(i);
1759                                 InitializeTokenVisibilityAt(i);
1760                         }
1761
1762                         if (__pDescriptionTextTextObject->GetTextLength() != 0)
1763                         {
1764                                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1765                         }
1766
1767                         __pTokenEdit->Draw();
1768                         __pTokenEdit->Show();
1769                 }
1770                 else
1771                 {
1772                         __prevScrollValue = 0;
1773                         __scrollValue = 0;
1774                 }
1775         }
1776
1777         r = SetInitialBounds();
1778         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
1779
1780         return r;
1781 }
1782
1783 result
1784 _TokenEditPresenter::ProcessTokeningByTouchEvent(const _Control& source, const _TouchInfo& touchinfo)
1785 {
1786         int tokenIndex = GetTokenIndexFromCoordinate(touchinfo.GetCurrentPosition());
1787         int prevPressedTokenIndex = __pressedTokenIndex;
1788
1789         result r = E_SUCCESS;
1790         if (IsFocused())
1791         {
1792                 if (__isEditModeEnabled && __pressedTokenIndex != -1 && __pressedTokenIndex == tokenIndex)
1793                 {
1794                         /*__edittingTokenIndex = __pressedTokenIndex;
1795                         __isEditingToken = true;
1796                         SetEditingTokenTextBounds(__edittingTokenIndex);
1797             SetCursorDisabled(false);*/
1798                 }
1799                 __pressedTokenIndex = tokenIndex;
1800         }
1801
1802         if (__pressedTokenIndex != -1)
1803         {
1804                 if (__isEditingToken && (prevPressedTokenIndex != __pressedTokenIndex))   // Selected another token while editing.
1805                 {
1806                         __isPopupVisible = false;
1807                         __isLongPressed = false;
1808
1809                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
1810                         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
1811
1812                         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
1813                         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
1814
1815                         _Token* pToken = null;
1816                         _VisualElement* pTokenVisualElement = null;
1817
1818                         pToken = static_cast <_Token*>(__pTokenList->GetAt(prevPressedTokenIndex));
1819
1820                         bool isParentChanged = false;
1821                         if (pToken)
1822                         {
1823                                 pTokenVisualElement = pToken->GetVisualElement();
1824                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
1825
1826                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
1827                                 {
1828                                         isParentChanged = true;
1829                                         result r = E_SUCCESS;
1830                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
1831                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1832
1833                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
1834                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1835                                 }
1836                         }
1837
1838                         r = RemoveTokenAt(prevPressedTokenIndex);
1839                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1840
1841                         if (GetText().GetLength() > 0)
1842                         {
1843                                 r = InsertTokenAt(prevPressedTokenIndex, GetText());
1844                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1845
1846                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(prevPressedTokenIndex));
1847                                 if (pToken)
1848                                 {
1849                                         pToken->currTokenLength = GetText().GetLength();
1850                                 }
1851                         }
1852
1853                         __edittingTokenIndex = -1;
1854                         __isTokenEditingFinished = true;
1855                         __isEditingToken = false;
1856                         SetCursorDisabled(true);
1857                 }
1858                 else
1859                 {
1860                         if (GetText().GetLength() > 0 && __pressedTokenIndex != prevPressedTokenIndex)
1861                         {
1862                                 r = MakeToken();
1863                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1864                         }
1865
1866                         if ((__isEditingToken == true) && (__pressedTokenIndex != -1))
1867                         {
1868                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
1869                                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
1870
1871                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
1872                                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
1873
1874                                 _Token* pToken = null;
1875                                 _VisualElement* pTokenVisualElement = null;
1876
1877                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
1878
1879                                 bool isParentChanged = false;
1880                                 if (pToken)
1881                                 {
1882                                         pTokenVisualElement = pToken->GetVisualElement();
1883                                         SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
1884
1885                                         if (pCursorVisualElement->GetParent() != pTokenVisualElement)
1886                                         {
1887                                                 isParentChanged = true;
1888                                                 result r = E_SUCCESS;
1889                                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
1890                                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1891
1892                                                 r = pTokenVisualElement->AttachChild(*pCursorVisualElement);
1893                                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1894                                         }
1895                                 }
1896                         }
1897
1898                         if (__isEditingToken == false)
1899                         {
1900                                 SetCursorDisabled(true);
1901                         }
1902                 }
1903         }
1904         else
1905         {
1906                 __isPopupVisible = false;
1907                 __isLongPressed = false;
1908
1909                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
1910                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
1911
1912                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
1913                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
1914
1915                 if (__isEditingToken)
1916                 {
1917                         _Token* pToken = null;
1918                         _VisualElement* pTokenVisualElement = null;
1919
1920                         pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
1921
1922                         bool isParentChanged = false;
1923                         if (pToken)
1924                         {
1925                                 pTokenVisualElement = pToken->GetVisualElement();
1926                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
1927
1928                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
1929                                 {
1930                                         isParentChanged = true;
1931                                         result r = E_SUCCESS;
1932                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
1933                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1934
1935                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
1936                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1937                                 }
1938                         }
1939
1940                         String inputTokenString = GetText();
1941                         String replacementString = inputTokenString;
1942                         bool enable = false;
1943
1944                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
1945                         if (enable)
1946                         {
1947                                 inputTokenString = replacementString;
1948                         }
1949
1950                         RemoveTokenAt(__edittingTokenIndex);
1951
1952                         if (inputTokenString.GetLength() > 0)
1953                         {
1954                                 InsertTokenAt(__edittingTokenIndex, inputTokenString);
1955
1956                                 if (isParentChanged)
1957                                 {
1958                                         pToken->currTokenLength = inputTokenString.GetLength();
1959                                 }
1960                                 __isTokenEditingFinished = true;
1961                                 __isEditingToken = false;
1962                                 __edittingTokenIndex = -1;
1963                         }
1964                         else
1965                         {
1966                                 __isTokenEditingFinished = true;
1967                                 __isEditingToken = false;
1968                                 __edittingTokenIndex = -1;
1969
1970                                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
1971                                 return E_INVALID_ARG;
1972                         }
1973
1974                         DrawText();
1975                 }
1976                 else
1977                 {
1978                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
1979                         {
1980                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
1981                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1982
1983                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
1984                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1985                         }
1986                 }
1987
1988                 SetCursorDisabled(false);
1989         }
1990
1991         for (int i = 0; i < __pTokenList->GetCount(); i++)
1992         {
1993                 TrimTokenAndAdjustEllipsisAt(i);
1994                 InitializeTokenVisibilityAt(i);
1995         }
1996
1997         return E_SUCCESS;
1998 }
1999
2000 result
2001 _TokenEditPresenter::CheckTokenScrolling(bool scrollToCursorPosition)
2002 {
2003         bool needToScroll = false;
2004         int tokenTopMargin = 0;
2005         int tokenBottomMargin = 0;
2006         int tokenVerticalSpacing = 0;
2007         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2008
2009         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2010         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2011         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2012
2013         int tokenCount = GetTokenCount();
2014         if (tokenCount == 0)            // There is no token to scroll
2015         {
2016                 __isNeedToScroll = false;
2017                 __maxScrollValue = 0;
2018                 __isTokenScrolling = false;
2019
2020                 return E_SUCCESS;
2021         }
2022
2023         _Token* pToken = static_cast<_Token*>(__pTokenList->GetAt(tokenCount - 1));
2024         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2025
2026         int newScrollValue = 0;
2027
2028         if (scrollToCursorPosition)
2029         {
2030                 Rectangle cursorBounds;
2031                 GetCursorBounds(false,  cursorBounds);
2032                 newScrollValue = cursorBounds.y + cursorBounds.height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBounds().height;
2033                 __isScrollValueModified = true;
2034         }
2035         else
2036         {
2037                 newScrollValue = GetTextBounds().y + GetTextBounds().height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBounds().height;
2038                 __isScrollValueModified = true;
2039         }
2040
2041         needToScroll = (newScrollValue > 0) ? (true) : (false);
2042
2043         __isNeedToScroll = needToScroll;
2044
2045         if (__isNeedToScroll)
2046         {
2047                 __maxScrollValue = newScrollValue;
2048                 __isTokenScrolling = true;
2049
2050                 RecalculateTokenBounds(__maxScrollValue);
2051         }
2052         else
2053         {
2054                 if (__scrollValue != 0)
2055                 {
2056                         __scrollValue = 0;
2057                         __maxScrollValue = 0;           // To prevent unnecessary token scrolling.
2058                         RecalculateTokenBounds(__scrollValue);
2059                         __isTokenScrolling = false;
2060                 }
2061         }
2062
2063         return E_SUCCESS;
2064 }
2065
2066 result
2067 _TokenEditPresenter::SetTokenVisualElementBounds(int index, const Rectangle& bounds)
2068 {
2069         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
2070         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
2071
2072         return pToken->SetBounds(pToken->displayRect);
2073 }
2074
2075 result
2076 _TokenEditPresenter::AdjustFlexibleHeight(void)
2077 {
2078         result r = E_SUCCESS;
2079
2080         Rectangle editRect = __pTokenEdit->GetBounds();
2081         Rectangle displayScrollBounds = GetDisplayScrollBounds();
2082         int calcHeight = CalculateFlexibleHeight();
2083
2084         if (editRect.height != calcHeight)
2085         {
2086                 displayScrollBounds.height = calcHeight;
2087                 SetScrollBarBounds(displayScrollBounds);
2088
2089                 editRect.height = calcHeight;
2090                 if (!__isEditingToken)
2091                 {
2092                         r = SetFlexBounds(editRect);
2093                 }
2094         }
2095
2096         return r;
2097 }
2098
2099 int
2100 _TokenEditPresenter::CalculateFlexibleHeight(void)
2101 {
2102         int tokenHeight = 0;
2103         int tokenVerticalSpacing = 0;
2104         int tokenTopMargin =0;
2105         int tokenBottomMargin = 0;
2106         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2107
2108         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2109         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2110         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2111         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2112
2113         int height = 0;
2114         int maxLinecount = GetMaxLineCount();
2115         int expectedEditHeight = GetTextBounds().y + GetTextBounds().height - __scrollValue + tokenBottomMargin;
2116         int initialHeight = GetInitialBounds().height;
2117         int maximumLineHeight = tokenTopMargin + maxLinecount * tokenHeight + tokenVerticalSpacing * (maxLinecount - 1) + tokenBottomMargin;
2118
2119         if (initialHeight < expectedEditHeight)
2120         {
2121                 if (expectedEditHeight > maximumLineHeight)
2122                 {
2123                         height = maximumLineHeight;
2124                 }
2125                 else
2126                 {
2127                         height = expectedEditHeight;
2128                 }
2129         }
2130         else
2131         {
2132                 height = initialHeight;
2133         }
2134
2135         return height;
2136 }
2137
2138 result
2139 _TokenEditPresenter::DrawScrollBar(void)
2140 {
2141         result r = E_SUCCESS;
2142
2143         _Scroll* pScroll = GetScrollBar();
2144         if (pScroll == null)
2145         {
2146                 return E_SYSTEM;
2147         }
2148
2149         if (IsFocused() == false)
2150         {
2151                 pScroll->SetScrollVisibility(false);
2152                 return E_SUCCESS;
2153         }
2154
2155         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(GetTokenCount() - 1));
2156         if (pToken == null)
2157         {
2158                 return E_SUCCESS;
2159         }
2160
2161         int tokenBottomMargin = 0;
2162         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
2163
2164         int totalHeight = GetTextBounds().y + GetTextBounds().height - __scrollValue + tokenBottomMargin;
2165         int dspHeight = __pTokenEdit->GetBounds().height;
2166         int firstDspY = -__scrollValue;
2167
2168         if (totalHeight <= dspHeight)
2169         {
2170                 pScroll->SetScrollVisibility(false);
2171                 pScroll->SetScrollRange(1, 1);
2172                 pScroll->SetScrollPosition(0);
2173
2174                 SetScrollBarVisible(false);
2175                 SetPreviousScrollBarPosition(0);
2176                 SetMaximumPreviousScrollBarPosition(0);
2177         }
2178         else
2179         {
2180                 pScroll->SetScrollVisibility(true);
2181 //              if (_pScroll->IsVisible())
2182 //              {
2183                         if (firstDspY > 0)
2184                         {
2185                                 pScroll->SetScrollRange(dspHeight, totalHeight);
2186                                 pScroll->SetScrollPosition(firstDspY);
2187
2188                                 SetScrollBarVisible(true);
2189                                 SetMaximumPreviousScrollBarPosition(totalHeight - dspHeight);
2190                         }
2191                         else
2192                         {
2193                                 pScroll->SetScrollRange(dspHeight, totalHeight);
2194                                 pScroll->SetScrollPosition(0);
2195
2196                                 SetScrollBarVisible(true);
2197                                 SetMaximumPreviousScrollBarPosition(0);
2198                         }
2199
2200                         SetScrollBarVisible(true);
2201 //              }
2202
2203                 SetPreviousScrollBarPosition(firstDspY);
2204         }
2205
2206         return r;
2207 }
2208
2209 void
2210 _TokenEditPresenter::SetAutoShrinkModeEnabled(bool enable)
2211 {
2212         __autoShrink = enable;
2213 }
2214
2215 bool
2216 _TokenEditPresenter::IsAutoShrinkModeEnabled(void) const
2217 {
2218         return __autoShrink;
2219 }
2220
2221 void
2222 _TokenEditPresenter::SetDescriptionText(String descriptionText)
2223 {
2224         __descriptionText = descriptionText;
2225 }
2226
2227 String
2228 _TokenEditPresenter::GetDescriptionText(void) const
2229 {
2230         return __descriptionText;
2231 }
2232
2233 int
2234 _TokenEditPresenter::CalculateVisibleTokenCount(void)
2235 {
2236         int visibleTokenCount = 0;
2237
2238         Rectangle intialBounds = GetInitialBounds();
2239         Rectangle tempInitialBounds = intialBounds;
2240         Rectangle hiddenTokenDisplayBounds;
2241         int tokenCount = GetTokenCount();
2242
2243         for (int i = 0; i < tokenCount; i++)
2244         {
2245                 tempInitialBounds = intialBounds;
2246
2247                 hiddenTokenDisplayBounds = CalculateHiddenTokenCountDisplayBounds(tokenCount - i - 1);
2248                 tempInitialBounds.width -= hiddenTokenDisplayBounds.width;
2249
2250                 Point leftTop(hiddenTokenDisplayBounds.x, hiddenTokenDisplayBounds.y);
2251                 Point rightBottom(hiddenTokenDisplayBounds.x + hiddenTokenDisplayBounds.width, hiddenTokenDisplayBounds.y + hiddenTokenDisplayBounds.height);
2252
2253                 if (tempInitialBounds.Contains(leftTop) == false || tempInitialBounds.Contains(rightBottom) == false)
2254                 {
2255                         break;
2256                 }
2257                 visibleTokenCount++;
2258         }
2259         return visibleTokenCount;
2260 }
2261
2262 Rectangle
2263 _TokenEditPresenter::CalculateHiddenTokenCountDisplayBounds(int hiddenTokenCount)
2264 {
2265         result r = E_SUCCESS;
2266         Rectangle displayBounds;
2267
2268         int textSize = 0;
2269         int margin = 0;
2270         int height = 0;
2271         int horizontalSpace = 0;
2272         Dimension textDim;
2273         TextSimple* pSimpleText = null;
2274         Font* pFont = null;
2275
2276         GET_SHAPE_CONFIG(TOKENEDIT::HIDDEN_TOKEN_COUNT_DISPLAY_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, textSize);
2277         GET_SHAPE_CONFIG(TOKENEDIT::HIDDEN_TOKEN_COUNT_DISPLAY_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, margin);
2278         GET_SHAPE_CONFIG(TOKENEDIT::HIDDEN_TOKEN_COUNT_DISPLAY_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, height);
2279         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, _CONTROL_ORIENTATION_PORTRAIT, horizontalSpace);
2280
2281         int tokenIndex = GetTokenCount() - hiddenTokenCount - 1;
2282         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(tokenIndex));
2283         SysTryReturn(NID_UI_CTRL, pToken != null && pToken->pTextObject != null, Rectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2284
2285         displayBounds.x = pToken->displayRect.x + pToken->displayRect.width + margin;
2286         displayBounds.y = pToken->displayRect.y;
2287         displayBounds.height = height;
2288
2289         String hiddenTokenCountString("+");
2290         hiddenTokenCountString.Append(hiddenTokenCount);
2291
2292         int length = hiddenTokenCountString.GetLength();
2293         wchar_t* pTempString = const_cast <wchar_t*>(hiddenTokenCountString.GetPointer());
2294
2295         if (__pHiddenTokenCountTextObject)
2296         {
2297                 delete __pHiddenTokenCountTextObject;
2298                 __pHiddenTokenCountTextObject = null;
2299         }
2300
2301         __pHiddenTokenCountTextObject = new (std::nothrow) TextObject;
2302         SysTryReturn(NID_UI_CTRL, __pHiddenTokenCountTextObject != null, Rectangle(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2303
2304         r = __pHiddenTokenCountTextObject->Construct();
2305         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2306
2307         pSimpleText = new (std::nothrow) TextSimple(pTempString, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
2308         SysTryCatch(NID_UI_CTRL, pSimpleText != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2309
2310         __pHiddenTokenCountTextObject->AppendElement(*pSimpleText);
2311         __pHiddenTokenCountTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
2312         __pHiddenTokenCountTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
2313
2314         pFont = new (std::nothrow) Font;
2315         SysTryCatch(NID_UI_CTRL, pFont != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2316
2317         r = pFont->Construct(FONT_STYLE_PLAIN, textSize);
2318         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2319
2320         __pHiddenTokenCountTextObject->SetFont(pFont, 0, __pHiddenTokenCountTextObject->GetTextLength());
2321
2322         textDim = __pHiddenTokenCountTextObject->GetTextExtent(0, length);
2323         displayBounds.width = margin + textDim.width + margin;
2324
2325         delete pFont;
2326         pFont = null;
2327
2328         return displayBounds;
2329
2330 CATCH:
2331
2332         delete __pHiddenTokenCountTextObject;
2333         __pHiddenTokenCountTextObject = null;
2334
2335         delete pSimpleText;
2336         pSimpleText = null;
2337
2338         delete pFont;
2339         pFont = null;
2340
2341         return Rectangle();
2342 }
2343
2344 int
2345 _TokenEditPresenter::CalculateAutoShrinkAlignmentValue(int visibleTokenCount) const
2346 {
2347         int value = 0;
2348
2349         _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(visibleTokenCount - 1));
2350         SysTryReturn(NID_UI_CTRL, pToken != null && pToken->pTextObject != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
2351
2352         value = pToken->displayRect.y;
2353
2354         pToken = static_cast <_Token*>(__pTokenList->GetAt(0));
2355         SysTryReturn(NID_UI_CTRL, pToken != null && pToken->pTextObject != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2356
2357         value = pToken->displayRect.y + pToken->displayRect.height - value;
2358
2359         int initialHeight = GetInitialBounds().height;
2360
2361         value = (initialHeight - value) / 2;
2362
2363         int topMargin = 0;
2364         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, topMargin);
2365
2366         value -= topMargin;
2367
2368         if (value < 0)
2369         {
2370                 value = 0;
2371         }
2372
2373         return value;
2374 }
2375
2376 result
2377 _TokenEditPresenter::DrawHiddenTokenCount(void)
2378 {
2379         result r = E_SUCCESS;
2380
2381         if (__pHiddenTokenCountVisualElement)
2382         {
2383                 Canvas* pHiddenTokenCountCanvas = __pHiddenTokenCountVisualElement->GetCanvasN();
2384
2385                 if (pHiddenTokenCountCanvas)
2386                 {
2387                         __pHiddenTokenCountTextObject->SetForegroundColor(Color::GetColor(COLOR_ID_WHITE), 0, __pHiddenTokenCountTextObject->GetTextLength());
2388                         __pHiddenTokenCountTextObject->Compose();
2389                         __pHiddenTokenCountTextObject->SetBounds(__hiddenTokenDisplayCountBounds);
2390                         __pHiddenTokenCountTextObject->Draw(*_CanvasImpl::GetInstance(*pHiddenTokenCountCanvas));
2391
2392                         delete pHiddenTokenCountCanvas;
2393                         pHiddenTokenCountCanvas = null;
2394                 }
2395
2396                 __pHiddenTokenCountVisualElement->SetFlushNeeded();
2397         }
2398
2399         return r;
2400 }
2401
2402 bool
2403 _TokenEditPresenter::OnFocusGained(void)
2404 {
2405         if (__autoShrink)
2406         {
2407                 int tempHeight = CalculateFlexibleHeight();
2408                 Rectangle tempRect = GetInitialBounds();
2409                 tempRect.height = tempHeight;
2410
2411                 _Token* pToken = null;
2412                 int tokenCount = GetTokenCount();
2413
2414                 for (int i = 0; i < tokenCount; i++)
2415                 {
2416                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2417
2418                         if (pToken)
2419                         {
2420                                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2421                                 if (pTokenVisualElement)
2422                                 {
2423                                         pTokenVisualElement->SetShowState(true);
2424                                 }
2425                         }
2426                 }
2427
2428                 if (__autoShrink)
2429                 {
2430                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2431                         SysTryReturn(NID_UI_CTRL, pEditVisualElement, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2432
2433                         if (__pHiddenTokenCountVisualElement)
2434                         {
2435                                 pEditVisualElement->DetachChild(*__pHiddenTokenCountVisualElement);
2436                                 __pHiddenTokenCountVisualElement->Destroy();
2437                                 __pHiddenTokenCountVisualElement = null;
2438                         }
2439
2440                         __scrollValue = -__autoShrinkAlignmentValue;
2441                         __autoShrinkAlignmentValue = 0;
2442                         CalculateTokenPositionFromIndex(0);
2443                 }
2444
2445                 SetFlexBounds(tempRect);
2446
2447                 SetEditingTokenTextBounds(__edittingTokenIndex, false);
2448
2449                 if (!__isFocus)
2450                 {
2451                         __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2452                         __isFocus = true;
2453                 }
2454
2455                 if (__isScrollValueChanged && !__isScrollValueModified)
2456                 {
2457                         __maxScrollValue = __maxScrollValue - (__pTokenEdit->GetBounds().height - GetInitialBounds().height);
2458                         __isScrollValueChanged = false;
2459                 }
2460         }
2461         return _EditPresenter::OnFocusGained();
2462 }
2463
2464 bool
2465 _TokenEditPresenter::OnFocusLost(void)
2466 {
2467         result r = E_SUCCESS;
2468
2469         __isFocus = false;
2470
2471         if (__edittingTokenIndex >= 0)
2472         {
2473                 _Token* pToken = null;
2474                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2475                 if (pToken)
2476                 {
2477                         if (GetText().GetLength() > 0)
2478                         {
2479                                 OnTextCommitted(L"\n");
2480                         }
2481                 }
2482         }
2483         else
2484         {
2485                 if (GetText().GetLength() > 0)
2486                 {
2487                         OnTextCommitted(L"\n");
2488                 }
2489         }
2490
2491         if (__autoShrink)
2492         {
2493                 _Scroll* pScroll = GetScrollBar();
2494                 if (pScroll)
2495                 {
2496                         pScroll->SetScrollVisibility(false);
2497                 }
2498                 __scrollValue = 0;
2499
2500                 int tokenCount = GetTokenCount();
2501                 _Token* pToken = null;
2502                 for (int i = 0; i < tokenCount; i++)
2503                 {
2504                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2505                         if (pToken)
2506                         {
2507                                 pToken->SetBounds(pToken->displayRect);
2508                         }
2509                 }
2510
2511                 r = SetInitialBounds();
2512                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating", GetErrorMessage(r));
2513
2514                 int visibleTokenCount = CalculateVisibleTokenCount();
2515                 __hiddenTokenDisplayCountBounds = CalculateHiddenTokenCountDisplayBounds(tokenCount - visibleTokenCount);
2516
2517                 __autoShrinkAlignmentValue = CalculateAutoShrinkAlignmentValue(visibleTokenCount);
2518
2519                 if (visibleTokenCount > 0)
2520                 {
2521                         _Token* pToken = null;
2522                         for (int i = visibleTokenCount; i < tokenCount; i++)
2523                         {
2524                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2525                                 if (pToken)
2526                                 {
2527                                         _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2528                                         if (pTokenVisualElement)
2529                                         {
2530                                                 pTokenVisualElement->SetShowState(false);
2531                                         }
2532                                 }
2533                         }
2534
2535                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2536                         SysTryReturn(NID_UI_CTRL, pEditVisualElement != null, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2537
2538                         if (__pHiddenTokenCountVisualElement)
2539                         {
2540                                 pEditVisualElement->DetachChild(*__pHiddenTokenCountVisualElement);
2541                                 __pHiddenTokenCountVisualElement->Destroy();
2542                                 __pHiddenTokenCountVisualElement = null;
2543                         }
2544
2545                         __pHiddenTokenCountVisualElement = new (std::nothrow) _VisualElement;
2546                         if (__pHiddenTokenCountVisualElement)
2547                         {
2548                                 r = __pHiddenTokenCountVisualElement->Construct();
2549                                 __pHiddenTokenCountVisualElement->SetShowState(true);
2550                                 // Draw hidden token count
2551                                 __pHiddenTokenCountVisualElement->SetBounds(FloatRectangle(__hiddenTokenDisplayCountBounds.x,
2552                                         __hiddenTokenDisplayCountBounds.y, __hiddenTokenDisplayCountBounds.width, __hiddenTokenDisplayCountBounds.height));
2553
2554                                 pEditVisualElement->AttachChild(*__pHiddenTokenCountVisualElement);
2555                         }
2556                 }
2557
2558                 __scrollValue = __autoShrinkAlignmentValue;
2559                 r = CalculateTokenPositionFromIndex(0);
2560                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
2561
2562                 int totalScrollValue = __maxScrollValue + (__pTokenEdit->GetBounds().height - GetInitialBounds().height);
2563
2564                 if (totalScrollValue > 0)
2565                 {
2566                         __isNeedToScroll = true;
2567                         __maxScrollValue = totalScrollValue;
2568                         __isTokenScrolling = true;
2569                         if (__lineAdded > 0)
2570                         {
2571                                 __isScrollValueChanged = true;
2572                                 __isScrollValueModified = false;
2573                         }
2574                 }
2575
2576                 Rectangle intialWindowBounds = GetInitialBounds();
2577                 SetFlexBounds(intialWindowBounds);
2578
2579                 SetEditingTokenTextBounds(__edittingTokenIndex, false);
2580
2581                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2582         }
2583
2584         return _EditPresenter::OnFocusLost();
2585 }
2586
2587 result
2588 _TokenEditPresenter::SetFlexBounds(const Rectangle& bounds)
2589 {
2590         if (__pTokenEdit->GetBounds().height > bounds.height)
2591         {
2592                 if (__lineAdded > 0)
2593                 {
2594                         __lineAdded--;
2595                 }
2596         }
2597         return _EditPresenter::SetFlexBounds(bounds);
2598 }
2599
2600 bool
2601 _TokenEditPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
2602 {
2603         int tokenIndex = GetTokenIndexFromCoordinate(touchinfo.GetCurrentPosition());
2604
2605         if (tokenIndex != -1)
2606         {
2607                 return false;
2608         }
2609
2610         return _EditPresenter::OnTouchPressed(source, touchinfo);
2611 }
2612
2613 bool
2614 _TokenEditPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
2615 {
2616         ProcessTokeningByTouchEvent(source, touchinfo);
2617
2618         if (__prevScrollValue)
2619         {
2620                 __prevScrollValue = 0;
2621                 __isTokenScrolling = false;
2622         }
2623
2624         _TouchInfo TouchInfo(touchinfo);
2625         _Token* pToken = null;
2626         if (__edittingTokenIndex >= 0)
2627         {
2628                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2629                 if (pToken)
2630                 {
2631                         Point point(touchinfo.GetCurrentPosition().x - pToken->displayRect.x, touchinfo.GetCurrentPosition().y - pToken->displayRect.y);
2632                         TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
2633                 }
2634                 _EditPresenter::OnTouchReleased(source, TouchInfo);
2635                 __previousCursorPosition = GetCursorPosition();
2636         }
2637         else
2638         {
2639                 _EditPresenter::OnTouchReleased(source, touchinfo);
2640         }
2641
2642         return false;
2643 }
2644
2645 void
2646 _TokenEditPresenter::OnInputConnectionTextCommitted(InputConnection& source, const String& committedText)
2647 {
2648         OnTextCommitted(committedText);
2649 }
2650
2651 void
2652 _TokenEditPresenter::OnTextCommitted(const String& commitText)
2653 {
2654         result r = E_SUCCESS;
2655         char enterText[2] = {'\n', };
2656         if (commitText == enterText)
2657         {
2658                 if (__edittingTokenIndex != -1)
2659                 {
2660                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2661                         SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2662
2663                         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2664                         SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
2665
2666                         _Token* pToken = null;
2667                         _VisualElement* pTokenVisualElement = null;
2668
2669                         pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2670
2671                         if (pToken)
2672                         {
2673                                 pTokenVisualElement = pToken->GetVisualElement();
2674                                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
2675
2676                                 if (pCursorVisualElement->GetParent() != pEditVisualElement)
2677                                 {
2678                                         r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
2679                                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
2680
2681                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2682                                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
2683                                 }
2684                         }
2685
2686                         String inputTokenString = GetText();
2687                         String replacementString = inputTokenString;
2688                         bool enable = false;
2689
2690                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
2691                         if (enable)
2692                         {
2693                                 inputTokenString = replacementString;
2694                         }
2695
2696                         r = RemoveTokenAt(__edittingTokenIndex);
2697                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
2698
2699                         if (inputTokenString.GetLength() > 0)
2700                         {
2701                                 r = InsertTokenAt(__edittingTokenIndex, inputTokenString);
2702                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
2703
2704                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2705                                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2706
2707                                 pToken->currTokenLength = inputTokenString.GetLength();
2708                         }
2709
2710                         CalculateTokenPositionFromIndex(0);
2711                         int lastTokenIndex = GetTokenCount() - 1;
2712                         for (int i = 0; i < lastTokenIndex + 1; i++)
2713                         {
2714                                 _Token* pToken = null;
2715                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2716
2717                                 if (pToken)
2718                                 {
2719                                         pToken->SetBounds(pToken->displayRect);
2720                                 }
2721                         }
2722
2723                         __pressedTokenIndex = -1;
2724                         __isTokenEditingFinished = true;
2725                         __edittingTokenIndex = -1;
2726                         __isEditingToken = false;
2727
2728                         SetCursorDisabled(false);
2729
2730                         if (inputTokenString.GetLength() <= 0)
2731                         {
2732                                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
2733                                 return;
2734                         }
2735
2736                 }
2737
2738                 if (GetText().GetLength() > 0)
2739                 {
2740                         MakeToken();
2741                 }
2742
2743                 for (int i = 0; i < __pTokenList->GetCount(); i++)
2744                 {
2745                         TrimTokenAndAdjustEllipsisAt(i);
2746                         InitializeTokenVisibilityAt(i);
2747                 }
2748
2749                 SetCursorDisabled(true);
2750                 __pTokenEdit->Draw();
2751                 SetCursorDisabled(false);
2752                 StartCursorTimer();
2753
2754                 return;
2755         }
2756
2757         _EditPresenter::OnTextCommitted(commitText);
2758         __previousCursorPosition = GetCursorPosition();
2759
2760         _Token* pToken = null;
2761         if (__edittingTokenIndex >= 0)
2762         {
2763                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2764                 SysTryReturnVoidResult(NID_UI_CTRL, pToken, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
2765
2766                 pToken->ResetToken(GetText());
2767                 TrimTokenAndAdjustEllipsisAt(__edittingTokenIndex);
2768
2769                 int tokenHeight = 0;
2770                 int tokenVerticalSpacing = 0;
2771                 int tokenTextLeftMargin = 0;
2772
2773                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2774                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2775                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2776                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
2777
2778                 Rectangle tempTextDspRect;
2779                 tempTextDspRect.x += tokenTextLeftMargin;
2780                 tempTextDspRect.y += tokenVerticalSpacing / 2;
2781                 tempTextDspRect.width = pToken->GetTextPixelWidth();
2782                 tempTextDspRect.height = tokenHeight;
2783
2784                 if (pToken->GetTextPixelWidth() > (pToken->displayRect.width - tokenTextLeftMargin - 20))
2785                 {
2786                         tempTextDspRect.width = pToken->displayRect.width - tokenTextLeftMargin - 20;
2787                 }
2788                 SetTextBounds(tempTextDspRect);
2789
2790                 if (GetCursorPosition() > pToken->currTokenLength)
2791                 {
2792                         _EditPresenter::SetCursorPosition(pToken->currTokenLength);
2793                 }
2794                 else
2795                 {
2796                         _EditPresenter::SetCursorPosition(GetCursorPosition());
2797                 }
2798         }
2799         else
2800         {
2801                 if (__isEditingToken == false)
2802                 {
2803                         __pressedTokenIndex = -1;
2804                         SetCursorDisabled(false);
2805                 }
2806
2807                 int tokenTopMargin = 0;
2808
2809                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2810                 GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2811
2812                 Rectangle textBounds = GetTextBounds();
2813                 textBounds.height = __pTokenEdit->GetTextSize() + (2 * tokenTopMargin);
2814
2815                 SetTextBounds(textBounds);
2816         }
2817
2818         CheckTokenScrolling(true);
2819         __pTokenEdit->Draw();
2820
2821         return;
2822 }
2823
2824 void
2825 _TokenEditPresenter::DeleteSurroundingText(InputConnection& source, int offset, int charCount)
2826 {
2827         OnSurroundingTextDeleted(offset, charCount);
2828 }
2829
2830 void
2831 _TokenEditPresenter::OnSurroundingTextDeleted(int offset, int charCount)
2832 {
2833         __lastTokenIndex = GetTokenCount() - 1;
2834         int cursorPosition = 0;
2835         int start = 0;
2836         int end = 0;
2837
2838         if ((offset == -1) && (charCount == 1))
2839         {
2840                 if (GetTextLength() == 0 && GetTokenCount())        // There is no candidate token.
2841                 {
2842                         if (__isEditingToken == true)
2843                         {
2844                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2845                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2846
2847                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2848                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
2849
2850                                 _Token* pToken = null;
2851                                 _VisualElement* pTokenVisualElement = null;
2852
2853                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
2854
2855                                 if (pToken)
2856                                 {
2857                                         pTokenVisualElement = pToken->GetVisualElement();
2858                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
2859
2860                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
2861                                         {
2862                                                 result r = E_SUCCESS;
2863                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
2864                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2865
2866                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2867                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2868                                         }
2869                                 }
2870
2871                                 RemoveTokenAt(__edittingTokenIndex);
2872
2873                                 CalculateTokenPositionFromIndex(__edittingTokenIndex);
2874                                 for (int i = __edittingTokenIndex; i < __lastTokenIndex + 1; i++)
2875                                 {
2876                                         _Token* pToken = null;
2877                                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2878                                         if (pToken)
2879                                         {
2880                                                 pToken->SetBounds(pToken->displayRect);
2881                                         }
2882                                 }
2883
2884                                 __pressedTokenIndex = -1;
2885                                 __edittingTokenIndex = -1;
2886                                 __isEditingToken = false;
2887                                 __isTokenEditingFinished = true;
2888                         }
2889                         else if (__pressedTokenIndex != -1)
2890                         {
2891                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2892                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2893
2894                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2895                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
2896
2897                                 _Token* pToken = null;
2898                                 _VisualElement* pTokenVisualElement = null;
2899
2900                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
2901
2902                                 if (pToken)
2903                                 {
2904                                         pTokenVisualElement = pToken->GetVisualElement();
2905                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
2906
2907                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
2908                                         {
2909                                                 result r = E_SUCCESS;
2910                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
2911                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2912
2913                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2914                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2915                                         }
2916                                 }
2917
2918                                 RemoveTokenAt(__pressedTokenIndex);
2919
2920                                 CalculateTokenPositionFromIndex(__pressedTokenIndex);
2921                                 for (int i = __pressedTokenIndex; i < __lastTokenIndex + 1; i++)
2922                                 {
2923                                         _Token* pToken = null;
2924                                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
2925
2926                                         if (pToken)
2927                                         {
2928                                                 pToken->SetBounds(pToken->displayRect);
2929                                         }
2930                                 }
2931                         }
2932                         else
2933                         {
2934                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2935                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
2936
2937                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2938                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
2939
2940                                 _Token* pToken = null;
2941                                 _VisualElement* pTokenVisualElement = null;
2942                                 if (__animatingIndex == (GetTokenCount() - 1))
2943                                 {
2944                                         pToken = static_cast <_Token*>(__pTokenList->GetAt(GetTokenCount() - 1));
2945                                         if (pToken)
2946                                         {
2947                                                 pTokenVisualElement = pToken->GetVisualElement();
2948                                                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
2949                                                 pTokenVisualElement->RemoveAnimation(L"TokenAnimation");
2950                                         }
2951                                 }
2952                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(GetTokenCount() - 1));
2953                                 if (pToken)
2954                                 {
2955                                         pTokenVisualElement = pToken->GetVisualElement();
2956                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
2957
2958                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
2959                                         {
2960                                                 result r = E_SUCCESS;
2961                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
2962                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2963
2964                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2965                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
2966                                         }
2967                                 }
2968
2969                                 __animatingIndex = GetTokenCount() - 1;
2970
2971                                 if (pTokenVisualElement && __animatingIndex >= 0)
2972                                 {
2973                                         VisualElementAnimation* pAnimation = CreateAnimationN(*pTokenVisualElement, false);
2974                                         pTokenVisualElement->AddAnimation(L"TokenAnimation", *pAnimation);
2975                                         delete pAnimation;
2976                                 }
2977                         }
2978                         DrawText();
2979                         __pTokenEdit->Invalidate();
2980                         return;
2981                 }
2982         }
2983
2984         if (__pressedTokenIndex >= 0 && __edittingTokenIndex < 0 && !__isEditingToken)
2985         {
2986                 RemoveTokenAt(__pressedTokenIndex);
2987                 __pTokenEdit->Invalidate();
2988                 return;
2989         }
2990
2991         //Backspace on Blocked text, delete full block
2992         if (IsBlocked() == true)
2993         {
2994                 GetBlockRange(start, end);
2995         }
2996         else
2997         {
2998                 cursorPosition = GetCursorPosition();
2999                 start = cursorPosition + offset;
3000                 if (start < 0)
3001                 {
3002                         return;
3003                 }
3004                 end = start + charCount;
3005                 if (end > GetTextLength())
3006                 {
3007                         return;
3008                 }
3009         }
3010
3011         DeleteText(start, end);
3012         __previousCursorPosition = start;
3013
3014         if (IsCopyPastePopupVisible())
3015         {
3016                 InitializeCopyPasteManager();
3017         }
3018         if (IsBlocked() == true)
3019         {
3020                 ReleaseTextBlock();
3021         }
3022         if (__isEditingToken != true)
3023         {
3024                 DrawText();
3025         }
3026
3027         __pTokenEdit->SendTextEvent(CORE_TEXT_EVENT_CHANGED);
3028
3029 #if 0 //ORIGINAL
3030         cursorPosition = GetCursorPosition();
3031         start = cursorPosition + offset;
3032         if (start < 0)
3033         {
3034                 return;
3035         }
3036         end = start + charCount;
3037         if (end > GetTextLength())
3038         {
3039                 return;
3040         }
3041
3042         Rectangle currBounds = __pTokenEdit->GetBounds();
3043         DeleteText(start, end);
3044         __previousCursorPosition = start;
3045         if (__isEditingToken != true)
3046         {
3047                 DrawText();
3048         }
3049 #endif
3050
3051         _Token* pToken = null;
3052
3053         if (__edittingTokenIndex >= 0 && __isEditingToken)
3054         {
3055                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
3056
3057                 if (pToken)
3058                 {
3059                         int tokenHeight = 0;
3060                         int tokenVerticalSpacing = 0;
3061                         int tokenTextLeftMargin = 0;
3062
3063                         SetText(GetText());
3064                         SetCursorPosition(__previousCursorPosition);
3065
3066                         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3067                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
3068                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
3069                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3070
3071                         Rectangle tempTextDspRect;
3072                         tempTextDspRect.x += tokenTextLeftMargin;
3073                         tempTextDspRect.y += tokenVerticalSpacing/2;
3074                         tempTextDspRect.width = pToken->GetTextPixelWidth();
3075                         tempTextDspRect.height = tokenHeight;
3076
3077                         SetTextBounds(tempTextDspRect);
3078                         _EditPresenter::SetCursorPosition(start);
3079
3080                         pToken->ResetToken(GetText());
3081                         TrimTokenAndAdjustEllipsisAt(__edittingTokenIndex);
3082                 }
3083         }
3084
3085         __pTokenEdit->Draw();
3086 }
3087
3088 bool
3089 _TokenEditPresenter::OnTapGestureDetected(void)
3090 {
3091         __isPopupVisible = true;
3092
3093         if (__pressedTokenIndex != -1)
3094         {
3095                 return true;
3096         }
3097         return _EditPresenter::OnTapGestureDetected();
3098 }
3099
3100 bool
3101 _TokenEditPresenter::OnLongPressGestureDetected(void)
3102 {
3103         __isLongPressed = true;
3104         if (__pressedTokenIndex != -1)
3105         {
3106                 return true;
3107         }
3108         return _EditPresenter::OnLongPressGestureDetected();
3109 }
3110
3111 void
3112 _TokenEditPresenter::OnCursorTimerExpired(void)
3113 {
3114         if (__edittingTokenIndex != -1)
3115         {
3116                 if (!IsFocused())
3117                 {
3118                         StopCursorTimer();
3119                         return;
3120                 }
3121
3122                 Rectangle cursorRect;
3123
3124                 _Token* pToken = static_cast <_Token*>(__pTokenList->GetAt(__edittingTokenIndex));
3125                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
3126
3127                 Rectangle cursorDspRect = pToken->displayRect;
3128                 cursorDspRect.x = 12;
3129                 cursorDspRect.y = 10;
3130                 if (CalculateCursorBounds(cursorDspRect, cursorRect) != E_SUCCESS)
3131                 {
3132                         StartCursorTimer();
3133                         return;
3134                 }
3135
3136                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3137                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3138
3139                 Canvas* pCursorCanvas = pCursorVisualElement->GetCanvasN();
3140                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get canvas of cursor visual element.");
3141
3142                 bool cursorEnable = IsCursorEnabled();
3143
3144                 pCursorVisualElement->SetBounds(FloatRectangle(cursorRect.x, cursorRect.y, cursorRect.width, cursorRect.height));
3145                 DrawCursor(*pCursorCanvas, cursorRect, cursorEnable);
3146
3147                 if (cursorEnable)
3148                 {
3149                         cursorEnable = false;
3150                 }
3151                 else
3152                 {
3153                         cursorEnable = true;
3154                 }
3155                 SetCursorEnabled(cursorEnable);
3156
3157                 pCursorCanvas->Show();
3158                 delete pCursorCanvas;
3159
3160                 StartCursorTimer();
3161         }
3162         else
3163         {
3164                 _EditPresenter::OnCursorTimerExpired();
3165         }
3166 }
3167
3168 bool
3169 _TokenEditPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
3170 {
3171         if (GetTokenCount())
3172         {
3173                 SetTokenBoundsByTouchInfo(touchinfo);
3174                 return false;           // Event through
3175         }
3176         else
3177         {
3178                 return _EditPresenter::OnTouchMoved(source, touchinfo);
3179         }
3180
3181 }
3182
3183 void
3184 _TokenEditPresenter::OnVisualElementAnimationFinished (const Tizen::Ui::Animations::VisualElementAnimation &animation, const Tizen::Base::String &keyName, Tizen::Ui::Animations::VisualElement &target, bool completedNormally)
3185 {
3186         RemoveTokenAt(GetTokenCount() - 1);
3187         CalculateTokenPositionFromIndex(GetTokenCount() - 1);
3188
3189         for (int i = GetTokenCount() - 1; i < GetTokenCount() - 1 + 1; i++)
3190         {
3191                 _Token* pToken = null;
3192                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3193
3194                 if (pToken)
3195                 {
3196                         pToken->SetBounds(pToken->displayRect);
3197                 }
3198         }
3199         if (__lastTokenIndex == __pressedTokenIndex)
3200         {
3201                 __pressedTokenIndex--;
3202         }
3203
3204         __animatingIndex = -1;
3205         InitializeCursor();
3206
3207         DrawToken();
3208
3209         return;
3210 }
3211
3212 int
3213 _TokenEditPresenter::ResizeTokenAndAdjustEllipsis(int index, int value, bool isTokenShrink)
3214 {
3215         if (isTokenShrink)
3216         {
3217                 int trimValue = value;
3218                 int extraTrimValue = 0;
3219
3220                 _Token* pToken = null;
3221                 pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
3222                 SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3223
3224                 int tokenMinimumSize = 0;
3225
3226                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3227
3228                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinimumSize);
3229
3230                 if ((pToken->displayRect.width - trimValue) >= tokenMinimumSize)
3231                 {
3232                         pToken->displayRect.width -= trimValue;
3233                 }
3234                 else
3235                 {
3236                         extraTrimValue = (tokenMinimumSize - (pToken->displayRect.width - trimValue));
3237                         pToken->displayRect.width = tokenMinimumSize;
3238                 }
3239
3240                 InitializeTokenVisibilityAt(index);
3241
3242                 pToken->pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
3243                 pToken->pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
3244
3245                 return extraTrimValue;
3246         }
3247         else
3248         {
3249                 int expandValue = value;
3250                 int extraExpandValue = 0;
3251
3252                 _Token* pToken = null;
3253                 pToken = static_cast <_Token*>(__pTokenList->GetAt(index));
3254                 SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3255
3256                 int tokenTextLeftMargin = 0;
3257                 int tokenTextRightMargin = 0;
3258
3259                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3260
3261                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3262                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
3263
3264                 int tokenTextWidth = pToken->GetTextPixelWidth();
3265
3266                 int tokenDisplayRectWidth = tokenTextLeftMargin + tokenTextWidth + tokenTextRightMargin;
3267                 if ((pToken->displayRect.width + expandValue) <= tokenDisplayRectWidth)
3268                 {
3269                         pToken->displayRect.width += expandValue;
3270                 }
3271                 else
3272                 {
3273                         extraExpandValue = ((pToken->displayRect.width + expandValue) - tokenDisplayRectWidth);
3274                         pToken->displayRect.width = tokenDisplayRectWidth;
3275                 }
3276
3277                 InitializeTokenVisibilityAt(index);
3278
3279                 pToken->pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
3280                 pToken->pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
3281
3282                 return extraExpandValue;
3283         }
3284
3285         return -1;
3286 }
3287
3288 int
3289 _TokenEditPresenter::PickToken(int index, Rectangle tokenDispRect, bool isTokenShrink)
3290 {
3291         int tokenCount = __pTokenList->GetCount();
3292
3293         int tokenMinimumSize = 0;
3294         int tokenTextLeftMargin = 0;
3295         int tokenTextRightMargin = 0;
3296
3297         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3298
3299         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinimumSize);
3300         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3301         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
3302
3303         if (isTokenShrink)
3304         {
3305                 if (index < tokenCount)
3306                 {
3307                         for (int i = index; i >= 0; i--)
3308                         {
3309                                 _Token* pToken = null;
3310                                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3311                                 SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3312
3313                                 Rectangle tokenRect = pToken->displayRect;
3314                                 if (tokenDispRect.y == tokenRect.y)
3315                                 {
3316                                         if (tokenRect.width > tokenMinimumSize)
3317                                         {
3318                                                 return i;
3319                                         }
3320                                 }
3321                         }
3322                 }
3323         }
3324         else
3325         {
3326                 for (int i = index; i < tokenCount; i++)
3327                 {
3328                         _Token* pToken = null;
3329                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3330                         SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3331
3332                         Rectangle tokenRect = pToken->displayRect;
3333                         if (tokenDispRect.y == tokenRect.y)
3334                         {
3335                                 int tokenTextWidth = pToken->GetTextPixelWidth();
3336
3337                                 if (tokenRect.width < (tokenTextLeftMargin + tokenTextWidth + tokenTextRightMargin))
3338                                 {
3339                                         return i;
3340                                 }
3341                         }
3342                 }
3343         }
3344
3345         return -1;
3346 }
3347
3348 void
3349 _TokenEditPresenter::RepositionToken(int currentTokenIndex, int moveDistance, bool isTokenShrink)
3350 {
3351         int tokenCount = __pTokenList->GetCount();
3352
3353         _Token* pCurrentToken = null;
3354         pCurrentToken = static_cast <_Token*>(__pTokenList->GetAt(currentTokenIndex));
3355         SysTryReturnVoidResult(NID_UI_CTRL, pCurrentToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3356
3357         Rectangle currentTokenRect = pCurrentToken->displayRect;
3358
3359         if (currentTokenIndex < (tokenCount - 1))
3360         {
3361                 for (int i = currentTokenIndex + 1; i < tokenCount; i++)
3362                 {
3363                         _Token* pToken = null;
3364                         pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3365                         SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3366
3367                         if (pToken)
3368                         {
3369                                 Rectangle tokenRect = pToken->displayRect;
3370                                 if (currentTokenRect.y == tokenRect.y)
3371                                 {
3372                                         if (isTokenShrink)
3373                                         {
3374                                                 pToken->displayRect.x -= moveDistance;
3375                                         }
3376                                         else
3377                                         {
3378                                                 pToken->displayRect.x += moveDistance;
3379                                         }
3380
3381                                         InitializeTokenVisibilityAt(i);
3382                                 }
3383                                 else
3384                                 {
3385                                         break;
3386                                 }
3387                         }
3388                 }
3389         }
3390
3391         return;
3392 }
3393
3394 void
3395 _TokenEditPresenter::ShrinkTokens()
3396 {
3397         int tokenTextRightMargin = 0;
3398
3399         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3400         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
3401
3402         int tokenCount = __pTokenList->GetCount();
3403         Rectangle currentEditBounds = __pTokenEdit->GetBounds();
3404
3405         for (int i = 0; i < tokenCount; i++)
3406         {
3407                 bool isTokenPicked = false;
3408                 _Token* pToken = null;
3409                 _Token* pNextToken = null;
3410
3411                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3412                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3413
3414                 Rectangle tokenRect = pToken->displayRect;
3415
3416                 Rectangle nextTokenRect;
3417                 if (i < tokenCount - 1)
3418                 {
3419                         pNextToken = static_cast <_Token*>(__pTokenList->GetAt(i + 1));
3420                         SysTryReturnVoidResult(NID_UI_CTRL, pNextToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3421
3422                         nextTokenRect = pNextToken->displayRect;
3423                 }
3424
3425                 if (pNextToken)
3426                 {
3427                         if (nextTokenRect.y > tokenRect.y)
3428                         {
3429                                 isTokenPicked = true;
3430                         }
3431                 }
3432                 else
3433                 {
3434                         isTokenPicked = true;
3435                 }
3436
3437                 //"pToken" (isTokenPicked = true) is the last token in a row
3438                 if (isTokenPicked == false)
3439                 {
3440                         continue;
3441                 }
3442
3443                 int tokenEndPoint = tokenRect.x + tokenRect.width + tokenTextRightMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);
3444                 if (tokenEndPoint <= currentEditBounds.width)
3445                 {
3446                         continue;
3447                 }
3448
3449                 //Trimming Token required
3450                 int trimValue = tokenEndPoint - currentEditBounds.width;
3451
3452                 while (trimValue > 0)
3453                 {
3454                         int tokenIndex = PickToken(i, tokenRect, true);
3455                         if (tokenIndex >= 0)
3456                         {
3457                                 int extraTrimValue = ResizeTokenAndAdjustEllipsis(tokenIndex, trimValue, true);
3458                                 if (extraTrimValue < 0)
3459                                 {
3460                                         SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3461                                         break;
3462                                 }
3463
3464                                 if ((trimValue - extraTrimValue) > 0)
3465                                 {
3466                                         RepositionToken(tokenIndex, (trimValue - extraTrimValue), true);
3467                                 }
3468
3469                                 trimValue = extraTrimValue;
3470                         }
3471                         else
3472                         {
3473                                 break;
3474                         }
3475                 }
3476         }
3477
3478         return;
3479 }
3480
3481 int
3482 _TokenEditPresenter::GetPickedTokenEndPoint(int index, Tizen::Graphics::Rectangle tokenDispRect)
3483 {
3484         int tokenTextRightMargin = 0;
3485         int endValue = 0;
3486
3487         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3488         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
3489
3490         int marginValue = tokenTextRightMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_RIGHT_MARGIN);;
3491
3492         Rectangle lastTokenRect(0,0,0,0);
3493
3494         int tokenCount = __pTokenList->GetCount();
3495         for (int i = index; i < tokenCount; i++)
3496         {
3497                 _Token* pToken = null;
3498                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3499                 SysTryReturn(NID_UI_CTRL, pToken != null, 0, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3500
3501                 Rectangle tokenRect = pToken->displayRect;
3502                 if (tokenDispRect.y == tokenRect.y)
3503                 {
3504                         lastTokenRect = tokenRect;
3505                 }
3506                 else
3507                 {
3508                         break;
3509                 }
3510         }
3511
3512         endValue = marginValue + lastTokenRect.x + lastTokenRect.width;
3513
3514         return endValue;
3515 }
3516
3517 void
3518 _TokenEditPresenter::ExpandTokens()
3519 {
3520         int tokenCount = __pTokenList->GetCount();
3521         Rectangle currentEditBounds = __pTokenEdit->GetBounds();
3522
3523         for (int i = tokenCount - 1; i >= 0; i--)
3524         {
3525                 bool isTokenPicked = false;
3526                 _Token* pToken = null;
3527                 _Token* pPrevToken = null;
3528
3529                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3530                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3531
3532                 Rectangle tokenRect = pToken->displayRect;
3533
3534                 Rectangle prevTokenRect;
3535                 if (i  > 0)
3536                 {
3537                         pPrevToken = static_cast <_Token*>(__pTokenList->GetAt(i - 1));
3538                         SysTryReturnVoidResult(NID_UI_CTRL, pPrevToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3539
3540                         prevTokenRect = pPrevToken->displayRect;
3541                 }
3542
3543                 if (pPrevToken)
3544                 {
3545                         if (prevTokenRect.y < tokenRect.y)
3546                         {
3547                                 isTokenPicked = true;
3548                         }
3549                 }
3550                 else
3551                 {
3552                         isTokenPicked = true;
3553                 }
3554
3555                 //"pToken" (isTokenPicked = true) is the first token in a row
3556                 if (isTokenPicked == false)
3557                 {
3558                         continue;
3559                 }
3560
3561                 int tokenEndPoint = GetPickedTokenEndPoint(i, tokenRect);;
3562
3563                 if ((tokenEndPoint == 0) || (tokenEndPoint >= currentEditBounds.width))
3564                 {
3565                         continue;
3566                 }
3567
3568                 //Expanding Token required
3569                 int expandValue = currentEditBounds.width - tokenEndPoint;
3570
3571                 while (expandValue > 0)
3572                 {
3573                         int tokenIndex = PickToken(i, tokenRect, false);
3574                         if (tokenIndex >= 0)
3575                         {
3576                                 int extraExpandValue = ResizeTokenAndAdjustEllipsis(tokenIndex, expandValue, false);
3577                                 if (extraExpandValue < 0)
3578                                 {
3579                                         SysLogException(NID_UI_CTRL, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3580                                         break;
3581                                 }
3582
3583                                 if ((expandValue - extraExpandValue) > 0)
3584                                 {
3585                                         RepositionToken(tokenIndex, (expandValue - extraExpandValue), false);
3586                                 }
3587
3588                                 expandValue = extraExpandValue;
3589                         }
3590                         else
3591                         {
3592                                 break;
3593                         }
3594                 }
3595         }
3596
3597         return;
3598 }
3599
3600 void
3601 _TokenEditPresenter::OnBoundsChanged(void)
3602 {
3603         if (!__isTokenEditPresenterInitialized)
3604         {
3605                 return;
3606         }
3607
3608         int tokenCount = __pTokenList->GetCount();
3609         SysTryReturnVoidResult(NID_UI_CTRL, tokenCount > 0, E_SUCCESS, "No Tokens available.");
3610
3611         Rectangle tokenEditBounds = __pTokenEdit->GetBounds();
3612
3613         if (tokenEditBounds.width < __previousEditBounds.width)
3614         {
3615                 ShrinkTokens();
3616         }
3617         else if (tokenEditBounds.width > __previousEditBounds.width)
3618         {
3619                 ExpandTokens();
3620         }
3621
3622         __previousEditBounds = tokenEditBounds;
3623
3624         return;
3625 }
3626
3627
3628 }}} //Tizen::Ui::Controls