KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > FileDialog


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: FileDialog.java,v $
11    Revision 1.1 2004/03/23 09:58:56 bobintetley
12    SystemColor and FileDialog implementation, plus JOptionPane.showOptionDialog support
13
14
15 */

16
17 package swingwt.awt;
18
19 import swingwtx.swing.*;
20
21 import java.io.File JavaDoc;
22 import java.io.FileFilter JavaDoc;
23
24 /**
25  * Wrapper around JFileChooser to enable
26  * AWT compatibility.
27  *
28  * @author Robin Rawson-Tetley
29  */

30 public class FileDialog extends Dialog {
31     
32     public static final int LOAD = 0;
33     public static final int SAVE = 1;
34     
35     protected int mode = LOAD;
36     protected JFileChooser swingFileChooser = null;
37     protected Component parent = null;
38     protected String JavaDoc file = null;
39     protected String JavaDoc dir = null;
40     protected FileFilter JavaDoc filter = null;
41     
42     public FileDialog(Frame parent) { this(parent, "Open", LOAD); }
43     public FileDialog(Frame parent, String JavaDoc title) { this(parent, title, LOAD); }
44     
45     public FileDialog(Frame parent, String JavaDoc title, int mode) {
46         swingFileChooser = new JFileChooser();
47         swingFileChooser.setTitle(title);
48         this.mode = mode;
49         this.parent = parent;
50     }
51     
52     public void show() {
53         setVisible(true);
54     }
55     public void setVisible(boolean b) {
56         if (!b) return;
57         int result = 0;
58         if (mode == LOAD)
59             result = swingFileChooser.showOpenDialog(parent);
60         else
61             result = swingFileChooser.showSaveDialog(parent);
62         if (result == JFileChooser.APPROVE_OPTION) {
63             file = swingFileChooser.getSelectedFile().getAbsolutePath();
64             dir = swingFileChooser.getSelectedFile().getPath();
65         }
66         else {
67             file = null;
68             dir = null;
69         }
70     }
71     
72     public String JavaDoc getDirectory() {
73         return dir;
74     }
75     
76     public String JavaDoc getFile() {
77         return file;
78     }
79     
80     public FileFilter JavaDoc getFilenameFilter() {
81         return filter;
82     }
83     
84     public void setFilenameFilter(FileFilter JavaDoc f) {
85         filter = f;
86     }
87     
88     public void setDirectory(String JavaDoc dir) {
89         swingFileChooser.setSelectedFile(new File JavaDoc(dir));
90     }
91     
92     public void setFile(String JavaDoc file) {
93         swingFileChooser.setSelectedFile(new File JavaDoc(file));
94     }
95     
96     public int getMode() {
97         return mode;
98     }
99     
100     public void setMode(int mode) {
101         this.mode = mode;
102     }
103 }
104
Popular Tags