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
|
package bjc.utils.gui;
import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
/**
* Utility class for easily prompting user for files.
* @author ben
*
*/
public class SimpleFileChooser {
private static File doOpenFile(Component par, String title,
JFileChooser files) {
files.setDialogTitle(title);
boolean success = false;
while (!success) {
try {
maybeDoOpenFile(par, files);
success = true;
} catch (FileNotChosenException e) {
SimpleDialogs.showError(par, "I/O Error",
"Please pick a file to open");
}
}
return files.getSelectedFile();
}
private static File doSaveFile(Component par, String title,
JFileChooser files) {
files.setDialogTitle(title);
boolean success = false;
while (!success) {
try {
maybeDoSaveFile(par, files);
return files.getSelectedFile();
} catch (FileNotChosenException e) {
SimpleDialogs.showError(par, "I/O Error",
"Please pick a file to save to");
}
}
return files.getSelectedFile();
}
private static void maybeDoSaveFile(Component par, JFileChooser files)
throws FileNotChosenException {
int res = files.showSaveDialog(par);
System.out.println("Result: " + res);
if (res != JFileChooser.APPROVE_OPTION) {
throw new FileNotChosenException();
}
}
private static void maybeDoOpenFile(Component par, JFileChooser files)
throws FileNotChosenException {
int res = files.showSaveDialog(par);
if (res != JFileChooser.APPROVE_OPTION) {
throw new FileNotChosenException();
}
}
/**
* Prompt the user with a "Open File..." dialog.
* Keeps prompting them until they pick a file.
* @param par 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 par, String title) {
JFileChooser files = new JFileChooser();
return doOpenFile(par, title, files);
}
/**
* Prompt the user with a "Open File..." dialog.
* Keeps prompting them until they pick a file.
* @param par 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 par, String title,
String... extensions) {
JFileChooser files = new JFileChooser();
files.addChoosableFileFilter(new ExtensionFileFilter(extensions));
return doOpenFile(par, title, files);
}
/**
* Prompt the user with a "Open File..." dialog.
* @param par 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 par, String title) {
JFileChooser files = new JFileChooser();
files.setDialogTitle(title);
try {
maybeDoOpenFile(par, files);
} catch (FileNotChosenException e) {
}
return files.getSelectedFile();
}
/**
* Prompt the user with a "Save File..." dialog.
* @param par 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 par, String title) {
JFileChooser files = new JFileChooser();
files.setDialogTitle(title);
try {
maybeDoSaveFile(par, files);
} catch (FileNotChosenException e) {
}
return files.getSelectedFile();
}
/**
* Prompt the user with a "Save File..." dialog.
*
* @param par 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 par, String title) {
JFileChooser files = new JFileChooser();
return doSaveFile(par, title, files);
}
/**
* Prompt the user with a "Save File..." dialog.
*
* @param par 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 par, String title,
String... extensions) {
JFileChooser files = new JFileChooser();
files.addChoosableFileFilter(new ExtensionFileFilter(extensions));
return doSaveFile(par, title, files);
}
}
|