javauis/lcdui_qt/src/javax/microedition/lcdui/TextWrapper.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package javax.microedition.lcdui;
       
    18 
       
    19 import org.eclipse.ercp.swt.mobile.ConstrainedText;
       
    20 import org.eclipse.ercp.swt.mobile.TextExtension;
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.events.ModifyListener;
       
    23 import org.eclipse.swt.events.SelectionListener;
       
    24 import org.eclipse.swt.graphics.Color;
       
    25 import org.eclipse.swt.graphics.Rectangle;
       
    26 import org.eclipse.swt.widgets.*;
       
    27 
       
    28 /**
       
    29  * Dynamic Text component implementation over the TextExtension/ConstrainedText
       
    30  * widgets as a wrapper.
       
    31  */
       
    32 class TextWrapper {
       
    33 
       
    34     private String text;
       
    35     private String inputMode;
       
    36     private int constraints;
       
    37     private int caret;
       
    38     private int size;
       
    39     private int maxSize;
       
    40 
       
    41     private Control control; // owned
       
    42     private int style;
       
    43     private int width = -1;
       
    44     private int height = -1;
       
    45     private int x = -1;
       
    46     private int y = -1;
       
    47     private Color fgCol; // owned
       
    48     private Color bgCol; // owned
       
    49     private boolean visible = true;
       
    50     private boolean focused = true;
       
    51 
       
    52     private Font font;   // not owned
       
    53     private ModifyListener modifyListener; // not owned
       
    54     private SelectionListener selectionListener; // not owned
       
    55 
       
    56     private String retSelectedStr;
       
    57     private int retLineHeight;
       
    58     private int retLineCount;
       
    59     private int retTopPixel;
       
    60 
       
    61     /**
       
    62      * Constructor.
       
    63      *
       
    64      * @param aText
       
    65      * @param aMaxSize
       
    66      * @param aConstraints
       
    67      */
       
    68     TextWrapper(String aText, int aMaxSize, int aConstraints) {
       
    69         setMaxSize(aMaxSize);
       
    70         setConstraints(aConstraints);
       
    71         setContent(aText);
       
    72     }
       
    73 
       
    74     /**
       
    75      * Construct eSWT control on specified composite.
       
    76      *
       
    77      * @param parent composite to create on
       
    78      * @param addStyle additional style
       
    79      */
       
    80     void construct(final Composite parent, final int addStyle) {
       
    81         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
    82             public void run() {
       
    83                 eswtConstruct(parent, addStyle);
       
    84             }
       
    85         });
       
    86     }
       
    87 
       
    88     /**
       
    89      * Construct eSWT control on specified composite.
       
    90      *
       
    91      * @param parent composite to create on
       
    92      * @param addStyle additional style
       
    93      */
       
    94     void eswtConstruct(Composite parent, int addStyle) {
       
    95         style = addStyle | SWT.MULTI | SWT.WRAP;
       
    96         if (parent != null) {
       
    97             eswtStoreStateAndDispose();
       
    98             eswtRestoreStateAndCreate(parent);
       
    99         }
       
   100         else {
       
   101             Logger.warning("Trying to construct TextWrapper with null parent");
       
   102         }
       
   103     }
       
   104 
       
   105     /**
       
   106      * Delete subset of characters from content.
       
   107      *
       
   108      * @param offset
       
   109      * @param length
       
   110      */
       
   111     void delete(int offset, int length) {
       
   112         if ((offset + length) > getSize()) {
       
   113             throw new StringIndexOutOfBoundsException(
       
   114                     MsgRepository.TEXT_EXCEPTION_ARRAY_INDEX_OUT_OF_BOUNDS);
       
   115         }
       
   116         StringBuffer sb = new StringBuffer(getContent());
       
   117         sb.delete(offset, offset + length);
       
   118         setContent(sb.toString());
       
   119     }
       
   120 
       
   121     /**
       
   122      * Dispose eSWT control.
       
   123      */
       
   124     void dispose() {
       
   125         if (control != null) {
       
   126             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   127                 public void run() {
       
   128                     eswtStoreStateAndDispose();
       
   129                 }
       
   130             });
       
   131         }
       
   132     }
       
   133 
       
   134     /**
       
   135      * Get caret position.
       
   136      */
       
   137     int getCaretPosition() {
       
   138         if (control != null) {
       
   139             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   140                 public void run() {
       
   141                     caret = eswtGetCaretPosition(control);
       
   142                 }
       
   143             });
       
   144         }
       
   145         return caret;
       
   146     }
       
   147 
       
   148     /**
       
   149      * Get constraints.
       
   150      */
       
   151     int getConstraints() {
       
   152         return constraints;
       
   153     }
       
   154 
       
   155     /**
       
   156      * Get text content.
       
   157      */
       
   158     String getContent() {
       
   159         if (control != null) {
       
   160             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   161                 public void run() {
       
   162                     text = eswtGetContent(control);
       
   163                 }
       
   164             });
       
   165         }
       
   166         return text;
       
   167     }
       
   168 
       
   169     /**
       
   170      * Gets the height of this Text control in pixels.
       
   171      */
       
   172     int getHeight() {
       
   173         return height;
       
   174     }
       
   175 
       
   176     /**
       
   177      * Get input mode.
       
   178      */
       
   179     String getInputMode() {
       
   180         return inputMode;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Get line count.
       
   185      */
       
   186     int getLineCount() {
       
   187         retLineCount = 1;
       
   188         if (control != null) {
       
   189             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   190                 public void run() {
       
   191                     retLineCount = eswtGetLineCount(control);
       
   192                 }
       
   193             });
       
   194         }
       
   195         return retLineCount;
       
   196     }
       
   197 
       
   198     /**
       
   199      * Get line height (in pixels).
       
   200      */
       
   201     int getLineHeight() {
       
   202         retLineHeight = 1;
       
   203         if (control != null) {
       
   204             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   205                 public void run() {
       
   206                     retLineHeight = eswtGetLineHeight(control);
       
   207                 }
       
   208             });
       
   209         }
       
   210         return retLineHeight;
       
   211     }
       
   212 
       
   213     /**
       
   214      * Get maximum size (in characters).
       
   215      */
       
   216     int getMaxSize() {
       
   217         if (control != null) {
       
   218             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   219                 public void run() {
       
   220                     maxSize = eswtGetMaxSize(control);
       
   221                 }
       
   222             });
       
   223         }
       
   224         return maxSize;
       
   225     }
       
   226 
       
   227     /**
       
   228      * Get the preferred height (in pixels) of the text control.
       
   229      *
       
   230      * @param maxVisibleLines maximum visible lines
       
   231      */
       
   232     int getPreferredHeight(int maxVisibleLines) {
       
   233         // lineCount (1 .. max) * lineHeight
       
   234         return Math.min(Math.max(1, getLineCount()), maxVisibleLines)
       
   235                 * getLineHeight();
       
   236     }
       
   237 
       
   238     /**
       
   239      * Get selected text content.
       
   240      */
       
   241     String getSelectedContent() {
       
   242         retSelectedStr = "";
       
   243         if (control != null) {
       
   244             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   245                 public void run() {
       
   246                     retSelectedStr = eswtGetSelectedContent(control);
       
   247                 }
       
   248             });
       
   249         }
       
   250         return retSelectedStr;
       
   251     }
       
   252 
       
   253     /**
       
   254      * Get content's length.
       
   255      */
       
   256     int getSize() {
       
   257         if (control != null) {
       
   258             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   259                 public void run() {
       
   260                     size = eswtGetSize(control);
       
   261                 }
       
   262             });
       
   263         }
       
   264         return size;
       
   265     }
       
   266 
       
   267     /**
       
   268      * Get visible top pixel position relative to whole content.
       
   269      */
       
   270     int getTopPixelPosition() {
       
   271         retTopPixel = 0;
       
   272         if (control != null) {
       
   273             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   274                 public void run() {
       
   275                     if (control instanceof TextExtension) {
       
   276                         retTopPixel = ((TextExtension) control).getTopPixel();
       
   277                     }
       
   278                     // ConstrainedText does not scroll -> value is 0
       
   279                 }
       
   280             });
       
   281         }
       
   282         return retTopPixel;
       
   283     }
       
   284 
       
   285     /**
       
   286      * Gets the width of this Text control (in pixels).
       
   287      */
       
   288     int getWidth() {
       
   289         return width;
       
   290     }
       
   291 
       
   292     /**
       
   293      * Insert text content.
       
   294      *
       
   295      * @param aText text
       
   296      * @param aPosition position where to instert
       
   297      */
       
   298     void insert(String aText, int aPosition) {
       
   299         if (aText == null) {
       
   300             throw new NullPointerException(
       
   301                     MsgRepository.TEXT_EXCEPTION_TXT_IS_NULL);
       
   302         }
       
   303         StringBuffer sb = new StringBuffer(getContent());
       
   304         if (aPosition < 0) {
       
   305             sb.insert(0, aText);
       
   306         }
       
   307         else if (aPosition > sb.length()) {
       
   308             sb.append(aText);
       
   309         }
       
   310         else {
       
   311             sb.insert(aPosition, aText);
       
   312         }
       
   313         setContent(sb.toString());
       
   314     }
       
   315 
       
   316     /**
       
   317      * Set background color.
       
   318      *
       
   319      * @param alpha alpha component (currently not used)
       
   320      * @param red color component
       
   321      * @param green color component
       
   322      * @param blue color component
       
   323      */
       
   324     void setBackgroundColor(final int alpha, final int red, final int green, final int blue) {
       
   325         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   326             public void run() {
       
   327                 if (bgCol != null) {
       
   328                     bgCol.dispose();
       
   329                     bgCol = null;
       
   330                 }
       
   331                 bgCol = new Color(ESWTUIThreadRunner.getInstance().getDisplay(),
       
   332                         red, green, blue);
       
   333             }
       
   334         });
       
   335     }
       
   336 
       
   337     /**
       
   338      * Set the bounds of the Text control.
       
   339      *
       
   340      * @param aBounds bounding rectangle
       
   341      */
       
   342     void setBounds(Rectangle aBounds) {
       
   343         if (aBounds.width < 0 || aBounds.height < 0) {
       
   344             throw new IllegalArgumentException(
       
   345                     MsgRepository.TEXT_EXCEPTION_INVALID_SIZE);
       
   346         }
       
   347         x = aBounds.x;
       
   348         y = aBounds.y;
       
   349         width = aBounds.width;
       
   350         height = aBounds.height;
       
   351         if (control != null && width >= 0 && height >= 0) {
       
   352             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   353                 public void run() {
       
   354                     control.setBounds(x, y, width, height);
       
   355                 }
       
   356             });
       
   357         }
       
   358     }
       
   359 
       
   360     /**
       
   361      * Set caret position.
       
   362      *
       
   363      * @param aPosition position
       
   364      */
       
   365     void setCaretposition(int aPosition) {
       
   366         caret = aPosition;
       
   367         if (control != null) {
       
   368             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   369                 public void run() {
       
   370                     eswtSetSelection(control, caret, caret);
       
   371                 }
       
   372             });
       
   373         }
       
   374     }
       
   375 
       
   376     /**
       
   377      * Set constraints. Note: this might reconstruct the eSWT text control.
       
   378      *
       
   379      * @param aConstraints text input constraints
       
   380      */
       
   381     void setConstraints(int aConstraints) {
       
   382         if (!isValidConstraints(aConstraints)) {
       
   383             throw new IllegalArgumentException(
       
   384                     MsgRepository.TEXT_EXCEPTION_INVALID_CONSTRAINTS);
       
   385         }
       
   386         constraints = aConstraints;
       
   387         if (control != null) {
       
   388             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   389                 public void run() {
       
   390                     eswtRestoreStateAndCreate(eswtStoreStateAndDispose());
       
   391                 }
       
   392             });
       
   393         }
       
   394     }
       
   395 
       
   396     /**
       
   397      * Set text content.
       
   398      *
       
   399      * @param aText new content
       
   400      */
       
   401     void setContent(String aText) {
       
   402         if (aText == null) {
       
   403             text = "";
       
   404         }
       
   405         else {
       
   406             if (aText.length() > maxSize) {
       
   407                 throw new IllegalArgumentException(
       
   408                         MsgRepository.TEXT_EXCEPTION_MAX_SIZE_EXCEEDED);
       
   409             }
       
   410             if (!isValidText(aText, constraints)) {
       
   411                 throw new IllegalArgumentException(
       
   412                         MsgRepository.TEXT_EXCEPTION_WRONG_TEXT);
       
   413             }
       
   414             text = aText;
       
   415         }
       
   416         size = text.length();
       
   417         caret = text.length();
       
   418         if (control != null) {
       
   419             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   420                 public void run() {
       
   421                     eswtSetContent(control, text);
       
   422                     eswtSetSelection(control, caret, caret);
       
   423                 }
       
   424             });
       
   425         }
       
   426     }
       
   427 
       
   428     /**
       
   429      * Set text font.
       
   430      *
       
   431      * @param aFont new font
       
   432      */
       
   433     void setFont(Font aFont) {
       
   434         font = aFont;
       
   435         if (control != null) {
       
   436             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   437                 public void run() {
       
   438                     if (font != null) {
       
   439                         // set custom font
       
   440                         control.setFont(Font.getESWTFont(font));
       
   441                     }
       
   442                     else {
       
   443                         // set default font
       
   444                         control.setFont(
       
   445                                 Font.getESWTFont(Font.getDefaultFont()));
       
   446                     }
       
   447                 }
       
   448             });
       
   449         }
       
   450     }
       
   451 
       
   452     /**
       
   453      * Set this Text control focused.
       
   454      *
       
   455      * @param aFocused
       
   456      */
       
   457     void setFocused(boolean aFocused) {
       
   458         focused = aFocused;
       
   459         if (control != null) {
       
   460             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   461                 public void run() {
       
   462                     eswtSetFocused(control, focused);
       
   463                 }
       
   464             });
       
   465         }
       
   466     }
       
   467 
       
   468     /**
       
   469      * Set foreground color.
       
   470      *
       
   471      * @param alpha alpha component (currently not used)
       
   472      * @param red color component
       
   473      * @param green color component
       
   474      * @param blue color component
       
   475      */
       
   476     void setForegroundColor(final int alpha, final int red, final int green, final int blue) {
       
   477         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   478             public void run() {
       
   479                 if (fgCol != null) {
       
   480                     fgCol.dispose();
       
   481                     fgCol = null;
       
   482                 }
       
   483                 fgCol = new Color(ESWTUIThreadRunner.getInstance().getDisplay(),
       
   484                         red, green, blue);
       
   485             }
       
   486         });
       
   487     }
       
   488 
       
   489     /**
       
   490      * Set input mode.
       
   491      *
       
   492      * @param inputMode
       
   493      */
       
   494     void setInputMode(String aInputMode) {
       
   495         inputMode = aInputMode;
       
   496         if (control != null && control instanceof TextExtension) {
       
   497             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   498                 public void run() {
       
   499                     eswtSetInputMode(control, inputMode, constraints);
       
   500                 }
       
   501             });
       
   502         }
       
   503     }
       
   504 
       
   505     /**
       
   506      * Set maximum size (in characters).
       
   507      *
       
   508      * @param aMaxSize
       
   509      */
       
   510     void setMaxSize(int aMaxSize) {
       
   511         if (aMaxSize < 1) {
       
   512             throw new IllegalArgumentException(
       
   513                     MsgRepository.TEXT_EXCEPTION_INVALID_MAX_SIZE);
       
   514         }
       
   515 
       
   516         String content = getContent();
       
   517         if (content != null && aMaxSize < content.length()) {
       
   518             // we have to truncate content - validates the new text
       
   519             setContent(content.substring(0, aMaxSize));
       
   520         }
       
   521 
       
   522         maxSize = aMaxSize;
       
   523         if (control != null) {
       
   524             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   525                 public void run() {
       
   526                     eswtSetMaxSize(control, maxSize);
       
   527                 }
       
   528             });
       
   529         }
       
   530     }
       
   531 
       
   532     /**
       
   533      * Set the modify listener;
       
   534      *
       
   535      * @param aListener modify listener
       
   536      */
       
   537     void setModifyListener(final ModifyListener aListener) {
       
   538         if (control != null) {
       
   539             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   540                 public void run() {
       
   541                     eswtRemoveModListener(control, modifyListener);
       
   542                     eswtAddModListener(control, aListener);
       
   543                 }
       
   544             });
       
   545         }
       
   546         modifyListener = aListener;
       
   547     }
       
   548 
       
   549     /**
       
   550      * Set the selection listener.
       
   551      *
       
   552      * @param aListener selection listener
       
   553      */
       
   554     void setSelectionListener(final SelectionListener aListener) {
       
   555         if (control != null) {
       
   556             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   557                 public void run() {
       
   558                     eswtRemoveSelListener(control, selectionListener);
       
   559                     eswtAddSelListener(control, aListener);
       
   560                 }
       
   561             });
       
   562         }
       
   563         selectionListener = aListener;
       
   564     }
       
   565 
       
   566     /**
       
   567      * Set the location of the eSWT control.
       
   568      *
       
   569      * @param aX
       
   570      * @param aY
       
   571      */
       
   572     void setPosition(int aX, int aY) {
       
   573         x = aX;
       
   574         y = aY;
       
   575         if (control != null) {
       
   576             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   577                 public void run() {
       
   578                     control.setLocation(x, y);
       
   579                 }
       
   580             });
       
   581         }
       
   582     }
       
   583 
       
   584     /**
       
   585      * Set the selection in the eSWT text control.
       
   586      *
       
   587      * @param sta selection start
       
   588      * @param end selection end
       
   589      */
       
   590     void setSelection(final int sta, final int end) {
       
   591         if (control != null) {
       
   592             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   593                 public void run() {
       
   594                     eswtSetSelection(control, sta, end);
       
   595                 }
       
   596             });
       
   597         }
       
   598     }
       
   599 
       
   600     /**
       
   601      * Set the size of the eSWT control.
       
   602      *
       
   603      * @param width
       
   604      * @param height
       
   605      */
       
   606     void setSize(int aWidth, int aHeight) {
       
   607         if (aWidth < 0 || aHeight < 0) {
       
   608             throw new IllegalArgumentException(
       
   609                     MsgRepository.TEXT_EXCEPTION_INVALID_SIZE);
       
   610         }
       
   611         width = aWidth;
       
   612         height = aHeight;
       
   613         if (control != null && width >= 0 && height >= 0) {
       
   614             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   615                 public void run() {
       
   616                     control.setSize(width, height);
       
   617                 }
       
   618             });
       
   619         }
       
   620     }
       
   621 
       
   622     /**
       
   623      * Set the visibility of the eSWT control.
       
   624      */
       
   625     void setVisible(boolean aVisible) {
       
   626         visible = aVisible;
       
   627         if (control != null) {
       
   628             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   629                 public void run() {
       
   630                     control.setVisible(visible);
       
   631                 }
       
   632             });
       
   633         }
       
   634     }
       
   635 
       
   636     /**
       
   637      * Restores a stored state and creates eSWT text control.
       
   638      *
       
   639      * @param parent parent composite
       
   640      */
       
   641     private void eswtRestoreStateAndCreate(Composite parent) {
       
   642         if (control == null) {
       
   643             control = eswtConstructText(parent, style, constraints);
       
   644             eswtSetInputMode(control, inputMode, constraints);
       
   645             eswtSetMaxSize(control, maxSize);
       
   646             eswtSetContent(control, text);
       
   647             eswtSetSelection(control, caret, caret);
       
   648             if (width >= 0 && height >= 0) {
       
   649                 control.setBounds(x, y, width, height);
       
   650             }
       
   651             if (fgCol != null) {
       
   652                 control.setForeground(fgCol);
       
   653             }
       
   654             if (bgCol != null) {
       
   655                 control.setBackground(bgCol);
       
   656             }
       
   657             if (font != null) {
       
   658                 control.setFont(Font.getESWTFont(font));
       
   659             }
       
   660             control.setVisible(visible);
       
   661             eswtSetFocused(control, focused);
       
   662             eswtAddModListener(control, modifyListener);
       
   663             eswtAddSelListener(control, selectionListener);
       
   664         }
       
   665     }
       
   666 
       
   667     /**
       
   668      * Stores the current state and disposes the eSWT text control.
       
   669      *
       
   670      * @return the control's parent composite.
       
   671      */
       
   672     private Composite eswtStoreStateAndDispose() {
       
   673         Composite parent = null;
       
   674         if (control != null) {
       
   675             parent = control.getParent();
       
   676             text = eswtGetContent(control);
       
   677             caret = eswtGetCaretPosition(control);
       
   678             eswtRemoveModListener(control, modifyListener);
       
   679             eswtRemoveSelListener(control, selectionListener);
       
   680             control.dispose();
       
   681             control = null;
       
   682         }
       
   683         return parent;
       
   684     }
       
   685 
       
   686     /**
       
   687      * Add modify listener.
       
   688      *
       
   689      * @param control text control
       
   690      * @param ltnr listener
       
   691      */
       
   692     static void eswtAddModListener(Control control, ModifyListener ltnr) {
       
   693         if (control != null && ltnr != null) {
       
   694             if (control instanceof TextExtension) {
       
   695                 ((TextExtension) control).addModifyListener(ltnr);
       
   696             }
       
   697             else {
       
   698                 ((ConstrainedText) control).addModifyListener(ltnr);
       
   699             }
       
   700         }
       
   701     }
       
   702 
       
   703     /**
       
   704      * Add selection listener.
       
   705      *
       
   706      * @param control text control
       
   707      * @param ltnr listener
       
   708      */
       
   709     static void eswtAddSelListener(Control control, SelectionListener ltnr) {
       
   710         if (control != null && ltnr != null) {
       
   711             if (control instanceof TextExtension) {
       
   712                 ((TextExtension) control).addSelectionListener(ltnr);
       
   713             }
       
   714             else {
       
   715                 ((ConstrainedText) control).addSelectionListener(ltnr);
       
   716             }
       
   717         }
       
   718     }
       
   719 
       
   720     /**
       
   721      * Construct text widget with given parameters.
       
   722      *
       
   723      * @param parent
       
   724      * @param aStyle
       
   725      * @param aConstraints
       
   726      * @return
       
   727      */
       
   728     static Control eswtConstructText(Composite parent, int aStyle, int aConstraints) {
       
   729         Control ret = null;
       
   730 
       
   731         int style = aStyle;
       
   732         int extractedFlag = aConstraints & ~TextField.CONSTRAINT_MASK;
       
   733         int extractedConstraint = aConstraints & TextField.CONSTRAINT_MASK;
       
   734 
       
   735         if ((extractedFlag & TextField.PASSWORD) == TextField.PASSWORD) {
       
   736             // Text class will remove incompatible flags for SINGLE
       
   737             style |= SWT.SINGLE | SWT.PASSWORD;
       
   738         }
       
   739         if ((extractedFlag & TextField.UNEDITABLE) == TextField.UNEDITABLE) {
       
   740             style |= SWT.READ_ONLY;
       
   741         }
       
   742 
       
   743         if (extractedConstraint == TextField.NUMERIC) {
       
   744             ret = new ConstrainedText(parent, style, ConstrainedText.NUMERIC);
       
   745         }
       
   746         else if (extractedConstraint == TextField.DECIMAL) {
       
   747             ret = new ConstrainedText(parent, style, ConstrainedText.DECIMAL);
       
   748         }
       
   749         else if (extractedConstraint == TextField.PHONENUMBER) {
       
   750             ret = new ConstrainedText(parent, style, ConstrainedText.PHONENUMBER);
       
   751         }
       
   752         else {
       
   753             ret = new TextExtension(parent, style);
       
   754         }
       
   755         return ret;
       
   756     }
       
   757 
       
   758     /**
       
   759      * Get caret position.
       
   760      *
       
   761      * @param control text control
       
   762      */
       
   763     static int eswtGetCaretPosition(Control control) {
       
   764         int ret = 0;
       
   765         if (control != null) {
       
   766             if (control instanceof TextExtension) {
       
   767                 ret = ((TextExtension) control).getCaretPosition();
       
   768             }
       
   769             else {
       
   770                 ret = ((ConstrainedText) control).getCaretPosition();
       
   771             }
       
   772         }
       
   773         return ret;
       
   774     }
       
   775 
       
   776     /**
       
   777      * Get caret line number.
       
   778      *
       
   779      * @param control text control
       
   780      */
       
   781     static int eswtGetCaretLine(Control control) {
       
   782         int ret = 0;
       
   783         if (control != null) {
       
   784             if (control instanceof TextExtension) {
       
   785                 ret = ((TextExtension) control).getCaretLineNumber();
       
   786             }
       
   787         }
       
   788         return ret;
       
   789     }
       
   790 
       
   791     /**
       
   792      * Get content.
       
   793      *
       
   794      * @param control text control
       
   795      */
       
   796     static String eswtGetContent(Control control) {
       
   797         String ret = "";
       
   798         if (control != null) {
       
   799             if (control instanceof TextExtension) {
       
   800                 ret = ((TextExtension) control).getText();
       
   801             }
       
   802             else {
       
   803                 ret = ((ConstrainedText) control).getText();
       
   804             }
       
   805         }
       
   806         return ret;
       
   807     }
       
   808 
       
   809     /**
       
   810      * Get line count.
       
   811      *
       
   812      * @param control text control
       
   813      */
       
   814     static int eswtGetLineCount(Control control) {
       
   815         int ret = 1;
       
   816         if (control != null) {
       
   817             if (control instanceof TextExtension) {
       
   818                 ret = ((TextExtension) control).getLineCount();
       
   819             }
       
   820         }
       
   821         return ret;
       
   822     }
       
   823 
       
   824     /**
       
   825      * Get line height.
       
   826      *
       
   827      * @param control text control
       
   828      */
       
   829     static int eswtGetLineHeight(Control control) {
       
   830         int ret = 0;
       
   831         if (control != null) {
       
   832             if (control instanceof TextExtension) {
       
   833                 ret = ((TextExtension) control).getLineHeight();
       
   834             }
       
   835             else {
       
   836                 ret = ((ConstrainedText) control).getSize().y;
       
   837             }
       
   838         }
       
   839         return ret;
       
   840     }
       
   841 
       
   842     /**
       
   843      * Get maximum content size.
       
   844      *
       
   845      * @param control text control
       
   846      */
       
   847     static int eswtGetMaxSize(Control control) {
       
   848         int ret = 0;
       
   849         if (control != null) {
       
   850             if (control instanceof TextExtension) {
       
   851                 ret = ((TextExtension) control).getTextLimit();
       
   852             }
       
   853             else {
       
   854                 ret = ((ConstrainedText) control).getTextLimit();
       
   855             }
       
   856         }
       
   857         return ret;
       
   858     }
       
   859 
       
   860     /**
       
   861      * Get selected content.
       
   862      *
       
   863      * @param control text control
       
   864      */
       
   865     static String eswtGetSelectedContent(Control control) {
       
   866         String ret = "";
       
   867         if (control != null) {
       
   868             if (control instanceof TextExtension) {
       
   869                 ret = ((TextExtension) control).getSelectionText();
       
   870             }
       
   871             else {
       
   872                 // TODO: eSWT support required - get selection in ConstrainedText
       
   873                 ret = ((ConstrainedText) control).getText();
       
   874             }
       
   875         }
       
   876         return ret;
       
   877     }
       
   878 
       
   879     /**
       
   880      * Get content size.
       
   881      *
       
   882      * @param control text control
       
   883      */
       
   884     static int eswtGetSize(Control control) {
       
   885         int ret = 0;
       
   886         if (control != null) {
       
   887             if (control instanceof TextExtension) {
       
   888                 ret = ((TextExtension) control).getCharCount();
       
   889             }
       
   890             else {
       
   891                 ret = ((ConstrainedText) control).getCharCount();
       
   892             }
       
   893         }
       
   894         return ret;
       
   895     }
       
   896 
       
   897     /**
       
   898      * Remove modify listener.
       
   899      *
       
   900      * @param control text control
       
   901      * @param ltnr listener
       
   902      */
       
   903     static void eswtRemoveModListener(Control control, ModifyListener ltnr) {
       
   904         if (control != null && ltnr != null) {
       
   905             if (control instanceof TextExtension) {
       
   906                 ((TextExtension) control).removeModifyListener(ltnr);
       
   907             }
       
   908             else {
       
   909                 ((ConstrainedText) control).removeModifyListener(ltnr);
       
   910             }
       
   911         }
       
   912     }
       
   913 
       
   914     /**
       
   915      * Remove modify listener.
       
   916      *
       
   917      * @param control text control
       
   918      * @param ltnr listener
       
   919      */
       
   920     static void eswtRemoveSelListener(Control control, SelectionListener ltnr) {
       
   921         if (control != null && ltnr != null) {
       
   922             if (control instanceof TextExtension) {
       
   923                 ((TextExtension) control).removeSelectionListener(ltnr);
       
   924             }
       
   925             else {
       
   926                 ((ConstrainedText) control).removeSelectionListener(ltnr);
       
   927             }
       
   928         }
       
   929     }
       
   930 
       
   931     /**
       
   932      * Set content.
       
   933      *
       
   934      * @param control text control
       
   935      * @param text content
       
   936      */
       
   937     static void eswtSetContent(Control control, String text) {
       
   938         if (control != null) {
       
   939             if (control instanceof TextExtension) {
       
   940                 ((TextExtension) control).setText(text);
       
   941             }
       
   942             else {
       
   943                 ((ConstrainedText) control).setText(text);
       
   944             }
       
   945         }
       
   946     }
       
   947 
       
   948     /**
       
   949      * Set text control focus on/off.
       
   950      *
       
   951      * @param control text control
       
   952      * @param focus
       
   953      */
       
   954     static void eswtSetFocused(Control control, boolean focus) {
       
   955         if (control != null) {
       
   956             if (focus) {
       
   957                 control.setFocus();
       
   958             }
       
   959             else {
       
   960                 control.getParent().forceFocus();
       
   961             }
       
   962         }
       
   963     }
       
   964 
       
   965     /**
       
   966      * Set input mode.
       
   967      *
       
   968      * @param control text control
       
   969      * @param inputMode input mode
       
   970      * @param aConstraints constraints
       
   971      */
       
   972     static void eswtSetInputMode(Control control, String inputMode,
       
   973             int aConstraints) {
       
   974         if (control != null && control instanceof TextExtension) {
       
   975             TextExtension te = (TextExtension) control;
       
   976             int capitalize = getCapitalize(aConstraints);
       
   977             if (inputMode == null) {
       
   978                 te.setInitialInputMode(TextExtension.TEXTCASE,
       
   979                         "UCB_BASIC_LATIN");
       
   980             }
       
   981             else if (inputMode.equals("MIDP_UPPERCASE_LATIN")) {
       
   982                 if (capitalize > 0 && !isUrlEmailSet(aConstraints)) {
       
   983                     te.setInitialInputMode(capitalize, "UCB_BASIC_LATIN");
       
   984                 }
       
   985                 else {
       
   986                     te.setInitialInputMode(TextExtension.UPPERCASE,
       
   987                             "UCB_BASIC_LATIN");
       
   988                 }
       
   989             }
       
   990             else if (inputMode.equals("MIDP_LOWERCASE_LATIN")) {
       
   991                 if (capitalize > 0 && !isUrlEmailSet(aConstraints)) {
       
   992                     te.setInitialInputMode(capitalize, "UCB_BASIC_LATIN");
       
   993                 }
       
   994                 else {
       
   995                     te.setInitialInputMode(TextExtension.LOWERCASE,
       
   996                             "UCB_BASIC_LATIN");
       
   997                 }
       
   998             }
       
   999             else {
       
  1000                 te.setInitialInputMode(TextExtension.TEXTCASE, inputMode);
       
  1001             }
       
  1002         }
       
  1003     }
       
  1004 
       
  1005     /**
       
  1006      * Set maximum size.
       
  1007      *
       
  1008      * @param control text control
       
  1009      * @param maxSize maximum size
       
  1010      */
       
  1011     static void eswtSetMaxSize(Control control, int maxSize) {
       
  1012         if (control != null) {
       
  1013             if (control instanceof TextExtension) {
       
  1014                 ((TextExtension) control).setTextLimit(maxSize);
       
  1015             }
       
  1016             else {
       
  1017                 ((ConstrainedText) control).setTextLimit(maxSize);
       
  1018             }
       
  1019         }
       
  1020     }
       
  1021 
       
  1022     /**
       
  1023      * Set selection.
       
  1024      *
       
  1025      * @param control text control
       
  1026      * @param sta start index
       
  1027      * @param end end index
       
  1028      */
       
  1029     static void eswtSetSelection(Control control, int sta, int end) {
       
  1030         if (control != null) {
       
  1031             if (control instanceof TextExtension) {
       
  1032                 ((TextExtension) control).setSelection(sta, end);
       
  1033             }
       
  1034             else {
       
  1035                 ((ConstrainedText) control).setSelection(sta, end);
       
  1036             }
       
  1037         }
       
  1038     }
       
  1039 
       
  1040     /**
       
  1041      * Update vertical scroll bar.
       
  1042      *
       
  1043      * @param control text control
       
  1044      */
       
  1045     static void eswtUpdateVScrollbar(Control control) {
       
  1046         if (control != null) {
       
  1047             if ((control.getStyle() & SWT.V_SCROLL) == SWT.V_SCROLL) {
       
  1048                 if (control instanceof TextExtension) {
       
  1049                     TextExtension te = (TextExtension) control;
       
  1050                     ScrollBar sb = te.getVerticalBar();
       
  1051                     if (sb != null) {
       
  1052                         int height = te.getLineCount() * te.getLineHeight();
       
  1053                         sb.setVisible(te.getSize().y < height);
       
  1054                     }
       
  1055                 }
       
  1056             }
       
  1057         }
       
  1058     }
       
  1059 
       
  1060     /**
       
  1061      * Get eSWT capitalize style flags for TextExtension.
       
  1062      *
       
  1063      * @param aConstraints constraints
       
  1064      */
       
  1065     static int getCapitalize(int aConstraints) {
       
  1066         int ret = 0;
       
  1067         int extractedFlag = aConstraints & ~TextField.CONSTRAINT_MASK;
       
  1068         if ((extractedFlag & TextField.INITIAL_CAPS_WORD)
       
  1069                 == TextField.INITIAL_CAPS_WORD) {
       
  1070             ret = TextExtension.TITLECASE;
       
  1071         }
       
  1072         if ((extractedFlag & TextField.INITIAL_CAPS_SENTENCE)
       
  1073                 == TextField.INITIAL_CAPS_SENTENCE) {
       
  1074             ret = TextExtension.TEXTCASE;
       
  1075         }
       
  1076         return ret;
       
  1077     }
       
  1078 
       
  1079     /**
       
  1080      * Is Url or Email constraints flags set.
       
  1081      *
       
  1082      * @param aConstraints constraints
       
  1083      */
       
  1084     static boolean isUrlEmailSet(int aConstraints) {
       
  1085         int extractedConstraint = aConstraints & TextField.CONSTRAINT_MASK;
       
  1086         return (extractedConstraint == TextField.EMAILADDR
       
  1087                 || extractedConstraint == TextField.URL);
       
  1088     }
       
  1089 
       
  1090     /**
       
  1091      * Validates input constraints.
       
  1092      *
       
  1093      * @param aConstraints constraints to check.
       
  1094      * @return true if constraints are OK, false otherwise.
       
  1095      */
       
  1096     static boolean isValidConstraints(int aConstraints) {
       
  1097         int smallestFlag;
       
  1098         int highestFlag;
       
  1099         smallestFlag = TextField.PASSWORD - 1;
       
  1100         highestFlag = TextField.PASSWORD + TextField.UNEDITABLE
       
  1101                 + TextField.SENSITIVE + TextField.NON_PREDICTIVE
       
  1102                 + TextField.INITIAL_CAPS_WORD + TextField.INITIAL_CAPS_SENTENCE
       
  1103                 + 1;
       
  1104 
       
  1105         int typeConstraint = getTypeConstraint(aConstraints);
       
  1106         if (typeConstraint == TextField.ANY
       
  1107                 || typeConstraint == TextField.EMAILADDR
       
  1108                 || typeConstraint == TextField.NUMERIC
       
  1109                 || typeConstraint == TextField.PHONENUMBER
       
  1110                 || typeConstraint == TextField.DECIMAL
       
  1111                 || typeConstraint == TextField.URL) {
       
  1112             int extractedFlag = aConstraints & ~TextField.CONSTRAINT_MASK;
       
  1113             if (extractedFlag == 0
       
  1114                     || ((extractedFlag > smallestFlag)
       
  1115                          && (extractedFlag < highestFlag))) {
       
  1116                 return true;
       
  1117             }
       
  1118         }
       
  1119         return false;
       
  1120     }
       
  1121 
       
  1122     /**
       
  1123      * Get constraint type.
       
  1124      *
       
  1125      * @param aConstraints constraints
       
  1126      */
       
  1127     static int getTypeConstraint(int aConstraints) {
       
  1128         return aConstraints & TextField.CONSTRAINT_MASK;
       
  1129     }
       
  1130 
       
  1131     /**
       
  1132      * Checks if the text meets the requirements from constraint.
       
  1133      *
       
  1134      * @param aText
       
  1135      * @return
       
  1136      */
       
  1137     static boolean isValidText(String aText, int aConstraints) {
       
  1138         return TextFieldLayouter.checkText(aConstraints, aText);
       
  1139     }
       
  1140 
       
  1141 }