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

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import cn.com.origin.autocodemanager.common.AutoCodeManagerImageKey;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
import cn.com.origin.autocodemanager.common.CacheImage;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;

public class AbstractTreeData {
	private boolean isChildrenLoader = false;
	private Image image;
	private AbstractTreeData parentTreeData = null;
	private AbstractTreeData childrenTreeData[] = new AbstractTreeData[0];
	private AbstractTreeData[] childrenDatas = new AbstractTreeData[0];
	public PackCodeNodeInfo packCodeNodeInfo;

	public AbstractTreeData() {
	}

	public AbstractTreeData(AbstractTreeData parentCodeNode, TCComponent tccomponent, String node_name, String node_type, boolean isChildrenLoader) {
		this.image = getImageByType(node_type);
		this.isChildrenLoader = isChildrenLoader;
		try {
			if ((tccomponent != null) && (tccomponent.getLogicalProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock))) {
				this.image = getImageByType(AutoCodeManagerPropertyName.LockName);
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
		this.parentTreeData=parentCodeNode;
		PackCodeNodeInfo packCodeNodeInfo = new PackCodeNodeInfo();
		packCodeNodeInfo.setNode_name(node_name);
		packCodeNodeInfo.setNodeComponent(tccomponent);
		packCodeNodeInfo.setNode_type(node_type);
		this.packCodeNodeInfo = packCodeNodeInfo;
	}

	public AbstractTreeData(AbstractTreeData parentCodeNode, TCComponent tccomponent, Image image, String displayTypeName, String realTypeName, String node_type, boolean isChildrenLoader) {
		this.image = image;
		this.isChildrenLoader = isChildrenLoader;
		PackCodeNodeInfo packCodeNodeInfo = new PackCodeNodeInfo();
		packCodeNodeInfo.setNode_name(displayTypeName);
		packCodeNodeInfo.setRealTypeName(realTypeName);
		packCodeNodeInfo.setNodeComponent(tccomponent);
		packCodeNodeInfo.setNode_type(node_type);
		this.packCodeNodeInfo = packCodeNodeInfo;
	}

	public AbstractTreeData(AbstractTreeData parentCodeNode, PackCodeNodeInfo codeNodeInfo) {
		this.packCodeNodeInfo = codeNodeInfo;
		this.parentTreeData=parentCodeNode;
		this.image = getImageByType(packCodeNodeInfo.getNode_type());
		if (packCodeNodeInfo.is_lock) {
			this.image = getImageByType(AutoCodeManagerPropertyName.LockName);
		}
	}

	public Image getImageByType(String type) {
		Image image = null;
		if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.RootNodeName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.HOME_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.TreeName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.TREE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LIST_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.TreeNodeName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.TREENODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTNODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListPropertyValueNodeName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTVALUE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ComponentName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ITEM_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.OrganizationName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ORGANIZATION_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.SegmentName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.GroupName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.GROUP_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.RoleName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ROLE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.UserName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.PERSON_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.LockName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LOCK_IMAGE);
		}  else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.CodeGroupName)) {
			image = CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODEGROUP_IMAGE);
		}
		return image;
	}

	public ImageDescriptor getImageDescriptorByType(String type) {
		ImageDescriptor imageDesc = null;
		if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.RootNodeName)) {
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.TreeName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.TREE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LIST_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.TreeNodeName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.TREENODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTNODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ListPropertyValueNodeName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTVALUE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.ComponentName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ITEM_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.OrganizationName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ORGANIZATION_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.SegmentName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.GroupName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.GROUP_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.RoleName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ROLE_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.UserName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.PERSON_IMAGE);
		} else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.LockName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LOCK_IMAGE);
		}else if (type.equalsIgnoreCase(AutoCodeManagerPropertyName.CodeGroupName)) {
			imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODEGROUP_IMAGE);
		}
		return imageDesc;
	}

	public void setTreeAutoCodeNodeChildren(AbstractTreeData[] childrenAutoCodeNodeData) {
		this.childrenTreeData = childrenAutoCodeNodeData;
		this.setChildrenDatas(childrenAutoCodeNodeData);
		// vectorChildren = new Vector<AbstractTreeData>();
		if (childrenAutoCodeNodeData != null && childrenAutoCodeNodeData.length > 0) {
			for (int i = 0; i < childrenAutoCodeNodeData.length; i++) {
				// vectorChildren.add(childrenAutoCodeNodeData[i]);
			}
		}
	}

	public void addChildrenNode(AbstractTreeData childrenNode) {
		if (childrenNode == null) {
			return;
		}
		AbstractTreeData[] childrenNodes = this.childrenTreeData;
		int childrenLength = 1;
		if (childrenNodes != null) {
			childrenLength = childrenLength + childrenNodes.length;
		}
		AbstractTreeData[] childrenNodes2 = new AbstractTreeData[childrenLength];
		for (int i = 0; i < childrenLength - 1; i++) {
			childrenNodes2[i] = childrenNodes[i];
		}
		childrenNodes2[childrenLength - 1] = childrenNode;
		this.setTreeAutoCodeNodeChildren(childrenNodes2);
	}

	public Image getImage() {
		return image;
	}

	/**
	 * @return the childrenDatas
	 */
	public AbstractTreeData[] getChildrenDatas() {
		return childrenDatas;
	}

	/**
	 * @param childrenDatas
	 *            the childrenDatas to set
	 */
	public void setChildrenDatas(AbstractTreeData[] childrenDatas) {
		this.childrenDatas = childrenDatas;
	}

	/**
	 * @return the childrenTreeData
	 */
	public AbstractTreeData[] getChildrenTreeData() {
		return childrenTreeData;
	}

	/**
	 * @param image
	 *            the image to set
	 */
	public void setImage(Image image) {
		this.image = image;
	}

	/**
	 * @return the parentTreeData
	 */
	public AbstractTreeData getParentTreeData() {
		return parentTreeData;
	}

	/**
	 * @param parentTreeData
	 *            the parentTreeData to set
	 */
	public void setParentTreeData(AbstractTreeData parentTreeData) {
		this.parentTreeData = parentTreeData;
	}

	/**
	 * @return the isChildrenLoader
	 */
	public boolean isChildrenLoader() {
		return isChildrenLoader;
	}

	/**
	 * @param isChildrenLoader
	 *            the isChildrenLoader to set
	 */
	public void setChildrenLoader(boolean isChildrenLoader) {
		this.isChildrenLoader = isChildrenLoader;
	}

	public PackCodeNodeInfo getPackCodeNodeInfo() {
		return packCodeNodeInfo;
	}

	public void setPackCodeNodeInfo(PackCodeNodeInfo packCodeNodeInfo) {
		this.packCodeNodeInfo = packCodeNodeInfo;
		if (packCodeNodeInfo.is_lock) {
			this.image = getImageByType(AutoCodeManagerPropertyName.LockName);
		} else {
			this.image = getImageByType(packCodeNodeInfo.getNode_type());
		}
	}

	public void setChildrenTreeData(AbstractTreeData[] childrenTreeData) {
		this.childrenTreeData = childrenTreeData;
	}

}
