001/***************************************************************************** 002 * Copyright by The HDF Group. * 003 * Copyright by the Board of Trustees of the University of Illinois. * 004 * All rights reserved. * 005 * * 006 * This file is part of the HDF Java Products distribution. * 007 * The full copyright notice, including terms governing use, modification, * 008 * and redistribution, is contained in the files COPYING and Copyright.html. * 009 * COPYING can be found at the root of the source code distribution tree. * 010 * Or, see https://support.hdfgroup.org/products/licenses.html * 011 * If you do not have access to either file, you may request a copy from * 012 * help@hdfgroup.org. * 013 ****************************************************************************/ 014 015package hdf.view; 016 017import java.util.Enumeration; 018import java.util.Hashtable; 019import java.util.StringTokenizer; 020 021/** 022 * A convenience implementation of FileFilter that filters out all files except 023 * for those type extensions that it knows about. 024 * 025 * @author Jordan T. Henderson 026 * @version 2.4 4/27/2016 027 */ 028public class DefaultFileFilter { 029 private Hashtable<String, DefaultFileFilter> filters = null; 030 private String description = null; 031 private String fullDescription = null; 032 private boolean useExtensionsInDescription = true; 033 034 /** 035 * Creates a file filter. If no filters are added, then all files are 036 * accepted. 037 * 038 * @see #addExtension 039 */ 040 public DefaultFileFilter() { 041 this.filters = new Hashtable<String, DefaultFileFilter>(); 042 } 043 044 /** 045 * Creates a file filter that accepts files with the given extension. 046 * Example: new DefaultFileFilter("jpg"); 047 * 048 * @see #addExtension 049 * 050 * @param extension the file extension to filter on 051 */ 052 public DefaultFileFilter(String extension) { 053 this(extension, null); 054 } 055 056 /** 057 * Creates a file filter that accepts the given file type. Example: new 058 * DefaultFileFilter("jpg", "JPEG Image Images"); 059 * 060 * Note that the "." before the extension is not needed. If provided, it 061 * will be ignored. 062 * 063 * @see #addExtension 064 * 065 * @param extension the file extension to filter on 066 * @param description the file extension full description 067 */ 068 public DefaultFileFilter(String extension, String description) { 069 this(); 070 if (extension != null) { 071 addExtension(extension); 072 } 073 if (description != null) { 074 setDescription(description); 075 } 076 } 077 078 /** 079 * Creates a file filter from the given string array. Example: new 080 * DefaultFileFilter(String {"gif", "jpg"}); 081 * 082 * Note that the "." before the extension is not needed and will be ignored. 083 * 084 * @see #addExtension 085 * 086 * @param filters 087 * the list of filter names 088 */ 089 public DefaultFileFilter(String[] filters) { 090 this(filters, null); 091 } 092 093 /** 094 * Creates a file filter from the given string array and description. 095 * Example: new DefaultFileFilter(String {"gif", "jpg"}, 096 * "Gif and JPG Images"); 097 * 098 * Note that the "." before the extension is not needed and will be ignored. 099 * 100 * @see #addExtension 101 * 102 * @param filters 103 * the list of filter names 104 * @param description 105 * the name of the filter list 106 */ 107 public DefaultFileFilter(String[] filters, String description) { 108 this(); 109 for (int i = 0; i < filters.length; i++) { 110 // add filters one by one 111 addExtension(filters[i]); 112 } 113 if (description != null) { 114 setDescription(description); 115 } 116 } 117 118 /** 119 * @return the file extensions associated with this DefaultFileFilter 120 */ 121 public String getExtensions() { 122 Enumeration<String> extensions = filters.keys(); 123 String extString = ""; 124 125 while (extensions.hasMoreElements()) { 126 extString += "*." + extensions.nextElement() + ";"; 127 } 128 129 return extString; 130 } 131 132 /** 133 * Adds a filetype "dot" extension to filter against. 134 * 135 * For example: the following code will create a filter that filters out all 136 * files except those that end in ".jpg" and ".tif": 137 * 138 * DefaultFileFilter filter = new DefaultFileFilter(); 139 * filter.addExtension("jpg"); filter.addExtension("tif"); or 140 * filter.addExtension("jpg, tif"); 141 * 142 * Note that the "." before the extension is not needed and will be ignored. 143 * 144 * @param extension the file extension to add to the file filter 145 */ 146 public void addExtension(String extension) { 147 if (filters == null) { 148 filters = new Hashtable<String, DefaultFileFilter>(5); 149 } 150 151 String ext = null; 152 StringTokenizer st = new StringTokenizer(extension, ","); 153 while (st.hasMoreElements()) { 154 ext = st.nextToken().trim(); 155 filters.put(ext.toLowerCase(), this); 156 } 157 158 fullDescription = null; 159 } 160 161 /** 162 * @return the human readable description of this filter. For example: 163 * "JPEG and GIF Image Files (*.jpg, *.gif)" 164 */ 165 public String getDescription() { 166 if (fullDescription == null) { 167 if ((description == null) || isExtensionListInDescription()) { 168 fullDescription = description == null ? "(" : description 169 + " ("; 170 // build the description from the extension list 171 Enumeration<String> extensions = filters.keys(); 172 if (extensions != null) { 173 174 if (!extensions.hasMoreElements()) { 175 fullDescription = null; 176 return null; 177 } 178 179 fullDescription += "." + extensions.nextElement(); 180 while (extensions.hasMoreElements()) { 181 fullDescription += ", " 182 + extensions.nextElement(); 183 } 184 } 185 fullDescription += ")"; 186 } 187 else { 188 fullDescription = description; 189 } 190 } 191 return fullDescription; 192 } 193 194 /** 195 * Sets the human readable description of this filter. For example: 196 * filter.setDescription("Gif and JPG Images"); 197 * 198 * @param description the full description of the file filter 199 */ 200 public void setDescription(String description) { 201 this.description = description; 202 fullDescription = null; 203 } 204 205 /** 206 * Determines whether the extension list (.jpg, .gif, etc) should show up in 207 * the human readable description. 208 * 209 * Only relevent if a description was provided in the constructor or using 210 * setDescription(); 211 * 212 * @param b the show state of the extension list 213 */ 214 public void setExtensionListInDescription(boolean b) { 215 useExtensionsInDescription = b; 216 fullDescription = null; 217 } 218 219 /** 220 * @return whether the extension list (.jpg, .gif, etc) should show up in 221 * the human readable description. 222 * 223 * Only relevent if a description was provided in the constructor or using 224 * setDescription(); 225 */ 226 public boolean isExtensionListInDescription() { 227 return useExtensionsInDescription; 228 } 229 230 /** @return a file filter for HDF4/5 file. */ 231 public static DefaultFileFilter getFileFilter() { 232 // update extensions 233 String fileExtensions = ViewProperties.getFileExtension(); 234 235 DefaultFileFilter filter = new DefaultFileFilter(); 236 filter.setDescription("HDF & more"); 237 238 filter.addExtension(fileExtensions); 239 240 return filter; 241 } 242 243 /** @return a file filter for HDF4 file. */ 244 public static DefaultFileFilter getFileFilterHDF4() { 245 DefaultFileFilter filter = new DefaultFileFilter(); 246 filter.addExtension("hdf"); 247 filter.addExtension("h4"); 248 filter.addExtension("hdf4"); 249 filter.setDescription("HDF4 files"); 250 251 return filter; 252 } 253 254 /** @return a file filter for HDF5 file. */ 255 public static DefaultFileFilter getFileFilterHDF5() { 256 DefaultFileFilter filter = new DefaultFileFilter(); 257 filter.addExtension("h5"); 258 filter.addExtension("hdf5"); 259 filter.setDescription("HDF5 files"); 260 261 return filter; 262 } 263 264 /** @return a file filter for JPEG image files. */ 265 public static DefaultFileFilter getFileFilterJPEG() { 266 DefaultFileFilter filter = new DefaultFileFilter(); 267 filter.addExtension("jpg"); 268 filter.addExtension("jpeg"); 269 filter.addExtension("jpe"); 270 filter.addExtension("jif"); 271 filter.addExtension("jfif"); 272 filter.addExtension("jfi"); 273 filter.setDescription("JPEG images"); 274 275 return filter; 276 } 277 278 /** @return a file filter for TIFF image files. */ 279 public static DefaultFileFilter getFileFilterTIFF() { 280 DefaultFileFilter filter = new DefaultFileFilter(); 281 filter.addExtension("tif"); 282 filter.addExtension("tiff"); 283 filter.setDescription("TIFF images"); 284 285 return filter; 286 } 287 288 /** @return a file filter for PNG image files. */ 289 public static DefaultFileFilter getFileFilterPNG() { 290 DefaultFileFilter filter = new DefaultFileFilter(); 291 filter.addExtension("png"); 292 filter.setDescription("PNG images"); 293 294 return filter; 295 } 296 297 /** @return a file filter for BMP image files. */ 298 public static DefaultFileFilter getFileFilterBMP() { 299 DefaultFileFilter filter = new DefaultFileFilter(); 300 filter.addExtension("bmp"); 301 filter.addExtension("dib"); 302 filter.setDescription("BMP images"); 303 304 return filter; 305 } 306 307 /** @return a file filter for GIF image files. */ 308 public static DefaultFileFilter getFileFilterGIF() { 309 DefaultFileFilter filter = new DefaultFileFilter(); 310 filter.addExtension("gif"); 311 filter.setDescription("GIF images"); 312 313 return filter; 314 } 315 316 /** @return a file filter for GIF, JPEG, BMP, or PNG image files. */ 317 public static DefaultFileFilter getImageFileFilter() { 318 DefaultFileFilter filter = new DefaultFileFilter(); 319 filter.addExtension("jpg"); 320 filter.addExtension("jpeg"); 321 filter.addExtension("jpe"); 322 filter.addExtension("jif"); 323 filter.addExtension("jfif"); 324 filter.addExtension("jfi"); 325 filter.addExtension("png"); 326 filter.addExtension("gif"); 327 filter.addExtension("bmp"); 328 filter.addExtension("dib"); 329 filter.setDescription("GIF, JPEG, BMP, or PNG images"); 330 331 return filter; 332 } 333 334 /** @return a file filter for text file. */ 335 public static DefaultFileFilter getFileFilterText() { 336 DefaultFileFilter filter = new DefaultFileFilter(); 337 filter.addExtension("txt"); 338 filter.addExtension("text"); 339 filter.setDescription("Text"); 340 341 return filter; 342 } 343 344 /** @return a file filter for binary file. */ 345 public static DefaultFileFilter getFileFilterBinary() { 346 DefaultFileFilter filter = new DefaultFileFilter(); 347 filter.addExtension("bin"); 348 filter.setDescription("Binary"); 349 350 return filter; 351 } 352}