package cn.net.connor.createTempDrawings.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Folder;
import javax.swing.*;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.ListOfValuesInfo;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentListOfValues;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.DateButton;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;
import com.teamcenter.rac.util.UIUtilities;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;
import com.teamcenter.services.rac.core.DataManagementService;
//导入类
//import com.teamcenter.services.rac.core.DataManagementService;
//import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateIn;
//import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateInput;
//import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateOut;
//import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateResponse;
import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateIn;
import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateInput;
import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateOut;
import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateResponse;

import cn.net.connor.createTempDrawings.pojo.LovProperty;
import cn.net.connor.createTempDrawings.utils.TCLOVUtil;

public class CreateTempDrawingDialog extends JFrame implements ActionListener{
	private static final Logger logger = LogManager.getLogger(CreateTempDrawingDialog.class);
	
	private AbstractAIFApplication application;
	private TCSession tcSession ;
	private InterfaceAIFComponent selectComponent;
	private JLabel drawingObjTypeLabel;//图纸对象类型标签
	private JComboBox<String> drawingObjTypeComboBox;//图纸对象类型下拉输入框
	private JButton okButton;//确认按钮
	private JButton button1;//按钮
	private JButton button2;//按钮
	private List<String> drawingObjTypeInfos = new ArrayList<String>();//首选项中配置的对象类型信息
	private JFrame buildFram;
	private String currentChoosePropPreferenceName;//当前选中类型的
	private JLabel idLabel;
	private JTextField idTextField;
	private JLabel revLabel;
	private JComboBox<String> revComboBox;
	private List<String> showPropsInfoList = new ArrayList<>();
	private JButton closeButton;
	private JButton buildButton;
	// 添加日期格式化工具
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 根据选中的对象类型，获取对象类型对应的首选项配置的属性信息
	 * 例如：*名称=Item.object_name=string
	 */
	private List<String> preferencePropList = new ArrayList<>();

	/**
	 * 属性组件集合
	 * 显示属性条目，属性组件
	 */
	private Map<String,JComponent> propComponentMap = new HashMap<>();

	/**
	 * 存放lov属性对象
	 */
	private List<LovProperty> lovPropertyList = new ArrayList<>();
	

	/**
	 * 类构造函数
	 * @param application
	 * @param selectComponent
	 */
	public CreateTempDrawingDialog(AbstractAIFApplication application, InterfaceAIFComponent selectComponent) {
		logger.info("haihaihai");
		try {
			this.application = application;
			this.tcSession = (TCSession) this.application.getSession();
			this.selectComponent = selectComponent;
			
			System.out.println("开始加载创建临时图纸面板！");
			logger.info("开始加载创建临时图纸面板！");
			List<String> list1 = getPreferenceInfos("SB6_Create_LS_Design");
			if (list1.isEmpty() || list1.size() <= 0) {
				MessageBox.post("获取首选项SB6_Create_LS_Design配置信息失败，请检查配置是否正确！" , "提示", MessageBox.INFORMATION);
				return;
			}else {
				if (this.drawingObjTypeInfos != null && this.drawingObjTypeInfos.size() > 0) {
					this.drawingObjTypeInfos.clear();
				}
				this.drawingObjTypeInfos.addAll(list1);
			}
			initUI();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}

	/**
	 * 根据首选项名称获取首选项配置信息
	 * @param preferenceName 首选项名称
	 * @return List<String>
	 */
	public List<String> getPreferenceInfos(String preferenceName) {
		List<String> drawingObjPropInfosList = new ArrayList<>();
		TCPreferenceService preferenceService = tcSession.getPreferenceService();
		String[] values = preferenceService.getStringValues(preferenceName);
		if (values != null && values.length > 0) {
			for (int i = 0; i < values.length; i++) {
				drawingObjPropInfosList.add(values[i]);
			}
		}else {
			System.out.println("获取首选项["+preferenceName+"]配置信息失败，请检查配置是否正确！");
		}
		System.out.println("共获取到首选项["+preferenceName+"]中的["+values.length+"]条配置信息");
		return drawingObjPropInfosList;

	}

	/**
	 * 创建临时图纸对象-选择类型的看板
	 */
	private void initUI() throws Exception {
		this.setTitle("请选择图纸类型");
		this.setSize(400, 500);
        this.setLayout(new BorderLayout());

        JPanel centerPanel = new JPanel(new GridBagLayout());
		JPanel buttomPanel = new JPanel(new FlowLayout());
		
		this.drawingObjTypeLabel = new JLabel("图纸对象类型		");
		//获取图纸对象类型
		List<String> typList = getDrawingObjTypeShowName();
		if (typList.isEmpty() || typList.size() <= 0) {
			System.out.println("获取图纸对象类型失败，请检查相关首选项是否配置正确！");
			return;
		}
		String[] drawingTypes = typList.toArray(new String[0]);
		this.drawingObjTypeComboBox = new JComboBox<String>(drawingTypes);
		drawingObjTypeComboBox.setPreferredSize(new Dimension(200, drawingObjTypeComboBox.getPreferredSize().height));
		drawingObjTypeComboBox.setEditable(false);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.WEST;
		gbc.insets = new Insets(0, 0, 0, 12); // 右边留出 10 像素的间距
		centerPanel.add(this.drawingObjTypeLabel, gbc);

		gbc.gridx = 1;
		centerPanel.add(drawingObjTypeComboBox, gbc);

		this.okButton = new JButton("确认");
		buttomPanel.add(okButton);
		

		//构造主面板
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(buttomPanel, BorderLayout.SOUTH);
		
		
		//按钮时间监听
		this.okButton.addActionListener(this);
		
		

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setResizable(true);
  		this.setPreferredSize(new Dimension(800,600));
  		this.pack(); // 自动调整窗口大小以适应内部组件

		//放到屏幕中央
		UIUtilities.centerToScreen(this);
		
		this.setVisible(true);
		this.revalidate();
		this.repaint();

	}
	
	public String getSourceValue(String[] split) throws TCException {
		String value = null;
		if (split.length == 4) {//配置信息中，存在继承属性来源
			String propSource = split[3];
			String[] sourceArr = propSource.split("\\.");
			if (sourceArr.length > 0) {
				String sourceLocation = sourceArr[0];
				String sourceTrueName = sourceArr[1];
				
				
				if (this.selectComponent instanceof TCComponentItemRevision) {
					TCComponentItemRevision itemRevision = (TCComponentItemRevision) selectComponent;
					TCComponentItem item = itemRevision.getItem();
					if (sourceLocation.equals("Item")) {
						value = item.getStringProperty(sourceTrueName);
					}else if (sourceLocation.equals("Rev")) {
						value = itemRevision.getStringProperty(sourceTrueName);
					}else if (sourceLocation.equals("Form")) {
						// 获取版本表单
						TCComponentForm form = (TCComponentForm) itemRevision.getRelatedComponent("IMAN_master_form_rev");
						if (form != null) {
							value = form.getStringProperty(sourceTrueName);
						}
					}
					
				}else {
					System.out.println("选中对象是文件夹，无法获取继承值。");
				}
			}
			
		}
		return value;
	}
	
	/**
	 * 创建对象
	 * @throws Exception 
	 */
	public void createItem() throws Exception {
//		Map<String, String> formMap = new HashMap<String, String>();
		//属性真实值，属性值，属性类型(string\date\lov:lovname)
		List<String[]> formList = new ArrayList<String[]>();
		CreateIn itemDef = new CreateIn();
		CreateInput itemRevisionDef = new CreateInput();
		//获取创建对象的类型
		List<String> typeList = this.drawingObjTypeInfos;//首选项中配置的类型信息
		if (typeList == null || typeList.size() <= 0) {
			System.out.println("类型获取异常，临时图纸创建失败！");
			return;
		}
		String itemType = null;
		String item_id = null;
		String item_revision_id = null;
		String chooseType = (String) this.drawingObjTypeComboBox.getSelectedItem();
		for (String string : typeList){
			String[] types = string.split("=");
			if (chooseType.equals(types[0])) {
				itemType = types[1];
				break;
			}
		}
		System.out.println("新建图纸对象类型为："+itemType);
		//获取对象和版本id
		for (Map.Entry<String, JComponent> entry : this.propComponentMap.entrySet()) {
			String key = entry.getKey();
			if (key.equals("ID*")){
				item_id = getComponentValue(entry.getValue());
			} else if (key.equals("版本")) {
				item_revision_id = getComponentValue(entry.getValue());//item_revision_id
			}
		}


		//item类型
		itemDef.data.boName = itemType;
		itemDef.data.stringProps.put("item_id", item_id);

		//版本类型
		itemRevisionDef.boName = itemType + "Revision";
		if (item_revision_id != null) {
			itemRevisionDef.stringProps.put("item_revision_id", item_revision_id);
		}else {
			System.out.println("获取版本id失败，对象创建失败！");
			return;
		}
		

		System.out.println("已有属性条目-组件数量="+this.propComponentMap.size());
		for (Map.Entry<String, JComponent> entry : this.propComponentMap.entrySet()) {
			String key = entry.getKey();//编辑的属性条目名称
			String value = getComponentValue(entry.getValue());//根据组件获取选中、输入的值
			int tag = 0;
			//遍历首选项配置，获取属性真实值和位置、类型等信息
			for (String prop : preferencePropList){
				String[] split = prop.split("=");
				if (split != null && split.length > 0) {
					String propName = split[0];
					String s1 = split[1];
					String[] split2 = s1.split("\\.");
					String location = null;
					String trueName = null;
					if (split2.length>0) {
						location = split2[0];//Item/Rev/Form
						trueName = split2[1];//属性真实名称
					}else {
						System.out.println("首选项中配置的属性信息【"+split[1]+"】异常！（不存在.符号）请检查配置");
					}
					String typeString = split[2];
					String configName = removeAsterisk(propName);
					String showName = removeAsterisk(key);
					if (configName.equals(showName)) {
						tag++;
						System.out.println("开始赋值属性："+propName+"，对应的属性值："+value);
						//判断是否需要继承，需要的话获取继承值
						String sourceValue = getSourceValue(split);
						if (sourceValue != null) {
							System.out.println("需要获取继承值："+sourceValue);
							value = sourceValue;
						}
						System.out.println(propName+"=>"+value);
						
						if (location.equals("Item")) {
							if (typeString.equals("string")) {
								itemDef.data.stringProps.put(trueName, value);
							}else if (typeString.equals("date")) {
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd HH:mm");
								Date date = sdf.parse(value);
								Calendar calendar = Calendar.getInstance();
					            calendar.setTime(date);
								itemDef.data.dateProps.put(trueName, calendar);
							}else if (typeString.split(":")[0].equals("lov")) {
								String trueValue = getLovTrueValue(trueName,value,this.lovPropertyList);
								itemDef.data.stringProps.put(trueName, trueValue);
							}
						}else if (location.equals("Rev")) {
							if (typeString.equals("string")) {
								itemRevisionDef.stringProps.put(trueName, value);
							}else if (typeString.equals("date")) {
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd HH:mm");
								Date date = sdf.parse(value);
								Calendar calendar = Calendar.getInstance();
					            calendar.setTime(date);
					            itemRevisionDef.dateProps.put(trueName, calendar);
							}else if (typeString.split(":")[0].equals("lov")) {
								String trueValue = getLovTrueValue(trueName,value,this.lovPropertyList);
								itemRevisionDef.stringProps.put(trueName, trueValue);
							}
//							itemRevisionDef.stringProps.put(trueName, value);
						}else if (location.equals("Form")) {
							//此处对表单属性暂时不做赋值，先暂存到一个Map集合中
//							itemRevisionDef.stringProps.put(trueName, value);
							String[] formPropInfos = {trueName, value, typeString};//属性真实名称，输入or选中的属性值，属性类型(string\date\lov:lovname)
							formList.add(formPropInfos);
						}else {
							System.out.println("首选项中配置了意料之外的属性位置。");
						}
					}else {
						continue;
					}
				}else {
					System.out.println("首选项中配置的属性信息【"+prop+"】异常！(不存在=符号)请检查配置");
				}
				
			}
			if (tag == 0) {
				System.out.println("属性条目["+key+"]未在首选项配置中找到对应的配置");
			}
		}
		
		System.out.println("0");

		itemDef.data.compoundCreateInput.put("revision", new CreateInput[] { itemRevisionDef });

		DataManagementService dmService = DataManagementService.getService(tcSession);
		CreateResponse createObjResponse = dmService.createObjects(new CreateIn[] { itemDef });

		if (createObjResponse.serviceData.sizeOfPartialErrors() == 0) {
		    for (CreateOut out : createObjResponse.output) {
		        for (TCComponent obj : out.objects) {
		            TCComponentItem item;
					TCComponentItemRevision itemRev;
					TCComponentForm form;
					if (obj instanceof TCComponentItem) {
						item = (TCComponentItem) obj;
						item.lock();
						try {
							System.out.println("开始挂载新建临时图纸到选中对象下");
							if (this.selectComponent instanceof TCComponentFolder) {
								System.out.println("挂载对象到文件夹下");
								TCComponentFolder folder = (TCComponentFolder) this.selectComponent;
								folder.add("contents", item);
							}else if (this.selectComponent instanceof TCComponentItemRevision) {
								TCComponentItemRevision revision = (TCComponentItemRevision) this.selectComponent;
								revision.add("SB6_LSSJRelation", item);
								
							}
							
						} catch (Exception e) {
							System.out.println("item处理异常：");
							e.printStackTrace();
						}finally {
							item.save();
							item.unlock();
						}
					}
		            else if (obj instanceof TCComponentItemRevision) {
		            	itemRev = (TCComponentItemRevision) obj;
		            	if (!setFormProperty(itemRev,formList)) {
							System.out.println("表单属性设置失败！");
						}
		            	itemRev.lock();
						try {
							itemRev.setStringProperty("sb6_ifls", "是");
						} catch (Exception e) {
							System.out.println("itemRev处理异常：");
							e.printStackTrace();
						}finally {
							itemRev.save();
							itemRev.unlock();
						}
		            }
		            else if (obj instanceof TCComponentForm) {
		            	form = (TCComponentForm) obj;
						/*
						 * form.lock(); try { if (formList.size()>0) { System.out.println("开始处理表单数据");
						 * for (String[] forms : formList) { if (forms.length == 3) { String tNameString
						 * = forms[0]; String tValueString = forms[1]; String tTypeString = forms[2];
						 * 
						 * setTCPropertyValue(form,tNameString,tValueString); } } } } catch (Exception
						 * e) { System.out.println("表单处理异常："); e.printStackTrace(); }finally {
						 * form.save(); form.unlock(); }
						 */
		            }
		                
		        }
		    }
		}
	}

	/**
	 * 	根据属性真实名称在lov属性集合中get对应的lov集合，通过lov显示值获取lov对应的真实值
	 * @param configName 首选项中配置的属性真实名称
	 * @param lovShowName lov的显示值
	 * @param lovPropertyList2 lov属性集合
	 * @return lov的真实值
	 */
	private String getLovTrueValue(String configName, String lovShowName, List<LovProperty> lovPropertyList2) {
		String trueNameString = null;
		for (LovProperty lovProperty : lovPropertyList2) {
			String propName = lovProperty.getPropTrueName();
			if (configName.equals(propName)) {
				Map<String, String> lovMap = lovProperty.getLovMap();
				return lovMap.get(lovShowName);
			}
		}
		System.out.println("未获取到lov-["+lovShowName+"]的真实值");
		return null;
	}

	/**
	 * 
	 * @param component
	 * @param formList
	 * @return
	 */
	private boolean setFormProperty(TCComponent component, List<String[]> formList) {
		try {
			TCComponentForm form = (TCComponentForm) component.getRelatedComponent("IMAN_master_form_rev");
			form.lock();
			try {
				if (formList.size()>0) {
					System.out.println("开始处理表单数据");
					for (String[] forms : formList) {
						String finalPropValue = null;
						if (forms.length == 3) {
							String tNameString = forms[0];//属性真实名称
							String tValueString = forms[1];//属性输入or选中值
							String tTypeString = forms[2];//属性类型string/data/lov:*
							if (tTypeString.contains("lov")) {
								finalPropValue = getLovTrueValue(tNameString,tValueString,this.lovPropertyList);
							}else {
								finalPropValue = tValueString;
							}
							

							System.out.println("设置表单属性["+tNameString+"]="+finalPropValue);
							setTCPropertyValue(form,tNameString,finalPropValue);
						}
					}
					System.out.println("表单属性赋值完成！");
				}else {
					System.out.println("没有需要赋值的表单属性！");
				}
				return true;
			} catch (Exception e) {
				System.out.println("表单处理异常：");
				e.printStackTrace();
			}finally {
				form.save();
				form.unlock();
			}
		} catch (TCException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		
		
		
		return false;
		
	}

	/**
	 * 
	 * @Title: setTCPropertyValue
	 * @Description: 根据对象属性类别赋值
	 * @param @param item
	 * @param @param propertyName
	 * @param @param value 参数
	 * @return void 返回类型
	 * @throws
	 */
	public static void setTCPropertyValue(TCComponent item, String propertyName, Object value) {
	    try {
	    	String type2 = item.getType();
	        TCProperty tcProperty = item.getTCProperty(propertyName);
	        String name = item.getUid();
	        if (tcProperty == null) {
				System.out.println("对象："+name+"，类型："+type2+"中不存在属性：【"+propertyName);
				return;
			}
	        switch (tcProperty.getPropertyType()) {
	        case TCProperty.PROP_double:
	            if (value instanceof Double) {
	                tcProperty.setDoubleValue((double) value);
	            } else {
	                tcProperty.setDoubleValue(Double.parseDouble(value.toString()));
	            }
	            break;
	        case TCProperty.PROP_int:
	            if (value instanceof Integer) {
	                tcProperty.setDoubleValue((int) value);
	            } else {
	                tcProperty.setDoubleValue(Integer.parseInt(value.toString()));
	            }
	            break;
	        case TCProperty.PROP_string:
	            tcProperty.setStringValue(value.toString());
	            break;
	        case TCProperty.PROP_long_string:
	            tcProperty.setStringValueArray((String[]) value);
	            break;
	        case TCProperty.PROP_date:
	            if (value instanceof Date) {
	                tcProperty.setDateValue((Date) value);
	            } else {
	                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd HH:mm");
	                tcProperty.setDateValue(sdf.parse(value.toString()));
	            }
	            break;
	        default:
	            break;
	        }

	    } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	}
	

	/**
	 * 获取创建临时图纸对象时，选择类型界面的类型名称集合
	 * @return List<String>
	 */
	private List<String> getDrawingObjTypeShowName() {
		List<String> drawingObjTypeList = new ArrayList<>();
		if (this.drawingObjTypeInfos != null && this.drawingObjTypeInfos.size() > 0){
			for (int i = 0; i < this.drawingObjTypeInfos.size(); i++) {
				String drawingObjTypeInfo = this.drawingObjTypeInfos.get(i);
				String[] drawingObjTypeInfoArray = drawingObjTypeInfo.split("=");
				if (drawingObjTypeInfoArray.length == 3) {//配置的信息中只有两个=符号
					drawingObjTypeList.add(drawingObjTypeInfoArray[0]);
				}else{
					System.out.println("配置信息：["+drawingObjTypeInfo+"]格式不正确，请检查相关首选项是否配置正确！");
				}
			}
		}

		return drawingObjTypeList;
	}

	/**
	 * 获取选中的图纸类型对应的首选项名称
	 * @return
	 */
	private String getSelectTypePreferenceName() {
		String returnValue = null;
		//当前选中的图纸类型
		String chooseType1 = (String) this.drawingObjTypeComboBox.getSelectedItem();

		List<String> drawingObjTypeList = new ArrayList<>();
		if (this.drawingObjTypeInfos != null && this.drawingObjTypeInfos.size() > 0){
			for (int i = 0; i < this.drawingObjTypeInfos.size(); i++) {
				String drawingObjTypeInfo = this.drawingObjTypeInfos.get(i);
				String[] drawingObjTypeInfoArray = drawingObjTypeInfo.split("=");
				if (drawingObjTypeInfoArray.length == 3) {//配置的信息中只有两个=符号
					//遍历到的类型名称
					String typeName = drawingObjTypeInfoArray[0];
					if (chooseType1 != null && typeName != null){
						if (chooseType1.equals(typeName)) {
							returnValue = drawingObjTypeInfoArray[2];
							break;
						}
					}
				}else{
					System.out.println("配置信息：["+drawingObjTypeInfo+"]格式不正确，请检查相关首选项是否配置正确！");
				}
			}
		}

		return returnValue;
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		Object s = e.getSource();
		try {
			if (s.equals(this.okButton)) {
				String chooseType = (String) this.drawingObjTypeComboBox.getSelectedItem();
				System.out.println("当前选中图纸对象类型="+chooseType);
				// 关闭当前窗口
				this.dispose();
				try {
					buildDrawingObjTypeFrame();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
				
			}else if (s.equals(this.closeButton)) {
				System.out.println("触发‘关闭’按钮");
				// 关闭新建窗口
				this.buildFram.dispose();
			}else if (s.equals(this.buildButton)) {
				System.out.println("触发‘新建’按钮");
				// 校验必填项
				if(!checkRequiredProp()){
					System.out.println("校验未通过！");
					return ;
				}
				System.out.println("校验已通过！");
				try {
					createItem();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
				
				System.out.println("创建临时图纸完成！");
				// 关闭新建窗口
				this.buildFram.dispose();
			}

			 
		} catch (Exception e2) {
			// TODO: handle exception
		}
			
		
	}

	/**
	 * 校验必填属性，弹出提示窗口
	 * @return
	 */
	private boolean checkRequiredProp() {
		 System.out.println("propComponentMap.size="+propComponentMap.size());
		if (propComponentMap.size()>0) {
			for (Map.Entry<String, JComponent> entry : propComponentMap.entrySet()) {
				String key = entry.getKey();
				Object value = getComponentValue(entry.getValue());
//				System.out.println("开始校验属性："+key);
				if (key.contains("*")) {
//					System.out.println("属性【"+key+"】包含必选项！");
					if (value == null || value.equals("")) {
						MessageBox.post("必填项属性【"+key+"】不可为空！" , "提示", MessageBox.INFORMATION);
						return false;
					}
				}
				
				System.out.println(">>>"+key + ": " + value);
			}
		}
		
		return true;
	}

	/**
	 * @根据不同类型的组件获取组件的值
	 * @param component
	 * @return
	 */
	public String getComponentValue(JComponent component) {
		System.out.println("开始根据不同类型的组件取值");
		if (component instanceof JTextField) {
			System.out.println("JTextField");
			return ((JTextField) component).getText();
		} else if (component instanceof JComboBox) {
			System.out.println("JComboBox");
			JComboBox<String> comboBox = (JComboBox<String>) component;
			return (String)comboBox.getSelectedItem();
		} else if (component instanceof DateButton) {
			System.out.println("DateButton");
			DateButton dateButton = (DateButton) component;
			Date date = dateButton.getDate();
			if (date == null){
				return null;
			}
			String dateInput = dateFormat.format(date).replaceAll("(\\d{4})-0(\\d)-", "$1-$2-") + " 00:00";
			System.out.println("dateInput="+dateInput);
			return dateInput;
		}else {
			System.out.println("意料之外的组件！");
		}
		return null;
	}
	

	/**
	 * 绘制对象类型的创建对话框
	 * @return
	 * @throws TCException 
	 */
	public JFrame buildDrawingObjTypeFrame() throws TCException{
		System.out.println("创建新图纸界面！");
		this.propComponentMap.clear();
		this.lovPropertyList.clear();
		this.preferencePropList.clear();

		// 创建新界面
		JFrame frame = new JFrame();
		frame.setTitle("临时图纸创建界面");
		this.setSize(400, 500);
        this.setLayout(new BorderLayout());

        JPanel centerPanel1 = new JPanel(new GridBagLayout());
		JPanel buttomPanel1 = new JPanel(new FlowLayout());

		//1.属性条目区域
		this.idLabel = new JLabel("ID*");
		String idValue = getIdValue(this.selectComponent);

		if (idValue != null){
			this.idTextField = new JTextField(idValue);
		}
		this.revLabel = new JLabel("版本");
		String[] revs = {"S01"};//版本下拉框数组
		this.revComboBox = new JComboBox<String>(revs);
//		this.revComboBox.setEditable(true);

//布局
		// ID Label
		GridBagConstraints gbcIdLabel = new GridBagConstraints();
		gbcIdLabel.gridx = 0;
		gbcIdLabel.gridy = 0;
		gbcIdLabel.anchor = GridBagConstraints.WEST;
		gbcIdLabel.insets = new Insets(5, 5, 5, 5);
		centerPanel1.add(this.idLabel, gbcIdLabel);

		// ID TextField
		GridBagConstraints gbcIdField = new GridBagConstraints();
		gbcIdField.gridx = 1;
		gbcIdField.gridy = 0;
		gbcIdField.anchor = GridBagConstraints.WEST;

		// 设置自定义宽度
		Dimension textFieldSize = new Dimension(200, idTextField.getPreferredSize().height); // 将200设为所需的宽度
		idTextField.setPreferredSize(textFieldSize);

		centerPanel1.add(this.idTextField, gbcIdField);
		propComponentMap.put("ID*", idTextField);


		// 版本 Label
		GridBagConstraints gbcRevLabel = new GridBagConstraints();
		gbcRevLabel.gridx = 0;
		gbcRevLabel.gridy = 1;
		gbcRevLabel.anchor = GridBagConstraints.WEST;
		gbcRevLabel.insets = new Insets(5, 5, 5, 5);
		centerPanel1.add(this.revLabel, gbcRevLabel);

		// 版本 ComboBox
		GridBagConstraints gbcRevCombo = new GridBagConstraints();
		gbcRevCombo.gridx = 1;
		gbcRevCombo.gridy = 1;
		gbcRevCombo.anchor = GridBagConstraints.WEST;

		// 设置自定义宽度
		Dimension comboBoxSize = new Dimension(200, revComboBox.getPreferredSize().height); // 将200设为所需的宽度
		revComboBox.setPreferredSize(comboBoxSize);

		centerPanel1.add(this.revComboBox, gbcRevCombo);
		propComponentMap.put("版本", revComboBox);


		String preferenceName = getSelectTypePreferenceName();
		int propIndex  = 2;
		if (preferenceName != null){
			List<String> preferenceInfoList = getPreferenceInfos(preferenceName);
			if (preferenceInfoList != null && preferenceInfoList.size()>0) {
				System.out.println("获取首选项["+preferenceName+"]中的属性配置成功！");
				this.preferencePropList = preferenceInfoList;
			}else {
				MessageBox.post("获取首选项"+preferenceName+"配置信息失败，请检查配置是否正确！" , "提示", MessageBox.INFORMATION);
				return null;
			}
			
			System.out.println("配置的属性数量 = "+this.preferencePropList.size());
			for(String propInfo : this.preferencePropList){//propInfo格式为：*名称=Item.object_name=string
				System.out.println("开始处理首选项配置的属性信息："+propInfo);
				String[] propConfigInfoArray= propInfo.split("=");
				String validPropName = null;
				if (propConfigInfoArray.length >= 3){
					String name = propConfigInfoArray[0];//“图纸类型”
					System.out.println("开始获取首选项配置属性["+name+"]的信息");
					String[] locationAndTruePropName = propConfigInfoArray[1].split("\\.");

					String trueName = locationAndTruePropName[1];//sb6_DRM03
					String type = propConfigInfoArray[2];//“lov:SB6_TZLXLOV”

					// prop Label
					if (name != null && name.contains("*")){
						validPropName = removeAsterisk(name);
						name = validPropName + "*";
					}
					validPropName = name;

					JLabel propLabel = new JLabel(name);
					GridBagConstraints gbcPropLabel = new GridBagConstraints();
					gbcPropLabel.gridx = 0;
					gbcPropLabel.gridy = propIndex;
					gbcPropLabel.anchor = GridBagConstraints.WEST;
					gbcPropLabel.insets = new Insets(5, 5, 5, 5);
					centerPanel1.add(propLabel, gbcPropLabel);

					// prop value组件
					if (type != null){
						String[] ss = type.split(":");//{"lov","SB6_TZLXLOV"}
						String lovName = null;
						String typeValue = ss[0];
						if (ss.length == 2) {
							lovName = ss[1];
						}
						if (typeValue.equals("string")){
							JTextField propTextField = new JTextField();

							GridBagConstraints gbcPropCombo = new GridBagConstraints();
							gbcPropCombo.gridx = 1;
							gbcPropCombo.gridy = propIndex;
							gbcPropCombo.anchor = GridBagConstraints.WEST;

							// 设置自定义宽度
							Dimension propValueSize = new Dimension(200, propTextField.getPreferredSize().height); // 将200设为所需的宽度
							propTextField.setPreferredSize(propValueSize);

							centerPanel1.add(propTextField, gbcPropCombo);
							propComponentMap.put(validPropName, propTextField);
						}else if (typeValue.equals("date")){
							DateButton dateButton = new DateButton(null, "yyyy-MM-dd", false, false, false);

							GridBagConstraints gbcPropCombo = new GridBagConstraints();
							gbcPropCombo.gridx = 1;
							gbcPropCombo.gridy = propIndex;
							gbcPropCombo.anchor = GridBagConstraints.WEST;

							// 设置自定义宽度
							Dimension propValueSize = new Dimension(200, dateButton.getPreferredSize().height); // 将200设为所需的宽度
							dateButton.setPreferredSize(propValueSize);
							
							centerPanel1.add(dateButton, gbcPropCombo);
							propComponentMap.put(validPropName, dateButton);
						} else if (typeValue.equals("lov")) {
							//TODO 根据冒号后面的LOV名称获取多个lov值，封装成数组添加到propComboBox中
							if (lovName == null) {
								System.out.println("未获取到属性["+name+"]配置的lov名称，请检查配置!");
								return null;
							}
							Map<String, String> lovMap = getLOVDisplay_value(tcSession, lovName);
							String[] displayLovName = lovMap.keySet().toArray(new String[0]);//lov显示值
							LovProperty lovProperty = new LovProperty();
							lovProperty.setPropName(name);
							lovProperty.setPropTrueName(trueName);
							lovProperty.setLov(true);
							lovProperty.setLovMap(lovMap);
							this.lovPropertyList.add(lovProperty);
							
							JComboBox<String> propComboBox = new JComboBox<String>(displayLovName);


							GridBagConstraints gbcPropCombo = new GridBagConstraints();
							gbcPropCombo.gridx = 1;
							gbcPropCombo.gridy = propIndex;
							gbcPropCombo.anchor = GridBagConstraints.WEST;

							// 设置自定义宽度
							Dimension propValueSize = new Dimension(200, propComboBox.getPreferredSize().height); // 将200设为所需的宽度
							propComboBox.setPreferredSize(propValueSize);

							centerPanel1.add(propComboBox, gbcPropCombo);
							propComponentMap.put(validPropName, propComboBox);
							
						}
					}else {
						System.out.println("首选项中配置的属性["+name+"]信息中，未正确配置属性类型！");
					}
					propIndex ++;
				}else {
					System.out.println("首选项["+preferenceName+"]配置格式不正确，请检查！");
				}
			}
		}else {
			System.out.println("未找到选中类型对应的首选项，请检查首选项SB6_Create_LS_Design配置是否正确！");
		}

		//2.按钮区域
		this.closeButton = new JButton("关闭");
		buttomPanel1.add(closeButton);
		this.buildButton = new JButton("新建");
		buttomPanel1.add(buildButton);

		//按钮事件监听
		this.closeButton.addActionListener(this);
		this.buildButton.addActionListener(this);

		frame.add(centerPanel1, BorderLayout.CENTER);
		frame.add(buttomPanel1, BorderLayout.SOUTH);

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setResizable(true);
		frame.setPreferredSize(new Dimension(800,600));
		frame.pack(); // 自动调整窗口大小以适应内部组件

		//放到屏幕中央
		UIUtilities.centerToScreen(this);

		frame.setVisible(true);
		this.revalidate();
		this.repaint();
		
		this.buildFram = frame;
		
		return frame;

	}

	/**
	 * 根据选中对象(业务逻辑)获取ID输入框中的默认值
	 * @param selectComponent2 选中对象
	 * @return ID输入框中的默认值
	 * @throws TCException 
	 */
	private String getIdValue(InterfaceAIFComponent selectComponent2) throws TCException {
//		String idValue = "";
		
		if(selectComponent2 instanceof TCComponentItemRevision){
			System.out.println("当前选中对象是版本对象，开始获取创建界面的ID属性值");
			TCComponentItemRevision itemRevision = (TCComponentItemRevision) selectComponent2;
			String idValue = itemRevision.getStringProperty("item_id");
			if (idValue != null && !idValue.equals("")){
				return idValue.replace("_","");
			}else {{
				return null;//获取版本对象的ID失败
			}}
		}else if (selectComponent2 instanceof TCComponentFolder) {
			System.out.println("当前选中对象是文件夹对象，创建界面的ID属性值置空");
			return "";
		}else {
			return  null;
		}
	}

	/**
	 * 去除字符串中的星号 *
	 */
	public String removeAsterisk(String str) {
		if (str == null || str.isEmpty()) {
			return "";
		}

		// 替换掉所有的 * 符号
		return str.replaceAll("\\*", "");
	}
	
	/**
	 * 获得TC LOV值的显示值-真实值集合
	 * @param seesion
	 * @return
	 * @throws TCException
	 */
	public static Map<String, String> getLOVDisplay_value(TCSession seesion,String lovProp) throws TCException
	{
		Map<String, String> Display_values = new HashMap<String, String>();
		TCComponentListOfValues unitLov = TCLOVUtil.findLOVByName(seesion, lovProp);
		if(unitLov !=null)
		{
			ListOfValuesInfo listOfValues = unitLov.getListOfValues();
			String[] realval = listOfValues.getStringListOfValues();
			String value="";
			boolean flag = false;
		    for (int i = 0; i < realval.length; i++) {			    	
		    	String disval = listOfValues.getDisplayableValue(realval[i]);
		    	Display_values.put(disval, realval[i]);		      	
		     }
		}
		return Display_values;
	}
	

}
