package cust.com.rac.changename;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;

public class ChangeNameDialog  extends AbstractAIFDialog implements ActionListener {

	private TCSession session;
	public JTextField srcIdText;
	public JTextField newIdText;
	public JButton okBtn;
	public JButton cancelBtn;
	public TCComponent targetComponent;
	public String srcName;
	public ChangeNameDialog(Frame arg0, String arg1, boolean arg2) {
		super(arg0, arg1, arg2);
		session=(TCSession) AIFUtility.getCurrentApplication().getSession();
		targetComponent=(TCComponent) AIFUtility.getCurrentApplication().getTargetComponent();
		try {
			srcName=targetComponent.getProperty("object_name");
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initUI();
		setVisible(true);
	}
	
	public void initUI(){
		JPanel panel1 = new JPanel(new PropertyLayout(20,20,20,20,20,20));
		srcIdText=new JTextField(23);
		srcIdText.setEditable(false);
		srcIdText.setText(srcName);
		newIdText=new JTextField(23);
		panel1.add("1.1.left",new JLabel("原名称:"));
		panel1.add("1.2.left",srcIdText);
		panel1.add("2.1.left",new JLabel("更改为:"));
		panel1.add("2.2.left",newIdText);
		JPanel panel2 = new JPanel(new FlowLayout());
		okBtn = new JButton("确定");
		cancelBtn = new JButton("取消");
		okBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		panel2.add(okBtn);
		panel2.add(cancelBtn);
		getContentPane().add(BorderLayout.CENTER, panel1);
		getContentPane().add(BorderLayout.SOUTH, panel2);
		centerToScreen();
		pack();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == okBtn){
			String newNameString=newIdText.getText().trim();
			if("".equals(newNameString)){
				MessageBox.post("请输入更改后名称！", "提示", 4);
				return;
			}
			try {
				targetComponent.setProperty("object_name", newNameString);
			} catch (TCException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				MessageBox.post("重命名失败！", "提示", 4);
			}
			this.setVisible(false);
			this.dispose();
		}else if(e.getSource() == cancelBtn){
			this.setVisible(false);
			this.dispose();
		}
	}

}

