summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java
blob: f39ff7c06946a005f2cfc85d65fe9593f014dd22 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package bjc.utils.gui;

import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;

import bjc.utils.exceptions.FileNotChosenException;

/**
 * Utility class for easily prompting user for files.
 * 
 * Built for Swing.
 * 
 * @author ben
 *
 */
public class SimpleFileChooser {
	private static File doOpenFile(Component parent, String title,
			JFileChooser files) {
		if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		files.setDialogTitle(title);

		boolean success = false;

		while (!success) {
			try {
				maybeDoOpenFile(parent, files);

				success = true;
			} catch (FileNotChosenException e) {
				SimpleDialogs.showError(parent, "I/O Error",
						"Please pick a file to open");
			}
		}

		return files.getSelectedFile();
	}

	private static File doSaveFile(Component parent, String title,
			JFileChooser files) {
		if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		files.setDialogTitle(title);

		boolean success = false;

		while (!success) {
			try {
				maybeDoSaveFile(parent, files);

				return files.getSelectedFile();
			} catch (FileNotChosenException e) {
				SimpleDialogs.showError(parent, "I/O Error",
						"Please pick a file to save to");
			}
		}

		return files.getSelectedFile();
	}

	/**
	 * Prompt the user with a "Open File..." dialog. Keeps prompting them
	 * until they pick a file.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @return The file the user has chosen.
	 */
	public static File getOpenFile(Component parent, String title) {
		JFileChooser files = new JFileChooser();

		return doOpenFile(parent, title, files);
	}

	/**
	 * Prompt the user with a "Open File..." dialog. Keeps prompting them
	 * until they pick a file.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @param extensions
	 *            The list of file extensions the file should have.
	 * @return The file the user has chosen.
	 */
	public static File getOpenFile(Component parent, String title,
			String... extensions) {
		JFileChooser files = new JFileChooser();

		files.addChoosableFileFilter(new ExtensionFileFilter(extensions));

		return doOpenFile(parent, title, files);
	}

	/**
	 * Prompt the user with a "Save File..." dialog.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @return The file the user chose.
	 */
	public static File getSaveFile(Component parent, String title) {
		JFileChooser files = new JFileChooser();

		return doSaveFile(parent, title, files);
	}

	/**
	 * Prompt the user with a "Save File..." dialog.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @param extensions
	 *            The extensions of the files the user can choose.
	 * @return The file the user chose.
	 */
	public static File getSaveFile(Component parent, String title,
			String... extensions) {
		JFileChooser files = new JFileChooser();

		files.addChoosableFileFilter(new ExtensionFileFilter(extensions));

		return doSaveFile(parent, title, files);
	}

	private static void maybeDoOpenFile(Component parent,
			JFileChooser files) throws FileNotChosenException {
		if (parent == null) {
			throw new NullPointerException("Parent must not be null");
		} else if (files == null) {
			throw new NullPointerException(
					"File chooser must not be null");
		}

		int dialogResult = files.showSaveDialog(parent);

		if (dialogResult != JFileChooser.APPROVE_OPTION) {
			throw new FileNotChosenException();
		}
	}

	private static void maybeDoSaveFile(Component parent,
			JFileChooser files) throws FileNotChosenException {
		if (parent == null) {
			throw new NullPointerException("Parent must not be null");
		} else if (files == null) {
			throw new NullPointerException(
					"File chooser must not be null");
		}

		int dialogResult = files.showSaveDialog(parent);

		if (dialogResult != JFileChooser.APPROVE_OPTION) {
			throw new FileNotChosenException();
		}
	}

	/**
	 * Prompt the user with a "Open File..." dialog.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @return The file if the user chose one or null if they didn't.
	 */
	public static File maybeOpenFile(Component parent, String title) {
		if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		JFileChooser files = new JFileChooser();
		files.setDialogTitle(title);

		try {
			maybeDoOpenFile(parent, files);
		} catch (FileNotChosenException e) {
		}

		return files.getSelectedFile();
	}

	/**
	 * Prompt the user with a "Save File..." dialog.
	 * 
	 * @param parent
	 *            The component to use as the parent for the dialog.
	 * @param title
	 *            The title of the dialog to prompt with.
	 * @return The file if the user chose one or null if they didn't.
	 */
	public static File maybeSaveFile(Component parent, String title) {
		if (title == null) {
			throw new NullPointerException("Title must not be null");
		}

		JFileChooser files = new JFileChooser();
		files.setDialogTitle(title);

		try {
			maybeDoSaveFile(parent, files);
		} catch (FileNotChosenException e) {
		}

		return files.getSelectedFile();
	}
}