PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sandbox/cheney/Sketch/src/java/javax/microedition/lcdui/List.java

http://github.com/cderici/moby-scheme
Java | 134 lines | 101 code | 29 blank | 4 comment | 13 complexity | 293807a7668c1e3634d2bd5c02de34d8 MD5 | raw file
  1. package javax.microedition.lcdui;
  2. import j2ab.android.lcdui.Toolkit;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.microedition.midlet.MIDlet;
  7. import android.view.KeyEvent;
  8. import android.view.View;
  9. import android.view.View.OnKeyListener;
  10. import android.widget.ListAdapter;
  11. import android.widget.ListView;
  12. import android.widget.SimpleAdapter;
  13. public class List extends Screen implements OnKeyListener {
  14. public static final int EXCLUSIVE = 0;
  15. public static final int IMPLICIT = 1;
  16. public static final int MULTIPLE = 2;
  17. public static final int POPUP = 3;
  18. private static final String KEY_IMAGE = "image";
  19. private static final String KEY_TEXT = "text";
  20. private String title;
  21. private int listType;
  22. private int selectedIndex;
  23. private ListView view;
  24. private ArrayList<Map<String,?>> data;
  25. private MIDlet midlet;
  26. private Command selectCommand;
  27. public List( String title, int listType ) {
  28. this.title = title;
  29. this.listType = listType;
  30. this.data = new ArrayList<Map<String,?>>();
  31. }
  32. public void disposeDisplayable() {
  33. this.view = null;
  34. this.data = null;
  35. }
  36. public View getView() {
  37. return this.view;
  38. }
  39. public void initDisplayable(MIDlet midlet) {
  40. this.midlet = midlet;
  41. // TODO : use appearance mode to choose item
  42. Toolkit toolkit = midlet.getToolkit();
  43. this.view = (ListView)toolkit.inflate(
  44. toolkit.getResourceId( "layout.midplist" )
  45. );
  46. this.view.setOnKeyListener( this );
  47. // set up the model
  48. ListAdapter model = new SimpleAdapter(
  49. midlet.getActivity(),
  50. this.data,
  51. toolkit.getResourceId( "layout.midplistitem" ),
  52. new String[] { KEY_IMAGE, KEY_TEXT },
  53. new int[]{
  54. toolkit.getResourceId( "id.midplistitem_image" ),
  55. toolkit.getResourceId( "id.midplistitem_text" )
  56. }
  57. );
  58. this.view.setAdapter( model );
  59. this.view.setSelection( this.selectedIndex );
  60. }
  61. public int append( String stringPart, Image imagePart ) {
  62. HashMap<String, Object> row = new HashMap<String, Object>( 2 );
  63. // TODO : this isn't quite right, would be better to translate the Bitmap using
  64. // some kind of renderer
  65. if( imagePart != null ) {
  66. row.put( KEY_IMAGE, imagePart.getBitmap() );
  67. }
  68. row.put( KEY_TEXT, stringPart );
  69. this.data.add( row );
  70. return this.data.size();
  71. }
  72. public Command getSelectCommand() {
  73. return selectCommand;
  74. }
  75. public void setSelectCommand(Command selectCommand) {
  76. this.selectCommand = selectCommand;
  77. }
  78. public int getSelectedIndex() {
  79. if( this.view != null ) {
  80. return this.view.getSelectedItemPosition();
  81. } else {
  82. return this.selectedIndex;
  83. }
  84. }
  85. public String getString( int index ) {
  86. Map<String, ?> row = this.data.get( index );
  87. return (String)row.get( KEY_TEXT );
  88. }
  89. public void setSelectedIndex( int index, boolean value ) {
  90. this.selectedIndex = index;
  91. if( this.view != null ) {
  92. this.view.setItemChecked( index, value );
  93. }
  94. }
  95. public boolean onKey( View source, int key, KeyEvent event ) {
  96. boolean handled;
  97. if( event.getKeyCode() == KeyEvent.KEYCODE_ENTER && this.commandListener != null && this.selectCommand != null ) {
  98. handled = true;
  99. this.commandListener.commandAction( this.selectCommand, this );
  100. } else{
  101. handled = false;
  102. }
  103. return handled;
  104. }
  105. }