package com.langtech.plm.performancemanagement.dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import com.langtech.plm.performancemanagement.operations.TaskFolderMatchingOperation;
import com.langtech.plm.performancemanagement.util.ProgressBarThread;
import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.PropertyLayout;

public class TaskFolderMatchingDialog extends AbstractAIFDialog implements ActionListener {
	private AbstractAIFApplication app;
	private TCSession session;
	private JLabel fileLabel;
	private JTextField fileField;
	private JButton searchButton;
	private JButton okButton;
	private JButton celButton;
	ProgressBarThread wait;
	TCComponent target;
	private JFileChooser jFileChooser;

//	不同于一般的构造方法，尤其是参数那儿
	public TaskFolderMatchingDialog(AbstractAIFApplication app, TCComponent target) {
		super(false);// 千万别忘记
		this.app = app;
		this.session = (TCSession) app.getSession();
		this.target = target;
	}

	public void initUI() {
		FileSystemView fsv = FileSystemView.getFileSystemView();
		String deskPath = fsv.getHomeDirectory().getPath();
		this.setSize(new Dimension(500, 300));
		this.setTitle("导出绩效报表");
		this.fileLabel = new JLabel("导出路径:");
		this.fileField = new JTextField(deskPath);
		this.fileField.setEditable(false);
		this.searchButton = new JButton("浏览...");
		this.searchButton.addActionListener(this);
		this.okButton = new JButton("确定");
		this.okButton.addActionListener(this);
		this.celButton = new JButton("取消");
		this.celButton.addActionListener(this);
		JPanel centerJPanel = new JPanel(new PropertyLayout());
		centerJPanel.add("1.1.left.top", this.fileLabel);
		centerJPanel.add("1.2.left.top", this.fileField);
		centerJPanel.add("1.3.left.top", this.searchButton);
		JPanel rootJPanel = new JPanel(new FlowLayout());
		rootJPanel.add(okButton);
		rootJPanel.add(celButton);
		this.setLayout(new BorderLayout());
		this.add(centerJPanel, BorderLayout.CENTER);
		this.add(rootJPanel, BorderLayout.SOUTH);
		this.centerToScreen();
		this.pack();
		this.showDialog();
//		初始化文件选择器
		this.jFileChooser = new JFileChooser();
//		设置当前打开文件选择器的默认的路径
		this.jFileChooser.setCurrentDirectory(new File(deskPath));
	}

	@Override
	public void run() {
		initUI();
	}

	public void selectFileButtonEvent() {
		// 设定只能选择到文件
		jFileChooser.setFileSelectionMode(1);// 只能选择到路径
		// 此句是打开文件选择器界面的触发语句
		int state = jFileChooser.showOpenDialog(null);
		if (state == 1) {
			return;
		} else {
			// f为选择到的目录,核心代码
			File f = jFileChooser.getSelectedFile();
			fileField.setText(f.getAbsolutePath());
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		Object sourceObj = arg0.getSource();
		if (sourceObj.equals(this.searchButton)) {
			selectFileButtonEvent();
		} else if (sourceObj.equals(this.okButton)) {
			this.disposeDialog();
			this.dispose();
			String filePath = this.fileField.getText();
			wait = new ProgressBarThread(" ", "执行中，请稍等...");
			wait.start();
			TaskFolderMatchingOperation operation = new TaskFolderMatchingOperation(filePath, target, session, wait);
			this.session.queueOperation(operation);
		} else {
			this.disposeDialog();
			this.dispose();
		}
	}
}
