[Schmitzm-commits] r1538 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 23 19:05:13 CET 2011


Author: alfonx
Date: 2011-03-23 19:05:11 +0100 (Wed, 23 Mar 2011)
New Revision: 1538

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java
Log:


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java	2011-03-23 17:44:25 UTC (rev 1537)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/FileExtensionFilter.java	2011-03-23 18:05:11 UTC (rev 1538)
@@ -11,72 +11,98 @@
  * 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;
-  }
+	/** 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 = "";
 
-  /**
-   * 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 toJFileChooserFilter() {
-    return this;
-  }
+	/**
+	 * 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 + ")";
+	}
 
-  /**
-   * Returns a {@link java.io.FileFilter} for this filter.
-   */
-  public java.io.FileFilter toJavaIOFilter() {
-    return new java.io.FileFilter() {
-      @Override
-      public boolean accept(File pathname) {
-        return FileExtensionFilter.this.accept(pathname);
-      }
-    };
-  }
+	/**
+	 * 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 toJFileChooserFilter() {
+		return this;
+	}
+
+	/**
+	 * Returns a {@link java.io.FileFilter} for this filter.
+	 */
+	public java.io.FileFilter toJavaIOFilter() {
+		return new java.io.FileFilter() {
+			@Override
+			public boolean accept(File pathname) {
+				return FileExtensionFilter.this.accept(pathname);
+			}
+		};
+	}
+
+	/**
+	 * new String[] { extensions, description}
+	 */
+	public String[] toNativeFileFilter() {
+		StringBuffer newNativeExtensions = new StringBuffer();
+		for (String ext : acceptFileExt) {
+
+			final String preFix = (acceptFileExtInclDot ? "*" : "*.");
+			newNativeExtensions.append(preFix + ext.toLowerCase() + ";"
+					+ preFix + ext.toUpperCase() + ";");
+			// Remove the last ;
+		}
+		newNativeExtensions.setLength(newNativeExtensions.length() - 1);
+
+		return new String[] { newNativeExtensions.toString(), getDescription() };
+	}
 }



More information about the Schmitzm-commits mailing list