/*
#=============================================================================
#																			   
#			copyright (c) 2009 Origin Enterprise Solution LTD.		   
#																			   
#=============================================================================
# File name: GetChoosePath.java
# File description: 										   	
#=============================================================================
#	Date		Name		    Action	Description of Change					   
#	2009-11-23	tyl  	                      创建		 得到用户所选路径						   
#=============================================================================
 */

package cn.com.origin.autocodemanager.common;

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 javax.swing.JFileChooser;

public class GetChoosePath {
	private String	title;

	public GetChoosePath(String title) {
		this.title = title;
	}

	/**
	 * getChoosePath:得到用户所选的路径
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public File getChooseFile() {
//		List<String> choosePath = new ArrayList<String>();
		String tempPath = System.getProperty("java.io.tmpdir");
		File tempFile = new File(tempPath + "\\importjobshop.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(title);
		jfc.setMultiSelectionEnabled(true);

		int openOrCancel = jfc.showSaveDialog(null);
		File file = 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());
			// }
			file = jfc.getSelectedFile();
		}
		// return choosePath;
		return file;
	}
}
