Class GenericListCellRenderer<T>

java.lang.Object
com.codename1.ui.list.GenericListCellRenderer<T>
All Implemented Interfaces:
CellRenderer<T>, ListCellRenderer<T>

public class GenericListCellRenderer<T> extends Object implements ListCellRenderer<T>, CellRenderer<T>

The generic list cell renderer can display containers or arbitrary Codename One components as items in a list, notice that we strongly discourage usage of lists.. It relies on the source data being a Map object. It extracts values from the Map using the component name as an indication to the Map key lookup.

This renderer supports label tickering, check boxes/radio buttons etc. seamlessly.

Please notice that you must use at least two distinct instances of the component when passing them to the constructor, reusing the same instance WILL NOT WORK!

Furthermore, the renderer instance cannot be reused for multiple lists, each list will need a new instance of this renderer!

Sample usage for this renderer follows:

public void showForm() {
    com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
    list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
    Form hi = new Form("GenericListCellRenderer", new BorderLayout());
    hi.add(BorderLayout.CENTER, list);
    hi.show();
}

private Container createGenericRendererContainer() {
    Label name = new Label();
    name.setFocusable(true);
    name.setName("Name");
    Label surname = new Label();
    surname.setFocusable(true);
    surname.setName("Surname");
    CheckBox selected = new CheckBox();
    selected.setName("Selected");
    selected.setFocusable(true);
    Container c = BorderLayout.center(name).
            add(BorderLayout.SOUTH, surname).
            add(BorderLayout.WEST, selected);
    c.setUIID("ListRenderer");
    return c;
}

private Object[] createGenericListCellRendererModelData() {
    Map[] data = new HashMap[5];
    data[0] = new HashMap<>();
    data[0].put("Name", "Shai");
    data[0].put("Surname", "Almog");
    data[0].put("Selected", Boolean.TRUE);
    data[1] = new HashMap<>();
    data[1].put("Name", "Chen");
    data[1].put("Surname", "Fishbein");
    data[1].put("Selected", Boolean.TRUE);
    data[2] = new HashMap<>();
    data[2].put("Name", "Ofir");
    data[2].put("Surname", "Leitner");
    data[3] = new HashMap<>();
    data[3].put("Name", "Yaniv");
    data[3].put("Surname", "Vakarat");
    data[4] = new HashMap<>();
    data[4].put("Name", "Meirav");
    data[4].put("Surname", "Nachmanovitch");
    return data;
}
public void showForm() {
    com.codename1.ui.List list = new com.codename1.ui.List(createGenericListCellRendererModelData());
    list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
    Form hi = new Form("GenericListCellRenderer", new BorderLayout());
    hi.add(BorderLayout.CENTER, list);
    hi.show();
}

private Container createGenericRendererContainer() {
    Label name = new Label();
    name.setFocusable(true);
    name.setName("Name");
    Label surname = new Label();
    surname.setFocusable(true);
    surname.setName("Surname");
    CheckBox selected = new CheckBox();
    selected.setName("Selected");
    selected.setFocusable(true);
    Container c = BorderLayout.center(name).
            add(BorderLayout.SOUTH, surname).
            add(BorderLayout.WEST, selected);
    c.setUIID("ListRenderer");
    return c;
}

private Object[] createGenericListCellRendererModelData() {
    Map[] data = new HashMap[5];
    data[0] = new HashMap<>();
    data[0].put("Name", "Shai");
    data[0].put("Surname", "Almog");
    data[0].put("Selected", Boolean.TRUE);
    data[1] = new HashMap<>();
    data[1].put("Name", "Chen");
    data[1].put("Surname", "Fishbein");
    data[1].put("Selected", Boolean.TRUE);
    data[2] = new HashMap<>();
    data[2].put("Name", "Ofir");
    data[2].put("Surname", "Leitner");
    data[3] = new HashMap<>();
    data[3].put("Name", "Yaniv");
    data[3].put("Surname", "Vakarat");
    data[4] = new HashMap<>();
    data[4].put("Name", "Meirav");
    data[4].put("Surname", "Nachmanovitch");
    return data;
}
  • Field Details

    • ENABLED

      public static final String ENABLED
      If this flag exists in a Map of data the renderer will enable/disable the entries, the flag assumes either Boolean.TRUE or Boolean.FALSE. Notice that just setting it to false when necessary will not work, when its used it must be applied to all entries otherwise the reuse of the renderer component will break this feature.
      See Also:
    • SELECT_ALL_FLAG

      public static final String SELECT_ALL_FLAG
      Put this flag as a Map key to indicate that a checkbox entry rendered by this renderer should act as a "select all" entry and toggle all other entries. The value for this entry is ignored
      See Also:
  • Constructor Details

    • GenericListCellRenderer

      public GenericListCellRenderer(Component selected, Component unselected)

      Constructs a generic renderer with the given selected/unselected components

      Parameters
      • selected: indicates the selected value for the renderer

      • unselected: indicates the unselected value for the renderer

    • GenericListCellRenderer

      public GenericListCellRenderer(Component odd, Component oddUnselected, Component even, Component evenUnselected)

      Constructs a generic renderer with the given selected/unselected components for odd/even values allowing a "pinstripe" effect

      Parameters
      • odd: indicates the selected value for the renderer

      • oddUnselected: indicates the unselected value for the renderer

      • even: indicates the selected value for the renderer

      • evenUnselected: indicates the unselected value for the renderer

  • Method Details

    • getDefaultAdapter

      public static URLImage.ImageAdapter getDefaultAdapter()

      The default adapter to use for image URLs

      Returns

      the defaultAdapter

    • setDefaultAdapter

      public static void setDefaultAdapter(URLImage.ImageAdapter aDefaultAdapter)

      The default adapter to use for image URLs

      Parameters
      • aDefaultAdapter: the defaultAdapter to set
    • updateIconPlaceholders

      public void updateIconPlaceholders()
      Updates the placeholder instances, this is useful for changing the URLImage placeholder in runtime as might happen in the designer
    • extractLastClickedComponent

      public Button extractLastClickedComponent()

      Allows partitioning the renderer into "areas" that can be clicked. When receiving an action event in the list this method allows a developer to query the renderer to "see" whether a button within the component was "touched" by the user on a touch screen device. This method will reset the value to null after returning a none-null value!

      Returns

      a button or null

    • getCellRendererComponent

      public Component getCellRendererComponent(Component list, Object model, T value, int index, boolean isSelected)

      Returns a component instance that is already set to render "value". While it is not a requirement many renderes often derive from a component (such as a label) and return "this". Notice that a null value for the value argument might be sent when refreshing the theme of the list.

      Parameters
      • list: the list component

      • model: the model behind the render

      • value: the value to render

      • index: the index in the list

      • isSelected: whether the entry is selected

      Returns

      a component to paint within the list

      Specified by:
      getCellRendererComponent in interface CellRenderer<T>
    • getListCellRendererComponent

      public Component getListCellRendererComponent(List list, T value, int index, boolean isSelected)

      Returns a component instance that is already set to render "value". While it is not a requirement many renderes often derive from a component (such as a label) and return "this". Notice that a null value for the value argument might be sent when refreshing the theme of the list.

      Parameters
      • list: the list component

      • value: the value to render

      • index: the index in the list

      • isSelected: whether the entry is selected

      Returns

      a component to paint within the list

      Specified by:
      getListCellRendererComponent in interface ListCellRenderer<T>
    • getListFocusComponent

      public Component getListFocusComponent(List list)

      Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling. When the selection moves, this component is drawn above/below the list items - it is recommended to give this component some level of transparency (see above code example). This method is optional, an implementation can choose to return null.

      Parameters
      • list: the parent list
      Returns

      a component to use as focus

      See also
      • List#setSmoothScrolling
      Specified by:
      getListFocusComponent in interface ListCellRenderer<T>
    • getFocusComponent

      public Component getFocusComponent(Component list)

      Returns a component instance that is painted under the currently focused renderer and is animated to provide smooth scrolling. When the selection moves, this component is drawn above/below the list items - it is recommended to give this component some level of transparency (see above code example). This method is optional an implementation can choose to return null.

      Parameters
      • list: the parent list
      Returns

      a component to use as focus

      See also
      • List#setSmoothScrolling
      Specified by:
      getFocusComponent in interface CellRenderer<T>
    • isSelectionListener

      public boolean isSelectionListener()
      Returns

      the selectionListener

    • setSelectionListener

      public void setSelectionListener(boolean selectionListener)
      Parameters
      • selectionListener: the selectionListener to set
    • getSelected

      public Component getSelected()
      Returns

      the selected

    • getUnselected

      public Component getUnselected()
      Returns

      the unselected

    • getSelectedEven

      public Component getSelectedEven()
      Returns

      the selectedEven

    • getUnselectedEven

      public Component getUnselectedEven()
      Returns

      the unselectedEven

    • isFisheye

      public boolean isFisheye()

      In fisheye rendering mode the renderer maintains selected component drawing

      Returns

      the fisheye

    • setFisheye

      public void setFisheye(boolean fisheye)

      In fisheye rendering mode the renderer maintains selected component drawing

      Parameters
      • fisheye: the fisheye to set
    • getAdapter

      public URLImage.ImageAdapter getAdapter()

      The adapter used when dealing with image URL's

      Returns

      the adapter

    • setAdapter

      public void setAdapter(URLImage.ImageAdapter adapter)

      The adapter used when dealing with image URL's

      Parameters
      • adapter: the adapter to set