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

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.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
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.BusinessType;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.progress.ExpandProgressBar;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;
import cn.com.origin.autocodemanager.views.AutoCodeTreeCodeView;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentGroup;
import com.teamcenter.rac.kernel.TCSession;

public abstract class AbstractCreateTreeView {
	public TreeViewer treeViewer;
	public TCSession session;
	public TCComponentGroup currentGroup;
	public BusinessType businessType;
	// public AbstractTreeData firstTreeData;
	public ViewPart viewPart;
	public Tree tree;
	public AbstractTreeData rootTreeData;
	public TreeAndListCodeOperation treeAndListCodeOperation;
	public TreeViewOperation treeViewOperation;
	public AutoCodeListCodeView codeListView;
	public AutoCodeSegmentView codeSegmentView;
	public AutoCodeTreeCodeView codeTreeView;
	public TCComponent currentRole;
	public TCComponent currentUser;

	public AbstractCreateTreeView(Composite composite, String rootName, ViewPart viewPart) {
		this.viewPart = viewPart;
		initParams();
		createView(composite, rootName);
		addRightKeyAction();
		addMouseAction();
	}

	public AbstractCreateTreeView() {
		initParams();
	}

	public void initParams() {
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		this.treeViewOperation = new TreeViewOperation();
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		currentGroup = session.getCurrentGroup();
		currentRole = session.getCurrentRole();
		currentUser = session.getUser();
		businessType = new BusinessType(session);
	}

	public AbstractCreateTreeView(Composite composite, String rootName) {
		initParams();
		createView(composite, rootName);
		addRightKeyAction();
		addMouseAction();
	}

	public AbstractCreateTreeView(Composite composite, String rootName, String itemType) {
		// createView(composite,itemType);
	}

	public Composite createView(Composite composite, String rootName) {
		createTree(composite, rootName);
		return composite;
	}

	public void addRightKeyAction() {
	};

	public void addMouseAction() {
	};

	public void initTree(String rootName) {
		treeViewer.setLabelProvider(new ORTreeLabelProvider());
		treeViewer.setContentProvider(new ORTreeContentProvider());
		System.out.println("rootName============"+rootName);
		AbstractTreeData treeData = new AbstractTreeData(null, null, rootName, AutoCodeManagerPropertyName.RootNodeName, false);
		treeViewer.setInput((new Object[] { treeData }));
	}

	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 e) {
					e.printStackTrace();
				}
			}
		});

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

	public void expandNode(AbstractTreeData codeData) {
		if (codeData == null) {
			return;
		}
		long time1 = System.nanoTime();
		AbstractTreeData[] codeDatas = new AbstractTreeData[] { codeData };
		ExpandProgressBar expandProgressBar = new ExpandProgressBar(Display.getDefault().getActiveShell(), "正在打开", this, codeDatas);
		try {
			expandProgressBar.expand();
		} catch (Exception e) {
			e.printStackTrace();
		}
		long time2 = System.nanoTime();
		System.out.println("加载【"+codeData.getPackCodeNodeInfo().getNode_name()+"】节点用时(ms)： " + (time2 - time1)/1000000);
	}

	public GridData getNewGridData() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = false;
		gridData.verticalAlignment = GridData.FILL;
		return gridData;
	}

	public Text createNewText(Composite parent, String initValue) {
		Text newText = new Text(parent, SWT.BORDER);
		newText.setLayoutData(getNewGridData());
		newText.setText(initValue);
		return newText;
	}

	public Label createNewLabel(Composite parent, String title) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title + "：");
		return label;
	}

	/**
	 * @param codeDatas
	 * @throws Exception
	 */
	public abstract void createOrgAutoCodeNodeChildren(AbstractTreeData[] codeDatas) throws Exception;
}
