001 /* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ------------------------
028 * ExtensionFileFilter.java
029 * ------------------------
030 * (C) Copyright 2000-2004, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * $Id: FilesystemFilter.java,v 1.6 2008/09/10 09:26:11 mungady Exp $
036 *
037 * Changes (from 26-Oct-2001)
038 * --------------------------
039 * 01-Jun-2005 : Updated javadoc.
040 */
041 package org.jfree.ui;
042
043 import java.io.File;
044 import java.io.FilenameFilter;
045 import javax.swing.filechooser.FileFilter;
046
047 /**
048 * A filesystem filter.
049 *
050 * @author David Gilbert
051 */
052 public class FilesystemFilter extends FileFilter implements FilenameFilter {
053
054 /** The file extension, which should be accepted. */
055 private String[] fileext;
056 /** The filter description. */
057 private String descr;
058 /** A flag indicating whether to accept directories. */
059 private boolean accDirs;
060
061 /**
062 * Creates a new filter.
063 *
064 * @param fileext the file extension.
065 * @param descr the description.
066 */
067 public FilesystemFilter(final String fileext, final String descr) {
068 this(fileext, descr, true);
069 }
070
071 /**
072 * Creates a new filter.
073 *
074 * @param fileext the file extension.
075 * @param descr the description.
076 * @param accDirs accept directories?
077 */
078 public FilesystemFilter(final String fileext, final String descr,
079 final boolean accDirs) {
080 this(new String[]{fileext}, descr, accDirs);
081 }
082
083 /**
084 * Creates a new filter.
085 *
086 * @param fileext the file extension.
087 * @param descr the description.
088 * @param accDirs accept directories?
089 * @throws NullPointerException if the file extensions are null.
090 */
091 public FilesystemFilter(final String[] fileext, final String descr,
092 final boolean accDirs) {
093 this.fileext = (String[]) fileext.clone();
094 this.descr = descr;
095 this.accDirs = accDirs;
096 }
097
098
099 /**
100 * Returns <code>true</code> if the file is accepted, and <code>false</code> otherwise.
101 *
102 * @param dir the directory.
103 * @param name the file name.
104 * @return A boolean.
105 */
106 public boolean accept(final File dir, final String name) {
107 final File f = new File(dir, name);
108 if (f.isDirectory() && acceptsDirectories()) {
109 return true;
110 }
111
112 for (int i = 0; i < this.fileext.length; i++) {
113 if (name.endsWith(this.fileext[i])) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 /**
121 * Returns <code>true</code> if the specified file matches the requirements of this
122 * filter, and <code>false</code> otherwise.
123 *
124 * @param dir the file or directory.
125 * @return A boolean.
126 */
127 public boolean accept(final File dir) {
128 if (dir.isDirectory() && acceptsDirectories()) {
129 return true;
130 }
131
132 for (int i = 0; i < this.fileext.length; i++) {
133 if (dir.getName().endsWith(this.fileext[i])) {
134 return true;
135 }
136 }
137 return false;
138 }
139
140 /**
141 * Returns the filter description.
142 *
143 * @return The filter description.
144 */
145 public String getDescription() {
146 return this.descr;
147 }
148
149 /**
150 * Sets the flag that controls whether or not the filter accepts directories.
151 *
152 * @param b a boolean.
153 */
154 public void acceptDirectories(final boolean b) {
155 this.accDirs = b;
156 }
157
158 /**
159 * Returns the flag that indicates whether or not the filter accepts directories.
160 *
161 * @return A boolean.
162 */
163 public boolean acceptsDirectories() {
164 return this.accDirs;
165 }
166
167 }