package me.coley.recaf.util;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FilePrompt {
	private static JFileChooser load, save;

	/**
	 * @return The file chooser. If it is null it is instantiated and set to the
	 *         working directory with a filter for jar files.
	 */
	public static JFileChooser getLoader() {
		return getLoader("Java Programs", "jar", "class");
	}

	/**
	 * @return The file chooser. If it is null it is instantiated and set to the
	 *         working directory with a filter for the given file type. To allow
	 *         any type, have the parameters be null.
	 *
	 * @param fileType
	 *            Name of the type of file.
	 * @param extension
	 *            Actual file extension.
	 */
	public static JFileChooser getLoader(String fileType, String... extension) {
		if (load == null) {
			load = new JFileChooser();
			String dir = System.getProperty("user.dir");
			File fileDir = new File(dir);
			load.setDialogTitle("Open File");
			load.setCurrentDirectory(fileDir);
		}
		if (fileType == null || extension.length == 0) {
			load.setFileFilter(null);
		} else {
			FileNameExtensionFilter filter = new FileNameExtensionFilter(fileType, extension);
			load.setFileFilter(filter);
		}
		return load;
	}

	/**
	 * @return The file chooser. If it is null it is instantiated and set to the
	 *         working directory with a filter for jar files.
	 */
	public static JFileChooser getSaver() {
		return getLoader("Java Programs", "jar", "class");
	}
	/**
	 * Creates and returns a file chooser set in the working directory.
	 *
	 * @return The file chooser.
	 */
	public static JFileChooser getSaver(String fileType, String extension) {
		if (save == null) {
			save = new JFileChooser();
			String dir = System.getProperty("user.dir");
			File fileDir = new File(dir);
			save.setDialogTitle("Save File");
			save.setCurrentDirectory(fileDir);
		}
		if (fileType == null || extension == null) {
			save.setFileFilter(null);
		} else {
			FileNameExtensionFilter filter = new FileNameExtensionFilter(fileType, extension);
			save.setFileFilter(filter);
		}
		return save;
	}
}
@@ -1069,35 +1069,32 @@
 tory(fileDir);%0A%09
-%09%7D%0A
 %09%09if (fileType =
@@ -1124,24 +1124,25 @@
 gth == 0) %7B%0A
+%09
 %09%09%09load.setF
@@ -1152,32 +1152,33 @@
 Filter(null);%0A%09%09
+%09
 %7D else %7B%0A%09%09%09File
@@ -1162,32 +1162,33 @@
 l);%0A%09%09%09%7D else %7B%0A
+%09
 %09%09%09FileNameExten
@@ -1257,16 +1257,17 @@
 nsion);%0A
+%09
 %09%09%09load.
@@ -1281,32 +1281,80 @@
 Filter(filter);%0A
+%09%09%09%7D%0A%09%09%09load.setAcceptAllFileFilterUsed(false);%0A
 %09%09%7D%0A%09%09return loa
@@ -2000,19 +2000,16 @@
 eDir);%0A%09
-%09%7D%0A
 %09%09if (fi
@@ -2046,24 +2046,25 @@
  null) %7B%0A%09%09%09
+%09
 save.setFile
@@ -2079,16 +2079,17 @@
 ull);%0A%09%09
+%09
 %7D else %7B
@@ -2089,16 +2089,17 @@
  else %7B%0A
+%09
 %09%09%09FileN
@@ -2175,24 +2175,25 @@
 ension);%0A%09%09%09
+%09
 save.setFile
@@ -2208,16 +2208,21 @@
 ilter);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09re

Fix #43, Fix duplicate filter entries when opening/saving files