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

import java.util.HashMap;
import java.util.List;
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.ui.part.ViewPart;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
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.common.tree.ORTreeContentProvider;
import cn.com.origin.autocodemanager.common.tree.ORTreeLabelProvider;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import cn.com.origin.autocodemanager.views.AutoCodeTreeCodeView;
import com.teamcenter.rac.kernel.TCComponent;

public class CreateCodeSegmentView extends AbstractCreateTreeView {
	private boolean isSelectedAction = false;

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

	public CreateCodeSegmentView(Composite composite, String rootName) {
		super(composite, rootName);
	}

	public void initTree(String rootName) {
		tree.setFocus(); // »ñµÃ½¹µã
		treeViewer.setLabelProvider(new ORTreeLabelProvider());
		treeViewer.setContentProvider(new ORTreeContentProvider());
		rootTreeData = new AbstractTreeData(null, null, rootName,
				AutoCodeManagerPropertyName.SegmentName, false);
		treeViewer.setInput((new Object[] { rootTreeData }));
		if (viewPart != null) {
			codeListView = (AutoCodeListCodeView) viewPart
					.getSite()
					.getPage()
					.findView(
							AutoCodeManangerConstants.AutoCodeManagerListCodeView_ID);
			codeTreeView = (AutoCodeTreeCodeView) viewPart
					.getSite()
					.getPage()
					.findView(
							AutoCodeManangerConstants.AutoCodeManagerTreeCodeView_ID);
		}
	}

	public void addRightKeyAction() {
		final Tree tree = treeViewer.getTree();
		tree.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (e.button == 3) {
					CodeSegmentViewRightKeyAction rightAction = new CodeSegmentViewRightKeyAction(
							CreateCodeSegmentView.this);
					rightAction.fillContextMenu();
				}
			}
		});
	}

	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) {
				isSelectedAction = false;
				AbstractTreeData codeData = (AbstractTreeData) treeEvent.item
						.getData();
				if (codeData == null) {
					return;
				}
				try {
					expandNode(codeData);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AbstractTreeData selectedData = (AbstractTreeData) e.item
						.getData();
				if (selectedData == null) {
					return;
				}
				isSelectedAction = true;
				if (selectedData.getParentTreeData() == null) {
					if (!selectedData.isChildrenLoader()) {
						expandNode(selectedData);
					}
					return;
				}
				if ((selectedData.getPackCodeNodeInfo().getNodeComponent() != null)
						&& (selectedData.getPackCodeNodeInfo().is_root)) {
					expandNode(selectedData);
				} else {
					if ((codeListView != null) && ((codeTreeView != null))) {
						codeListView.createView.treeViewer.setInput(null);
						codeTreeView.createView.treeViewer.setInput(null);
					}
				}
			}
		});
	}

	public void createOrgAutoCodeNodeChildren(AbstractTreeData[] codeDatas)
			throws Exception {
		if ((codeListView != null) && ((codeTreeView != null))) {
			codeListView.createView.treeViewer.setInput(null);
			codeTreeView.createView.treeViewer.setInput(null);
		}
		if (codeDatas != null && codeDatas.length > 0) {
			for (int i = 0; i < codeDatas.length; i++) {
				AbstractTreeData autoCodeData = (AbstractTreeData) codeDatas[i];
				if (autoCodeData == null) {
					continue;
				}
				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;
				}
				String[] propertyNames = AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames;
				if (isSelectedAction) {
					propertyNames = AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames;
				}
				List<PackCodeNodeInfo> packCodeNodeInfos = treeAndListCodeOperation
						.searchTreeORListNodeInfos(hashMap, parentStr2,
								propertyNames);
				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.is_root) {
							if (packCodeNodeInfo.isContainsChildren()) {
								autoCodeDataTCComs[k]
										.setTreeAutoCodeNodeChildren(new AbstractTreeData[] { new AbstractTreeData() });
							}
						}
					}
					if (autoCodeData.getPackCodeNodeInfo().is_root) {
						String type = autoCodeData.getPackCodeNodeInfo()
								.getNode_type();
						if ((codeListView != null) && ((codeTreeView != null))) {
							if (type
									.equals(AutoCodeManagerPropertyName.ListName)) {
								codeListView.createView.treeViewer
										.setInput(autoCodeDataTCComs);
							} else if (type
									.equals(AutoCodeManagerPropertyName.TreeName)) {
								codeTreeView.createView.treeViewer
										.setInput(autoCodeDataTCComs);
							}
						}
					} else {
						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);
					continue;
				}
			}
		}
	}
}
