1 /* 2 * $Id: DefaultFileFilter.java,v 1.1 2009-10-23 11:32:08 gaudenz Exp $ 3 * Copyright (c) 2001-2005, Gaudenz Alder 4 * 5 * All rights reserved. 6 * 7 * See LICENSE file for license details. If you are unable to locate 8 * this file please contact info (at) jgraph (dot) com. 9 */ 10 package com.mxgraph.examples.swing.editor; 11 12 import java.io.File; 13 14 import javax.imageio.ImageIO; 15 import javax.swing.JFileChooser; 16 import javax.swing.filechooser.FileFilter; 17 18 /** 19 * Filter for use in a {@link JFileChooser}. 20 */ 21 public class DefaultFileFilter extends FileFilter 22 { 23 24 /** 25 * Extension of accepted files. 26 */ 27 protected String ext; 28 29 /** 30 * Description of accepted files. 31 */ 32 protected String desc; 33 34 /** 35 * Constructs a new filter for the specified extension and descpription. 36 * 37 * @param extension 38 * The extension to accept files with. 39 * @param description 40 * The description of the file format. 41 */ DefaultFileFilter(String extension, String description)42 public DefaultFileFilter(String extension, String description) 43 { 44 ext = extension.toLowerCase(); 45 desc = description; 46 } 47 48 /** 49 * Returns true if <code>file</code> is a directory or ends with 50 * {@link #ext}. 51 * 52 * @param file 53 * The file to be checked. 54 * @return Returns true if the file is accepted. 55 */ accept(File file)56 public boolean accept(File file) 57 { 58 return file.isDirectory() || file.getName().toLowerCase().endsWith(ext); 59 } 60 61 /** 62 * Returns the description for accepted files. 63 * 64 * @return Returns the description. 65 */ getDescription()66 public String getDescription() 67 { 68 return desc; 69 } 70 71 /** 72 * Returns the extension for accepted files. 73 * 74 * @return Returns the extension. 75 */ getExtension()76 public String getExtension() 77 { 78 return ext; 79 } 80 81 /** 82 * Sets the extension for accepted files. 83 * 84 * @param extension 85 * The extension to set. 86 */ setExtension(String extension)87 public void setExtension(String extension) 88 { 89 this.ext = extension; 90 } 91 92 /** 93 * Utility file filter to accept all image formats supported by image io. 94 * 95 * @see ImageIO#getReaderFormatNames() 96 */ 97 public static class ImageFileFilter extends FileFilter 98 { 99 100 /** 101 * Holds the accepted file format extensions for images. 102 */ 103 protected static String[] imageFormats = ImageIO.getReaderFormatNames(); 104 105 /** 106 * Description of the filter. 107 */ 108 protected String desc; 109 110 /** 111 * Constructs a new file filter for all supported image formats using 112 * the specified description. 113 * 114 * @param description 115 * The description to use for the file filter. 116 */ ImageFileFilter(String description)117 public ImageFileFilter(String description) 118 { 119 desc = description; 120 } 121 122 /** 123 * Returns true if the file is a directory or ends with a known image 124 * extension. 125 * 126 * @param file 127 * The file to be checked. 128 * @return Returns true if the file is accepted. 129 */ accept(File file)130 public boolean accept(File file) 131 { 132 if (file.isDirectory()) 133 { 134 return true; 135 } 136 137 String filename = file.toString().toLowerCase(); 138 139 for (int j = 0; j < imageFormats.length; j++) 140 { 141 if (filename.endsWith("." + imageFormats[j].toLowerCase())) 142 { 143 return true; 144 } 145 } 146 147 return false; 148 } 149 150 /** 151 * Returns the description. 152 * 153 * @return Returns the description. 154 */ getDescription()155 public String getDescription() 156 { 157 return desc; 158 } 159 160 } 161 162 /** 163 * Utility file filter to accept editor files, namely .xml and .xml.gz 164 * extensions. 165 * 166 * @see ImageIO#getReaderFormatNames() 167 */ 168 public static class EditorFileFilter extends FileFilter 169 { 170 171 /** 172 * Description of the File format 173 */ 174 protected String desc; 175 176 /** 177 * Constructs a new editor file filter using the specified description. 178 * 179 * @param description 180 * The description to use for the filter. 181 */ EditorFileFilter(String description)182 public EditorFileFilter(String description) 183 { 184 desc = description; 185 } 186 187 /** 188 * Returns true if the file is a directory or has a .xml or .xml.gz 189 * extension. 190 * 191 * @return Returns true if the file is accepted. 192 */ accept(File file)193 public boolean accept(File file) 194 { 195 if (file.isDirectory()) 196 { 197 return true; 198 } 199 200 String filename = file.getName().toLowerCase(); 201 202 return filename.endsWith(".xml") || filename.endsWith(".xml.gz"); 203 } 204 205 /** 206 * Returns the description. 207 * 208 * @return Returns the description. 209 */ getDescription()210 public String getDescription() 211 { 212 return desc; 213 } 214 215 } 216 } 217