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 COPYING file, which can be found * 009 * at the root of the source code distribution tree, * 010 * or in https://www.hdfgroup.org/licenses. * 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() { this.filters = new Hashtable<>(); } 041 042 /** 043 * Creates a file filter that accepts files with the given extension. 044 * Example: new DefaultFileFilter("jpg"); 045 * 046 * @see #addExtension 047 * 048 * @param extension the file extension to filter on 049 */ 050 public DefaultFileFilter(String extension) { this(extension, null); } 051 052 /** 053 * Creates a file filter that accepts the given file type. Example: new 054 * DefaultFileFilter("jpg", "JPEG Image Images"); 055 * 056 * Note that the "." before the extension is not needed. If provided, it 057 * will be ignored. 058 * 059 * @see #addExtension 060 * 061 * @param extension the file extension to filter on 062 * @param description the file extension full description 063 */ 064 public DefaultFileFilter(String extension, String description) 065 { 066 this(); 067 if (extension != null) { 068 addExtension(extension); 069 } 070 if (description != null) { 071 setDescription(description); 072 } 073 } 074 075 /** 076 * Creates a file filter from the given string array. Example: new 077 * DefaultFileFilter(String {"gif", "jpg"}); 078 * 079 * Note that the "." before the extension is not needed and will be ignored. 080 * 081 * @see #addExtension 082 * 083 * @param filters 084 * the list of filter names 085 */ 086 public DefaultFileFilter(String[] filters) { this(filters, null); } 087 088 /** 089 * Creates a file filter from the given string array and description. 090 * Example: new DefaultFileFilter(String {"gif", "jpg"}, 091 * "Gif and JPG Images"); 092 * 093 * Note that the "." before the extension is not needed and will be ignored. 094 * 095 * @see #addExtension 096 * 097 * @param filters 098 * the list of filter names 099 * @param description 100 * the name of the filter list 101 */ 102 public DefaultFileFilter(String[] filters, String description) 103 { 104 this(); 105 for (int i = 0; i < filters.length; i++) { 106 // add filters one by one 107 addExtension(filters[i]); 108 } 109 if (description != null) { 110 setDescription(description); 111 } 112 } 113 114 /** 115 * @return the file extensions associated with this DefaultFileFilter 116 */ 117 public String getExtensions() 118 { 119 Enumeration<String> extensions = filters.keys(); 120 String extString = ""; 121 122 while (extensions.hasMoreElements()) { 123 extString += "*." + extensions.nextElement(); 124 if (extensions.hasMoreElements()) { 125 extString += ";"; 126 } 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 { 148 if (filters == null) { 149 filters = new Hashtable<>(5); 150 } 151 152 String ext = null; 153 StringTokenizer st = new StringTokenizer(extension, ","); 154 while (st.hasMoreElements()) { 155 ext = st.nextToken().trim(); 156 filters.put(ext.toLowerCase(), this); 157 } 158 159 fullDescription = null; 160 } 161 162 /** 163 * @return the human readable description of this filter. For example: 164 * "JPEG and GIF Image Files (*.jpg, *.gif)" 165 */ 166 public String getDescription() 167 { 168 if (fullDescription == null) { 169 if ((description == null) || isExtensionListInDescription()) { 170 fullDescription = description == null ? "(" : description + " ("; 171 // build the description from the extension list 172 Enumeration<String> extensions = filters.keys(); 173 if (extensions != null) { 174 175 if (!extensions.hasMoreElements()) { 176 fullDescription = null; 177 return null; 178 } 179 180 fullDescription += "." + extensions.nextElement(); 181 while (extensions.hasMoreElements()) { 182 fullDescription += ", " 183 + "." + extensions.nextElement(); 184 } 185 } 186 fullDescription += ")"; 187 } 188 else { 189 fullDescription = description; 190 } 191 } 192 return fullDescription; 193 } 194 195 /** 196 * Sets the human readable description of this filter. For example: 197 * filter.setDescription("Gif and JPG Images"); 198 * 199 * @param description the full description of the file filter 200 */ 201 public void setDescription(String description) 202 { 203 this.description = description; 204 fullDescription = null; 205 } 206 207 /** 208 * Determines whether the extension list (.jpg, .gif, etc) should show up in 209 * the human readable description. 210 * 211 * Only relevent if a description was provided in the constructor or using 212 * setDescription(); 213 * 214 * @param b the show state of the extension list 215 */ 216 public void setExtensionListInDescription(boolean b) 217 { 218 useExtensionsInDescription = b; 219 fullDescription = null; 220 } 221 222 /** 223 * @return whether the extension list (.jpg, .gif, etc) should show up in 224 * the human readable description. 225 * 226 * Only relevent if a description was provided in the constructor or using 227 * setDescription(); 228 */ 229 public boolean isExtensionListInDescription() { return useExtensionsInDescription; } 230 231 /** @return a file filter for HDF4/5 file. */ 232 public static DefaultFileFilter getFileFilter() 233 { 234 // update extensions 235 String fileExtensions = ViewProperties.getFileExtension(); 236 237 DefaultFileFilter filter = new DefaultFileFilter(); 238 filter.setDescription("HDF & more"); 239 240 filter.addExtension(fileExtensions); 241 242 return filter; 243 } 244 245 /** @return a file filter for NetCDF3 file. */ 246 public static DefaultFileFilter getFileFilterNetCDF3() 247 { 248 DefaultFileFilter filter = new DefaultFileFilter(); 249 filter.addExtension("nc"); 250 filter.setDescription("NetCDF3 files"); 251 252 return filter; 253 } 254 255 /** @return a file filter for HDF4 file. */ 256 public static DefaultFileFilter getFileFilterHDF4() 257 { 258 DefaultFileFilter filter = new DefaultFileFilter(); 259 filter.addExtension("hdf"); 260 filter.addExtension("h4"); 261 filter.addExtension("hdf4"); 262 filter.setDescription("HDF4 files"); 263 264 return filter; 265 } 266 267 /** @return a file filter for HDF5 file. */ 268 public static DefaultFileFilter getFileFilterHDF5() 269 { 270 DefaultFileFilter filter = new DefaultFileFilter(); 271 filter.addExtension("h5"); 272 filter.addExtension("hdf5"); 273 filter.setDescription("HDF5 files"); 274 275 return filter; 276 } 277 278 /** @return a file filter for JPEG image files. */ 279 public static DefaultFileFilter getFileFilterJPEG() 280 { 281 DefaultFileFilter filter = new DefaultFileFilter(); 282 filter.addExtension("jpg"); 283 filter.addExtension("jpeg"); 284 filter.addExtension("jpe"); 285 filter.addExtension("jif"); 286 filter.addExtension("jfif"); 287 filter.addExtension("jfi"); 288 filter.setDescription("JPEG images"); 289 290 return filter; 291 } 292 293 /** @return a file filter for TIFF image files. */ 294 public static DefaultFileFilter getFileFilterTIFF() 295 { 296 DefaultFileFilter filter = new DefaultFileFilter(); 297 filter.addExtension("tif"); 298 filter.addExtension("tiff"); 299 filter.setDescription("TIFF images"); 300 301 return filter; 302 } 303 304 /** @return a file filter for PNG image files. */ 305 public static DefaultFileFilter getFileFilterPNG() 306 { 307 DefaultFileFilter filter = new DefaultFileFilter(); 308 filter.addExtension("png"); 309 filter.setDescription("PNG images"); 310 311 return filter; 312 } 313 314 /** @return a file filter for BMP image files. */ 315 public static DefaultFileFilter getFileFilterBMP() 316 { 317 DefaultFileFilter filter = new DefaultFileFilter(); 318 filter.addExtension("bmp"); 319 filter.addExtension("dib"); 320 filter.setDescription("BMP images"); 321 322 return filter; 323 } 324 325 /** @return a file filter for GIF image files. */ 326 public static DefaultFileFilter getFileFilterGIF() 327 { 328 DefaultFileFilter filter = new DefaultFileFilter(); 329 filter.addExtension("gif"); 330 filter.setDescription("GIF images"); 331 332 return filter; 333 } 334 335 /** @return a file filter for GIF, JPEG, BMP, or PNG image files. */ 336 public static DefaultFileFilter getImageFileFilter() 337 { 338 DefaultFileFilter filter = new DefaultFileFilter(); 339 filter.addExtension("jpg"); 340 filter.addExtension("jpeg"); 341 filter.addExtension("jpe"); 342 filter.addExtension("jif"); 343 filter.addExtension("jfif"); 344 filter.addExtension("jfi"); 345 filter.addExtension("png"); 346 filter.addExtension("gif"); 347 filter.addExtension("bmp"); 348 filter.addExtension("dib"); 349 filter.setDescription("GIF, JPEG, BMP, or PNG images"); 350 351 return filter; 352 } 353 354 /** @return a file filter for text file. */ 355 public static DefaultFileFilter getFileFilterText() 356 { 357 DefaultFileFilter filter = new DefaultFileFilter(); 358 filter.addExtension("txt"); 359 filter.addExtension("text"); 360 filter.setDescription("Text"); 361 362 return filter; 363 } 364 365 /** @return a file filter for binary file. */ 366 public static DefaultFileFilter getFileFilterBinary() 367 { 368 DefaultFileFilter filter = new DefaultFileFilter(); 369 filter.addExtension("bin"); 370 filter.setDescription("Binary"); 371 372 return filter; 373 } 374}