View Javadoc

1   
2   package org.xrn.gui;
3   
4   import java.io.File;
5   import java.util.Enumeration;
6   import java.util.Hashtable;
7   
8   import javax.swing.filechooser.FileFilter;
9   
10  public class XMLFileFilter extends FileFilter {
11  
12      private static String TYPE_UNKNOWN = "Type Unknown";
13      private static String HIDDEN_FILE = "Hidden File";
14  
15      private Hashtable filters = null;
16      private String description = null;
17      private String fullDescription = null;
18      private boolean useExtensionsInDescription = true;
19  
20      /***
21       * Creates a file filter. If no filters are added, then all
22       * files are accepted.
23       *
24       * @see #addExtension
25       */
26      public XMLFileFilter() {
27  	this.filters = new Hashtable();
28      }
29  
30      /***
31       * Creates a file filter that accepts files with the given extension.
32       * Example: new ExampleFileFilter("jpg");
33       *
34       * @see #addExtension
35       */
36      public XMLFileFilter(String extension) {
37  	this(extension,null);
38      }
39  
40      /***
41       * Creates a file filter that accepts the given file type.
42       *
43       * Note that the "." before the extension is not needed. If
44       * provided, it will be ignored.
45       *
46       * @see #addExtension
47       */
48      public XMLFileFilter(String extension, String description) {
49          this();
50          if( extension != null )
51              addExtension(extension);
52          if( description != null )
53              setDescription(description);
54      }
55  
56      /***
57       * Creates a file filter from the given string array.
58       *
59       * Note that the "." before the extension is not needed adn
60       * will be ignored.
61       *
62       * @see #addExtension
63       */
64      public XMLFileFilter(String[] filters) {
65          this(filters, null);
66      }
67  
68      /***
69       * Creates a file filter from the given string array and description.
70       *
71       * Note that the "." before the extension is not needed and will be ignored.
72       *
73       * @see #addExtension
74       */
75      public XMLFileFilter(String[] filters, String description) {
76          this();
77          for (int i = 0; i < filters.length; i++) {
78              // add filters one by one
79  	    	addExtension(filters[i]);
80  		}
81   		if(description!=null) setDescription(description);
82      }
83  
84      /***
85       * Return true if this file should be shown in the directory pane,
86       * false if it shouldn't.
87       *
88       * Files that begin with "." are ignored.
89       *
90       * @see #getExtension
91       * @see FileFilter#accepts
92       */
93      public boolean accept(File f) {
94          if(f != null) {
95              if(f.isDirectory()) {
96                  return true;
97              }
98              String extension = getExtension(f);
99              if(extension != null && filters.get(getExtension(f)) != null) {
100                 return true;
101             };
102         }
103         return false;
104     }
105 
106     /***
107      * Return the extension portion of the file's name .
108      *
109      * @see #getExtension
110      * @see FileFilter#accept
111      */
112      public String getExtension(File f) {
113          if(f != null) {
114              String filename = f.getName();
115              int i = filename.lastIndexOf('.');
116              if(i>0 && i<filename.length()-1) {
117                  return filename.substring(i+1).toLowerCase();
118              };
119          }
120          return null;
121     }
122 
123     /***
124      * Adds a filetype "dot" extension to filter against.
125      *
126      * For example: the following code will create a filter that filters
127      * out all files except those that end in ".jpg" and ".tif":
128      *
129      * Note that the "." before the extension is not needed and will be ignored.
130      */
131     public void addExtension(String extension) {
132         if(filters == null) {
133 	    	filters = new Hashtable(5);
134         }
135         filters.put(extension.toLowerCase(), this);
136         fullDescription = null;
137     }
138 
139 
140     /***
141      * Returns the human readable description of this filter.
142      *
143      * @see setDescription
144      * @see setExtensionListInDescription
145      * @see isExtensionListInDescription
146      * @see FileFilter#getDescription
147      */
148     public String getDescription() {
149         if(fullDescription == null) {
150             if(description == null || isExtensionListInDescription()) {
151                 fullDescription = description==null ? "(" : description + " (";
152                 // build the description from the extension list
153                 Enumeration extensions = filters.keys();
154                 if(extensions != null) {
155                     fullDescription += "." + (String) extensions.nextElement();
156                     while (extensions.hasMoreElements()) {
157                         fullDescription += ", ." + (String) extensions.nextElement();
158                     }
159                 }
160                 fullDescription += ")";
161             } else {
162 			fullDescription = description;
163             }
164         }
165 		return fullDescription;
166     }
167 
168     /***
169      * Sets the human readable description of this filter.
170      *
171      * @see setDescription
172      * @see setExtensionListInDescription
173      * @see isExtensionListInDescription
174      */
175     public void setDescription(String description) {
176         this.description = description;
177 		fullDescription = null;
178     }
179 
180     /***
181      * Determines whether the extension list (.jpg, .gif, etc) should
182      * show up in the human readable description.
183      *
184      * Only relevent if a description was provided in the constructor
185      * or using setDescription();
186      *
187      * @see getDescription
188      * @see setDescription
189      * @see isExtensionListInDescription
190      */
191     public void setExtensionListInDescription(boolean b) {
192         useExtensionsInDescription = b;
193         fullDescription = null;
194     }
195 
196     /***
197      * Returns whether the extension list (.jpg, .gif, etc) should
198      * show up in the human readable description.
199      *
200      * Only relevent if a description was provided in the constructor
201      * or using setDescription();
202      *
203      * @see getDescription
204      * @see setDescription
205      * @see setExtensionListInDescription
206      */
207     public boolean isExtensionListInDescription() {
208         return useExtensionsInDescription;
209     }
210 }