KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > FileChooser


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: FileChooser.java,v 1.6 2000/07/05 14:07:43 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 1999 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog;
31
32 // {{{ imports
33

34 import java.io.File JavaDoc;
35
36 import java.awt.Component JavaDoc;
37 import javax.swing.JFileChooser JavaDoc;
38 import javax.swing.filechooser.FileFilter JavaDoc;
39
40 import de.qfs.lib.log.Log;
41 import de.qfs.lib.log.Logger;
42
43 // }}}
44

45 /**
46  * This FileChooser implementation for log files includes convenience methods
47  * to open a FileChooser and get the chosen File.
48  */

49 public class FileChooser extends JFileChooser JavaDoc
50 {
51     // {{{ variables
52

53     /**
54      * Whether to trace method calls.
55      */

56     public static boolean TRACE = true;
57     /**
58      * The Logger used for logging.
59      */

60     private static Logger logger = new Logger (FileChooser.class);
61
62     /**
63      * The FileFilter for log files.
64      */

65     private static LSFileFilter ffls = new LSFileFilter (".lsv");
66
67     // }}}
68

69     // {{{ constructor
70

71     /**
72      * Create a new FileChooser.
73      */

74     public FileChooser ()
75     {
76     }
77
78     // }}}
79

80     // {{{ openLog
81

82     /**
83      * Bring up a FileChooser to open a logfile.
84      *
85      * @param parent The parent Component for the FileChooser.
86      *
87      * @return The chosen File.
88      */

89     public static File JavaDoc openLog(Component JavaDoc parent)
90     {
91         JFileChooser JavaDoc fc = new JFileChooser JavaDoc (new File JavaDoc ("."));
92
93         fc.setApproveButtonText
94             (App.getResources().getString("dialog.open.name"));
95         fc.setApproveButtonMnemonic
96             (App.getResources().getString("dialog.open.mnemonic").charAt(0));
97
98         fc.setFileFilter(ffls);
99
100         if (fc.showDialog(parent, null) == JFileChooser.APPROVE_OPTION) {
101             return fc.getSelectedFile();
102         }
103         return null;
104     }
105
106     // }}}
107
// {{{ saveLogAs
108

109     /**
110      * Bring up a FileChooser to save a logfile.
111      *
112      * @param parent The parent Component for the FileChooser.
113      *
114      * @return The chosen File.
115      */

116     public static File JavaDoc saveLogAs(Component JavaDoc parent)
117     {
118         return saveAs(parent, ffls);
119     }
120
121     // }}}
122

123     // {{{ saveAs
124

125     /**
126      * Bring up a FileChooser to save a logfile.
127      *
128      * @param parent The parent Component for the FileChooser.
129      *
130      * @return The chosen File.
131      */

132     private static File JavaDoc saveAs(Component JavaDoc parent, FileFilter JavaDoc filter)
133     {
134         JFileChooser JavaDoc fc = new JFileChooser JavaDoc (new File JavaDoc ("."));
135
136         fc.setApproveButtonText
137             (App.getResources().getString("dialog.save.name"));
138         fc.setApproveButtonMnemonic
139             (App.getResources().getString("dialog.save.mnemonic").charAt(0));
140
141         fc.setFileFilter(filter);
142
143         if (fc.showDialog(parent, null) == JFileChooser.APPROVE_OPTION) {
144             return fc.getSelectedFile();
145         }
146         return null;
147     }
148
149     // }}}
150

151     //----------------------------------------------------------------------
152
// A FileFilter implementation
153
//----------------------------------------------------------------------
154
// {{{ class LSFileFilter
155

156     /**
157      * A FileFilter that accepts directories and one type of log files.
158      */

159     private static class LSFileFilter extends FileFilter JavaDoc
160     {
161         /**
162          * The file name extension to accept.
163          */

164         private String JavaDoc ext;
165
166         // {{{ LSFileFilter
167

168         /**
169          * Create a new LSFileFilter.
170          *
171          * @param ext The file name extension to accept.
172          */

173         public LSFileFilter (String JavaDoc ext)
174         {
175             this.ext = ext;
176         }
177
178         // }}}
179
// {{{ accept
180

181         /**
182          * Accept or reject a File.
183          *
184          * @param file The file to evaluate.
185          *
186          * @return True if the file is a directory or has the required
187          * extension, false otherwise.
188          */

189         public boolean accept(File JavaDoc file)
190         {
191             return file.isDirectory() || file.getName().endsWith(ext);
192         }
193
194         // }}}
195
// {{{ getDescription
196

197         /**
198          * Get a description for the type of file this FileFilter
199          * accepts.
200          *
201          * @return A description take from the application's resources.
202          */

203         public String JavaDoc getDescription()
204         {
205             return App.getResources().getString("fileDescFor" + ext);
206         }
207
208         // }}}
209
}
210
211     // }}}
212
}
213
Popular Tags