/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package com.fmc.view;

import minime.Portability;
import minime.Rectangle;
import minime.core.Event;
import minime.core.EventDispatcher;
import minime.core.ResourceManager;
import minime.core.Runtime;
import minime.ui.BaseView;
import minime.ui.ChoiceGroup;
import minime.ui.Label;

import com.fmc.midlet.SimpleMidlet;
import minime.gen.ImageRscId;
import minime.gen.TextRscId;

/**
 * This view demostrate how to use RadioButton component.
 * @author yishu
 * 
 */
public class RadioButtonView extends BaseView
{
    private ChoiceGroup cg;
    private Label message ;

    // define array of the 
    int[] elements = { TextRscId.TX_RB_SetToBlue, TextRscId.TX_RB_SetToAqua, TextRscId.TX_RB_SetToAzure, 
    		TextRscId.TX_RB_SetToYellow, TextRscId.TX_RB_SetToLightYellow, TextRscId.TX_RB_SetToIvory};

    public RadioButtonView()
    {
        super(null, true);
        // initialize the view
        setViewTitle(TextRscId.TX_RadioButtonViewName) ;
        setBgColor(Portability.NAVYBLUE) ;
        
        message = new Label(TextRscId.TX_RadioButtonViewText, Portability.RED) ;
        message.setBgColor(Portability.VIOLET) ;
        
        // create the softKeyBar
        setSoftbar(TextRscId.TX_BACK, TextRscId.TX_SELECT, -1);
        // construct a choicegroup instance with image resouce ids, items text
        // resource ids, the choicegroup model is radio button.
        cg = ChoiceGroup.createRadioButtonGroup(elements) ;
        cg.setSelectionFrame(Portability.defaultMenuItemFrameThickness,Portability.BLUEVIOLET) ;
    }

    public void activate()
    {
        super.activate();

        addToView(cg);
        addToView(message) ;
        // set focus on ChoiceGroup so it'll receive event first
        cg.setFocus(true);
    }

    public void deactivate()
    {
        removeFromView(message) ;
        removeFromView(cg);
        super.deactivate();
    }

    public Rectangle layout()
    {
        Rectangle da = super.layout();

        // layout the message
        message.setWidth(da.w) ;
        message.layout() ;
        int x = da.x + (da.w - message.getWidth())/2 ;   // center the message horizontaly
        int y = da.y + (da.h - message.getHeight())/5 ;  // put the message in the upper quarter of the screen
        message.setPosition(x, y) ;
        
        // layout the menu before calculating its position
        cg.layout();
        x = da.x + (da.w - cg.getWidth())/2 ;
        y = da.y + (da.h - cg.getHeight())/2 ;
        cg.setPosition(x,y);

        return da;
    }

    public boolean onEvent(Event evt)
    {
        boolean handled = false;

        if (evt.type==Event.RADIO_SELECTED_EVENT)
        {
        	switch(evt.paramB)
        	{
	        	case TextRscId.TX_RB_SetToBlue:
	            	setBgColor(Portability.BLUE) ;
	            	handled=true ;
	        		break ;
	        	case TextRscId.TX_RB_SetToAqua:
	        		setBgColor(Portability.AQUA) ;
	            	handled=true ;
	        		break ;
	        	case TextRscId.TX_RB_SetToAzure:
	        		setBgColor(Portability.AZURE) ;
	            	handled=true ;
	        		break ;
	        	case TextRscId.TX_RB_SetToYellow: 
	        		setBgColor(Portability.YELLOW) ;
	            	handled=true ;
	        		break ;
	        	case TextRscId.TX_RB_SetToLightYellow:
	        		setBgColor(Portability.LIGHTYELLOW) ;
	            	handled=true ;
	        		break ;
	        	case TextRscId.TX_RB_SetToIvory:
	        		setBgColor(Portability.IVORY) ;
	            	handled=true ;
	        		break ;
        	}
        	// change the color of the view
        }
        else if (evt.type == Event.SB_EVENT)
        {
            if (evt.paramB == TextRscId.TX_BACK)
            {
                Runtime.getInstance().popView();
                handled = true;
            }
        }
        if (handled)
        {
            Runtime.getInstance().repaint();
            return true;
        }

        return super.onEvent(evt);
    }

}
