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

import minime.Drawable;
import minime.Font;

/**
 * An drawable object that has focus and a event listener associated with it.
 * The contents displayed and their interaction with the user are defined by
 * subclasses.
 * 
 * @author yishu
 * 
 */
public abstract class Component extends Drawable implements EventListener
{
	private boolean focused ;
	
    public Component()
    {
    	focused = false ;
    }

    // focused use/override selected attribute
	public boolean isFocused() {
		return focused ;
	}

	public void setFocused(boolean focused) {
		this.focused = focused ;
	}

    
    /**
     * Sets the component object focus to true or false based on boolean
     * parameter.

     * @param focus, boolean true to set the focus, false to remove the focus
     * 
     */
    public void setFocus(boolean focus)
    {
    	if (isFocused())
    	{
    		// if object already have the focus, return
    		if (focus)
    			return ;
    		
    		FocusManager.getInstance().removeFocus(this) ;
    		return ;
    	}
    	
        // set the focus on the component
    	if (focus)
    		FocusManager.getInstance().setFocus(this) ;
    }

    public int getPriority()
    {
        return MEDIUM_PRIORITY; // average priority
    }

    /*
     * Activate the Component, add it to the event loop
     * @see minime.Drawable#activate()
     */
	public void activate()
	{
		EventDispatcher.getInstance().attach(this) ;		
	}

	/*
     * De-activate the component
     * 
     * When the component is de-activated, it is removed from the focused list,
     * and from the event list
     * 
     * @see minime.Drawable#deactivate()
     */
	public void deactivate()
	{
		// ... don't test if hasFocus because component could be hidden by a popupComponent
		FocusManager.getInstance().removeFocus(this) ;
		EventDispatcher.getInstance().detach(this) ;
	}
	


}
