javauis/lcdui_qt/src/javax/microedition/lcdui/TextBox.java
branchRCL_3
changeset 65 ae942d28ec0e
equal deleted inserted replaced
60:6c158198356e 65:ae942d28ec0e
       
     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 com.nokia.mj.impl.rt.support.ApplicationInfo;
       
    20 
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.events.ModifyEvent;
       
    23 import org.eclipse.swt.events.ModifyListener;
       
    24 import org.eclipse.swt.widgets.*;
       
    25 
       
    26 /**
       
    27  * This class represents LCDUI TextBox.
       
    28  * The behavior of this editor may be different according
       
    29  * to applied constraints.
       
    30  */
       
    31 public class TextBox extends Screen
       
    32 {
       
    33 
       
    34     private TextModifyListener modListener = new TextModifyListener();
       
    35 
       
    36     private TextWrapper textWrapper;
       
    37 
       
    38     private int numLines;
       
    39 
       
    40     /**
       
    41      * Constructor.
       
    42      *
       
    43      * @param title - Title of the TextBox
       
    44      * @param text - default text appeared in the TextBox
       
    45      * @param maxSize - maximum number of characters to insert
       
    46      * @param constraints - text constraint (e.g. URL, EMEAIL etc)
       
    47      */
       
    48     public TextBox(String title, String text, int maxSize, int constraints)
       
    49     {
       
    50         super(title);
       
    51         textWrapper = new TextWrapper(text, maxSize, constraints);
       
    52         construct();
       
    53     }
       
    54 
       
    55     /**
       
    56      * Constructs custom eSWT shell for alert dialog.
       
    57      *
       
    58      * @return custom eSWT dialog shell
       
    59      */
       
    60     Shell eswtConstructShell(int style)
       
    61     {
       
    62         Shell topShell = super.eswtConstructShell(style);
       
    63         // TextBox with null title and ANY constraint, should be Full-Screen
       
    64         if(getTitle() == null && TextWrapper.getTypeConstraint(
       
    65                     textWrapper.getConstraints()) == TextField.ANY)
       
    66         {
       
    67             return topShell;
       
    68         }
       
    69         else
       
    70         {
       
    71             if(JadAttributeUtil.isValue(JadAttributeUtil.ATTRIB_NOKIA_UI_ENHANCEMENT, JadAttributeUtil.VALUE_FULLSCREEN_TEXTBOX))
       
    72             {
       
    73                 return topShell;
       
    74             }
       
    75             else
       
    76             {
       
    77                 return new Shell(topShell, style | SWT.DIALOG_TRIM | SWT.RESIZE);
       
    78             }
       
    79         }
       
    80     }
       
    81 
       
    82     /* (non-Javadoc)
       
    83      * @see Displayable#eswtConstructContent(int)
       
    84      */
       
    85     Composite eswtConstructContent(int style)
       
    86     {
       
    87         Composite ret = super.eswtConstructContent(style);
       
    88         textWrapper.eswtConstruct(ret, SWT.V_SCROLL);
       
    89         return ret;
       
    90     }
       
    91 
       
    92     /* (non-Javadoc)
       
    93      * @see Displayable#eswtHandleShowCurrentEvent()
       
    94      */
       
    95     void eswtHandleShowCurrentEvent()
       
    96     {
       
    97         super.eswtHandleShowCurrentEvent();
       
    98         textWrapper.setModifyListener(modListener);
       
    99         eswtSetPreferredContentSize(-1, textWrapper
       
   100                                     .getPreferredHeight(Config.TEXTBOX_MAX_VISIBLE_LINES));
       
   101     }
       
   102 
       
   103     /* (non-Javadoc)
       
   104      * @see Displayable#eswtHandleHideCurrentEvent()
       
   105      */
       
   106     void eswtHandleHideCurrentEvent()
       
   107     {
       
   108         super.eswtHandleHideCurrentEvent();
       
   109         textWrapper.setModifyListener(null);
       
   110     }
       
   111 
       
   112     /* (non-Javadoc)
       
   113      * @see Displayable#eswtHandleResizeEvent(int, int)
       
   114      */
       
   115     void eswtHandleResizeEvent(int width, int height)
       
   116     {
       
   117         super.eswtHandleResizeEvent(width, height);
       
   118         textWrapper.setBounds(getContentComp().getClientArea());
       
   119     }
       
   120 
       
   121     /**
       
   122      * Get current caret position.
       
   123      *
       
   124      * @return current caret position
       
   125      */
       
   126     public int getCaretPosition()
       
   127     {
       
   128         return textWrapper.getCaretPosition();
       
   129     }
       
   130 
       
   131     /**
       
   132      * Returns String with the content of TextBox.
       
   133      *
       
   134      * @return String with TexBox content
       
   135      */
       
   136     public String getString()
       
   137     {
       
   138         return textWrapper.getContent();
       
   139     }
       
   140 
       
   141     /**
       
   142      * Set new text into TextBox. Old content is substituted with newTxt
       
   143      *
       
   144      * @param newText - String to set into TextBox
       
   145      */
       
   146     public void setString(String newText)
       
   147     {
       
   148         textWrapper.setContent(newText);
       
   149     }
       
   150 
       
   151     /**
       
   152      * Copies TextBox content into char[] charData.
       
   153      *
       
   154      * @param charData array where to copy TextBox content
       
   155      * @return number of copied characters.
       
   156      */
       
   157     public int getChars(char[] charData)
       
   158     {
       
   159         if(charData == null)
       
   160         {
       
   161             throw new NullPointerException(
       
   162                 MsgRepository.TEXT_EXCEPTION_ARRAY_IS_NULL);
       
   163         }
       
   164         if(charData.length < getString().length())
       
   165         {
       
   166             throw new ArrayIndexOutOfBoundsException(
       
   167                 MsgRepository.TEXT_EXCEPTION_ARRAY_IS_TOO_SHORT);
       
   168         }
       
   169         String content = textWrapper.getContent();
       
   170         content.getChars(0, content.length(), charData, 0);
       
   171         return content.length();
       
   172     }
       
   173 
       
   174     /**
       
   175      * Set data from char[] array into TextBox. Previous content from TextBox is
       
   176      * substituted. Behavior is quite the same as TextBox.SetString().
       
   177      *
       
   178      * @param charData array of chars from where to copy.
       
   179      * @param offset start index in charData.
       
   180      * @param length how many characters to copy.
       
   181      */
       
   182     public void setChars(char[] charData, int offset, int length)
       
   183     {
       
   184         String extractedString = null;
       
   185         if(charData != null)
       
   186         {
       
   187             try
       
   188             {
       
   189                 extractedString = new String(charData, offset, length);
       
   190             }
       
   191             catch(IndexOutOfBoundsException e)
       
   192             {
       
   193                 throw new ArrayIndexOutOfBoundsException();
       
   194             }
       
   195         }
       
   196         textWrapper.setContent(extractedString);
       
   197     }
       
   198 
       
   199     /**
       
   200      * Inserts text into specified position.
       
   201      *
       
   202      * @param text text to insert, must not be null.
       
   203      * @param position where to insert.
       
   204      */
       
   205     public void insert(String text, int position)
       
   206     {
       
   207         textWrapper.insert(text, position);
       
   208     }
       
   209 
       
   210     /**
       
   211      * Inserts into TextBox range of characters from []charData array.
       
   212      *
       
   213      * @param charData array of chars to copy from.
       
   214      * @param offset start index in array to copy from.
       
   215      * @param length number of characters to copy.
       
   216      * @param position in TextBox where to insert.
       
   217      */
       
   218     public void insert(char[] charData, int offset, int length, int position)
       
   219     {
       
   220         if(charData == null)
       
   221         {
       
   222             throw new NullPointerException(
       
   223                 MsgRepository.TEXT_EXCEPTION_ARRAY_IS_NULL);
       
   224         }
       
   225         String extractedString = null;
       
   226         try
       
   227         {
       
   228             extractedString = new String(charData, offset, length);
       
   229         }
       
   230         catch(IndexOutOfBoundsException e)
       
   231         {
       
   232             throw new ArrayIndexOutOfBoundsException();
       
   233         }
       
   234         textWrapper.insert(extractedString, position);
       
   235     }
       
   236 
       
   237     /**
       
   238      * Delete range of characters from TextBox.
       
   239      *
       
   240      * @param offset - start index in TextBox to delete from.
       
   241      * @param length number of characters to delete.
       
   242      */
       
   243     public void delete(int offset, int length)
       
   244     {
       
   245         textWrapper.delete(offset, length);
       
   246     }
       
   247 
       
   248     /**
       
   249      * get number if characters TextBox can contain.
       
   250      *
       
   251      * @return number of characters allowed for the TextBox.
       
   252      */
       
   253     public int getMaxSize()
       
   254     {
       
   255         return textWrapper.getMaxSize();
       
   256     }
       
   257 
       
   258     /**
       
   259      * Set Max number of characters. The actual maximum size
       
   260      * may be less then newMaxSize due to platform limitations.
       
   261      *
       
   262      * @param newMaxSize sets the capacity of TextBox.
       
   263      * @return maxSize that was set.
       
   264      */
       
   265     public int setMaxSize(int newMaxSize)
       
   266     {
       
   267         textWrapper.setMaxSize(newMaxSize);
       
   268         return textWrapper.getMaxSize();
       
   269     }
       
   270 
       
   271     /**
       
   272      * Get number of characters in the TextBox.
       
   273      *
       
   274      * @return number if inputed Characters.
       
   275      */
       
   276     public int size()
       
   277     {
       
   278         return textWrapper.getSize();
       
   279     }
       
   280 
       
   281     /**
       
   282      * Set constraint for the TextBox.
       
   283      *
       
   284      * @param newConstraints constraint to apply to TextBox
       
   285      */
       
   286     public void setConstraints(int newConstraints)
       
   287     {
       
   288         textWrapper.setConstraints(newConstraints);
       
   289 
       
   290         if(!textWrapper.isValidText(getString() , textWrapper.getTypeConstraint(newConstraints)))
       
   291     	          setString("");        
       
   292     }
       
   293 
       
   294     /**
       
   295      * Get Current applied constraints.
       
   296      *
       
   297      * @return current applied constraints
       
   298      */
       
   299     public int getConstraints()
       
   300     {
       
   301         return textWrapper.getConstraints();
       
   302     }
       
   303 
       
   304     /**
       
   305      * Set initial input mode.
       
   306      *
       
   307      * @param inputMode input mode to set.
       
   308      */
       
   309     public void setInitialInputMode(String inputMode)
       
   310     {
       
   311         textWrapper.setInputMode(inputMode);
       
   312     }
       
   313 
       
   314     /**
       
   315      * Text modify listener.
       
   316      */
       
   317     class TextModifyListener implements ModifyListener
       
   318     {
       
   319 
       
   320         public void modifyText(ModifyEvent me)
       
   321         {
       
   322             int lines = TextWrapper.eswtGetLineCount((Control) me.widget);
       
   323             if(numLines != lines)
       
   324             {
       
   325                 // the number of lines changed
       
   326                 numLines = lines;
       
   327                 eswtSetPreferredContentSize(-1, textWrapper
       
   328                                             .getPreferredHeight(Config.TEXTBOX_MAX_VISIBLE_LINES));
       
   329             }
       
   330         }
       
   331 
       
   332     }
       
   333 
       
   334 }