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() { 041 this.filters = new Hashtable<>(); 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 if (extensions.hasMoreElements()) { 128 extString += ";"; 129 } 130 } 131 132 return extString; 133 } 134 135 /** 136 * Adds a filetype "dot" extension to filter against. 137 * 138 * For example: the following code will create a filter that filters out all 139 * files except those that end in ".jpg" and ".tif": 140 * 141 * DefaultFileFilter filter = new DefaultFileFilter(); 142 * filter.addExtension("jpg"); filter.addExtension("tif"); or 143 * filter.addExtension("jpg, tif"); 144 * 145 * Note that the "." before the extension is not needed and will be ignored. 146 * 147 * @param extension the file extension to add to the file filter 148 */ 149 public void addExtension(String extension) { 150 if (filters == null) { 151 filters = new Hashtable<>(5); 152 } 153 154 String ext = null; 155 StringTokenizer st = new StringTokenizer(extension, ","); 156 while (st.hasMoreElements()) { 157 ext = st.nextToken().trim(); 158 filters.put(ext.toLowerCase(), this); 159 } 160 161 fullDescription = null; 162 } 163 164 /** 165 * @return the human readable description of this filter. For example: 166 * "JPEG and GIF Image Files (*.jpg, *.gif)" 167 */ 168 public String getDescription() { 169 if (fullDescription == null) { 170 if ((description == null) || isExtensionListInDescription()) { 171 fullDescription = description == null ? "(" : description 172 + " ("; 173 // build the description from the extension list 174 Enumeration<String> extensions = filters.keys(); 175 if (extensions != null) { 176 177 if (!extensions.hasMoreElements()) { 178 fullDescription = null; 179 return null; 180 } 181 182 fullDescription += "." + extensions.nextElement(); 183 while (extensions.hasMoreElements()) { 184 fullDescription += ", " 185 + "." + extensions.nextElement(); 186 } 187 } 188 fullDescription += ")"; 189 } 190 else { 191 fullDescription = description; 192 } 193 } 194 return fullDescription; 195 } 196 197 /** 198 * Sets the human readable description of this filter. For example: 199 * filter.setDescription("Gif and JPG Images"); 200 * 201 * @param description the full description of the file filter 202 */ 203 public void setDescription(String description) { 204 this.description = description; 205 fullDescription = null; 206 } 207 208 /** 209 * Determines whether the extension list (.jpg, .gif, etc) should show up in 210 * the human readable description. 211 * 212 * Only relevent if a description was provided in the constructor or using 213 * setDescription(); 214 * 215 * @param b the show state of the extension list 216 */ 217 public void setExtensionListInDescription(boolean b) { 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() { 230 return useExtensionsInDescription; 231 } 232 233 /** @return a file filter for HDF4/5 file. */ 234 public static DefaultFileFilter getFileFilter() { 235 // update extensions 236 String fileExtensions = ViewProperties.getFileExtension(); 237 238 DefaultFileFilter filter = new DefaultFileFilter(); 239 filter.setDescription("HDF & more"); 240 241 filter.addExtension(fileExtensions); 242 243 return filter; 244 } 245 246 /** @return a file filter for NetCDF3 file. */ 247 public static DefaultFileFilter getFileFilterNetCDF3() { 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 DefaultFileFilter filter = new DefaultFileFilter(); 258 filter.addExtension("hdf"); 259 filter.addExtension("h4"); 260 filter.addExtension("hdf4"); 261 filter.setDescription("HDF4 files"); 262 263 return filter; 264 } 265 266 /** @return a file filter for HDF5 file. */ 267 public static DefaultFileFilter getFileFilterHDF5() { 268 DefaultFileFilter filter = new DefaultFileFilter(); 269 filter.addExtension("h5"); 270 filter.addExtension("hdf5"); 271 filter.setDescription("HDF5 files"); 272 273 return filter; 274 } 275 276 /** @return a file filter for JPEG image files. */ 277 public static DefaultFileFilter getFileFilterJPEG() { 278 DefaultFileFilter filter = new DefaultFileFilter(); 279 filter.addExtension("jpg"); 280 filter.addExtension("jpeg"); 281 filter.addExtension("jpe"); 282 filter.addExtension("jif"); 283 filter.addExtension("jfif"); 284 filter.addExtension("jfi"); 285 filter.setDescription("JPEG images"); 286 287 return filter; 288 } 289 290 /** @return a file filter for TIFF image files. */ 291 public static DefaultFileFilter getFileFilterTIFF() { 292 DefaultFileFilter filter = new DefaultFileFilter(); 293 filter.addExtension("tif"); 294 filter.addExtension("tiff"); 295 filter.setDescription("TIFF images"); 296 297 return filter; 298 } 299 300 /** @return a file filter for PNG image files. */ 301 public static DefaultFileFilter getFileFilterPNG() { 302 DefaultFileFilter filter = new DefaultFileFilter(); 303 filter.addExtension("png"); 304 filter.setDescription("PNG images"); 305 306 return filter; 307 } 308 309 /** @return a file filter for BMP image files. */ 310 public static DefaultFileFilter getFileFilterBMP() { 311 DefaultFileFilter filter = new DefaultFileFilter(); 312 filter.addExtension("bmp"); 313 filter.addExtension("dib"); 314 filter.setDescription("BMP images"); 315 316 return filter; 317 } 318 319 /** @return a file filter for GIF image files. */ 320 public static DefaultFileFilter getFileFilterGIF() { 321 DefaultFileFilter filter = new DefaultFileFilter(); 322 filter.addExtension("gif"); 323 filter.setDescription("GIF images"); 324 325 return filter; 326 } 327 328 /** @return a file filter for GIF, JPEG, BMP, or PNG image files. */ 329 public static DefaultFileFilter getImageFileFilter() { 330 DefaultFileFilter filter = new DefaultFileFilter(); 331 filter.addExtension("jpg"); 332 filter.addExtension("jpeg"); 333 filter.addExtension("jpe"); 334 filter.addExtension("jif"); 335 filter.addExtension("jfif"); 336 filter.addExtension("jfi"); 337 filter.addExtension("png"); 338 filter.addExtension("gif"); 339 filter.addExtension("bmp"); 340 filter.addExtension("dib"); 341 filter.setDescription("GIF, JPEG, BMP, or PNG images"); 342 343 return filter; 344 } 345 346 /** @return a file filter for text file. */ 347 public static DefaultFileFilter getFileFilterText() { 348 DefaultFileFilter filter = new DefaultFileFilter(); 349 filter.addExtension("txt"); 350 filter.addExtension("text"); 351 filter.setDescription("Text"); 352 353 return filter; 354 } 355 356 /** @return a file filter for binary file. */ 357 public static DefaultFileFilter getFileFilterBinary() { 358 DefaultFileFilter filter = new DefaultFileFilter(); 359 filter.addExtension("bin"); 360 filter.setDescription("Binary"); 361 362 return filter; 363 } 364}