/**
 * This package include a number of views which present the UI to the users.
 */
/*
 * 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 javax.microedition.lcdui.Graphics;

import com.fmc.midlet.SimpleMidlet;

import minime.Drawable;
import minime.Image;
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 minime.ui.MenuItemComposite;

import minime.gen.ImageRscId;
import minime.gen.TextRscId;

/**
 * This view demostrate how to use CheckBox component.
 * @author yishu
 * 
 */
public class CheckBoxView extends BaseView
{
    private ChoiceGroup cg;
    private Label message ;
    
    // an strings ids array for choicegroup as it's item text
    int[] elements = {TextRscId.TX_CB_RemoveMessage, TextRscId.TX_CB_SetBackground, TextRscId.TX_CB_DrawAFrame, TextRscId.TX_CB_DisplayCircle, TextRscId.TX_CB_ChangeSelectionFrame };
    boolean drawFrame = false ;
    boolean drawCircle = false ;
    
    public CheckBoxView()
    {
        super(null, true);
        setViewTitle(TextRscId.TX_CheckBoxViewName) ;
        setSoftbar(TextRscId.TX_BACK, TextRscId.TX_MARK, -1);
        setBgColor(Portability.BLUE) ;

        message = new Label(TextRscId.TX_CheckBoxViewText, Portability.RED) ;
        message.setBgColor(Portability.VIOLET) ;

        // construct a choicegroup instance with image resouce ids, items text
        // resource ids, the choicegroup model is checkbox.
        cg = ChoiceGroup.createCheckBoxGroup(elements);
        cg.setSelectionFrame(3,Portability.BLUEVIOLET) ;
    }

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

        addToView(message) ;
        addToView(cg) ;
        // 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.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 ;
        
        // set the label position.
        cg.setPosition(x,y);
        return da;
    }

    public boolean onEvent(Event evt)
    {
        boolean handled = false;
        if (evt.type == Event.BOX_MARKED_EVENT)
        {
        	switch(evt.paramB)
        	{
	        	case TextRscId.TX_CB_SetBackground:
	        		setBgImage(ImageRscId.IM_BACKGROUND) ;
	            	handled = true ;
	        		break ;
	        	// draw a frame on the view, just set the flag, work will be done by renderImp
	        	case TextRscId.TX_CB_DrawAFrame:
	        		drawFrame = true ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_DisplayCircle:
	        		drawCircle = true ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_ChangeSelectionFrame:
	        		// access the selected item (paramA is the item index)
	        		MenuItemComposite mic = cg.getItem(evt.paramA) ;
	        		// then set a new color
	        		mic.setFrameThicknessAndColor(3, Portability.YELLOW) ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_RemoveMessage:
	        		removeFromView(message) ;
	            	handled = true ;
	            	break ;
        	}
        	// change the softkey button to Unmark 
        	if (handled==true)
                setSoftbar(TextRscId.TX_BACK, TextRscId.TX_UNMARK, -1);
        }
        else if (evt.type == Event.BOX_UNMARKED_EVENT)
        {
        	switch(evt.paramB)
        	{
	        	case TextRscId.TX_CB_SetBackground:
	        		// discard background image an restore background color
	        		setBgImage(-1) ;
	                setBgColor(Portability.NAVYBLUE) ;
	            	handled = true ;
	            	break ;
	        	// discard the frame
	        	case TextRscId.TX_CB_DrawAFrame:
	        		drawFrame = false ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_DisplayCircle:
	        		drawCircle = false ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_ChangeSelectionFrame:
	        		// restore previous color
	        		MenuItemComposite mic = cg.getItem(evt.paramA) ;
	        		mic.setFrameThicknessAndColor(3, Portability.BLUEVIOLET) ;
	            	handled = true ;
	            	break ;
	        	case TextRscId.TX_CB_RemoveMessage:
	        		addToView(message) ;
	            	handled = true ;
	            	break ;
        	}
        	
        	// change the softkey button to Mark 
        	if (handled==true)
                setSoftbar(TextRscId.TX_BACK, TextRscId.TX_MARK, -1);
        }
        // MENU_EVENT is fired when menu UP & menu Down
        else if (evt.type == Event.MENU_EVENT)
        {
        	// change the center key label to Mark or UnMark
            if (cg.isSelected(evt.paramA))
                setSoftbar(TextRscId.TX_BACK, TextRscId.TX_UNMARK, -1);
            else
                setSoftbar(TextRscId.TX_BACK, TextRscId.TX_MARK, -1);
        	handled = true ;
        }
        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);
    }

    // render method overwritten to draw rectangle and circle
    public void render(Graphics gc)
    {
    	// render the view
    	super.render(gc) ;
    	
    	// then draw a frame 
    	if (drawFrame==true)
    	{
    		// save previous color
    		int c = gc.getColor() ;
    		Rectangle da = getDisplayArea() ;
    		gc.setColor(Portability.RED) ;

    		// move the coordinate system to view's left & top
    		gc.translate(getLeft(), getTop()) ;
    		gc.drawRect(da.x, da.y, da.w-1, da.h-1) ;
    		gc.translate(-getLeft(), -getTop()) ;

    		// switch back to old color
    		gc.setColor(c) ;
    	}
    	
    	if (drawCircle==true)
    	{
    		int radius = 15 ;
    		int c = gc.getColor() ;
    		Rectangle da = getDisplayArea() ;
    		gc.setColor(Portability.YELLOW) ;
    		gc.fillArc(da.x + (da.w/2) - radius, da.h - radius*3 , radius*2, radius*2, 0, 360) ;
    		// switch back to old color
    		gc.setColor(c) ;
    	}
    		
    }
}
