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

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
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.dialog.AbstractCodeDialog;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;

public class NewCodeNodeDialog extends AbstractCodeDialog {

	String actionType;

	public NewCodeNodeDialog(Shell parentShell, AbstractCreateTreeView codeTreeView, String actionType, String title, boolean isEdit) {
		super(parentShell, codeTreeView, title, isEdit);
		this.actionType = actionType;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		if (actionType.equals(AutoCodeManagerPropertyName.AddRuleGroupNodeName)) {
			Group nodeGroup = createCompositeGroup(container, "组");
			createGroupComposite(nodeGroup);
		} else {
			Group nodeGroup = createCompositeGroup(container, "编码规则");
			createNodeComposite(nodeGroup);
			Group seqGroup = createCompositeGroup(container, "流水设置");
			createSeqComposite(seqGroup);
		}
		return container;
	}

	public void createGroupComposite(Composite parent) {
		Composite composite = new Composite(parent, 0);
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		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);
		initNewGroupComposite(composite);
		if (selectedTreeData != null) {
			if (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() != null) {
				setTextValue(nodeNameText, selectedTreeData.getPackCodeNodeInfo().getNode_name());
				setTextValue(nodeDescriptionText, selectedTreeData.getPackCodeNodeInfo().getNode_desc());
			}
		}
	}

	public void initNewGroupComposite(Composite composite) {
		createNewLabel(composite, "组名称");
		nodeNameText = createNewText(composite, "");
		createNewLabel(composite, "备注");
		nodeDescriptionText = createNewText(composite, "");
	}

	public void initNewCodeComposite(Composite composite) {
		createNewLabel(composite, "规则名称");
		nodeNameText = createNewText(composite, "");

		createNewLabel(composite, "首位码");
		nodeValueText = createNewText(composite, "");

		createNewLabel(composite, "备注");
		nodeDescriptionText = createNewText(composite, "");

		createNewLabel(composite, "编码样式");
		codeStyleText = createNewText(composite, "[T][L]");

		createNewLabel(composite, "编码长度");
		codeLengthText = createNewText(composite, "14");

		createNewLabel(composite, "类型");
		String[] items = new String[] { "编码树", "列表" };
		typeCombo = createNewCombo(composite, items);

	}

	// 检查节点和组是否为空
	@Override
	protected void okPressed() {
		String nodename = nodeNameText.getText().trim();
		if (nodename == null || nodename.trim().length() == 0) {
			postMesssageBox("规则名称不能为空！");
			nodeNameText.setFocus();
			return;
		}
		String type = "";
		if (typeCombo != null) {
			if (typeCombo.getText().equals("编码树")) {
				type = AutoCodeManagerPropertyName.TreeName;
			} else {
				type = AutoCodeManagerPropertyName.ListName;
			}
		}
		if (actionType.equals(AutoCodeManagerPropertyName.AddRuleGroupNodeName)) {
			type = AutoCodeManagerPropertyName.CodeGroupName;
		}
		if (actionType.equals(AutoCodeManagerPropertyName.AddRuleGroupNodeName)) {
			saveNewCodeNode(selectedTreeData, selectedComponent, createView.treeViewer, type, false);
		} else {
			if ((treeViewOperation.isRuleRootNode(selectedTreeData))) {
				saveNewCodeNode(selectedTreeData.getParentTreeData(), selectedComponent, createView.treeViewer, type, true);
			} else {
				saveNewCodeNode(selectedTreeData, selectedComponent, createView.treeViewer, type, true);
			}
		}
		close();
	}

	// 设置对话框初始大小
	@Override
	protected Point getInitialSize() {
		if (actionType.equals(AutoCodeManagerPropertyName.AddRuleGroupNodeName)) {
			return new Point(250, 200);
		} else {
			return new Point(270, 440);
		}

	}

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