javauis/lcdui_akn/javalcdui/javasrc/javax/microedition/lcdui/List.java
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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 List extends Screen implements Choice
       
    24 {
       
    25     //
       
    26     // This is the default select command sent to the command listener
       
    27     // when an choice is selected on an implicit list.
       
    28     //
       
    29     public static final Command SELECT_COMMAND = new Command(Command.SCREEN);
       
    30 
       
    31     // the real select command
       
    32     private Command iSelectCommand; //pointer to the current select command
       
    33     private int iListType;
       
    34     private final ChoiceComponent iChoiceComponent;
       
    35 
       
    36     public List(String aTitle,int aListType)
       
    37     {
       
    38         this(aTitle,aListType,new String[] {},null);
       
    39     }
       
    40 
       
    41     public List(String aTitle,int aListType,String[] aTextArray,Image[] aIconArray)
       
    42     {
       
    43         super();
       
    44         if (aListType < Choice.EXCLUSIVE || aListType > Choice.IMPLICIT)
       
    45         {
       
    46             throw new IllegalArgumentException();
       
    47         }
       
    48         aIconArray = ChoiceComponent.validateElements(aTextArray, aIconArray);
       
    49         synchronized (iToolkit)
       
    50         {
       
    51             iListType = aListType;
       
    52             iSelectCommand = SELECT_COMMAND;
       
    53             iChoiceComponent = new ChoiceComponent();
       
    54 
       
    55             final int[] createListReturn = new int[3];
       
    56             NativeError.check(_create(iToolkit.getHandle(), getContainerHandle(), iListType, createListReturn));
       
    57             setContent(createListReturn[0], createListReturn[1], createListReturn[2]);
       
    58 
       
    59             final int count = aTextArray.length;
       
    60             for (int i=0; i<count; i++)
       
    61             {
       
    62                 Insert(i, aTextArray[i], aIconArray[i]);
       
    63             }
       
    64         }
       
    65         setTitle(aTitle);
       
    66     }
       
    67 
       
    68     public int size()
       
    69     {
       
    70         return iChoiceComponent.size();
       
    71     }
       
    72 
       
    73     public String getString(int aIndex)
       
    74     {
       
    75         synchronized (iToolkit)
       
    76         {
       
    77             return iChoiceComponent.GetString(aIndex);
       
    78         }
       
    79     }
       
    80 
       
    81     public Image getImage(int aIndex)
       
    82     {
       
    83         synchronized (iToolkit)
       
    84         {
       
    85             return iChoiceComponent.GetImage(aIndex);
       
    86         }
       
    87     }
       
    88 
       
    89     public int append(String aText, Image aIcon)
       
    90     {
       
    91         int pos;
       
    92         synchronized (iToolkit)
       
    93         {
       
    94             pos = size();
       
    95             Insert(pos, aText, aIcon);
       
    96         }
       
    97         return pos;
       
    98     }
       
    99 
       
   100     public void insert(int aIndex, String aText, Image aIcon)
       
   101     {
       
   102         synchronized (iToolkit)
       
   103         {
       
   104             Insert(aIndex, aText, aIcon);
       
   105         }
       
   106     }
       
   107 
       
   108     private void Insert(int aIndex, String aText, Image aIcon)
       
   109     {
       
   110         iChoiceComponent.Insert(aIndex, aText, aIcon);
       
   111         final int image = aIcon==null?0:aIcon.getHandle(true);
       
   112         NativeError.check(_insert(getContentHandle(), iToolkit.getHandle(), aIndex, aText, image));
       
   113     }
       
   114 
       
   115     public void delete(int aIndex)
       
   116     {
       
   117         synchronized (iToolkit)
       
   118         {
       
   119             iChoiceComponent.Delete(aIndex);
       
   120             NativeError.check(_delete(getContentHandle(),iToolkit.getHandle(),aIndex));
       
   121         }
       
   122     }
       
   123 
       
   124     public void deleteAll()
       
   125     {
       
   126         synchronized (iToolkit)
       
   127         {
       
   128             iChoiceComponent.DeleteAll();
       
   129             NativeError.check(_deleteAll(getContentHandle(),iToolkit.getHandle()));
       
   130         }
       
   131     }
       
   132 
       
   133     public void set(int aIndex,String aText,Image aIcon)
       
   134     {
       
   135         synchronized (iToolkit)
       
   136         {
       
   137             iChoiceComponent.Set(aIndex,aText,aIcon);
       
   138             final int image = aIcon==null?0:aIcon.getHandle(true);
       
   139             NativeError.check(_set(getContentHandle(),iToolkit.getHandle(),aIndex,aText,image));
       
   140         }
       
   141     }
       
   142 
       
   143     public boolean isSelected(int aIndex)
       
   144     {
       
   145         synchronized (iToolkit)
       
   146         {
       
   147             return IsSelected(aIndex);
       
   148         }
       
   149     }
       
   150 
       
   151     /**
       
   152      Internal implementation of isSelected
       
   153      SYNC NOTE: requires LCDUI lock to be held
       
   154      */
       
   155     boolean IsSelected(int aIndex)
       
   156     {
       
   157         iChoiceComponent.checkIndexInBounds(aIndex,iChoiceComponent.size());
       
   158         return _isSelected(getContentHandle(),iToolkit.getHandle(),aIndex);
       
   159     }
       
   160 
       
   161     public int getSelectedIndex()
       
   162     {
       
   163         synchronized (iToolkit)
       
   164         {
       
   165             if (iListType != Choice.MULTIPLE)
       
   166             {
       
   167                 final int len = iChoiceComponent.size();
       
   168                 for (int i=0; i<len; i++)
       
   169                 {
       
   170                     // SYNC NOTE: cannot call public API method here in case
       
   171                     // it has been overridden by application subclass.
       
   172                     if (IsSelected(i))
       
   173                         return i;
       
   174                 }
       
   175             }
       
   176             return -1;
       
   177         }
       
   178     }
       
   179 
       
   180     public int getSelectedFlags(boolean[] aSelectedArrayReturn)
       
   181     {
       
   182         synchronized (iToolkit)
       
   183         {
       
   184             final int len = aSelectedArrayReturn.length;
       
   185             final int choiceSize = iChoiceComponent.size();
       
   186             if (len < choiceSize)
       
   187                 throw new IllegalArgumentException();
       
   188             int nsel=0;
       
   189             for (int i=0; i<len; i++)
       
   190             {
       
   191                 // SYNC NOTE: cannot call public API method here in case
       
   192                 // it has been overridden by application subclass.
       
   193                 if (i<choiceSize && IsSelected(i))
       
   194                 {
       
   195                     nsel++;
       
   196                     aSelectedArrayReturn[i] = true;
       
   197                 }
       
   198                 else
       
   199                     aSelectedArrayReturn[i] = false;
       
   200             }
       
   201             return nsel;
       
   202         }
       
   203     }
       
   204 
       
   205 
       
   206     public void setSelectedIndex(int aIndex,boolean aSelected)
       
   207     {
       
   208         synchronized (iToolkit)
       
   209         {
       
   210             SetSelectedIndex(aIndex,aSelected);
       
   211         }
       
   212     }
       
   213 
       
   214     /**
       
   215      Internal implementation of setSelectedIndex
       
   216      SYNC NOTE: requires LCDUI lock to be held
       
   217      */
       
   218     void SetSelectedIndex(int aIndex,boolean aSelected)
       
   219     {
       
   220         iChoiceComponent.checkIndexInBounds(aIndex,iChoiceComponent.size());
       
   221         NativeError.check(_select(getContentHandle(),iToolkit.getHandle(),aIndex,aSelected));
       
   222     }
       
   223 
       
   224     public void setSelectedFlags(boolean[] aSelectedArray)
       
   225     {
       
   226         if (aSelectedArray==null)
       
   227             throw new NullPointerException();
       
   228         synchronized (iToolkit)
       
   229         {
       
   230             final int len = iChoiceComponent.size();
       
   231             if (len==0)
       
   232                 return;
       
   233             if (aSelectedArray.length < len)
       
   234                 throw new IllegalArgumentException();
       
   235             //
       
   236             boolean hasTrue=false;
       
   237             for (int i=0; i<len; ++i)
       
   238                 hasTrue = hasTrue || aSelectedArray[i];
       
   239             //
       
   240             final boolean notMultiple = iListType != Choice.MULTIPLE;
       
   241             if (notMultiple && !hasTrue)
       
   242             {
       
   243                 // SYNC NOTE: cannot call public API method here in case
       
   244                 // it has been overridden by application subclass.
       
   245                 SetSelectedIndex(0,true);
       
   246                 return;
       
   247             }
       
   248             //
       
   249             for (int i=0; i<len; ++i)
       
   250             {
       
   251                 // SYNC NOTE: cannot call public API method here
       
   252                 SetSelectedIndex(i,aSelectedArray[i]);
       
   253                 if (aSelectedArray[i] && notMultiple)
       
   254                 {
       
   255                     for (int j=i+1; j<len; ++j) //set the rest to false
       
   256                     {
       
   257                         // SYNC NOTE: cannot call public API method here.
       
   258                         SetSelectedIndex(j, false);
       
   259                     }
       
   260                     return;
       
   261                 }
       
   262             }
       
   263         }
       
   264     }
       
   265 
       
   266     public void removeCommand(Command aCommand)
       
   267     {
       
   268         synchronized (iToolkit)
       
   269         {
       
   270             if (aCommand == iSelectCommand)
       
   271             {
       
   272                 iSelectCommand = null;
       
   273                 if (iListType == Choice.IMPLICIT)
       
   274                 {
       
   275                     _setSelectCommand(getContentHandle(), iToolkit.getHandle(), 0);
       
   276                 }
       
   277             }
       
   278             DoRemoveCommand(aCommand);
       
   279         }
       
   280     }
       
   281 
       
   282     public void setSelectCommand(Command aCommand)
       
   283     {
       
   284         synchronized (iToolkit)
       
   285         {
       
   286             if (iListType != Choice.IMPLICIT)
       
   287             {
       
   288                 return;
       
   289             }
       
   290 
       
   291             int commandID = 0;
       
   292 
       
   293             if (aCommand != null)
       
   294             {
       
   295                 if (aCommand != SELECT_COMMAND)
       
   296                 {
       
   297                     DoAddCommand(aCommand);
       
   298                 }
       
   299                 CommandPeer peer = iCommands.getPeer(aCommand);
       
   300                 if (null != peer)
       
   301                 {
       
   302                     commandID = peer.getId();
       
   303                 }
       
   304                 else
       
   305                 {
       
   306                     commandID = CommandPeer.SELECT_CMD_ID;
       
   307                 }
       
   308             }
       
   309 
       
   310             _setSelectCommand(getContentHandle(), iToolkit.getHandle(), commandID);
       
   311             iSelectCommand = aCommand;
       
   312         }
       
   313     }
       
   314 
       
   315     public void setFitPolicy(int aFitPolicy)
       
   316     {
       
   317         synchronized (iToolkit)
       
   318         {
       
   319             iChoiceComponent.SetFitPolicy(aFitPolicy); //will validate aFitPolicy
       
   320             NativeError.check(_setFitPolicy(getContentHandle(), iToolkit.getHandle(), aFitPolicy));
       
   321         }
       
   322     }
       
   323 
       
   324     public int getFitPolicy()
       
   325     {
       
   326         return iChoiceComponent.getFitPolicy();
       
   327     }
       
   328 
       
   329     public void setFont(int aElementNum, Font aFont)
       
   330     {
       
   331         if (aFont == null || aFont.iIsFreeSizeFont == true)
       
   332         {
       
   333             aFont = Font.getDefaultFont();
       
   334         }
       
   335         synchronized (iToolkit)
       
   336         {
       
   337             iChoiceComponent.SetFont(aElementNum, aFont);
       
   338             NativeError.check(_setFont(getContentHandle(), iToolkit.getHandle(),
       
   339                                        aElementNum, aFont == null ? 0 : aFont.iHandle));
       
   340         }
       
   341     }
       
   342 
       
   343     public Font getFont(int aElementNum)
       
   344     {
       
   345         synchronized (iToolkit)
       
   346         {
       
   347             return iChoiceComponent.GetFont(aElementNum);
       
   348         }
       
   349     }
       
   350 
       
   351     final void handleEvent(int aEvent,int aData0, int aData1)
       
   352     {
       
   353         if (aEvent == Toolkit.EVENT_SELECT)
       
   354         {
       
   355             final Command command;
       
   356             final int count;
       
   357             synchronized (iToolkit)
       
   358             {
       
   359                 command = iSelectCommand;
       
   360                 count   = iChoiceComponent.size();
       
   361             }
       
   362             if (count > 0)
       
   363             {
       
   364                 callCommandAction(command);
       
   365             }
       
   366         }
       
   367         else
       
   368         {
       
   369             super.handleEvent(aEvent, aData0, aData1);
       
   370         }
       
   371     }
       
   372 
       
   373     private native int _create(int aToolkit, int aDisplayable, int aType, int[] aCreateListReturn);
       
   374     private native int _insert(int aHandle,int aToolkit,int aIndex,String aStringElement,int aImage);
       
   375     private native int _delete(int aHandle,int aToolkit,int aIndex);
       
   376     private native int _deleteAll(int aHandle,int aToolkit);
       
   377     private native int _set(int aHandle,int aToolkit,int aIndex,String aStringElement,int aImage);
       
   378     private native boolean _isSelected(int aHandle,int aToolkit,int aIndex);
       
   379     private native int _select(int aHandle,int aToolkit,int aIndex,boolean aSelected);
       
   380     private native int _setFont(int aHandle,int aToolkit,int aElementNum,int aFont);
       
   381     private native int _setFitPolicy(int aHandle,int aToolkit,int aFitPolicy);
       
   382     private native void _setSelectCommand(int aHandle,int aToolkit,int aCommandId);
       
   383 }