-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileChooser.java
executable file
·125 lines (106 loc) · 3.29 KB
/
FileChooser.java
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
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.util.Properties;
import java.io.*;
import java.net.*;
/**
* A class to make working with a file chooser easier
* for students. It uses a JFileChooser to let the user
* pick a file and returns the chosen file name.
*
* @author Barb Ericson [email protected]
*/
public class FileChooser
{
/////////////////////// methods /////////////////////////////
/**
* Method to get the full path for the passed file name
* @param fileName the name of a file
* @return the full path for the file
*/
public static String getMediaPath(String fileName)
{
String path = null;
String directory = getMediaDirectory();
boolean done = true;
// get the full path
path = directory + fileName;
return path;
}
/**
* Method to pick an item using the file chooser
* @param fileChooser the file Chooser to use
* @return the path name
*/
public static String pickPath(JFileChooser fileChooser)
{
String path = null;
/* create a JFrame to be the parent of the file
* chooser open dialog if you don't do this then
* you may not see the dialog.
*/
JFrame frame = new JFrame();
frame.setAlwaysOnTop(true);
// get the return value from choosing a file
int returnVal = fileChooser.showOpenDialog(frame);
// if the return value says the user picked a file
if (returnVal == JFileChooser.APPROVE_OPTION)
path = fileChooser.getSelectedFile().getPath();
return path;
}
/**
* Method to let the user pick a file and return
* the full file name as a string. If the user didn't
* pick a file then the file name will be null.
* @return the full file name of the picked file or null
*/
public static String pickAFile()
{
JFileChooser fileChooser = null;
// start off the file name as null
String fileName = null;
// get the current media directory
String mediaDir = getMediaDirectory();
/* create a file for this and check that the directory exists
* and if it does set the file chooser to use it
*/
try {
File file = new File(mediaDir);
if (file.exists())
fileChooser = new JFileChooser(file);
} catch (Exception ex) {
}
// if no file chooser yet create one
if (fileChooser == null)
fileChooser = new JFileChooser();
// pick the file
fileName = pickPath(fileChooser);
return fileName;
}
/**
* Method to get the directory for the media
* @return the media directory
*/
public static String getMediaDirectory()
{
String directory = null;
boolean done = false;
File dirFile = null;
// try to find the images directory
try {
// get the URL for where we loaded this class
Class currClass = Class.forName("FileChooser");
URL classURL = currClass.getResource("FileChooser.class");
URL fileURL = new URL(classURL,"images/");
directory = fileURL.getPath();
directory = URLDecoder.decode(directory, "UTF-8");
dirFile = new File(directory);
if (dirFile.exists()) {
//setMediaPath(directory);
return directory;
}
} catch (Exception ex) {
}
return directory;
}
}