package cn.com.origin.autocodemanager.views.treecodeview;

import org.eclipse.swt.graphics.Point;
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.AbstractTreeData;

public class NewTreeCodeNodeDialog extends AbstractCodeDialog {
	boolean isNewChildren = false;

	public NewTreeCodeNodeDialog(Shell parentShell, CreateTreeCodeView codeTreeView, String title, boolean isEdit, boolean isNewChildren) {
		super(parentShell, codeTreeView, title, isEdit);
		this.isNewChildren = isNewChildren;
		

	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		Group nodeGroup = createCompositeGroup(container, "编码树");
		createNodeComposite(nodeGroup);
		return container;
	}

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

		createNewLabel(composite, "码值");
		nodeValueText = createNewText(composite, "");

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

	// 检查节点和组是否为空
	@Override
	protected void okPressed() {
		String nodename = nodeNameText.getText().trim();
		if (nodename == null || nodename.trim().length() == 0) {
			postMesssageBox("节点名称不能为空！");
			nodeNameText.setFocus();
			return;
		}
		if (selectedTreeData == null) {
			AbstractTreeData rootData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
			saveNewCodeNode(rootData, selectedComponent, codeTreeView.createView.treeViewer, AutoCodeManagerPropertyName.TreeNodeName, false);
		} else {
			if (isNewChildren) {
				saveNewCodeNode(selectedTreeData, selectedComponent, createView.treeViewer, AutoCodeManagerPropertyName.TreeNodeName, false);
			} else {
				saveNewCodeNode(selectedTreeData.getParentTreeData(), selectedComponent, createView.treeViewer, AutoCodeManagerPropertyName.TreeNodeName, false);
			}
		}
		close();
	}

	// 设置对话框初始大小
	@Override
	protected Point getInitialSize() {
		return new Point(400, 230);
	}

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

	}

}
