javauis/lcdui_akn/javalcdui/javasrc/javax/microedition/lcdui/ChoiceGroup.java
branchRCL_3
changeset 26 2455ef1f5bbc
parent 14 04becd199f91
equal deleted inserted replaced
25:ae942d28ec0e 26:2455ef1f5bbc
       
     1 /*
       
     2 * Copyright (c) 1999 - 2004 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 import com.nokia.mj.impl.rt.legacy.NativeError;
       
    22 
       
    23 public class ChoiceGroup extends Item implements Choice
       
    24 {
       
    25     private final int iChoiceType;
       
    26     private final ChoiceComponent iChoiceComponent;
       
    27 
       
    28     public ChoiceGroup(String aLabel,int aChoiceType)
       
    29     {
       
    30         this(aLabel,aChoiceType,new String[] {},null);
       
    31     }
       
    32 
       
    33     public ChoiceGroup(String aLabel,int aChoiceType,String[] aTextArray,Image[] aIconArray)
       
    34     {
       
    35         if (aChoiceType != Choice.EXCLUSIVE && aChoiceType != Choice.MULTIPLE && aChoiceType != Choice.POPUP)
       
    36         {
       
    37             throw new IllegalArgumentException();
       
    38         }
       
    39         aIconArray = ChoiceComponent.validateElements(aTextArray,aIconArray);
       
    40         synchronized (iToolkit)
       
    41         {
       
    42             iChoiceComponent = new ChoiceComponent();
       
    43             if (aLabel == null)
       
    44             {
       
    45                 aLabel = "";
       
    46             }
       
    47             iLabel  = aLabel;
       
    48             iChoiceType = aChoiceType;
       
    49             iHandle = Toolkit.checkHandle(_create(iToolkit.getHandle(), aLabel, aChoiceType));
       
    50             final int count = aTextArray.length;
       
    51             for (int i=0; i<count; i++)
       
    52             {
       
    53                 Insert(i, aTextArray[i], aIconArray[i]);
       
    54             }
       
    55         }
       
    56     }
       
    57 
       
    58     public int size()
       
    59     {
       
    60         return iChoiceComponent.size();
       
    61     }
       
    62 
       
    63     public String getString(int aIndex)
       
    64     {
       
    65         synchronized (iToolkit)
       
    66         {
       
    67             return iChoiceComponent.GetString(aIndex);
       
    68         }
       
    69     }
       
    70 
       
    71     public Image getImage(int aIndex)
       
    72     {
       
    73         synchronized (iToolkit)
       
    74         {
       
    75             return iChoiceComponent.GetImage(aIndex);
       
    76         }
       
    77     }
       
    78 
       
    79     public int append(String aStringElement,Image aImageElement)
       
    80     {
       
    81         final int pos;
       
    82         synchronized (iToolkit)
       
    83         {
       
    84             pos = iChoiceComponent.size();
       
    85             // SYNC NOTE: cannot call public API method here
       
    86             Insert(pos,aStringElement,aImageElement);
       
    87             RefreshForm();
       
    88         }
       
    89         return pos;
       
    90     }
       
    91 
       
    92     public void insert(int aIndex,String aStringElement,Image aImageElement)
       
    93     {
       
    94         synchronized (iToolkit)
       
    95         {
       
    96             Insert(aIndex,aStringElement,aImageElement);
       
    97             RefreshForm();
       
    98         }
       
    99     }
       
   100 
       
   101     /**
       
   102      * Internal implementation of insert
       
   103      * SYNC NOTE: requires LCDUI lock to be held
       
   104      */
       
   105     void Insert(int aIndex,String aStringElement,Image aImageElement)
       
   106     {
       
   107         iChoiceComponent.Insert(aIndex,aStringElement,aImageElement);
       
   108         final int image = aImageElement==null?0:aImageElement.getHandle(true);
       
   109         NativeError.check(_insert(getHandle(),iToolkit.getHandle(),aIndex,aStringElement,image));
       
   110     }
       
   111 
       
   112     public void delete(int aIndex)
       
   113     {
       
   114         synchronized (iToolkit)
       
   115         {
       
   116             iChoiceComponent.Delete(aIndex);
       
   117             NativeError.check(_delete(getHandle(),iToolkit.getHandle(),aIndex));
       
   118             RefreshForm();
       
   119         }
       
   120     }
       
   121 
       
   122     public void deleteAll()
       
   123     {
       
   124         synchronized (iToolkit)
       
   125         {
       
   126             iChoiceComponent.DeleteAll();
       
   127             NativeError.check(_deleteAll(getHandle(),iToolkit.getHandle()));
       
   128             RefreshForm();
       
   129         }
       
   130     }
       
   131 
       
   132     public void set(int aIndex,String aStringElement,Image aImageElement)
       
   133     {
       
   134         synchronized (iToolkit)
       
   135         {
       
   136             iChoiceComponent.Set(aIndex,aStringElement,aImageElement);
       
   137             final int image = aImageElement==null?0:aImageElement.getHandle(true);
       
   138             NativeError.check(_set(getHandle(),iToolkit.getHandle(),aIndex,aStringElement,image));
       
   139         }
       
   140     }
       
   141 
       
   142     public boolean isSelected(int aIndex)
       
   143     {
       
   144         synchronized (iToolkit)
       
   145         {
       
   146             return IsSelected(aIndex);
       
   147         }
       
   148     }
       
   149 
       
   150     /**
       
   151      * Internal implementation of isSelected
       
   152      * SYNC NOTE: requires LCDUI lock to be held
       
   153      */
       
   154     boolean IsSelected(int aIndex)
       
   155     {
       
   156         iChoiceComponent.checkIndexInBounds(aIndex,iChoiceComponent.size());
       
   157         return _isSelected(getHandle(),iToolkit.getHandle(),aIndex);
       
   158     }
       
   159 
       
   160     public int getSelectedIndex()
       
   161     {
       
   162         synchronized (iToolkit)
       
   163         {
       
   164             if (iChoiceType != Choice.MULTIPLE)
       
   165             {
       
   166                 final int len = iChoiceComponent.size();
       
   167                 for (int i=0; i<len; i++)
       
   168                 {
       
   169                     // SYNC NOTE: cannot call public API method here in case
       
   170                     // it has been overridden by application subclass.
       
   171                     if (IsSelected(i))
       
   172                         return i;
       
   173                 }
       
   174             }
       
   175             return -1;
       
   176         }
       
   177     }
       
   178 
       
   179     public int getSelectedFlags(boolean[] aSelectedArrayReturn)
       
   180     {
       
   181         synchronized (iToolkit)
       
   182         {
       
   183             final int len = aSelectedArrayReturn.length;
       
   184             final int choiceSize = iChoiceComponent.size();
       
   185             if (len < choiceSize)
       
   186                 throw new IllegalArgumentException();
       
   187             int nsel=0;
       
   188             for (int i=0; i<len; i++)
       
   189             {
       
   190                 // SYNC NOTE: cannot call public API method here in case
       
   191                 // it has been overridden by application subclass.
       
   192                 if (i<choiceSize && IsSelected(i))
       
   193                 {
       
   194                     nsel++;
       
   195                     aSelectedArrayReturn[i] = true;
       
   196                 }
       
   197                 else
       
   198                     aSelectedArrayReturn[i] = false;
       
   199             }
       
   200             return nsel;
       
   201         }
       
   202     }
       
   203 
       
   204     public void setSelectedIndex(int aIndex,boolean aSelected)
       
   205     {
       
   206         synchronized (iToolkit)
       
   207         {
       
   208             SetSelectedIndex(aIndex,aSelected);
       
   209         }
       
   210     }
       
   211 
       
   212     /**
       
   213      * Internal implementation of setSelectedIndex
       
   214      * SYNC NOTE: requires LCDUI lock to be held
       
   215      */
       
   216     void SetSelectedIndex(int aIndex,boolean aSelected)
       
   217     {
       
   218         iChoiceComponent.checkIndexInBounds(aIndex,iChoiceComponent.size());
       
   219         NativeError.check(_select(getHandle(),iToolkit.getHandle(),aIndex,aSelected));
       
   220     }
       
   221 
       
   222     public void setSelectedFlags(boolean[] aSelectedArray)
       
   223     {
       
   224         if (aSelectedArray==null)
       
   225             throw new NullPointerException();
       
   226         synchronized (iToolkit)
       
   227         {
       
   228             final int len = iChoiceComponent.size();
       
   229             if (len==0)
       
   230                 return;
       
   231             if (aSelectedArray.length < len)
       
   232                 throw new IllegalArgumentException();
       
   233             //
       
   234             boolean hasTrue=false;
       
   235             for (int i=0; i<len; ++i)
       
   236                 hasTrue = hasTrue || aSelectedArray[i];
       
   237             //
       
   238             final boolean notMultiple = iChoiceType != Choice.MULTIPLE;
       
   239             if (notMultiple && !hasTrue)
       
   240             {
       
   241                 // SYNC NOTE: cannot call public API method here in case
       
   242                 // it has been overridden by application subclass.
       
   243                 SetSelectedIndex(0,true);
       
   244                 return;
       
   245             }
       
   246             //
       
   247             for (int i=0; i<len; ++i)
       
   248             {
       
   249                 // SYNC NOTE: cannot call public API method here
       
   250                 SetSelectedIndex(i,aSelectedArray[i]);
       
   251                 if (aSelectedArray[i] && notMultiple)
       
   252                 {
       
   253                     for (int j=i+1; j<len; ++j) //set the rest to false
       
   254                     {
       
   255                         // SYNC NOTE: cannot call public API method here.
       
   256                         SetSelectedIndex(j, false);
       
   257                     }
       
   258                     return;
       
   259                 }
       
   260             }
       
   261         }
       
   262     }
       
   263 
       
   264     public void setFitPolicy(int aFitPolicy)
       
   265     {
       
   266         synchronized (iToolkit)
       
   267         {
       
   268             iChoiceComponent.SetFitPolicy(aFitPolicy); //will validate aFitPolicy
       
   269             NativeError.check(_setFitPolicy(getHandle(), iToolkit.getHandle(), aFitPolicy));
       
   270             RefreshForm();
       
   271         }
       
   272     }
       
   273 
       
   274     public int getFitPolicy()
       
   275     {
       
   276         return iChoiceComponent.getFitPolicy();
       
   277     }
       
   278 
       
   279     public void setFont(int aElementNum, Font aFont)
       
   280     {
       
   281         if (aFont == null || aFont.iIsFreeSizeFont == true)
       
   282         {
       
   283             aFont = Font.getDefaultFont();
       
   284         }
       
   285         synchronized (iToolkit)
       
   286         {
       
   287             iChoiceComponent.SetFont(aElementNum, aFont);
       
   288             NativeError.check(_setFont(getHandle(), iToolkit.getHandle(),
       
   289                                        aElementNum, aFont == null ? 0 : aFont.iHandle));
       
   290             RefreshForm();
       
   291         }
       
   292     }
       
   293 
       
   294     public Font getFont(int aElementNum)
       
   295     {
       
   296         return iChoiceComponent.GetFont(aElementNum);
       
   297     }
       
   298 
       
   299     private native int _create(int aToolkit, String aLabel, int aType);
       
   300     private native int _insert(int aHandle,int aToolkit,int aIndex,String aStringElement,int aImage);
       
   301     private native int _delete(int aHandle,int aToolkit,int aIndex);
       
   302     private native int _deleteAll(int aHandle,int aToolkit);
       
   303     private native int _set(int aHandle,int aToolkit,int aIndex,String aStringElement,int aImage);
       
   304     private native boolean _isSelected(int aHandle,int aToolkit,int aIndex);
       
   305     private native int _select(int aHandle,int aToolkit,int aIndex,boolean aSelected);
       
   306     private native int _setFont(int aHandle,int aToolkit,int aElementNum,int aFont);
       
   307     private native int _setFitPolicy(int aHandle,int aToolkit,int aFitPolicy);
       
   308 }