1. package org.jr.swing.filter;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年4月2日
  6. * @author Cherami
  7. */
  8. import java.io.*;
  9. import org.jr.util.*;
  10. /**
  11. * 扩展名文件过滤器。
  12. * <b>此类在0.6版本的时候方式重大变化,通过继承自CombineFileFilter实现了双重功能。</b>
  13. * @since 0.4
  14. */
  15. public class FileTypeFilter
  16. extends CombineFileFilter{
  17. String[] suffixList;
  18. String description;
  19. boolean caseSensitive = false;
  20. /**
  21. * 使用指定的扩展名构造一个FileTypeFilter。
  22. * @param suffix 扩展名
  23. * @since 0.5
  24. */
  25. public FileTypeFilter(String suffix) {
  26. this(suffix,SWING);
  27. }
  28. /**
  29. * 使用指定的扩展名和类型构造一个FileTypeFilter。
  30. * @param suffix 扩展名
  31. * @param type 过滤器类型
  32. * @since 0.6
  33. */
  34. public FileTypeFilter(String suffix,int type) {
  35. super(type);
  36. suffixList = new String[1];
  37. suffixList[0] = suffix;
  38. }
  39. /**
  40. * 使用指定的扩展名数组构造一个FileTypeFilter。
  41. * @param suffixList 扩展名数组
  42. * @since 0.4
  43. */
  44. public FileTypeFilter(String[] suffixList) {
  45. this(suffixList,SWING);
  46. }
  47. /**
  48. * 使用指定的扩展名数组构造一个FileTypeFilter。
  49. * @param suffixList 扩展名数组
  50. * @param type 过滤器类型
  51. * @since 0.6
  52. */
  53. public FileTypeFilter(String[] suffixList,int type) {
  54. super(type);
  55. this.suffixList = suffixList;
  56. }
  57. /**
  58. * 使用指定的扩展名数组构造一个FileTypeFilter。
  59. * @param suffixList 扩展名数组
  60. * @param description 过滤器的描述文本
  61. * @since 0.4
  62. */
  63. public FileTypeFilter(String[] suffixList, String description) {
  64. super(SWING);
  65. this.suffixList = suffixList;
  66. this.description = description;
  67. }
  68. /**
  69. * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
  70. * @param suffixList 扩展名数组
  71. * @param caseSensitive 是否大小写敏感
  72. * @since 0.4
  73. */
  74. public FileTypeFilter(String[] suffixList, boolean caseSensitive) {
  75. this(suffixList,caseSensitive,SWING);
  76. }
  77. /**
  78. * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
  79. * @param suffixList 扩展名数组
  80. * @param caseSensitive 是否大小写敏感
  81. * @param type 过滤器类型
  82. * @since 0.6
  83. */
  84. public FileTypeFilter(String[] suffixList, boolean caseSensitive,int type) {
  85. super(type);
  86. this.suffixList = suffixList;
  87. this.caseSensitive = caseSensitive;
  88. }
  89. /**
  90. * 使用指定的扩展名数组和是否大小写敏感构造一个FileTypeFilter。
  91. * @param suffixList 扩展名数组
  92. * @param caseSensitive 是否大小写敏感
  93. * @param description 过滤器的描述文本
  94. * @since 0.4
  95. */
  96. public FileTypeFilter(String[] suffixList, boolean caseSensitive,
  97. String description) {
  98. super(SWING);
  99. this.suffixList = suffixList;
  100. this.caseSensitive = caseSensitive;
  101. this.description = description;
  102. }
  103. /**
  104. * 设置是否大小写敏感。
  105. * @param caseSensitive 是否大小写敏感
  106. * @since 0.4
  107. */
  108. public void setCaseSensitive(boolean caseSensitive) {
  109. this.caseSensitive = caseSensitive;
  110. }
  111. /**
  112. * 是否大小写敏感
  113. * @return 大小写敏感时返回true,否则返回false
  114. * @since 0.4
  115. */
  116. public boolean getCaseSensitive() {
  117. return caseSensitive;
  118. }
  119. /**
  120. * 判断指定的文件是否可以被接受。
  121. * @param file 需要判断的文件
  122. * @return 扩展名符合指定的要求时返回true,如果是目录也返回true,否则返回false
  123. * @since 0.6
  124. */
  125. protected boolean acceptFile(File file) {
  126. String filename = file.getName();
  127. int dot = filename.lastIndexOf(".");
  128. if (dot == -1) {
  129. return false;
  130. }
  131. return StringUtil.contains(suffixList, filename.substring(dot + 1),
  132. caseSensitive);
  133. }
  134. /**
  135. * 返回过滤器的描述字符串。
  136. * @return 所有扩展名的列表
  137. * @since 0.4
  138. */
  139. public String getDescription() {
  140. if (description != null) {
  141. return description;
  142. }
  143. return StringUtil.combineStringArray(suffixList, "/");
  144. }
  145. }