package com.connor.bomCompare;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.TreePath;

import org.apache.logging.log4j.core.pattern.AbstractStyleNameConverter.White;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentBOMWindow;
import com.teamcenter.rac.kernel.TCComponentBOMWindowType;
import com.teamcenter.rac.kernel.TCComponentDataset;
import com.teamcenter.rac.kernel.TCComponentDatasetType;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentTcFile;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class DDGYDialog extends AbstractAIFDialog implements ActionListener {

	private TCSession session;
	private InterfaceAIFComponent comp;

	private List<List<String>> StationDatas=new ArrayList<List<String>>();//工步
	private TCComponentItem target;
	private String outPathString =System.getenv("TEMP") + "\\";//导出路径
	private String nameString="";
	private Map<String, File> Files=new HashMap<String, File>();//工位工艺作业指导书
	private List<String> files=new ArrayList<String>();//工位工艺作业指导书
	
	
	private JLabel headLabel;//源对象
	private JTextArea headName;// 源对象名称
	private JLabel targetLabel;//目标对象
	private JTextArea targetName;// 目标对象名称
	private JButton addButton;// 添加
	private JButton removeButton;// 移除
	private JButton sureButton;// 确定
	private JButton cancelButton;// 取消
	private InterfaceAIFComponent comp1;
	private InterfaceAIFComponent comp2;
	//InterfaceAIFComponent comp, TCSession session,Tree05 tree,TCComponentItem target
	public DDGYDialog(TCSession session) throws IOException {
		this.session = session;
		//this.nameString="test";
		init();
		
	}
//	public DDGYSOPDialog(TCSession session) throws IOException {
//		this.session = session;
//		init();
//	}

	private void init()  {
		this.setTitle("对比关联");
		this.setSize(500, 500);
		this.setLayout(new BorderLayout()); 
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setAlwaysOnTop(true);
		//源目标
		JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panel1.setLayout(new FlowLayout(FlowLayout.LEADING,15,15));
		this.headLabel=new JLabel("源目标");
		this.headLabel.setPreferredSize(new Dimension(300,20));
		String name1="";
		try {
			comp1=AIFUtility.getCurrentApplication().getTargetComponent();
			if(((TCComponentBOMLine)comp1).getItem().getType().equals("ML8_OrdMBOMItem")||((TCComponentBOMLine)comp1).getItem().getType().equals("ML8_OrderProcess")) {
				name1=((TCComponentBOMLine)comp1).getStringProperty("bl_line_name");
			}else {
				MessageBox.post("请选择订单BOM或订单工艺！", "错误", MessageBox.ERROR);
				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.headName=new JTextArea(name1);
		this.headName.setPreferredSize(new Dimension(350,40));
		this.headName.setLineWrap(true);
		this.headName.setWrapStyleWord(true);
		this.headName.setEditable(false);
		this.headName.setOpaque(true);
		this.headName.setBackground(new Color(220,220,220));
		panel1.add(this.headLabel);
		panel1.add(this.headName);
		panel1.setPreferredSize(new Dimension(100,150));
		
		
		this.targetLabel=new JLabel("目标对象");
		this.targetLabel.setPreferredSize(new Dimension(300,20));
		this.targetName=new JTextArea();
		this.targetName.setPreferredSize(new Dimension(350,40));
		this.targetName.setLineWrap(true);
		this.targetName.setWrapStyleWord(true);
		this.targetName.setEditable(false);
		this.targetName.setOpaque(true);
		this.targetName.setBackground(new Color(220,220,220));
		panel2.add(this.targetLabel);
		panel2.add(this.targetName);
		panel2.setPreferredSize(new Dimension(100,200));

		
		this.addButton=new JButton("添加");
		this.removeButton=new JButton("移除");
		panel3.add(this.addButton);
		panel3.add(this.removeButton);
		panel3.setPreferredSize(new Dimension(100,200));
		
		this.sureButton=new JButton("确定");
		this.cancelButton=new JButton("取消");
		panel4.add("1.1.left.top",this.sureButton);
		panel4.add("1.2.left.top",this.cancelButton);
		panel4.setPreferredSize(new Dimension(100,200));
		//添加监听
		createActionEvent();
		
		this.add(panel1,BorderLayout.NORTH);
		this.add(panel2,BorderLayout.CENTER);
		this.add(panel3,BorderLayout.EAST);
		this.add(panel4,BorderLayout.SOUTH);
		this.setVisible(true);
	}

	// 添加监听
	public void createActionEvent() {
		this.addButton.addActionListener(this);
		this.removeButton.addActionListener(this);
		this.sureButton.addActionListener(this);
		this.cancelButton.addActionListener(this);
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source.equals(this.addButton)) {
				comp2=AIFUtility.getCurrentApplication().getTargetComponent();
				String name2="";
				try {
					if(comp2 instanceof TCComponentBOMLine&&
							(((TCComponentBOMLine)comp2).getItem().getType().equals("ML8_OrdMBOMItem")||
									((TCComponentBOMLine)comp2).getItem().getType().equals("ML8_OrderProcess"))) {
						
							if(((TCComponentBOMLine)comp1).getItem().getType().equals(((TCComponentBOMLine)comp2).getItem().getType())||
									((TCComponentBOMLine)comp1).getItem().getType()==((TCComponentBOMLine)comp2).getItem().getType()) {
								this.setAlwaysOnTop(false);
								MessageBox.post("请勿选择同类型关联对象", "错误", MessageBox.ERROR);	
								this.setAlwaysOnTop(true);
								return;
								}else {
									name2=((TCComponentBOMLine)comp2).getStringProperty("bl_line_name");
								}
					}else {
						comp2=null;
						this.setAlwaysOnTop(false);
						MessageBox.post("请在结构管理器或制造工艺规划器中选择订单BOM或订单工艺！", "错误", MessageBox.ERROR);
						this.setAlwaysOnTop(true);
						return;
					}
					
					this.targetName.setText(name2);
				} catch (TCException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			else if(source.equals(this.removeButton)) {
				comp2=null;
				this.targetName.setText("");
			}
			else if(source.equals(this.sureButton)) {
				String[] Names = {headName.getText(),targetName.getText()};
				
				if(comp1!=null&&comp2!=null) {
					this.dispose();
					DDGYFrame sj=new DDGYFrame(session,comp1,comp2,Names);
				}else{
					this.setAlwaysOnTop(false);
					MessageBox.post("请选择对比目标", "错误", MessageBox.ERROR);	
					this.setAlwaysOnTop(true);
					return;
				}
				
			}
			else if(source.equals(this.cancelButton)) {
				dispose();
			}
		
	}


	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub

		super.dispose();
	}
	
	
}
