/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ht.svg.util;

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

import javafx.application.Platform;
import netscape.javascript.JSException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import com.ht.ui.MainApplication;

/**
 * 
 * @author Administrator
 */
public class DomUtil {
	public static final String svgNS = "http://www.w3.org/2000/svg";
	public static final String xlinkNS = "http://www.w3.org/1999/xlink";
	public static int curID = 0;

	public static synchronized String generateID() {
		curID++;
		return Integer.toHexString(curID);
	}

	/**
	 * 启动一个图元动画，图元动画为非标准动画实现
	 * 
	 * @param element
	 * @param newLink
	 */
	public static void activeSymbolAnimat(final Element element,
			final String newLink) {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				element.setAttributeNS(xlinkNS, "xlink:href", newLink);
			}
		});
	}

	public static void updateTextElement(final Element element,
			final String textContent) {

		Platform.runLater(new Runnable() {

			@Override
			public void run() {

				// 移除文本元素
				Node child = element.getFirstChild();
				while (child != null) {
					if ((child.getNodeName().equals("tspan"))
							|| (child.getNodeName().equals("#text"))) {
						Node temp = child.getNextSibling();
						element.removeChild(child);
						child = temp;
					} else {
						child = child.getNextSibling();
					}
				}

				// 添加新的文本元素
				String x = element.getAttributeNS(null, "x");
				Element tspan = element.getOwnerDocument().createElementNS(
						svgNS, "tspan");
				tspan.setAttributeNS(null, "dx", "0");
				tspan.setAttributeNS(null, "x", x);
				tspan.setAttributeNS(null, "dy", "0");
				Text textNode = element.getOwnerDocument().createTextNode(
						textContent);
				tspan.appendChild(textNode);
				element.appendChild(tspan);
			}
		});
		// // 移除文本元素
		// Node child = element.getFirstChild();
		// while (child != null) {
		// if ((child.getNodeName().equals("tspan"))
		// || (child.getNodeName().equals("#text"))) {
		// Node temp = child.getNextSibling();
		// element.removeChild(child);
		// child = temp;
		// } else {
		// child = child.getNextSibling();
		// }
		// }
		//
		// // 添加新的文本元素
		// String x = element.getAttributeNS(null, "x");
		// Element tspan = element.getOwnerDocument().createElementNS(svgNS,
		// "tspan");
		// tspan.setAttributeNS(null, "dx", "0");
		// tspan.setAttributeNS(null, "x", x);
		// tspan.setAttributeNS(null, "dy", "0");
		// Text textNode =
		// element.getOwnerDocument().createTextNode(textContent);
		// tspan.appendChild(textNode);
		// element.appendChild(tspan);
	}

}
