package cn.com.origin.autocode.newitem.generatcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;

import com.teamcenter.rac.kernel.ListOfValuesInfo;
import com.teamcenter.rac.kernel.TCComponentListOfValues;
import com.teamcenter.rac.kernel.TCException;

/**
 * @copyRight 浙江康勒科技有限公司
 * @author 作者 E-mail:hub@connor.net.cn
 * @date 创建时间：2017-8-16 下午11:14:39
 * @version v1.0
 * @parameter
 * @since
 * @return
 */

public class ShowLovDialog extends Dialog {

	protected final LovKeyValue result = new LovKeyValue();
	protected Shell shell;
	private Tree lovTree;
	private String msg;
	private Point p;
	private Text text;
	private TCComponentListOfValues lov;
	private String data;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ShowLovDialog(Shell parent, int style, String msg, Object data,
			Point p, TCComponentListOfValues lov) {
		super(parent, style);

		this.msg = msg;
		this.p = p;
		this.lov = lov;
		if (data != null) {
			this.data = data.toString();
		}
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	// private void initLovTree(TreeItem treeItem, TCComponentListOfValues lov)
	// {

	// }

	private void initLovTree(Widget lovTree, TCComponentListOfValues lov) {

		if (lov != null) {
			try {
				ListOfValuesInfo info = lov.getListOfValues();
				String[] valuesList = info.getStringListOfValues();

				for (String v : valuesList) {
					String disPlayv = info.getDisplayableValue(v);
					if (msg != null && disPlayv.equals(msg)) {
						data = v;
					}
					TreeItem treeItem0 = null;
					if (lovTree instanceof Tree) {
						treeItem0 = new TreeItem((Tree) lovTree, 0);
					} else {
						treeItem0 = new TreeItem((TreeItem) lovTree, 0);

					}
					treeItem0.setText(disPlayv);
					treeItem0.setData(v);
					TCComponentListOfValues cLov = lov
							.getListOfFilterOfValue(v);
					if (cLov != null) {
						initLovTree(treeItem0, cLov);
					}
				}
			} catch (TCException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(335, 281);
		shell.setBounds(p.x, p.y, 330, 270);
		OS.SetWindowPos(shell.handle, OS.HWND_TOPMOST, p.x, p.y, 330, 270,
				SWT.NULL);
		shell.setText(getText());

		lovTree = new Tree(shell, SWT.BORDER);
		lovTree.setBounds(10, 10, 304, 168);
		// initTree();
		initLovTree(lovTree, this.lov);
		lovTree.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				if (event.detail == SWT.CHECK) {
					text.setText(((TreeItem) event.item).getText());
					data = ((TreeItem) event.item).getData().toString();
				} else {
					text.setText(((TreeItem) event.item).getText());
					data = ((TreeItem) event.item).getData().toString();
				}
			}
		});

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				result.value = text.getText();
				result.key = data;
				shell.dispose();
			}
		});
		btnNewButton.setBounds(38, 228, 80, 27);
		btnNewButton.setText("确定");

		Button btnNewButton_1 = new Button(shell, SWT.NONE);

		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result.key = null;
				result.value = null;
				shell.dispose();
			}
		});
		btnNewButton_1.setBounds(199, 228, 80, 27);
		btnNewButton_1.setText("取消");

		text = new Text(shell, SWT.BORDER);
		text.setEditable(false);
		text.setBounds(10, 191, 304, 23);
	}
}
