package com.ht.ui.taginfo;

import java.util.Map;

import javafx.beans.value.ChangeListener;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import com.ht.tags.model.index.IndexTreeNode;
import com.ht.ui.MainApplication;
import com.ht.ui.controls.IndexNodeTreeItem;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.remoting.HessianResponse;

public class BuildingTreeView extends TreeView<IndexTreeNode> {
	
	private Map<String, IndexNodeTreeItem> indexTreeNodeMap;

	public BuildingTreeView(Map<String, IndexNodeTreeItem> indexTreeNodeMap, ChangeListener<TreeItem<IndexTreeNode>> listener) {
		super();
		
		getStyleClass().add("page-tree");
		setShowRoot(false);
		setEditable(false);

		getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		getSelectionModel().selectedItemProperty().addListener(listener);
		
		updateIndexTree();
	}
	
	void updateIndexTree() {

		try {
			HessianResponse resp = HessianFactoryManger.INSTANCE
					.getTagService().getBuildingIndex();

			if (resp.isSuccessful() && resp.getData() != null) {

				IndexTreeNode root = (IndexTreeNode) resp.getData();
				// tagVarTreeView.setRoot(new IndexNodeTreeItem(root));
				setRoot(new IndexNodeTreeItem(root, indexTreeNodeMap));

			} else {
				MainApplication.getMainApp().showMessage(resp.getMsg());
			}
		} catch (Exception e) {
			MainApplication.getMainApp().showMessage("服务器未响应");
		}
	}
	
}
