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: ExtensionFileFilter.java,v 1.5 2007/11/02 17:50:36 taqua Exp $
036 *
037 * Changes (from 26-Oct-2001)
038 * --------------------------
039 * 26-Oct-2001 : Changed package to com.jrefinery.ui.* (DG);
040 * 26-Jun-2002 : Updated imports (DG);
041 * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042 *
043 */
044 package org.jfree.ui;
045
046 import java.io.File;
047 import javax.swing.filechooser.FileFilter;
048
049 /**
050 * A filter for JFileChooser that filters files by extension.
051 *
052 * @author David Gilbert
053 */
054 public class ExtensionFileFilter extends FileFilter {
055
056 /** A description for the file type. */
057 private String description;
058
059 /** The extension (for example, "png" for *.png files). */
060 private String extension;
061
062 /**
063 * Standard constructor.
064 *
065 * @param description a description of the file type;
066 * @param extension the file extension;
067 */
068 public ExtensionFileFilter(final String description, final String extension) {
069 this.description = description;
070 this.extension = extension;
071 }
072
073 /**
074 * Returns true if the file ends with the specified extension.
075 *
076 * @param file the file to test.
077 *
078 * @return A boolean that indicates whether or not the file is accepted by the filter.
079 */
080 public boolean accept(final File file) {
081
082 if (file.isDirectory()) {
083 return true;
084 }
085
086 final String name = file.getName().toLowerCase();
087 if (name.endsWith(this.extension)) {
088 return true;
089 }
090 else {
091 return false;
092 }
093
094 }
095
096 /**
097 * Returns the description of the filter.
098 *
099 * @return a description of the filter.
100 */
101 public String getDescription() {
102 return this.description;
103 }
104
105 }