javauis/lcdui_akn/javalcdui/javasrc/javax/microedition/lcdui/ChoiceComponent.java
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002 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 
       
    18 
       
    19 package javax.microedition.lcdui;
       
    20 
       
    21 /**
       
    22 Utility class for ChoiceGroup and List
       
    23 Methods which require the LCDUI lock to be held have UpUp() style names
       
    24 */
       
    25 final class ChoiceComponent
       
    26 {
       
    27     private class ChoiceDataItem
       
    28     {
       
    29         String iString;
       
    30         Image iImage;
       
    31         Font iFont;
       
    32 
       
    33         ChoiceDataItem(String aString, Image aImage, Font aFont)
       
    34         {
       
    35             iString = aString;
       
    36             iImage = aImage;
       
    37             iFont = aFont;
       
    38         }
       
    39     }
       
    40 
       
    41     private static final int ARRAY_INCREMENT = 4;
       
    42 
       
    43     private ChoiceDataItem[] iChoiceArray;
       
    44     private int              iChoiceCount;
       
    45     private int              iFitPolicy;
       
    46 
       
    47     //
       
    48     // Validate choicegroup/list constructor array args
       
    49     //
       
    50     static Image[] validateElements(String[] aTextArray, Image[] aIconArray)
       
    51     {
       
    52         final int size = aTextArray.length;
       
    53         if (aIconArray == null)
       
    54         {
       
    55             aIconArray = new Image[size];
       
    56         }
       
    57         if (aIconArray.length != size)
       
    58         {
       
    59             throw new IllegalArgumentException();
       
    60         }
       
    61         for (int i=0; i<size; i++)
       
    62         {
       
    63             checkItem(aTextArray[i]);// could just call length() and let the VM raise NPE
       
    64         }
       
    65         return aIconArray;
       
    66     }
       
    67 
       
    68     ChoiceComponent()
       
    69     {
       
    70         iFitPolicy   = Choice.TEXT_WRAP_DEFAULT;
       
    71         iChoiceArray = new ChoiceDataItem[] {};
       
    72         iChoiceCount = 0;
       
    73     }
       
    74 
       
    75     final String GetString(int aIndex)
       
    76     {
       
    77         checkIndexInBounds(aIndex, iChoiceCount);
       
    78         return iChoiceArray[aIndex].iString;
       
    79     }
       
    80 
       
    81     final Image GetImage(int aIndex)
       
    82     {
       
    83         checkIndexInBounds(aIndex, iChoiceCount);
       
    84         return iChoiceArray[aIndex].iImage;
       
    85     }
       
    86 
       
    87     final int size()
       
    88     {
       
    89         return iChoiceCount;
       
    90     }
       
    91 
       
    92     final int  Append(String aText, Image aIcon)
       
    93     {
       
    94         final int pos = iChoiceCount;
       
    95         Insert(pos, aText, aIcon);
       
    96         return pos;
       
    97     }
       
    98 
       
    99     final void Insert(int aIndex, String aText, Image aIcon)
       
   100     {
       
   101         checkItem(aText);
       
   102         checkIndexInBounds(aIndex, iChoiceCount+1);
       
   103         ChoiceDataItem item = new ChoiceDataItem(aText, aIcon, Font.getDefaultFont());
       
   104         CheckCapacity(iChoiceCount+1);
       
   105         System.arraycopy(iChoiceArray, aIndex, iChoiceArray, aIndex+1, iChoiceCount-aIndex);
       
   106         iChoiceArray[aIndex] = item;
       
   107         iChoiceCount++;
       
   108     }
       
   109 
       
   110     final void Delete(int aIndex)
       
   111     {
       
   112         checkIndexInBounds(aIndex, iChoiceCount);
       
   113         if (aIndex != iChoiceCount-1)
       
   114         {
       
   115             System.arraycopy(iChoiceArray, aIndex+1, iChoiceArray, aIndex, iChoiceCount-aIndex-1);
       
   116         }
       
   117         iChoiceCount--;
       
   118     }
       
   119 
       
   120     final void DeleteAll()
       
   121     {
       
   122         for (int i=0; i<iChoiceCount; i++)
       
   123         {
       
   124             iChoiceArray[i] = null;
       
   125         }
       
   126         iChoiceCount=0;
       
   127     }
       
   128 
       
   129     final void Set(int aIndex, String aString,Image aImage)
       
   130     {
       
   131         checkIndexInBounds(aIndex, iChoiceCount);
       
   132         checkItem(aString);
       
   133 
       
   134         //
       
   135         // Snapshot
       
   136         //
       
   137         if (aImage != null)
       
   138         {
       
   139             aImage = Image.createImage(aImage);
       
   140         }
       
   141 
       
   142         final ChoiceDataItem item = iChoiceArray[aIndex];
       
   143         item.iString = aString;
       
   144         item.iImage  = aImage;
       
   145     }
       
   146 
       
   147     final void SetFitPolicy(int aFitPolicy)
       
   148     {
       
   149         if (aFitPolicy != Choice.TEXT_WRAP_DEFAULT &&
       
   150                 aFitPolicy != Choice.TEXT_WRAP_ON &&
       
   151                 aFitPolicy != Choice.TEXT_WRAP_OFF)
       
   152             throw new IllegalArgumentException();
       
   153         // SYNC NOTE: Requires lock to be held to ensure that if another
       
   154         // thread reads this variable several times while holding the lock,
       
   155         // the reads will return consistent values.
       
   156         iFitPolicy=aFitPolicy;
       
   157     }
       
   158 
       
   159     final int getFitPolicy()
       
   160     {
       
   161         return iFitPolicy;
       
   162     }
       
   163 
       
   164     final void SetFont(int aElementNum,Font aFont)
       
   165     {
       
   166         checkIndexInBounds(aElementNum, iChoiceCount);
       
   167         final ChoiceDataItem item = iChoiceArray[aElementNum];
       
   168         item.iFont = aFont;
       
   169     }
       
   170 
       
   171     final Font GetFont(int aElementNum)
       
   172     {
       
   173         checkIndexInBounds(aElementNum, iChoiceCount);
       
   174         return iChoiceArray[aElementNum].iFont;
       
   175     }
       
   176 
       
   177     final static void checkIndexInBounds(int aIndex,int aSize)
       
   178     {
       
   179         if (aIndex <0 || aIndex >= aSize)
       
   180             throw new IndexOutOfBoundsException();
       
   181     }
       
   182 
       
   183     private final static void checkItem(String aString)
       
   184     {
       
   185         if (aString == null)
       
   186             throw new NullPointerException();
       
   187     }
       
   188 
       
   189     /**
       
   190     Grows the array if required.
       
   191     */
       
   192     private final void CheckCapacity(int aRequiredLength)
       
   193     {
       
   194         final int length = iChoiceArray.length;
       
   195         if (aRequiredLength>length)
       
   196         {
       
   197             ChoiceDataItem[] newItems = new ChoiceDataItem[length + ARRAY_INCREMENT];
       
   198             System.arraycopy(iChoiceArray, 0, newItems, 0, length);
       
   199             iChoiceArray = newItems;
       
   200         }
       
   201     }
       
   202 }