/**
 * @author tyl
 * @Action  ini
 * 2011-4-13
 */
package cn.com.origin.autocodemanager.views.treecodeview;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
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.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.treetable.ORNewItemTreeTableLabelProvider;
import cn.com.origin.autocodemanager.common.treetable.ORTreeTableContentProvider;
import com.teamcenter.rac.kernel.TCComponent;

public class NewItemTreeCodeView extends CreateTreeCodeView {

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

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

	public void initTree(String rootName) {
		tree.setHeaderVisible(true); // 设置表头可见
		tree.setLinesVisible(true); // 表格线可见
		tree.setFocus(); // 获得焦点
		treeViewer.setLabelProvider(new ORNewItemTreeTableLabelProvider());
		treeViewer.setContentProvider(new ORTreeTableContentProvider());
		createTreeColumn();
	}

	public void createTreeColumn() {
		createColumn(tree, "码段名称", 250);
		createColumn(tree, "码值", 100);
		createColumn(tree, "备注", 50);
	}

	public void createTree(Composite parent, String rootName) {
		Composite composite = parent;
		treeViewer = new TreeViewer(composite, SWT.BORDER);
		tree = treeViewer.getTree();
		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;
				}
				TCComponent component = codeData.getPackCodeNodeInfo().getNodeComponent();
				if ((component != null) && (codeData.getPackCodeNodeInfo().is_lock)) {
					codeData.setTreeAutoCodeNodeChildren(null);
					codeData.setChildrenLoader(false);
					return;
				} else {
					expandNode(codeData);
				}
			}
		});

		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AbstractTreeData selectedData = (AbstractTreeData) e.item.getData();
				if (selectedData == null) {
					return;
				}
				if (selectedData.getParentTreeData() == null) {
					expandNode(selectedData);
				}
			}
		});
	}
}
