/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/util/WriterFileSuffixFilter.java

https://github.com/conceptboard/TwelveMonkeys · Java · 99 lines · 48 code · 13 blank · 38 comment · 6 complexity · aadf60ca011e0808319bf71964402fb9 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, Harald Kuhr
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name "TwelveMonkeys" nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package com.twelvemonkeys.imageio.util;
  29. import com.twelvemonkeys.io.FileUtil;
  30. import com.twelvemonkeys.lang.StringUtil;
  31. import javax.imageio.ImageIO;
  32. import javax.swing.filechooser.FileFilter;
  33. import java.io.File;
  34. import java.util.HashMap;
  35. import java.util.Iterator;
  36. import java.util.Map;
  37. /**
  38. * WriterFileSuffixFilter
  39. * <p/>
  40. *
  41. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  42. * @author last modified by $Author: haku$
  43. * @version $Id: WriterFileSuffixFilter.java,v 1.0 11.okt.2006 20:05:36 haku Exp$
  44. */
  45. public final class WriterFileSuffixFilter extends FileFilter implements java.io.FileFilter {
  46. private final String description;
  47. private Map<String, Boolean> knownSuffixes = new HashMap<String, Boolean>(32);
  48. public WriterFileSuffixFilter() {
  49. this("Images (all supported output formats)");
  50. }
  51. public WriterFileSuffixFilter(String pDescription) {
  52. description = pDescription;
  53. }
  54. public boolean accept(File pFile) {
  55. // Directories are always supported
  56. if (pFile.isDirectory()) {
  57. return true;
  58. }
  59. // Test if we have an ImageWriter for this suffix
  60. String suffix = FileUtil.getExtension(pFile);
  61. return !StringUtil.isEmpty(suffix) && hasWriterForSuffix(suffix);
  62. }
  63. private boolean hasWriterForSuffix(String pSuffix) {
  64. if (knownSuffixes.get(pSuffix) == Boolean.TRUE) {
  65. return true;
  66. }
  67. try {
  68. // Cahce lookup
  69. Iterator iterator = ImageIO.getImageWritersBySuffix(pSuffix);
  70. if (iterator.hasNext()) {
  71. knownSuffixes.put(pSuffix, Boolean.TRUE);
  72. return true;
  73. }
  74. else {
  75. knownSuffixes.put(pSuffix, Boolean.FALSE);
  76. return false;
  77. }
  78. }
  79. catch (IllegalArgumentException iae) {
  80. return false;
  81. }
  82. }
  83. public String getDescription() {
  84. return description;
  85. }
  86. }