1 /*
   2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.gui;
  36 
  37 import java.io.File;
  38 import java.util.Hashtable;
  39 import java.util.Enumeration;
  40 import javax.swing.filechooser.*;
  41 
  42 //### Renamed from 'ExampleFileFilter.java' provided with Swing demos.
  43 
  44 /**
  45  * A convenience implementation of FileFilter that filters out
  46  * all files except for those type extensions that it knows about.
  47  *
  48  * Extensions are of the type ".foo", which is typically found on
  49  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
  50  *
  51  * Example - create a new filter that filerts out all files
  52  * but gif and jpg image files:
  53  *
  54  *     JFileChooser chooser = new JFileChooser();
  55  *     ExampleFileFilter filter = new ExampleFileFilter(
  56  *                   new String{"gif", "jpg"}, "JPEG & GIF Images")
  57  *     chooser.addChoosableFileFilter(filter);
  58  *     chooser.showOpenDialog(this);
  59  *
  60  * @author Jeff Dinkins
  61  */
  62 
  63 public class JDBFileFilter extends FileFilter {
  64 
  65     private static String TYPE_UNKNOWN = "Type Unknown";
  66     private static String HIDDEN_FILE = "Hidden File";
  67 
  68     private Hashtable<String, JDBFileFilter> filters = null;
  69     private String description = null;
  70     private String fullDescription = null;
  71     private boolean useExtensionsInDescription = true;
  72 
  73     /**
  74      * Creates a file filter. If no filters are added, then all
  75      * files are accepted.
  76      *
  77      * @see #addExtension
  78      */
  79     public JDBFileFilter() {
  80         this.filters = new Hashtable<String, JDBFileFilter>();
  81     }
  82 
  83     /**
  84      * Creates a file filter that accepts files with the given extension.
  85      * Example: new JDBFileFilter("jpg");
  86      *
  87      * @see #addExtension
  88      */
  89     public JDBFileFilter(String extension) {
  90         this(extension,null);
  91     }
  92 
  93     /**
  94      * Creates a file filter that accepts the given file type.
  95      * Example: new JDBFileFilter("jpg", "JPEG Image Images");
  96      *
  97      * Note that the "." before the extension is not needed. If
  98      * provided, it will be ignored.
  99      *
 100      * @see #addExtension
 101      */
 102     public JDBFileFilter(String extension, String description) {
 103         this();
 104         if(extension!=null) {
 105          addExtension(extension);
 106       }
 107         if(description!=null) {
 108          setDescription(description);
 109       }
 110     }
 111 
 112     /**
 113      * Creates a file filter from the given string array.
 114      * Example: new JDBFileFilter(String {"gif", "jpg"});
 115      *
 116      * Note that the "." before the extension is not needed adn
 117      * will be ignored.
 118      *
 119      * @see #addExtension
 120      */
 121     public JDBFileFilter(String[] filters) {
 122         this(filters, null);
 123     }
 124 
 125     /**
 126      * Creates a file filter from the given string array and description.
 127      * Example: new JDBFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
 128      *
 129      * Note that the "." before the extension is not needed and will be ignored.
 130      *
 131      * @see #addExtension
 132      */
 133     public JDBFileFilter(String[] filters, String description) {
 134         this();
 135         for (String filter : filters) {
 136             // add filters one by one
 137             addExtension(filter);
 138         }
 139         if(description!=null) {
 140          setDescription(description);
 141       }
 142     }
 143 
 144     /**
 145      * Return true if this file should be shown in the directory pane,
 146      * false if it shouldn't.
 147      *
 148      * Files that begin with "." are ignored.
 149      *
 150      * @see #getExtension
 151      * @see FileFilter#accepts
 152      */
 153     @Override
 154     public boolean accept(File f) {
 155         if(f != null) {
 156             if(f.isDirectory()) {
 157                 return true;
 158             }
 159             String extension = getExtension(f);
 160             if(extension != null && filters.get(getExtension(f)) != null) {
 161                 return true;
 162             };
 163         }
 164         return false;
 165     }
 166 
 167     /**
 168      * Return the extension portion of the file's name .
 169      *
 170      * @see #getExtension
 171      * @see FileFilter#accept
 172      */
 173      public String getExtension(File f) {
 174         if(f != null) {
 175             String filename = f.getName();
 176             int i = filename.lastIndexOf('.');
 177             if(i>0 && i<filename.length()-1) {
 178                 return filename.substring(i+1).toLowerCase();
 179             };
 180         }
 181         return null;
 182     }
 183 
 184     /**
 185      * Adds a filetype "dot" extension to filter against.
 186      *
 187      * For example: the following code will create a filter that filters
 188      * out all files except those that end in ".jpg" and ".tif":
 189      *
 190      *   JDBFileFilter filter = new JDBFileFilter();
 191      *   filter.addExtension("jpg");
 192      *   filter.addExtension("tif");
 193      *
 194      * Note that the "." before the extension is not needed and will be ignored.
 195      */
 196     public void addExtension(String extension) {
 197         if(filters == null) {
 198             filters = new Hashtable<String, JDBFileFilter>(5);
 199         }
 200         filters.put(extension.toLowerCase(), this);
 201         fullDescription = null;
 202     }
 203 
 204 
 205     /**
 206      * Returns the human readable description of this filter. For
 207      * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
 208      *
 209      * @see setDescription
 210      * @see setExtensionListInDescription
 211      * @see isExtensionListInDescription
 212      * @see FileFilter#getDescription
 213      */
 214     @Override
 215     public String getDescription() {
 216         if(fullDescription == null) {
 217             if(description == null || isExtensionListInDescription()) {
 218                 fullDescription = description==null ? "(" : description + " (";
 219                 // build the description from the extension list
 220                 Enumeration<String> extensions = filters.keys();
 221                 if(extensions != null) {
 222                     fullDescription += "." + extensions.nextElement();
 223                     while (extensions.hasMoreElements()) {
 224                         fullDescription += ", " + extensions.nextElement();
 225                     }
 226                 }
 227                 fullDescription += ")";
 228             } else {
 229                 fullDescription = description;
 230             }
 231         }
 232         return fullDescription;
 233     }
 234 
 235     /**
 236      * Sets the human readable description of this filter. For
 237      * example: filter.setDescription("Gif and JPG Images");
 238      *
 239      * @see setDescription
 240      * @see setExtensionListInDescription
 241      * @see isExtensionListInDescription
 242      */
 243     public void setDescription(String description) {
 244         this.description = description;
 245         fullDescription = null;
 246     }
 247 
 248     /**
 249      * Determines whether the extension list (.jpg, .gif, etc) should
 250      * show up in the human readable description.
 251      *
 252      * Only relevent if a description was provided in the constructor
 253      * or using setDescription();
 254      *
 255      * @see getDescription
 256      * @see setDescription
 257      * @see isExtensionListInDescription
 258      */
 259     public void setExtensionListInDescription(boolean b) {
 260         useExtensionsInDescription = b;
 261         fullDescription = null;
 262     }
 263 
 264     /**
 265      * Returns whether the extension list (.jpg, .gif, etc) should
 266      * show up in the human readable description.
 267      *
 268      * Only relevent if a description was provided in the constructor
 269      * or using setDescription();
 270      *
 271      * @see getDescription
 272      * @see setDescription
 273      * @see setExtensionListInDescription
 274      */
 275     public boolean isExtensionListInDescription() {
 276         return useExtensionsInDescription;
 277     }
 278 }