00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * General Robotix Inc. 00008 * National Institute of Advanced Industrial Science and Technology (AIST) 00009 */ 00010 package com.generalrobotix.ui.view.tdview; 00011 00012 /* 00013 * @(#)ExampleFileFilter.java 1.9 99/04/23 00014 * 00015 * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved. 00016 * 00017 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 00018 * modify and redistribute this software in source and binary code form, 00019 * provided that i) this copyright notice and license appear on all copies of 00020 * the software; and ii) Licensee does not utilize the software in a manner 00021 * which is disparaging to Sun. 00022 * 00023 * This software is provided "AS IS," without a warranty of any kind. ALL 00024 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 00025 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 00026 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 00027 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 00028 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 00029 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 00030 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 00031 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 00032 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGES. 00034 * 00035 * This software is not designed or intended for use in on-line control of 00036 * aircraft, air traffic, aircraft navigation or aircraft communications; or in 00037 * the design, construction, operation or maintenance of any nuclear 00038 * facility. Licensee represents and warrants that it will not use or 00039 * redistribute the Software for such purposes. 00040 */ 00041 00042 00043 import java.io.File; 00044 import java.util.Hashtable; 00045 import java.util.Enumeration; 00046 import javax.swing.filechooser.*; 00047 00067 public class ExampleFileFilter extends FileFilter { 00068 00069 // private static String TYPE_UNKNOWN = "Type Unknown"; 00070 // private static String HIDDEN_FILE = "Hidden File"; 00071 00072 private Hashtable<String, ExampleFileFilter> filters = null; 00073 private String description = null; 00074 private String fullDescription = null; 00075 private boolean useExtensionsInDescription = true; 00076 00083 public ExampleFileFilter() { 00084 this.filters = new Hashtable<String, ExampleFileFilter>(); 00085 } 00086 00093 public ExampleFileFilter(String extension) { 00094 this(extension,null); 00095 } 00096 00106 public ExampleFileFilter(String extension, String description) { 00107 this(); 00108 if(extension!=null) addExtension(extension); 00109 if(description!=null) setDescription(description); 00110 } 00111 00121 public ExampleFileFilter(String[] filters) { 00122 this(filters, null); 00123 } 00124 00133 public ExampleFileFilter(String[] filters, String description) { 00134 this(); 00135 for (int i = 0; i < filters.length; i++) { 00136 // add filters one by one 00137 addExtension(filters[i]); 00138 } 00139 if(description!=null) setDescription(description); 00140 } 00141 00151 public boolean accept(File f) { 00152 if(f != null) { 00153 if(f.isDirectory()) { 00154 return true; 00155 } 00156 String extension = getExtension(f); 00157 if(extension != null && filters.get(getExtension(f)) != null) { 00158 return true; 00159 }; 00160 } 00161 return false; 00162 } 00163 00170 public String getExtension(File f) { 00171 if(f != null) { 00172 String filename = f.getName(); 00173 int i = filename.lastIndexOf('.'); 00174 if(i>0 && i<filename.length()-1) { 00175 return filename.substring(i+1).toLowerCase(); 00176 }; 00177 } 00178 return null; 00179 } 00180 00193 public void addExtension(String extension) { 00194 if(filters == null) { 00195 filters = new Hashtable<String, ExampleFileFilter>(5); 00196 } 00197 filters.put(extension.toLowerCase(), this); 00198 fullDescription = null; 00199 } 00200 00201 00211 public String getDescription() { 00212 if(fullDescription == null) { 00213 if(description == null || isExtensionListInDescription()) { 00214 fullDescription = description==null ? "(" : description + " ("; 00215 // build the description from the extension list 00216 Enumeration<String> extensions = filters.keys(); 00217 if(extensions != null) { 00218 fullDescription += "." + (String) extensions.nextElement(); 00219 while (extensions.hasMoreElements()) { 00220 fullDescription += ", " + (String) extensions.nextElement(); 00221 } 00222 } 00223 fullDescription += ")"; 00224 } else { 00225 fullDescription = description; 00226 } 00227 } 00228 return fullDescription; 00229 } 00230 00239 public void setDescription(String description) { 00240 this.description = description; 00241 fullDescription = null; 00242 } 00243 00255 public void setExtensionListInDescription(boolean b) { 00256 useExtensionsInDescription = b; 00257 fullDescription = null; 00258 } 00259 00271 public boolean isExtensionListInDescription() { 00272 return useExtensionsInDescription; 00273 } 00274 }