001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit.swing;
025:
026: import jacareto.toolkit.io.FileUtilities;
027:
028: import java.io.File;
029:
030: import javax.swing.filechooser.FileFilter;
031:
032: /**
033: * A FileFilter which filters files regarding to their file extension.
034: *
035: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
036: * @version 0.9
037: */
038: public class ExtensionFileFilter extends FileFilter {
039: /** The description of this FileFilter. */
040: private String description;
041:
042: /** The file extension. */
043: private String extension;
044:
045: /**
046: * Creates an extension file filter with the default description. The default description is
047: * ".<i>Extension</i>".
048: *
049: * @param extension the file extension (<i>xml</i>, for example)
050: */
051: public ExtensionFileFilter(String extension) {
052: this (extension, "*." + extension.toUpperCase());
053: }
054:
055: /**
056: * Creates an extension file filter with the specified description.
057: *
058: * @param extension the file extension (<i>xml</i>, for example)
059: * @param description The description for this filter
060: */
061: public ExtensionFileFilter(String extension, String description) {
062: super ();
063: setExtension(extension);
064: setDescription(description);
065: }
066:
067: /**
068: * Returns the description of this filter.
069: *
070: * @return The description
071: *
072: * @see #setDescription(String)
073: */
074: public String getDescription() {
075: return description;
076: }
077:
078: /**
079: * Sets the description of this filter. The default is ".CSV"
080: *
081: * @param description The new description
082: *
083: * @see #getDescription()
084: */
085: public void setDescription(String description) {
086: this .description = description;
087: }
088:
089: /**
090: * Sets the file extension of this filter.
091: *
092: * @param extension DOCUMENT ME!
093: */
094: public void setExtension(String extension) {
095: this .extension = extension.toLowerCase();
096: }
097:
098: /**
099: * Returns the file extension of this filter.
100: *
101: * @return DOCUMENT ME!
102: */
103: public String getExtension() {
104: return extension;
105: }
106:
107: /**
108: * Whether the given File is a XML file. Directories are accepted, too.
109: *
110: * @param file The file to be tested
111: *
112: * @return Returns true when the given File is accepted
113: */
114: public boolean accept(File file) {
115: try {
116: if (file.isDirectory()) {
117: return true;
118: } else {
119: String extension = FileUtilities.getExtension(file);
120:
121: return extension.equals(this .extension);
122: }
123: } catch (Exception e) {
124: // just for mysterious InterruptedExceptions
125: return false;
126: }
127: }
128: }
|