/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  GetChoosePath.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-18      下午04:59:35    tyl  	Ini	            初始化文件
#=======================================================================================================
*/
package cn.com.customization.imports.importfiles;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;

public class GetChoosePath {

	/**
	 * Creates a new instance of GetChoosePath.
	 */
	private List<String>	fileTypeList	= new ArrayList<String>();
	boolean					isImportFolder;

	public GetChoosePath(AbstractAIFUIApplication app, List<String> filterFileTypes, boolean isImportFolder) {
		this.fileTypeList = filterFileTypes;
		this.isImportFolder = isImportFolder;
	}

	/**
	 * getChoosePath:得到用户所选的路径
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public List<String> getChoosePath() {
		List<String> choosePath = new ArrayList<String>();
//		System.out.println("-----------------------------------------------------0");
		String tempPath = System.getProperty("java.io.tmpdir");
		System.out.println("tempPath="+tempPath);
		File tempFile = new File(tempPath + "\\batchimportfile.txt");
		JFileChooser jfc = null;
		if (!tempFile.exists()) {
			try {
				tempFile.createNewFile();
				jfc = new JFileChooser();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			if (tempFile.exists()) {
				BufferedReader in;
				try {
					in = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
					String line;
					try {
						line = in.readLine();
						if ((line != null) && (line.length() > 0)) {
							File file = new File(line);
							if (file.exists()) {
								jfc = new JFileChooser(file);
							} else {
								jfc = new JFileChooser();
							}
						} else {
							jfc = new JFileChooser();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		}
		jfc.setFileSelectionMode(JFileChooser.CUSTOM_DIALOG);
		jfc.setInheritsPopupMenu(true);
		jfc.setDialogTitle("导入数据集");
		jfc.setMultiSelectionEnabled(true);

		if ((fileTypeList != null) && (fileTypeList.size() > 0)) {
			jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
				@Override
				public boolean accept(File f) {
					if (f.isDirectory())
						return true;
					String name = f.getName();
					int p = name.lastIndexOf('.');
					if (p == -1)
						return false;
					String suffix = name.substring(p + 1).toLowerCase();
					return fileTypeList.contains(suffix);
				}

				@Override
				public String getDescription() {
					return null;
				}
			});
		}

		int openOrCancel = jfc.showSaveDialog(null);
		if (openOrCancel == 0) {
			try {
				BufferedWriter output;
				output = new BufferedWriter(new FileWriter(tempFile));
				output.write(jfc.getSelectedFile().getAbsolutePath());
				output.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			File[] files = jfc.getSelectedFiles();
			for (int i = 0; i < files.length; i++) {
				choosePath.add(files[i].getAbsolutePath());
			}
		}
		return choosePath;
	}
}
