/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  GetChoosePath.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-17      下午03:45:21    tyl  	Ini	            初始化文件
#=======================================================================================================
*/
package cn.com.customization.util;

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 {

	public String getPath(String filename) {
		String tempPath = System.getProperty("java.io.tmpdir");
		File tempFile = new File(tempPath +  filename);
		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.SAVE_DIALOG);
		jfc.setInheritsPopupMenu(true);
//		jfc.setDialogTitle("...");
		jfc.setMultiSelectionEnabled(false);

		jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
			@Override
			public boolean accept(File f) {
				if (f.isDirectory())
					return true;
				else
					return false;
			}

			@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 file = jfc.getSelectedFile();
			return file.getAbsolutePath();
		}
		return "";
	}
}