/**
 * @author tyl
 * @Action  ini
 * 2011-4-1
 */
package cn.com.origin.autocodemanager.common.dialog;

import org.eclipse.jface.dialogs.Dialog;
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.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.Shell;
import org.eclipse.swt.widgets.Tree;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
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.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.progress.ExpandProgressBar;
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.common.tree.TreeViewOperation;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;

public class AbstractCodeSegmentDialog extends Dialog {
	public TreeViewer treeViewer;
	public Tree tree;
	public AbstractCreateTreeView abstractTreeView;
	public AbstractCreateTreeView createView;
	public String title;
	public TreeViewOperation treeViewOperation;
	public int selected = 0;
	public String action;
	public AutoCodeSegmentView codeSegmentView;
	public TreeAndListCodeOperation treeAndListCodeOperation;

	public AbstractCodeSegmentDialog(Shell parentShell, AbstractCreateTreeView createView, String title) {
		super(parentShell);
		this.createView = createView;
		this.title = title;
		initParams();
		codeSegmentView = (AutoCodeSegmentView) createView.viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);
	}
	
	public AbstractCodeSegmentDialog(Shell parentShell, AbstractCreateTreeView createView, String title, String action) {
		super(parentShell);
		this.createView = createView;
		this.title = title;
		initParams();
		this.action = action;
		codeSegmentView = (AutoCodeSegmentView) createView.viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);
	}

	public AbstractCodeSegmentDialog(Shell parentShell, String title) {
		super(parentShell);
		this.title = title;
		initParams();
	}

private void initParams()
{
	this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE 
			| SWT.APPLICATION_MODAL);
	this.treeViewOperation = new TreeViewOperation();
	this.treeAndListCodeOperation=new TreeAndListCodeOperation();

}

	public void createBottomComposite(Composite parent) {
	}

	public void createTreeComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		FillLayout gridLayout1 = new FillLayout();
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalSpan = 20;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		composite.setLayoutData(gridData1);
		composite.setLayout(gridLayout1);
		createTree(composite, "编码规则");
	}

	public void initTree(String rootName) {
		treeViewer.setLabelProvider(new ORTreeLabelProvider());
		treeViewer.setContentProvider(new ORTreeContentProvider());
		AbstractTreeData treeData = new AbstractTreeData(null, null, rootName, AutoCodeManagerPropertyName.SegmentName, false);
		treeViewer.setInput((new Object[] { treeData }));
		addRightKeyAction();
	}

	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) {
				if (selected <= 1) {
					selected++;
				}
				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) {
				if (selected <= 1) {
					selected++;
				}
				AbstractTreeData selectedData = (AbstractTreeData) e.item.getData();
				if (selectedData == null) {
					return;
				}
				if (selectedData.getParentTreeData() == null) {
					expandNode(selectedData);
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView#addRightKeyAction()
	 */
	public void addRightKeyAction() {}
	public void expandNode(AbstractTreeData codeData) {
		if (selected == 1) {
			return;
		}
		AbstractTreeData[] codeDatas = new AbstractTreeData[] { codeData };
		if (codeData == null) {
			return;
		}
		if (codeData.isChildrenLoader()) {
			codeDatas = codeData.getChildrenDatas();
		}
		ExpandProgressBar expandProgressBar = new ExpandProgressBar(Display.getDefault().getActiveShell(), "正在打开", this, codeDatas);
		try {
			expandProgressBar.expand();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView#createOrgAutoCodeNodeChildren(cn.com.origin.autocodemanager.common.tree.AbstractTreeData[])
	 */
//
	public void createOrgAutoCodeNodeChildren(AbstractTreeData[] codeDatas) throws Exception {}

	// 设置对话框窗口标题和图标
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(title);
		newShell.setImage(CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODENODE_IMAGE));
	}
}
