javauis/lcdui_qt/src/javax/microedition/lcdui/ChoiceImpl.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
child 56 abc41079b313
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description: 
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import java.util.Vector;
    19 import java.util.Vector;
    21 /**
    21 /**
    22  * ChoiceImpl class implements the basic functionality of the Choice interface.
    22  * ChoiceImpl class implements the basic functionality of the Choice interface.
    23  * <br> List and ChoiceGroup class use this class as their member to implement
    23  * <br> List and ChoiceGroup class use this class as their member to implement
    24  * their own functionality.
    24  * their own functionality.
    25  */
    25  */
    26 final class ChoiceImpl {
    26 final class ChoiceImpl
       
    27 {
    27 
    28 
    28     /**
    29     /**
    29      * Internal class for storing Choice data.
    30      * Internal class for storing Choice data.
    30      */
    31      */
    31     private class ChoiceData {
    32     private class ChoiceData
       
    33     {
    32         String text;
    34         String text;
    33         Image img;
    35         Image img;
    34         Font font;
    36         Font font;
    35         boolean sel;
    37         boolean sel;
    36 
    38 
    37         ChoiceData(String text, Image img, Font font, boolean sel) {
    39         ChoiceData(String text, Image img, Font font, boolean sel)
       
    40         {
    38             this.text = text;
    41             this.text = text;
    39             this.img = img;
    42             this.img = img;
    40             this.font = font;
    43             this.font = font;
    41             this.sel = sel;
    44             this.sel = sel;
    42         }
    45         }
    60     /**
    63     /**
    61      * Constructor.
    64      * Constructor.
    62      *
    65      *
    63      * @param multiSel multi or single selection
    66      * @param multiSel multi or single selection
    64      */
    67      */
    65     ChoiceImpl(boolean multiSel) {
    68     ChoiceImpl(boolean multiSel)
       
    69     {
    66         this.multiSel = multiSel;
    70         this.multiSel = multiSel;
    67     }
    71     }
    68 
    72 
    69     /**
    73     /**
    70      * Check if the arrays are valid.
    74      * Check if the arrays are valid.
    71      *
    75      *
    72      * @param textElements string array
    76      * @param textElements string array
    73      * @param imgElements  image array
    77      * @param imgElements  image array
    74      */
    78      */
    75     void check(String[] textElements, Image[] imgElements) {
    79     void check(String[] textElements, Image[] imgElements)
       
    80     {
    76         // check textElements
    81         // check textElements
    77         if (textElements == null) {
    82         if(textElements == null)
       
    83         {
    78             throw new NullPointerException(
    84             throw new NullPointerException(
    79                     MsgRepository.CHOICE_EXCEPTION_TEXT_ARRAY_NULL);
    85                 MsgRepository.CHOICE_EXCEPTION_TEXT_ARRAY_NULL);
    80         }
    86         }
    81         // check every text element
    87         // check every text element
    82         for (int i = 0; i < textElements.length; i++) {
    88         for(int i = 0; i < textElements.length; i++)
    83             if (textElements[i] == null) {
    89         {
       
    90             if(textElements[i] == null)
       
    91             {
    84                 throw new NullPointerException(
    92                 throw new NullPointerException(
    85                         MsgRepository.CHOICE_EXCEPTION_ITEM_NULL + i);
    93                     MsgRepository.CHOICE_EXCEPTION_ITEM_NULL + i);
    86             }
    94             }
    87         }
    95         }
    88         // check imgElements array length
    96         // check imgElements array length
    89         if (imgElements != null && imgElements.length != textElements.length) {
    97         if(imgElements != null && imgElements.length != textElements.length)
       
    98         {
    90             throw new IllegalArgumentException(
    99             throw new IllegalArgumentException(
    91                     MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_LENGTHS);
   100                 MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_LENGTHS);
    92         }
   101         }
    93     }
   102     }
    94 
   103 
    95     /**
   104     /**
    96      * Append item with specified text and image.
   105      * Append item with specified text and image.
    97      */
   106      */
    98     int append(String text, Image image) {
   107     int append(String text, Image image)
       
   108     {
    99         validateString(text);
   109         validateString(text);
   100         ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(),
   110         ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(),
   101                 (!multiSel && size() == 0));
   111                                          (!multiSel && size() == 0));
   102         items.addElement(data);
   112         items.addElement(data);
   103         // return index of added element
   113         // return index of added element
   104         return size() - 1;
   114         return size() - 1;
   105     }
   115     }
   106 
   116 
   107     /**
   117     /**
   108      * Insert item with specified text and image.
   118      * Insert item with specified text and image.
   109      */
   119      */
   110     void insert(int elementNum, String text, Image image) {
   120     void insert(int elementNum, String text, Image image)
       
   121     {
   111         validateString(text);
   122         validateString(text);
   112         validatePosition(elementNum, size() + 1);
   123         validatePosition(elementNum, size() + 1);
   113         ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(),
   124         ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(),
   114                 (!multiSel && size() == 0));
   125                                          (!multiSel && size() == 0));
   115         items.insertElementAt(data, elementNum);
   126         items.insertElementAt(data, elementNum);
   116     }
   127     }
   117 
   128 
   118     /**
   129     /**
   119      * Set item with specified text and image.
   130      * Set item with specified text and image.
   120      */
   131      */
   121     void set(int elementNum, String text, Image image) {
   132     void set(int elementNum, String text, Image image)
       
   133     {
   122         validateString(text);
   134         validateString(text);
   123         validatePosition(elementNum, size());
   135         validatePosition(elementNum, size());
   124         ChoiceData data = (ChoiceData) items.elementAt(elementNum);
   136         ChoiceData data = (ChoiceData) items.elementAt(elementNum);
   125         data.text = text;
   137         data.text = text;
   126         data.img = image;
   138         data.img = image;
   127     }
   139     }
   128 
   140 
   129     /**
   141     /**
   130      * Set item's font.
   142      * Set item's font.
   131      */
   143      */
   132     void setFont(int elementNum, Font font) {
   144     void setFont(int elementNum, Font font)
       
   145     {
   133         validatePosition(elementNum, size());
   146         validatePosition(elementNum, size());
   134         Font checkedFont = (font == null ? Font.getDefaultFont() : font);
   147         Font checkedFont = (font == null ? Font.getDefaultFont() : font);
   135         ((ChoiceData) items.elementAt(elementNum)).font = checkedFont;
   148         ((ChoiceData) items.elementAt(elementNum)).font = checkedFont;
   136     }
   149     }
   137 
   150 
   138     /**
   151     /**
   139      * Delete item from specified position.
   152      * Delete item from specified position.
   140      */
   153      */
   141     void delete(int elementNum) {
   154     void delete(int elementNum)
   142         validatePosition(elementNum, size());
   155     {
   143         if (!multiSel && isSelected(elementNum) && size() > 1) {
   156         validatePosition(elementNum, size());
       
   157         if(!multiSel && isSelected(elementNum) && size() > 1)
       
   158         {
   144             // we are removing the selected item and there are more
   159             // we are removing the selected item and there are more
   145             if (elementNum == size() - 1) {
   160             if(elementNum == size() - 1)
       
   161             {
   146                 // select previous item
   162                 // select previous item
   147                 setSelected(elementNum - 1, true);
   163                 setSelected(elementNum - 1, true);
   148             }
   164             }
   149             else {
   165             else
       
   166             {
   150                 // select next item
   167                 // select next item
   151                 setSelected(elementNum + 1, true);
   168                 setSelected(elementNum + 1, true);
   152             }
   169             }
   153         }
   170         }
   154         items.removeElementAt(elementNum);
   171         items.removeElementAt(elementNum);
   155     }
   172     }
   156 
   173 
   157     /**
   174     /**
   158      * Delete all items.
   175      * Delete all items.
   159      */
   176      */
   160     void deleteAll() {
   177     void deleteAll()
       
   178     {
   161         items.removeAllElements();
   179         items.removeAllElements();
   162     }
   180     }
   163 
   181 
   164     /**
   182     /**
   165      * Get fit policy.
   183      * Get fit policy.
   166      */
   184      */
   167     int getFitPolicy() {
   185     int getFitPolicy()
       
   186     {
   168         return fitPolicy;
   187         return fitPolicy;
   169     }
   188     }
   170 
   189 
   171     /**
   190     /**
   172      * Get the Font of a given item.
   191      * Get the Font of a given item.
   173      */
   192      */
   174     Font getFont(int elementNum) {
   193     Font getFont(int elementNum)
       
   194     {
   175         validatePosition(elementNum, size());
   195         validatePosition(elementNum, size());
   176         return ((ChoiceData) items.elementAt(elementNum)).font;
   196         return ((ChoiceData) items.elementAt(elementNum)).font;
   177     }
   197     }
   178 
   198 
   179     /**
   199     /**
   180      * Get the image of a given item.
   200      * Get the image of a given item.
   181      */
   201      */
   182     Image getImage(int elementNum) {
   202     Image getImage(int elementNum)
       
   203     {
   183         validatePosition(elementNum, size());
   204         validatePosition(elementNum, size());
   184         return ((ChoiceData) items.elementAt(elementNum)).img;
   205         return ((ChoiceData) items.elementAt(elementNum)).img;
   185     }
   206     }
   186 
   207 
   187     /**
   208     /**
   188      * Get the text of a given item.
   209      * Get the text of a given item.
   189      */
   210      */
   190     String getString(int elementNum) {
   211     String getString(int elementNum)
       
   212     {
   191         validatePosition(elementNum, size());
   213         validatePosition(elementNum, size());
   192         return ((ChoiceData) items.elementAt(elementNum)).text;
   214         return ((ChoiceData) items.elementAt(elementNum)).text;
   193     }
   215     }
   194 
   216 
   195     /**
   217     /**
   196      * Set fit policy.
   218      * Set fit policy.
   197      */
   219      */
   198     void setFitPolicy(int newFitPolicy) {
   220     void setFitPolicy(int newFitPolicy)
   199         switch (newFitPolicy) {
   221     {
   200             case Choice.TEXT_WRAP_DEFAULT:
   222         switch(newFitPolicy)
   201             case Choice.TEXT_WRAP_OFF:
   223         {
   202             case Choice.TEXT_WRAP_ON:
   224         case Choice.TEXT_WRAP_DEFAULT:
   203                 fitPolicy = newFitPolicy;
   225         case Choice.TEXT_WRAP_OFF:
   204                 break;
   226         case Choice.TEXT_WRAP_ON:
   205             default:
   227             fitPolicy = newFitPolicy;
   206                 throw new IllegalArgumentException(
   228             break;
   207                         MsgRepository.CHOICE_EXCEPTION_INVALID_FIT_POLICY);
   229         default:
       
   230             throw new IllegalArgumentException(
       
   231                 MsgRepository.CHOICE_EXCEPTION_INVALID_FIT_POLICY);
   208         }
   232         }
   209     }
   233     }
   210 
   234 
   211     /**
   235     /**
   212      * Get the selection index. Returns the first selection index if multiple
   236      * Get the selection index. Returns the first selection index if multiple
   213      * selection is allowed.
   237      * selection is allowed.
   214      */
   238      */
   215     int getSelectedIndex() {
   239     int getSelectedIndex()
       
   240     {
   216         final int size = size();
   241         final int size = size();
   217         if (!multiSel) {
   242         if(!multiSel)
   218             for (int i = 0; i < size; i++) {
   243         {
   219                 if (isSelected(i)) {
   244             for(int i = 0; i < size; i++)
       
   245             {
       
   246                 if(isSelected(i))
       
   247                 {
   220                     return i;
   248                     return i;
   221                 }
   249                 }
   222             }
   250             }
   223         }
   251         }
   224         return -1;
   252         return -1;
   225     }
   253     }
   226 
   254 
   227     /**
   255     /**
   228      * Checks if an item is selected.
   256      * Checks if an item is selected.
   229      */
   257      */
   230     boolean isSelected(int elementNum) {
   258     boolean isSelected(int elementNum)
       
   259     {
   231         validatePosition(elementNum, size());
   260         validatePosition(elementNum, size());
   232         return ((ChoiceData) items.elementAt(elementNum)).sel;
   261         return ((ChoiceData) items.elementAt(elementNum)).sel;
   233     }
   262     }
   234 
   263 
   235     /**
   264     /**
   236      * Sets an item's selection.
   265      * Sets an item's selection.
   237      */
   266      */
   238     void setSelected(int elementNum, boolean select) {
   267     void setSelected(int elementNum, boolean select)
   239         validatePosition(elementNum, size());
   268     {
   240         if (multiSel) {
   269         validatePosition(elementNum, size());
       
   270         if(multiSel)
       
   271         {
   241             ((ChoiceData) items.elementAt(elementNum)).sel = select;
   272             ((ChoiceData) items.elementAt(elementNum)).sel = select;
   242         }
   273         }
   243         else {
   274         else
   244             if (select) {
   275         {
       
   276             if(select)
       
   277             {
   245                 // clear all
   278                 // clear all
   246                 final int size = size();
   279                 final int size = size();
   247                 for (int i = 0; i < size; i++) {
   280                 for(int i = 0; i < size; i++)
       
   281                 {
   248                     ((ChoiceData) items.elementAt(i)).sel = false;
   282                     ((ChoiceData) items.elementAt(i)).sel = false;
   249                 }
   283                 }
   250                 // set just one
   284                 // set just one
   251                 ((ChoiceData) items.elementAt(elementNum)).sel = true;
   285                 ((ChoiceData) items.elementAt(elementNum)).sel = true;
   252             }
   286             }
   254     }
   288     }
   255 
   289 
   256     /**
   290     /**
   257      * Get selection flags.
   291      * Get selection flags.
   258      */
   292      */
   259     int getSelectedFlags(boolean[] selectedArray) {
   293     int getSelectedFlags(boolean[] selectedArray)
       
   294     {
   260         validateSelectedArray(selectedArray);
   295         validateSelectedArray(selectedArray);
   261         final int size = size();
   296         final int size = size();
   262         int numSelected = 0;
   297         int numSelected = 0;
   263         for (int i = 0; i < size; i++) {
   298         for(int i = 0; i < size; i++)
   264             if (((ChoiceData) items.elementAt(i)).sel) {
   299         {
       
   300             if(((ChoiceData) items.elementAt(i)).sel)
       
   301             {
   265                 selectedArray[i] = true;
   302                 selectedArray[i] = true;
   266                 numSelected++;
   303                 numSelected++;
   267             }
   304             }
   268             else {
   305             else
       
   306             {
   269                 selectedArray[i] = false;
   307                 selectedArray[i] = false;
   270             }
   308             }
   271         }
   309         }
   272         return numSelected;
   310         return numSelected;
   273     }
   311     }
   274 
   312 
   275     /**
   313     /**
   276      * Set selection flags.
   314      * Set selection flags.
   277      */
   315      */
   278     void setSelectedFlags(boolean[] selectedArray) {
   316     void setSelectedFlags(boolean[] selectedArray)
       
   317     {
   279         validateSelectedArray(selectedArray);
   318         validateSelectedArray(selectedArray);
   280         final int size = size();
   319         final int size = size();
   281         if (size > 0) {
   320         if(size > 0)
   282             if (multiSel) {
   321         {
   283                 for (int i = 0; i < size; i++) {
   322             if(multiSel)
       
   323             {
       
   324                 for(int i = 0; i < size; i++)
       
   325                 {
   284                     ((ChoiceData) items.elementAt(i)).sel = selectedArray[i];
   326                     ((ChoiceData) items.elementAt(i)).sel = selectedArray[i];
   285                 }
   327                 }
   286             }
   328             }
   287             else {
   329             else
       
   330             {
   288                 int firstSelected = 0;
   331                 int firstSelected = 0;
   289                 for (int i = 0; i < size; i++) {
   332                 for(int i = 0; i < size; i++)
   290                     if (selectedArray[i]) {
   333                 {
       
   334                     if(selectedArray[i])
       
   335                     {
   291                         firstSelected = i;
   336                         firstSelected = i;
   292                         break;
   337                         break;
   293                     }
   338                     }
   294                 }
   339                 }
   295                 setSelected(firstSelected, true);
   340                 setSelected(firstSelected, true);
   300     /**
   345     /**
   301      * Returns the size of the list.
   346      * Returns the size of the list.
   302      *
   347      *
   303      * @return the lists size
   348      * @return the lists size
   304      */
   349      */
   305     int size() {
   350     int size()
       
   351     {
   306         return items.size();
   352         return items.size();
   307     }
   353     }
   308 
   354 
   309     /**
   355     /**
   310      * Validates position.
   356      * Validates position.
   311      *
   357      *
   312      * @param position an index of the text array
   358      * @param position an index of the text array
   313      * @param upperBoundary upper boundary for position
   359      * @param upperBoundary upper boundary for position
   314      */
   360      */
   315     private void validatePosition(int position, int upperBoundary) {
   361     private void validatePosition(int position, int upperBoundary)
   316         if (position < 0 || position >= upperBoundary) {
   362     {
       
   363         if(position < 0 || position >= upperBoundary)
       
   364         {
   317             throw new IndexOutOfBoundsException(
   365             throw new IndexOutOfBoundsException(
   318                     MsgRepository.CHOICE_EXCEPTION_INVALID_ITEM_INDEX);
   366                 MsgRepository.CHOICE_EXCEPTION_INVALID_ITEM_INDEX);
   319         }
   367         }
   320     }
   368     }
   321 
   369 
   322     /**
   370     /**
   323      * Validates a string.
   371      * Validates a string.
   324      *
   372      *
   325      * @param str string
   373      * @param str string
   326      */
   374      */
   327     private void validateString(String str) {
   375     private void validateString(String str)
   328         if (str == null) {
   376     {
       
   377         if(str == null)
       
   378         {
   329             throw new NullPointerException(
   379             throw new NullPointerException(
   330                     MsgRepository.CHOICE_EXCEPTION_STRING_NULL);
   380                 MsgRepository.CHOICE_EXCEPTION_STRING_NULL);
   331         }
   381         }
   332     }
   382     }
   333 
   383 
   334     /**
   384     /**
   335      * Validates an array containing selections.
   385      * Validates an array containing selections.
   336      *
   386      *
   337      * @param selectedArray selected array
   387      * @param selectedArray selected array
   338      */
   388      */
   339     private void validateSelectedArray(boolean[] selectedArray) {
   389     private void validateSelectedArray(boolean[] selectedArray)
   340         if (selectedArray == null) {
   390     {
       
   391         if(selectedArray == null)
       
   392         {
   341             throw new NullPointerException(
   393             throw new NullPointerException(
   342                     MsgRepository.CHOICE_EXCEPTION_ARRAY_NULL);
   394                 MsgRepository.CHOICE_EXCEPTION_ARRAY_NULL);
   343         }
   395         }
   344         if (selectedArray.length < size()) {
   396         if(selectedArray.length < size())
       
   397         {
   345             throw new IllegalArgumentException(
   398             throw new IllegalArgumentException(
   346                     MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_SIZE);
   399                 MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_SIZE);
   347         }
   400         }
   348     }
   401     }
   349 
   402 
   350 }
   403 }