/*
 * 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.PopupComponent;
import minime.core.Runtime;
import minime.ui.BaseView;
import minime.ui.Label;
import minime.ui.Menu;
import minime.ui.MenuItem;
import minime.ui.PopupAlertComponent;

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

/**
 * this view demostrate how to use menu component.
 * @author David
 *
 */

public class SimpleMenuView extends BaseView
{
    // an TextID for menu item
    int[] elements = { TextRscId.TX_SM_RadioView, TextRscId.TX_SM_CheckBoxView, TextRscId.TX_SM_EditField, TextRscId.TX_SM_MenuItem4, TextRscId.TX_SM_MenuItem5 };
    Menu menu ;

    public SimpleMenuView()
    {
        super(null, true);
        // set the background color of the view 
        setBgColor(Portability.OLIVEDRAB) ;
        
        // set the view title
        Label t = new Label(TextRscId.TX_SimpleMenuViewName,Portability.PLUM) ;
        t.setWidth(getWidth()) ;
        t.setFrameThicknessAndColor(5, Portability.RED) ;
        setViewTitle(t) ;
        
        // set the softbar
        setSoftbar(TextRscId.TX_EXIT, TextRscId.TX_SELECT, -1);

        // create menu items 
        MenuItem[] items = MenuItem.createMenuItems(elements,Portability.defaultMenuItemTextColor) ;
        // create the menu and attach menu items
        menu = new Menu(items) ;
        // set the thickness and color of the menu selection frame
        menu.setSelectionFrame(Portability.defaultMenuItemFrameThickness,Portability.defaultMenuSelectionFrameColor) ;
    }

	
	public void activate()
    {
		// activate ancestor (Mandatory)
        super.activate();
        // attach the menu to the view to it can be repainted and can receive events 
        addToView(menu);
        setSoftbar(TextRscId.TX_EXIT, TextRscId.TX_SELECT, -1);
        // set focus on menu and so make it selectable
        menu.setFocus(true);
    }

    public void deactivate()
    {
    	// remove menu from view
    	removeFromView(menu);
    	// desactivate ancestors (Mamdatory)
        super.deactivate();
    }

    public Rectangle layout()
    {
    	// Layout ancestor first (Mandatory)
        Rectangle da = super.layout();

        // layout the menu before calculating its position
        menu.layout();
        int x = da.x + (da.w - menu.getWidth())/2 ;
        int y = da.y + (da.h - menu.getHeight())/2 ;
        
        // set the label position.
        menu.setPosition(x,y);

        // return the displayable area for potential child 
        return da;
    }

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

        // check event fired from menu
        if (evt.type == Event.MENU_SELECTION_EVENT)
        {
        	switch(evt.paramB)
        	{
        		// go to radio view
        		case TextRscId.TX_SM_RadioView:
                    Runtime.getInstance().pushView(new RadioButtonView());
                    handled = true;
                    break ;
                // go to check box view
        		case TextRscId.TX_SM_CheckBoxView:
                    Runtime.getInstance().pushView(new CheckBoxView());
                    handled = true;
                    break ; 
        		case TextRscId.TX_SM_EditField:
                    Runtime.getInstance().pushView(new EditFieldView());
                    handled = true;
                    break ; 
                default:
                	
                	// display an Alert
                	PopupComponent alertMsg = PopupAlertComponent.createAlertBox("Not Implemented yet", TextRscId.TX_OK) ;
                	alertMsg.show(this) ;
                    handled = true;
                	break ;
                	
                	//throw new IllegalStateException("Menu selection items not catched") ;
        	}
        }
        // check event from SoftKeyBar
        else if (evt.type == Event.SB_EVENT)
        {
        	// quit the midlet
        	if (evt.paramB == TextRscId.TX_EXIT)
        	{
                SimpleMidlet.inst.quitMidlet();
                return true ;
        	}
        }
        
        // if event was handled, we repaint and return
        if (handled)
        {
            Runtime.getInstance().repaint();
            return true;
        }

        // event was not handled here, so dispatch to ancestor
        return super.onEvent(evt);
    }


}
