package echopointng;

/* 
 * This file is part of the Echo Point Project.  This project is a collection
 * of Components that have extended the Echo Web Application Framework.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Style;
import echopointng.able.AccessKeyable;
import echopointng.able.Attributeable;
import echopointng.able.Borderable;
import echopointng.able.Insetable;
import echopointng.able.MouseCursorable;
import echopointng.able.Sizeable;
import echopointng.able.ToolTipable;
import echopointng.util.ColorKit;

/**
 * 
 * The <code>ButtonEx</code> class provides extra EchoPoint features beyond the
 * <code>nextapp.echo2.app.ButtonEx</code>
 * <p>
 * Its rollover functions are enabled by default and its supports the extra
 * EchoPoint interfaces such as Borderable, MouseCursorable, AccessKeyable.
 * 
 * @author Brad Baker
 */
public class ButtonEx extends Button implements Borderable, MouseCursorable, AccessKeyable, Insetable, Sizeable, ToolTipable, Attributeable  {
	
	/** Default serial Id */
	private static final long serialVersionUID = 1L;
	
	
	
	public static final Style DEFAULT_STYLE;
	static {
		MutableStyleEx style = new MutableStyleEx();
		
		style.setProperty(PROPERTY_LINE_WRAP, false);
		
		style.setProperty(PROPERTY_BACKGROUND,ColorKit.makeColor("#F2F2ED"));
		style.setProperty(Borderable.PROPERTY_BORDER,new Border(1, ColorKit.makeColor("#D6D3CE"), Border.STYLE_SOLID));
		
		style.setProperty(PROPERTY_ROLLOVER_ENABLED,true);
		style.setProperty(PROPERTY_ROLLOVER_BACKGROUND,ColorKit.makeColor("#DEF3FF"));
		style.setProperty(PROPERTY_ROLLOVER_BORDER,new Border(1, ColorKit.makeColor("#3169C6"), Border.STYLE_SOLID));

		style.setProperty(Insetable.PROPERTY_INSETS,new Insets(2));
		style.setProperty(Insetable.PROPERTY_OUTSETS,new Insets(1));
		
		style.setProperty(PROPERTY_TEXT_ALIGNMENT, new Alignment(Alignment.TRAILING, Alignment.DEFAULT));
		style.setProperty(PROPERTY_ICON_TEXT_MARGIN, new Extent(3));
		style.setProperty(PROPERTY_MOUSE_CURSOR, CURSOR_POINTER);
		
		DEFAULT_STYLE = style;
	}
	
	private Map attributeMap;

	/**
	 * Creates a button with no text or icon.
	 */
	public ButtonEx() {
		this(null, null);
	}

	/**
	 * Creates a button with text.
	 * 
	 * @param text
	 *            A text label to display in the button.
	 */
	public ButtonEx(String text) {
		this(text, null);
	}

	/**
	 * Creates a button with an icon.
	 * 
	 * @param icon
	 *            An icon to display in the button.
	 */
	public ButtonEx(ImageReference icon) {
		this(null, icon);
	}

	/**
	 * Creates a button with text and an icon.
	 * 
	 * @param text
	 *            A text label to display in the button.
	 * @param icon
	 *            An icon to display in the button.
	 */
	public ButtonEx(String text, ImageReference icon) {
		super();

		setIcon(icon);
		setText(text);
	}

	public String toString() {
		return "echopointng.ButtonEx : " + getText();
	}


	/**
	 * @see echopointng.able.AccessKeyable#getAccessKey()
	 */
	public String getAccessKey() {
		return (String) getProperty(PROPERTY_ACCESS_KEY);
	}

	/**
	 * @see echopointng.able.Insetable#getInsets()
	 */
	public Insets getInsets() {
		return (Insets) getProperty(Insetable.PROPERTY_INSETS);
	}

	/**
	 * @see echopointng.able.MouseCursorable#getMouseCursor()
	 */
	public int getMouseCursor() {
	    return ComponentEx.getProperty(this,MouseCursorable.PROPERTY_MOUSE_CURSOR,MouseCursorable.CURSOR_AUTO);
	}

	/**
	 * @see echopointng.able.MouseCursorable#getMouseCursorURI()
	 */
	public String getMouseCursorURI() {
	    return (String) getProperty(PROPERTY_MOUSE_CURSOR_URI);
	}

	/**
	 * @see echopointng.able.Insetable#getOutsets()
	 */
	public Insets getOutsets() {
		return (Insets) getProperty(PROPERTY_OUTSETS);
	}

	/**
	 * @see echopointng.able.AccessKeyable#setAccessKey(java.lang.String)
	 */
	public void setAccessKey(String newValue) {
		setProperty(PROPERTY_ACCESS_KEY,newValue);
	}

	/**
	 * @see echopointng.able.Insetable#setInsets(nextapp.echo2.app.Insets)
	 */
	public void setInsets(Insets newValue) {
		setProperty(Insetable.PROPERTY_INSETS,newValue);
	}

	/**
	 * @see echopointng.able.MouseCursorable#setMouseCursor(int)
	 */
	public void setMouseCursor(int mouseCursor) {
	    ComponentEx.setProperty(this,MouseCursorable.PROPERTY_MOUSE_CURSOR,mouseCursor);
	}

	/**
	 * @see echopointng.able.MouseCursorable#setMouseCursorURI(java.lang.String)
	 */
	public void setMouseCursorURI(String mouseCursorURI) {
	    setProperty(PROPERTY_MOUSE_CURSOR_URI,mouseCursorURI);
	}

	/**
	 *  @see echopointng.able.Insetable#setOutsets(nextapp.echo2.app.Insets)
	 */
	public void setOutsets(Insets newValue) {
		setProperty(PROPERTY_OUTSETS,newValue);
	}

	/**
	 * @see echopointng.able.Attributeable#getAttribute(java.lang.String)
	 */
	public Object getAttribute(String attributeName) {
		if (attributeMap != null) {
			return attributeMap.get(attributeName);
		}
		return null;
	}

	/**
	 * @see echopointng.able.Attributeable#setAttribute(java.lang.String,
	 *      java.lang.Object)
	 */
	public void setAttribute(String attributeName, Object attributeValue) {
		if (attributeMap == null) {
			attributeMap = new HashMap();
		}
		attributeMap.put(attributeName, attributeValue);
	}

	/**
	 * @see echopointng.able.Attributeable#getAttributeNames()
	 */
	public String[] getAttributeNames() {
		if (attributeMap == null) {
			return new String[0];
		}
		int count = 0;
		String[] attributeNames = new String[attributeMap.keySet().size()];
		for (Iterator iter = attributeMap.keySet().iterator(); iter.hasNext();) {
			attributeNames[count++] = (String) iter.next();
		}
		return attributeNames;
	}
}
