/**
 * @author tyl
 * @Action  ini
 * 2011-3-16
 */
package cn.com.origin.autocodemanager.common.treetable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.part.ViewPart;

import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.views.listcodeview.NewItemListCodeView;
import cn.com.origin.autocodemanager.views.treecodeview.NewItemTreeCodeView;
import org.eclipse.swt.graphics.Color;
import com.teamcenter.rac.kernel.TCComponent;

public class AbstractTreeTable extends AbstractCreateTreeView {

	public AbstractTreeTable(Composite composite, String rootName, ViewPart viewPart) {
		super(composite, rootName, viewPart);
		// createTree(composite);
	}

	public AbstractTreeTable() {

	}

	public AbstractTreeTable(Composite composite, String rootName) {
		super(composite, rootName);

	}

	public TreeColumn createColumn(Composite parent, String treeColumnName, int width) {
		TreeColumn treeColumn = new TreeColumn(tree, SWT.LEFT);
		treeColumn.setText(treeColumnName);
		treeColumn.setWidth(width);
		return treeColumn;
	}

	public void initTree(String rootName) {
		tree.setHeaderVisible(true); // 设置表头可见
		tree.setLinesVisible(true); // 表格线可见
		
		tree.setBackground(new Color(Display.getCurrent(),255,255,255));
		tree.setVisible(true);
		tree.setFocus(); // 获得焦点
		treeViewer.setLabelProvider(new ORTreeTableLabelProvider());
		treeViewer.setContentProvider(new ORTreeTableContentProvider());
		createTreeColumn();
	}

	public void createTreeColumn() {
		createColumn(tree, "码段名称", 150);
		createColumn(tree, "码值", 100);
		createColumn(tree, "备注", 100);
		createColumn(tree, "是否为默认值", 100);
		createColumn(tree, "所属规则", 100);
		createColumn(tree, "是否锁定", 100);
		createColumn(tree, "创建日期", 100);
		createColumn(tree, "创建者", 100);
		createColumn(tree, "修改日期", 100);
		createColumn(tree, "最后修改者", 100);
	}

	@Override
	public void addMouseAction() {

	}

	@Override
	public void addRightKeyAction() {
	}

	@Override
	public void createOrgAutoCodeNodeChildren(AbstractTreeData[] codeDatas) throws Exception {
		if (codeDatas != null && codeDatas.length > 0) {
			for (int i = 0; i < codeDatas.length; i++) {
				AbstractTreeData autoCodeData = (AbstractTreeData) codeDatas[i];
				if (autoCodeData == null) {
					return;
				}
				List<String> newItemViewNames = new ArrayList<String>();
				newItemViewNames.add(NewItemTreeCodeView.class.getSimpleName());
				newItemViewNames.add(NewItemListCodeView.class.getSimpleName());

				String thisClassName = this.getClass().getSimpleName();
				if (newItemViewNames.contains(thisClassName)) {
					TCComponent component = autoCodeData.getPackCodeNodeInfo().getNodeComponent();
					if ((component != null) &&(autoCodeData.getPackCodeNodeInfo().is_lock)) {
						autoCodeData.setTreeAutoCodeNodeChildren(null);
						autoCodeData.setChildrenLoader(false);
						return;
					}
				}
				TCComponent autoCodeDataComponent = autoCodeData.getPackCodeNodeInfo().getNodeComponent();
				TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				String parentStr = "";
				parentStr = treeAndListCodeOperation.getParentStr(autoCodeData, parentStr);
				if (autoCodeDataComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, autoCodeDataComponent.getUid());
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr);
				} else {
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, "");
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, "");
				}
				String parentStr2 = "";
				if (autoCodeDataComponent != null) {
					if (parentStr.trim().length() == 0) {
						parentStr2 = autoCodeDataComponent.getUid();
					} else {
						parentStr2 = parentStr + "," + autoCodeDataComponent.getUid();
					}
				} else {
					parentStr2 = parentStr;
				}

				List<PackCodeNodeInfo> packCodeNodeInfos = treeAndListCodeOperation.searchTreeORListNodeInfos(hashMap, parentStr2,AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);

				if (packCodeNodeInfos != null && packCodeNodeInfos.size() > 0) {
					AbstractTreeData autoCodeDataTCComs[] = new AbstractTreeData[packCodeNodeInfos.size()];
					for (int k = 0; k < packCodeNodeInfos.size(); k++) {
						if (packCodeNodeInfos.get(k).getNodeComponent() == null) {
							continue;
						}
						PackCodeNodeInfo packCodeNodeInfo = packCodeNodeInfos.get(k);
						autoCodeDataTCComs[k] = new AbstractTreeData(autoCodeData, packCodeNodeInfo);
						if (packCodeNodeInfo.isContainsChildren()) {
							autoCodeDataTCComs[k].setTreeAutoCodeNodeChildren(new AbstractTreeData[] { new AbstractTreeData() });
						}
					}
					autoCodeData.setTreeAutoCodeNodeChildren(autoCodeDataTCComs);
					autoCodeData.setChildrenLoader(true);
					treeViewer.refresh(autoCodeData);
					treeViewer.expandToLevel(autoCodeData, 1);
				} else {
					autoCodeData.setTreeAutoCodeNodeChildren(null);
					autoCodeData.setChildrenLoader(true);
					treeViewer.refresh(autoCodeData);
					treeViewer.expandToLevel(autoCodeData, 1);
				}
			}
		}
	}

}
