FilesystemFilter.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ------------------------
  28. * ExtensionFileFilter.java
  29. * ------------------------
  30. * (C) Copyright 2000-2004, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: FilesystemFilter.java,v 1.6 2008/09/10 09:26:11 mungady Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 01-Jun-2005 : Updated javadoc.
  40. */
  41. package org.jfree.ui;
  42. import java.io.File;
  43. import java.io.FilenameFilter;
  44. import javax.swing.filechooser.FileFilter;
  45. /**
  46. * A filesystem filter.
  47. *
  48. * @author David Gilbert
  49. */
  50. public class FilesystemFilter extends FileFilter implements FilenameFilter {
  51. /** The file extension, which should be accepted. */
  52. private String[] fileext;
  53. /** The filter description. */
  54. private String descr;
  55. /** A flag indicating whether to accept directories. */
  56. private boolean accDirs;
  57. /**
  58. * Creates a new filter.
  59. *
  60. * @param fileext the file extension.
  61. * @param descr the description.
  62. */
  63. public FilesystemFilter(final String fileext, final String descr) {
  64. this(fileext, descr, true);
  65. }
  66. /**
  67. * Creates a new filter.
  68. *
  69. * @param fileext the file extension.
  70. * @param descr the description.
  71. * @param accDirs accept directories?
  72. */
  73. public FilesystemFilter(final String fileext, final String descr,
  74. final boolean accDirs) {
  75. this(new String[]{fileext}, descr, accDirs);
  76. }
  77. /**
  78. * Creates a new filter.
  79. *
  80. * @param fileext the file extension.
  81. * @param descr the description.
  82. * @param accDirs accept directories?
  83. * @throws NullPointerException if the file extensions are null.
  84. */
  85. public FilesystemFilter(final String[] fileext, final String descr,
  86. final boolean accDirs) {
  87. this.fileext = (String[]) fileext.clone();
  88. this.descr = descr;
  89. this.accDirs = accDirs;
  90. }
  91. /**
  92. * Returns <code>true</code> if the file is accepted, and <code>false</code> otherwise.
  93. *
  94. * @param dir the directory.
  95. * @param name the file name.
  96. * @return A boolean.
  97. */
  98. public boolean accept(final File dir, final String name) {
  99. final File f = new File(dir, name);
  100. if (f.isDirectory() && acceptsDirectories()) {
  101. return true;
  102. }
  103. for (int i = 0; i < this.fileext.length; i++) {
  104. if (name.endsWith(this.fileext[i])) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. /**
  111. * Returns <code>true</code> if the specified file matches the requirements of this
  112. * filter, and <code>false</code> otherwise.
  113. *
  114. * @param dir the file or directory.
  115. * @return A boolean.
  116. */
  117. public boolean accept(final File dir) {
  118. if (dir.isDirectory() && acceptsDirectories()) {
  119. return true;
  120. }
  121. for (int i = 0; i < this.fileext.length; i++) {
  122. if (dir.getName().endsWith(this.fileext[i])) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. /**
  129. * Returns the filter description.
  130. *
  131. * @return The filter description.
  132. */
  133. public String getDescription() {
  134. return this.descr;
  135. }
  136. /**
  137. * Sets the flag that controls whether or not the filter accepts directories.
  138. *
  139. * @param b a boolean.
  140. */
  141. public void acceptDirectories(final boolean b) {
  142. this.accDirs = b;
  143. }
  144. /**
  145. * Returns the flag that indicates whether or not the filter accepts directories.
  146. *
  147. * @return A boolean.
  148. */
  149. public boolean acceptsDirectories() {
  150. return this.accDirs;
  151. }
  152. }