/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2012 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.widget.utility.gwt.client.action;

import org.geomajas.gwt.client.action.ConfigurableAction;
import org.geomajas.gwt.client.action.ToolbarAction;
import org.geomajas.gwt.client.action.ToolbarBaseAction;
import org.geomajas.gwt.client.action.event.ToolbarActionDisabledEvent;
import org.geomajas.gwt.client.action.event.ToolbarActionEnabledEvent;
import org.geomajas.gwt.client.action.event.ToolbarActionHandler;
import org.geomajas.widget.utility.common.client.action.ButtonAction;
import org.geomajas.widget.utility.common.client.event.DisabledEvent;
import org.geomajas.widget.utility.common.client.event.EnabledEvent;
import org.geomajas.widget.utility.common.client.event.EnabledHandler;
import org.geomajas.widget.utility.common.client.event.HasEnabledHandlers;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * Button action implementation that delegates to a {@link ToolbarBaseAction} instance.
 * 
 * @author Pieter De Graef
 * @author Emiel Ackermann
 */
public class ToolbarButtonAction implements ButtonAction, HasEnabledHandlers {

	protected ToolbarBaseAction toolbarAction;

	// ------------------------------------------------------------------------
	// Constructors:
	// ------------------------------------------------------------------------

	/**
	 * Initialize this action with a toolbar action.
	 * 
	 * @param toolbarAction
	 *            The toolbar action instance to delegate to. By default a {@link ToolbarAction} is expected.
	 */
	public ToolbarButtonAction(ToolbarBaseAction toolbarAction) {
		this.toolbarAction = toolbarAction;
		setTitle(toolbarAction.getTitle());
	}

	// ------------------------------------------------------------------------
	// ButtonAction implementation:
	// ------------------------------------------------------------------------

	/**
	 * If the given toolbar action during construction was of the type {@link ToolbarAction}, this method is passed to
	 * it. Note though that the event types are different, and thus <code>null</code> is passed.
	 */
	public void onClick(ClickEvent event) {
		if (toolbarAction instanceof ToolbarAction) {
			((ToolbarAction) toolbarAction).onClick(null);
		}
	}

	/** {@inheritDoc} */
	public void configure(String key, String value) {
		if ("icon".equalsIgnoreCase(key)) {
			setIcon(value);
		} else if ("title".equalsIgnoreCase(key)) {
			setTitle(value);
		} else if ("tooltip".equalsIgnoreCase(key)) {
			setTooltip(value);
		} else if ("description".equalsIgnoreCase(key)) {
			setTooltip(value);
		} else if ("buttonLayout".equalsIgnoreCase(key)) {
			setButtonLayout(value);
		} else if (toolbarAction instanceof ConfigurableAction) {
			ConfigurableAction ca = (ConfigurableAction) toolbarAction;
			ca.configure(key, value);
		}
	}

	/** {@inheritDoc} */
	public String getIcon() {
		return toolbarAction.getIcon();
	}

	/** {@inheritDoc} */
	public void setIcon(String icon) {
		toolbarAction.setIcon(icon);
	}

	/** {@inheritDoc} */
	public String getTitle() {
		return toolbarAction.getTitle();
	}

	/** {@inheritDoc} */
	public void setTitle(String title) {
		toolbarAction.setTitle(title);
	}

	/** {@inheritDoc} */
	public String getTooltip() {
		return toolbarAction.getTooltip();
	}

	/** {@inheritDoc} */
	public void setTooltip(String tooltip) {
		toolbarAction.setTooltip(tooltip);
	}

	public ToolbarBaseAction getToolbarAction() {
		return toolbarAction;
	}

	/** {@inheritDoc} */
	public String getButtonLayout() {
		return toolbarAction.getButtonLayout();
	}
	/** {@inheritDoc} */
	public void setButtonLayout(String buttonLayout) {
		toolbarAction.setButtonLayout(buttonLayout);
	}

	
	/** {@inheritDoc} */
	public boolean isEnabled() {
		return !toolbarAction.isDisabled();
	}

	/** {@inheritDoc} */
	public HandlerRegistration addEnabledHandler(EnabledHandler handler) {
		return toolbarAction.addToolbarActionHandler(new ToolbarActionForwarder(handler));
	}
	
	/**
	 * Forwards {@link ToolbarActionEnabledEvent} and {@link ToolbarActionDisabledEvent} to {@link EnabledHandler}s.
	 * 
	 * @author Jan De Moerloose
	 */
	class ToolbarActionForwarder implements ToolbarActionHandler {

		private EnabledHandler handler;

		ToolbarActionForwarder(EnabledHandler handler) {
			this.handler = handler;
		}

		/** {@inheritDoc} */
		public void onToolbarActionEnabled(ToolbarActionEnabledEvent event) {
			handler.onEnabled(new EnabledEvent(ToolbarButtonAction.this));
		}

		/** {@inheritDoc} */
		public void onToolbarActionDisabled(ToolbarActionDisabledEvent event) {
			handler.onDisabled(new DisabledEvent(ToolbarButtonAction.this));
		}

	}
}
@@ -908,32 +908,107 @@
 n.ButtonAction;%0A
+import org.geomajas.widget.utility.common.client.action.RibbonColumnAware;%0A
 import org.geoma
@@ -1053,32 +1053,32 @@
 .DisabledEvent;%0A
-
 import org.geoma
@@ -1275,16 +1275,86 @@
 andlers;
+%0Aimport org.geomajas.widget.utility.common.client.ribbon.RibbonColumn;
 %0A%0Aimport
@@ -1609,16 +1609,16 @@
 ann%0A */%0A
-
 public c
@@ -1685,16 +1685,35 @@
 Handlers
+, RibbonColumnAware
  %7B%0A%0A%09pro
@@ -5226,10 +5226,423 @@
 %0A%09%09%7D%0A%0A%09%7D
+%0A%0A%09/** %7B@inheritDoc%7D */%0A%09public void setRibbonColumn(RibbonColumn column) %7B%0A%09%09if (toolbarAction instanceof RibbonColumnAware) %7B%0A%09%09%09((RibbonColumnAware) toolbarAction).setRibbonColumn(column);%0A%09%09%7D%0A%09%7D%0A%0A%09/** %7B@inheritDoc%7D */%0A%09public RibbonColumn getRibbonColumn() %7B%0A%09%09if (toolbarAction instanceof RibbonColumnAware) %7B%0A%09%09%09return ((RibbonColumnAware) toolbarAction).getRibbonColumn();%0A%09%09%7D else %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D
 %0A%7D

implement RibbonColumnAware