/*
 * Created on 27 janv. 2005
 */
package com.ht.svg.animat;

import javafx.application.Platform;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import com.ht.data.RealTimeDataManager;
import com.ht.svg.util.DomUtil;

/**
 * the listener to the changes of data for the "text" animation node
 * 
 * @author ITRIS, Jordi SUC
 */
public class SimpleText extends AbstractAnimationDataListener {

	private static final Logger log = LoggerFactory.getLogger(SimpleText.class);

	/**
	 * the last text value
	 */
	private String lastTextValue = null;
	// private String id = null;
	private String tagVarName = "";

	/**
	 * the constructor of the class
	 * 
	 * @param animationHandler
	 * @param animationElement
	 *            an animation node
	 */
	public SimpleText(AnimatHandler animationHandler, Element animationElement) {

		super(animationHandler, animationElement);

		// id = ID_PREFIX + DomUtil.generateID();
		// parentElement.setAttribute("id", id);

		if (dataNames.size() <= 0) {
			log.error("未设置变量!!!!!!!");
		}
		tagVarName = dataNames.iterator().next();
	}

	/**
	 * the method called when the data to which the listener is registered, is
	 * modified
	 */
	@Override
	public void dataChanged(String ...varNames) {

		// getting the value of the tag
		final String newTextValue = (String) RealTimeDataManager.INSTANCE
				.getVarValue(tagVarName);

		if (newTextValue == null) {
			return;

		}

		if (newTextValue != null && !newTextValue.equals(lastTextValue)) {
			try {
				DomUtil.updateTextElement(parentElement, newTextValue);
				lastTextValue = newTextValue;
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
