package com.tivo.examples;

import com.tivo.hme.bananas.BList;
import com.tivo.hme.bananas.BText;
import com.tivo.hme.bananas.BView;

public class SelectableList extends BList {

        public SelectableList(BView parent, int x, int y, int width, int height, int rowHeight) {
            super(parent, x, y, width, height, rowHeight);
            setBarAndArrows(BAR_HANG, BAR_DEFAULT, null, null);
        }

        protected void createRow(BView parent, int index) {
            ListItem li = (ListItem)get(index);
            li.txt = new BText(parent, 7, 0, parent.getWidth(), parent.getHeight());
            li.txt.setFlags(RSRC_HALIGN_LEFT);
			li.icon = new BView(parent, 0, 5, 24, 24, false);
			li.icon.setResource("musicnote.png");
			li.icon.setVisible(false);

            updateListItem(li);
        }
        
        protected void updateListItem(ListItem li) {
			li.txt.setValue("   " + li.text);
			li.icon.setVisible(li.selected);
        }
        
        public boolean handleKeyPress(int code, long rawcode) {
            switch (code) {
                case KEY_SELECT:
					int currentIndex = getFocus();
                    ListItem li = (ListItem)get(currentIndex);
                    if (li != null) {
                        li.selected = !li.selected;
                        updateListItem(li);
						this.setFocus(currentIndex+1, false);
                    }
                    return true;
            }
            return super.handleKeyPress(code, rawcode);
        }
}

class ListItem {
    boolean selected;
    String text;
    BText txt = null;
	BView icon = null;
    
    public ListItem(boolean selected, String text) {
        this.selected = selected;
        this.text = text;
    }
}
 

