[Schmitzm-commits] r1536 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Mar 23 17:41:06 CET 2011
Author: mojays
Date: 2011-03-23 17:41:04 +0100 (Wed, 23 Mar 2011)
New Revision: 1536
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java
Log:
new FileExtensionFilter
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java 2011-03-22 10:24:49 UTC (rev 1535)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java 2011-03-23 16:41:04 UTC (rev 1536)
@@ -0,0 +1,82 @@
+package de.schmitzm.swing;
+
+import java.io.File;
+
+import javax.swing.filechooser.FileFilter;
+
+import de.schmitzm.io.IOUtil;
+import de.schmitzm.lang.LangUtil;
+
+/**
+ * A file filter based on file extensions.
+ */
+public class FileExtensionFilter extends FileFilter {
+ /** Holds the file extensions to accept. */
+ protected String[] acceptFileExt = null;
+ /** Indicates whether the file extensions to accept are defined with the
+ * leading dot. */
+ protected boolean acceptFileExtInclDot = false;
+ /** Holds a description for the file extensions to accept. */
+ protected String acceptFileExtDesc = "";
+
+ /**
+ * Creates a new filter based on file extensions.
+ * @param desc description for the filter (if {@code null} only a list of extensions will
+ * be used as descriptions)
+ * @param inclDot indicated whether the file extensions are defined with
+ * the leading dot
+ * @param fileExt the file extensions to accept
+ */
+ public FileExtensionFilter(String desc, boolean inclDot, String... fileExt) {
+ this.acceptFileExt = fileExt;
+ this.acceptFileExtInclDot = inclDot;
+ this.acceptFileExtDesc = LangUtil.stringConcatWithSep(", ",(Object[])acceptFileExt);
+ if ( desc != null )
+ this.acceptFileExtDesc = desc + " ("+this.acceptFileExtDesc+")";
+ }
+
+ /**
+ * Tests whether a file satisfies the filter.
+ * @return {@code true} if the file is a directory (so that directories
+ * are also shown in the file chooser.
+ */
+ @Override
+ public boolean accept(File f) {
+ if ( f.isDirectory() )
+ return true;
+
+ String fileExt = IOUtil.getFileExt(f, acceptFileExtInclDot);
+ for (String acceptExt : acceptFileExt)
+ if ( acceptExt.equalsIgnoreCase(fileExt) )
+ return true;
+ return false;
+ }
+
+ /**
+ * Returns a description for the accepted files.
+ */
+ @Override
+ public String getDescription() {
+ return acceptFileExtDesc;
+ }
+
+ /**
+ * Returns a {@link javax.swing.filechooser.FileFilter} for this filter.
+ * @return {@code this}
+ */
+ public FileFilter convertForJFileChooser() {
+ return this;
+ }
+
+ /**
+ * Returns a {@link java.io.FileFilter} for this filter.
+ */
+ public java.io.FileFilter convertForJavaIO() {
+ return new java.io.FileFilter() {
+ @Override
+ public boolean accept(File pathname) {
+ return FileExtensionFilter.this.accept(pathname);
+ }
+ };
+ }
+}
More information about the Schmitzm-commits
mailing list