/**
 * @author tyl
 * @Action  ini
 * 2011-3-17
 */
package cn.com.origin.autocode.views.codeview;

import java.util.HashMap;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

import cn.com.origin.autocode.common.operations.ClassificationOperation;
import cn.com.origin.autocode.views.AutoCodeItemTypeView;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.ORTreeContentProvider;
import cn.com.origin.autocodemanager.common.tree.ORTreeLabelProvider;
import cn.com.origin.autocodemanager.views.codeview.CreateCodeSegmentView;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class CreateAutoCodeView extends CreateCodeSegmentView {

	public AutoCodeItemTypeView autocodeItemTypeView;

	public CreateAutoCodeView(Composite composite, String rootName, ViewPart viewPart) {
		super(composite, rootName, viewPart);
	}

	public void initTree(String rootName) {
		tree.setFocus(); // 获得焦点
		treeViewer.setLabelProvider(new ORTreeLabelProvider());
		treeViewer.setContentProvider(new ORTreeContentProvider());
		autocodeItemTypeView = (AutoCodeItemTypeView) viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeItemTypeView_ID);
		rootTreeData = new AbstractTreeData(null, null, rootName, AutoCodeManagerPropertyName.SegmentName, false);
		treeViewer.setInput((new Object[] { rootTreeData }));
	}

	public void createTree(Composite parent, String rootName) {
		Composite composite = new Composite(parent, 0);
		FillLayout fillLayout = new FillLayout();
		composite.setLayout(fillLayout);

		treeViewer = new TreeViewer(composite, SWT.BORDER);
		tree = treeViewer.getTree();
		tree.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		initTree(rootName);

		treeViewer.expandToLevel(1);
		tree.addTreeListener(new TreeListener() {
			public void treeCollapsed(TreeEvent treeEvent) {
			}
			public void treeExpanded(TreeEvent treeEvent) {
				AbstractTreeData codeData = (AbstractTreeData) treeEvent.item.getData();
				if (codeData == null) {
					return;
				}
				try {
					expandNode(codeData);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AbstractTreeData selectedData = (AbstractTreeData) e.item.getData();
				if (selectedData == null) {
					return;
				}
				if (selectedData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.SegmentName)) {
					if (selectedData.isChildrenLoader()) {
						return;
					}
					expandNode(selectedData);
				}
			}
		});
	}

	public void addMouseAction() {
		treeViewer.getTree().addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent e) {
				TreeItem[] treeItems = ((Tree) e.getSource()).getSelection();
				TreeItem[] items = autocodeItemTypeView.getCreateView().treeViewer.getTree().getSelection();
				TreeItem parentItem = null;
				for (int i = 0; i < items.length; i++) {
					parentItem = items[0];
				}
				for (int m = 0; m < treeItems.length; m++) {
					TreeItem childrenItem = treeItems[m];
					addSegmentNode(autocodeItemTypeView.getCreateView().treeViewer, (AbstractTreeData) parentItem.getData(), ((AbstractTreeData) childrenItem.getData()));
				}
			}
		});
	}

	public void addRightKeyAction() {
		final Tree tree = treeViewer.getTree();
		tree.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (e.button == 3) {
					// 鼠标单击右键，3D鼠标
					// 检查是否点到了具体的节点
					CodeViewRightKey rightAction = new CodeViewRightKey(CreateAutoCodeView.this);
					// TreeItem treeItem = tree.getItem(new Point(e.x, e.y));
					rightAction.fillContextMenu();
					// 没有点到具体结点
				} else if (e.button == 1) {
					// 鼠标单击，左键，对应的代码
				}
			}
		});
	}

	public void addSegmentNode(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData sourceChildrenData) {
		try {
			if (parentData == null) {
				MessageBox.post("请选择分类节点!", "INFO", MessageBox.WARNING);
				return;
			}
			if (!parentData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ItemName)) {
				MessageBox.post("请选择分类节点!", "INFO", MessageBox.WARNING);
				return;
			}

//			AbstractTreeData childrenData = new AbstractTreeData(parentData, sourceChildrenData.getPackCodeNodeInfo().getNodeComponent(), sourceChildrenData.getPackCodeNodeInfo().getNode_name(), sourceChildrenData.getPackCodeNodeInfo().getNode_type(), false);
			PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(sourceChildrenData.getPackCodeNodeInfo().getNodeComponent(), AutoCodeManagerPropertyName.searchSegmentPropNames);
			AbstractTreeData	childrenData = new AbstractTreeData(parentData, packCodeNodeInfo);
			
			boolean isContain = treeViewOperation.appendChildrenData(treeViewer, parentData, childrenData);
			if (isContain) {
				return;
			}
			TCComponent childrenComponent = childrenData.getPackCodeNodeInfo().getNodeComponent();
			ClassificationOperation classificationOperation = new ClassificationOperation();
			TCComponent updateComponent = null;

			String classificationName = parentData.getPackCodeNodeInfo().getNode_name();
			String segmentName = childrenData.getPackCodeNodeInfo().getNode_name();

			HashMap<Object, Object> hashMap1 = new HashMap<Object, Object>();
			hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_node_name, classificationName);
			hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_segment, segmentName);
			TCComponent[] components = classificationOperation.searchClassificationNodes(hashMap1);
			if ((components != null) && (components.length > 0)) {
				updateComponent = components[0];
			}
			if (updateComponent == null) {
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_node_index, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_segment, childrenComponent.getUid());
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_node_name, parentData.getPackCodeNodeInfo().getNode_name());
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_create_date, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_last_revise_date, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_create_user, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_last_revise_user, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_groups, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_roles, "");
				hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_users, "");
				classificationOperation.createNewClassificationNode(hashMap);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}
}
