summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
blob: 041a9a2570599cf7fe429e125ab553fcd1a28efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package bjc.utils.gui.awt;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FilenameFilter;

import bjc.utils.gui.SimpleDialogs;

/**
 * A simple way to get the user to pick a file
 * 
 * Built for AWT.
 * 
 * @author ben
 *
 */
public class SimpleFileDialog {
	/**
	 * Prompt the user to pick a file to open
	 * 
	 * @param parent
	 *                The parent of the file picker
	 * @param title
	 *                The title of the file picker
	 * @return The file the user picked
	 */
	public static File getOpenFile(Frame parent, String title) {
		return getOpenFile(parent, title, (String[]) null);
	}

	/**
	 * Prompt the user to pick a file to open
	 * 
	 * @param parent
	 *                The parent of the file picker
	 * @param title
	 *                The title of the file picker
	 * @param extensions
	 *                The extensions to accept as valid
	 * @return The file the user picked
	 */
	public static File getOpenFile(Frame parent, String title, String... extensions) {
		if (parent == null) {
			throw new NullPointerException("Parent must not be null");
		} else if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		FileDialog chooser = new FileDialog(parent, title, FileDialog.LOAD);

		if (extensions != null) {
			FilenameFilter filter = new ExtensionFileFilter(extensions);
			chooser.setFilenameFilter(filter);
		}

		chooser.setVisible(true);

		while (chooser.getFile() == null) {
			SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to open.");
			chooser.setVisible(true);
		}

		return chooser.getFiles()[0];
	}

	/**
	 * Prompt the user to pick a file to open
	 * 
	 * @param parent
	 *                The parent of the file picker
	 * @param title
	 *                The title of the file picker
	 * @param extensions
	 *                The extensions to accept as valid
	 * @return The file the user picked
	 */
	public static File[] getOpenFiles(Frame parent, String title, String... extensions) {
		if (parent == null) {
			throw new NullPointerException("Parent must not be null");
		} else if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		FileDialog chooser = new FileDialog(parent, title, FileDialog.LOAD);

		if (extensions != null) {
			FilenameFilter filter = new ExtensionFileFilter(extensions);
			chooser.setFilenameFilter(filter);
		}

		chooser.setMultipleMode(true);
		chooser.setVisible(true);

		while (chooser.getFile() == null) {
			SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to open.");
			chooser.setVisible(true);
		}

		return chooser.getFiles();
	}

	/**
	 * Prompt the user to pick a file to save
	 * 
	 * @param parent
	 *                The parent of the file picker
	 * @param title
	 *                The title of the file picker
	 * @return The file the user picked
	 */
	public static File getSaveFile(Frame parent, String title) {
		return getSaveFile(parent, title, (String[]) null);
	}

	/**
	 * Prompt the user to pick a file to save
	 * 
	 * @param parent
	 *                The parent of the file picker
	 * @param title
	 *                The title of the file picker
	 * @param extensions
	 *                The extensions to accept as valid
	 * @return The file the user picked
	 */
	public static File getSaveFile(Frame parent, String title, String... extensions) {
		if (parent == null) {
			throw new NullPointerException("Parent must not be null");
		} else if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		FileDialog chooser = new FileDialog(parent, title, FileDialog.SAVE);

		if (extensions != null) {
			FilenameFilter filter = new ExtensionFileFilter(extensions);
			chooser.setFilenameFilter(filter);
		}

		chooser.setVisible(true);

		while (chooser.getFile() == null) {
			SimpleDialogs.showError(parent, "File I/O Error", "Please choose a file to save to.");
			chooser.setVisible(true);
		}

		return chooser.getFiles()[0];
	}
}