arcgissamples\addins\util\ShapeFileFilter.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.addins.util; import java.io.File; import java.util.Enumeration; import java.util.Hashtable; import javax.swing.filechooser.FileFilter; public class ShapeFileFilter extends FileFilter { /* All Global Declarations */ private Hashtable filters = null; private String description = null; private String fullDescription = null; private boolean useExtensionsInDescription = true; public ShapeFileFilter() { this.filters = new Hashtable(); } public boolean accept(File f) { if(f != null) { if(f.isDirectory()) { return true; } String extension = getExtension(f); if(extension != null && filters.get(getExtension(f)) != null) { return true; }; } return false; } public String getExtension(File f) { if(f != null) { String filename = f.getName(); int i = filename.lastIndexOf('.'); if(i>0 && i<filename.length()-1) { return filename.substring(i+1).toLowerCase(); }; } return null; } public void addExtension(String extension) { if(filters == null) { filters = new Hashtable(5); } filters.put(extension.toLowerCase(), this); fullDescription = null; } public String getDescription() { if(fullDescription == null) { if(description == null || isExtensionListInDescription()) { fullDescription = description==null ? "(" : description + " ("; // build the description from the extension list Enumeration extensions = filters.keys(); if(extensions != null) { fullDescription += "." + (String) extensions.nextElement(); while (extensions.hasMoreElements()) { fullDescription += ", ." + (String) extensions.nextElement(); } } fullDescription += ")"; }else { fullDescription = description; } } return fullDescription; } public boolean isExtensionListInDescription() { return useExtensionsInDescription; } }