/*
 * 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.EditField;
import minime.ui.ImagePanel;
import minime.ui.Label;

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

/**
 * This view demostrate how to use EditField componet.
 * @author yishu
 * 
 */
public class EditFieldView extends BaseView
{

    // define array of the 
	private final int NAME = 0 ;
	private final int PASSWORD = 1 ;
	private final int CONFIRMED = 2 ;
	
    int[] messages = { TextRscId.TX_EF_EnterYourName, TextRscId.TX_EF_Password1, TextRscId.TX_EF_Password2};
    EditField[] editFields ;
    Label label ;
    ImagePanel arrow ;
    
    int selectedField = 0 ;
    int arrowLeftPos ;
    
    public EditFieldView()
    {
        super(null, true);
        // initialize the view
        setViewTitle(TextRscId.TX_EditFieldViewName) ;
        setBgColor(Portability.C_GREY) ;
        
        label = new Label(messages[0], Portability.RED) ;

        editFields = new EditField[3] ;
        editFields[NAME] = new EditField("",Portability.defaultMenuItemTextColor,10,20,EditField.LETTER,Portability.defaultMenuItemFrameThickness, Portability.defaultMenuSelectionFrameColor,Portability.C_WHITE) ;
        editFields[PASSWORD] = new EditField("",Portability.defaultMenuItemTextColor,8,8,EditField.LETTER,Portability.defaultMenuItemFrameThickness, Portability.defaultMenuSelectionFrameColor,Portability.C_WHITE) ;
        editFields[CONFIRMED] = new EditField("",Portability.defaultMenuItemTextColor,8,8,EditField.PASSWORD,Portability.defaultMenuItemFrameThickness, Portability.defaultMenuSelectionFrameColor,Portability.C_WHITE) ;
    
        arrow = new ImagePanel(ImageRscId.IM_ARROW) ;
        
        // create the softKeyBar
        setSoftbar(TextRscId.TX_BACK, TextRscId.TX_OK, -1);
    }

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

        addToView(label);
        addToView(editFields[NAME]) ;
        addToView(editFields[PASSWORD]);
        addToView(editFields[CONFIRMED]) ;
        addToView(arrow) ;
        editFields[NAME].setSelected(true) ;
        editFields[NAME].setFocus(true);
        
    }

    public void deactivate()
    {
        removeFromView(label);
        removeFromView(editFields[NAME]) ;
        removeFromView(editFields[PASSWORD]);
        removeFromView(editFields[CONFIRMED]) ;
        removeFromView(arrow) ;
        super.deactivate();
    }

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

        int x = da.x ;
        int y = da.y + 20 ;

        editFields[NAME].layout();
        x = (getWidth() - editFields[NAME].getWidth()) / 2 ;
        editFields[NAME].setPosition(x, y) ;
        y+=editFields[NAME].getHeight()+10 ;

        editFields[PASSWORD].layout();
        x = (getWidth() - editFields[PASSWORD].getWidth()) / 2 ;
        editFields[PASSWORD].setPosition(x, y) ;
        y+=editFields[PASSWORD].getHeight()+10 ;

        editFields[CONFIRMED].layout();
        x = (getWidth() - editFields[CONFIRMED].getWidth()) / 2 ;
        editFields[CONFIRMED].setPosition(x, y) ;
        y+=editFields[CONFIRMED].getHeight()+20 ;

        label.layout() ;
        x = (getWidth() - label.getWidth()) / 2 ;
        label.setPosition(x, y) ;

        // position arrow 
        arrowLeftPos=da.x+10 ;
        y = editFields[selectedField].getTop() - (arrow.getHeight() - editFields[selectedField].getHeight())/2 ;
        arrow.setPosition(arrowLeftPos, y) ;
        return da;
    }

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

        if (evt.type==Event.KEY_EVENT)
        {
        	if (evt.paramA==Portability.KEY_DOWN && selectedField<2)
        	{
        		editFields[selectedField].setSelected(false) ;
        		editFields[selectedField].setFocus(false) ;
        		selectedField ++ ;
            	handled=true ;
        	}
        	else if (evt.paramA==Portability.KEY_UP && selectedField>0)
        	{
        		editFields[selectedField].setSelected(false) ;
        		editFields[selectedField].setFocus(false) ;
        		selectedField -- ;
            	handled=true ;
        	} 
        	
        }
        else if (evt.type == Event.SB_EVENT)
        {
            if (evt.paramB == TextRscId.TX_BACK)
            {
                Runtime.getInstance().popView();
                handled = true;
            }
        }
        if (handled)
        {
        	// set the new edit field, new text and move the arrow
    		editFields[selectedField].setSelected(true) ;
    		editFields[selectedField].setFocus(true) ;
    		label.setString(messages[selectedField]) ;

            Runtime.getInstance().repaint();
            return true;
        }

        return super.onEvent(evt);
    }

}
