Source code: org/mitre/cvw/CVWFileChooser.java
1 /**
2 * A file chooser dialog that implements features we want.
3 * dage 2/11/00 - need to set icon image
4 *
5 * @author S.R.Jones
6 */
7
8 package org.mitre.cvw;
9
10 import javax.swing.*;
11 import javax.swing.filechooser.*;
12 import java.io.File;
13
14 public class CVWFileChooser extends JFileChooser {
15
16 // private static String currentDir = System.getProperty("java.home");
17 private static final String[] pics = {"gif", "jpg", "tif"};
18 private static final String[] audios = {"au", "aiff", "wav"};
19 private static final String[] office = {"doc", "ppt", "xls"};
20 private static final String[] text = {"txt"};
21
22 /**
23 * Constructor that adds some filters to a standard file chooser
24 */
25 public CVWFileChooser() {
26 super();
27 // set directory and properties
28 // setCurrentDirectory(currentDir);
29
30 addChoosableFileFilter(new SimpleFileFilter(pics,
31 "Images (*.gif, *.jpg, *.tif)"));
32
33 addChoosableFileFilter(new SimpleFileFilter(audios,
34 "Sounds (*.aiff, *.au, *.wav)"));
35 addChoosableFileFilter(new SimpleFileFilter(office,
36 "MSOffice files (*.doc, *.ppt, *.xls)"));
37
38 addChoosableFileFilter(new SimpleFileFilter(text,
39 "Text files (*.txt)"));
40
41 }
42
43 /**
44 * Displays a file chooser dialog.
45 *
46 * @param type The file chooser dialog type. Can be
47 * <code>JFileChooser.OPEN_DIALOG</code> or <code>JFileChooser.SAVE_DIALOG</code>
48 * @param defaultName A default name to appear in the File name field
49 *
50 * @return The full path of the file selected.
51 */
52 public String showDialog(int type, String defaultName) {
53 int option;
54 int answer = JOptionPane.NO_OPTION;
55 File fileSelected = null;
56
57 // do some re-initialization
58 rescanCurrentDirectory();
59 if (defaultName != null) {
60 setSelectedFile(new File(getCurrentDirectory(), defaultName));
61 } else {
62 setSelectedFile(null);
63 }
64
65 setFileFilter(getAcceptAllFileFilter());
66
67 while (answer == JOptionPane.NO_OPTION) {
68 answer = JOptionPane.YES_OPTION; // will only change if user selects "NO" again.
69 if (type == JFileChooser.OPEN_DIALOG) {
70 option = showOpenDialog(new JFrame());
71 } else {
72 option = showSaveDialog(new JFrame());
73 }
74
75 if (((fileSelected = getSelectedFile()) == null) || (option == JFileChooser.CANCEL_OPTION)) {
76 return null; // nothing selected or Cancel button hit
77 }
78 else {
79 // if this is a save and the file chosen exists, confirm overwrite
80 if ((type == JFileChooser.SAVE_DIALOG) && (fileSelected.exists())) {
81 answer = JOptionPane.showConfirmDialog(null, "File already exists. Overwrite?",
82 "File exists", JOptionPane.YES_NO_OPTION);
83 }
84 }
85 }
86 return (fileSelected.getPath());
87 }
88 /** for testing (!!)
89 */
90 public static void main(String[] argv) {
91 JFrame testFrame = new JFrame("Test file chooser frame");
92 CVWFileChooser cfc = new CVWFileChooser();
93
94 cfc.showOpenDialog(testFrame);
95 }
96
97 }
98
99 /**
100 * Provides filtering for the filechooser dialog.
101 *
102 * Taken largely from: Eckstein, Robert et al.,"Java Swing", O'Reilly and Associates,
103 * Inc., 1998, p.363-364.
104 */
105 class SimpleFileFilter extends FileFilter {
106 String[] extensions;
107 String description;
108
109 public SimpleFileFilter(String ext) {
110 this (new String[] {ext}, null);
111 }
112
113 public SimpleFileFilter(String[] exts, String descr) {
114 extensions = new String[exts.length];
115 for (int i = exts.length - 1; i >= 0; i--) {
116 extensions[i] = exts[i].toLowerCase();
117 }
118 description = (descr == null ? exts[0] + " files" : descr);
119 }
120
121 public boolean accept(File f) {
122 if (f.isDirectory()) {return true; }
123
124 // sort by the extensions
125 String name = f.getName().toLowerCase();
126 for (int i = extensions.length - 1; i >= 0; i--) {
127 if (name.endsWith(extensions[i])) {
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135 public String getDescription() {return description; }
136
137 }