package com.ht.svg.action;

import java.util.HashSet;
import java.util.Set;

import javafx.application.Platform;

import org.w3c.dom.Element;
import org.w3c.dom.events.Event;

import com.ht.data.listener.IDataListener;
import com.ht.ui.util.UserSecurityUtil;

/**
 * the abstract class that each custom action should extend
 * 
 * @author ITRIS, Jordi SUC
 */
public abstract class AbstractActionListener implements IDataListener {

	/**
	 * the attribute name for the confirmation dialog
	 */
	protected static final String confirmationDialogAtt = "confirmationDialog";
	private ActionsHandler actionsHandler;
	
	protected Element parentElement;// 动作源
	/**
	 * the dom element defining the action
	 */
	protected Element actionElement;// 动作
	/**
	 * the set of the data names that are handled by this action
	 */
	protected Set<String> dataNames = new HashSet<String>();
	/**
	 * the tool tip text
	 */
	protected String toolTipText = "";
	private String eventType;
	protected String confirmationMessage;

	/**
	 * the constructor of the class
	 * 
	 * @param actionElement
	 *            the dom element defining the action
	 */
	public AbstractActionListener(Element actionElement) {

		this.actionElement = actionElement;
		eventType = actionElement.getAttribute("event");

		if (eventType != null && !eventType.isEmpty()
				&& actionElement.getParentNode() != null
				&& actionElement.getParentNode() instanceof Element) {

			parentElement = (Element) actionElement.getParentNode();

		}

		confirmationMessage = actionElement.getAttribute(confirmationDialogAtt);
	}

	public void setActionsHandler(ActionsHandler actionsHandler) {
		this.actionsHandler = actionsHandler;
	}

	public ActionsHandler getActionsHandler() {
		return actionsHandler;
	}

	public String getEventType() {
		return eventType;
	}

	/**
	 * 验证用户权限
	 * 
	 * @return whether the user has the rights to execute this action
	 */
	public void isEntitled(final Runnable run) {

		Platform.runLater(new Runnable() {

			@Override
			public void run() {

				UserSecurityUtil.verifyUserAuthority(run);
			}
		});
	}

	/**
	 * notifies that an action should be performed
	 * 
	 * @param event
	 *            the event for the action
	 */
	public abstract void performAction(Event event);

	/**
	 * sets the value of the given tag
	 * 
	 * @param tagName
	 *            the name of a tag
	 * @param tagValue
	 *            the value of a tag
	 */
//	public final void putTagValue(String tagName, Object tagValue) {
//
//		if (tagValue != null && tagValue instanceof String) {
//
//			tagValue = ((String) tagValue).toLowerCase();
//		}
//
//	}

	/**
	 * @see fr.itris.glips.rtda.animaction.ListenerAction#getTooltipText()
	 */
	public String getTooltipText() {

		return toolTipText;
	}

	/**
	 * @return the object to which the action applies, if it is not the
	 *         component
	 */
	public Element getParentElement() {

		return parentElement;
	}

	/**
	 * @return the dom element defining the action
	 */
	public Element getActionElement() {

		return actionElement;
	}

	/**
	 * @see fr.itris.glips.rtda.animaction.ListenerAction#getDataVarNames()
	 */
	public Set<String> getDataVarNames() {

		return dataNames;
	}
	
	@Override
	public void dataChanged(String ...varNames) {

	}

	/**
	 * disposes this action
	 */
	public void dispose() {
	}
}
