01: /*
02: * The contents of this file are subject to the Mozilla Public License
03: * Version 1.1 (the "License"); you may not use this file except in
04: * compliance with the License. You may obtain a copy of the License at
05: * http://www.mozilla.org/MPL/
06: *
07: * Software distributed under the License is distributed on an "AS IS"
08: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
09: * License for the specific language governing rights and limitations
10: * under the License.
11: *
12: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
13: *
14: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
15: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
16: *
17: * Contributor(s):
18: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
19: *
20: * If you didn't download this code from the following link, you should check
21: * if you aren't using an obsolete version: http://www.isqlviewer.com
22: */
23: package org.isqlviewer.swing;
24:
25: import java.io.File;
26: import java.text.MessageFormat;
27:
28: import javax.swing.filechooser.FileFilter;
29:
30: /**
31: * Simple class for creating file filters.
32: * <p>
33: * This class is used in conjunction with JFileChoosers.
34: *
35: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
36: * @version 1.0
37: * @see javax.swing.JFileChooser#addChoosableFileFilter(javax.swing.filechooser.FileFilter)
38: */
39: public final class ExtensionFileFilter extends FileFilter {
40:
41: private String ext = "";
42: private String desc = "";
43:
44: /**
45: * Default constructor.
46: * <p>
47: * Creates a files using the given extension and description for the type of files this filters.
48: *
49: * @param ext is the extenstion to use like "txt"
50: * @param Desc Something that describes the file type to the user.
51: */
52: public ExtensionFileFilter(String ext, String description) {
53:
54: if (ext == null) {
55: throw new NullPointerException(
56: "File Extensions Cannont Be null");
57: }
58:
59: if (description == null) {
60: throw new NullPointerException(
61: "File Description Cannont Be null");
62: }
63: this .ext = new String(ext);
64: this .desc = MessageFormat.format("{0} (*.{1})", new Object[] {
65: description, ext });
66: }
67:
68: public File applyExtension(File original) {
69:
70: return new File(original.getParentFile(), original.getName()
71: .concat(".".concat(ext)));
72: }
73:
74: @Override
75: public boolean accept(File f) {
76:
77: if (f.canRead() || f.canWrite())
78: return (f.getName().endsWith("." + ext) || f.isDirectory() || (f
79: .isDirectory()
80: && f.isHidden() && f.getName().startsWith(".")));
81:
82: return false;
83: }
84:
85: @Override
86: public String getDescription() {
87:
88: return desc;
89: }
90: }
|