001: /*
002: * FileSelector.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.io.File;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028:
029: import javax.swing.filechooser.FileFilter;
030:
031: /* ----------------------------------------------------------
032: * CVS NOTE: Changes to the CVS repository prior to the
033: * release of version 3.0.0beta1 has meant a
034: * resetting of CVS revision numbers.
035: * ----------------------------------------------------------
036: */
037:
038: /**
039: * A convenience implementation of FileFilter that filters out
040: * all files except for those type extensions that it knows about.
041: *
042: * Extensions are of the type ".foo", which is typically found on
043: * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
044: *
045: * Example - create a new filter that filters out all files
046: * but gif and jpg image files:
047: *
048: * JFileChooser chooser = new JFileChooser();
049: * ExampleFileFilter filter = new ExampleFileFilter(
050: * new String{"gif", "jpg"}, "JPEG & GIF Images")
051: * chooser.addChoosableFileFilter(filter);
052: * chooser.showOpenDialog(this);
053: *
054: * @version 1.9 04/23/99
055: * @author Jeff Dinkins
056: */
057: public class FileSelector extends FileFilter {
058:
059: private static String TYPE_UNKNOWN = "Type Unknown";
060: private static String HIDDEN_FILE = "Hidden File";
061:
062: private Hashtable filters = null;
063: private String description = null;
064: private String fullDescription = null;
065: private boolean useExtensionsInDescription = true;
066:
067: /**
068: * Creates a file filter. If no filters are added, then all
069: * files are accepted.
070: *
071: * @see #addExtension
072: */
073: public FileSelector() {
074: this .filters = new Hashtable();
075: }
076:
077: /**
078: * Creates a file filter that accepts files with the given extension.
079: * Example: new ExampleFileFilter("jpg");
080: *
081: * @see #addExtension
082: */
083: public FileSelector(String extension) {
084: this (extension, null);
085: }
086:
087: /**
088: * Creates a file filter that accepts the given file type.
089: * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
090: *
091: * Note that the "." before the extension is not needed. If
092: * provided, it will be ignored.
093: *
094: * @see #addExtension
095: */
096: public FileSelector(String extension, String description) {
097: this ();
098: if (extension != null)
099: addExtension(extension);
100: if (description != null)
101: setDescription(description);
102: }
103:
104: /**
105: * Creates a file filter from the given string array.
106: * Example: new ExampleFileFilter(String {"gif", "jpg"});
107: *
108: * Note that the "." before the extension is not needed and
109: * will be ignored.
110: *
111: * @see #addExtension
112: */
113: public FileSelector(String[] filters) {
114: this (filters, null);
115: }
116:
117: /**
118: * Creates a file filter from the given string array and description.
119: * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
120: *
121: * Note that the "." before the extension is not needed and will be ignored.
122: *
123: * @see #addExtension
124: */
125: public FileSelector(String[] filters, String description) {
126: this ();
127: for (int i = 0; i < filters.length; i++) {
128: // add filters one by one
129: addExtension(filters[i]);
130: }
131: if (description != null)
132: setDescription(description);
133: }
134:
135: /**
136: * Return true if this file should be shown in the directory pane,
137: * false if it shouldn't.
138: *
139: * Files that begin with "." are ignored.
140: *
141: * @see #getExtension
142: * @see FileFilter#accepts
143: */
144: public boolean accept(File f) {
145: if (f != null) {
146: if (f.isDirectory()) {
147: return true;
148: }
149: String extension = getExtension(f);
150: if (extension != null
151: && filters.get(getExtension(f)) != null) {
152: return true;
153: }
154: ;
155: }
156: return false;
157: }
158:
159: /**
160: * Return the extension portion of the file's name .
161: *
162: * @see #getExtension
163: * @see FileFilter#accept
164: */
165: public String getExtension(File f) {
166: if (f != null) {
167: String filename = f.getName();
168: int i = filename.lastIndexOf('.');
169: if (i > 0 && i < filename.length() - 1) {
170: return filename.substring(i + 1).toLowerCase();
171: }
172: ;
173: }
174: return null;
175: }
176:
177: /**
178: * Adds a filetype "dot" extension to filter against.
179: *
180: * For example: the following code will create a filter that filters
181: * out all files except those that end in ".jpg" and ".tif":
182: *
183: * ExampleFileFilter filter = new ExampleFileFilter();
184: * filter.addExtension("jpg");
185: * filter.addExtension("tif");
186: *
187: * Note that the "." before the extension is not needed and will be ignored.
188: */
189: public void addExtension(String extension) {
190: if (filters == null) {
191: filters = new Hashtable(5);
192: }
193: filters.put(extension.toLowerCase(), this );
194: fullDescription = null;
195: }
196:
197: /**
198: * Returns the human readable description of this filter. For
199: * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
200: *
201: * @see setDescription
202: * @see setExtensionListInDescription
203: * @see isExtensionListInDescription
204: * @see FileFilter#getDescription
205: */
206: public String getDescription() {
207: if (fullDescription == null) {
208: if (description == null || isExtensionListInDescription()) {
209: fullDescription = description == null ? "("
210: : description + " (";
211: // build the description from the extension list
212: Enumeration extensions = filters.keys();
213: if (extensions != null) {
214: fullDescription += "."
215: + (String) extensions.nextElement();
216: while (extensions.hasMoreElements()) {
217: fullDescription += ", "
218: + (String) extensions.nextElement();
219: }
220: }
221: fullDescription += ")";
222: } else {
223: fullDescription = description;
224: }
225: }
226: return fullDescription;
227: }
228:
229: /**
230: * Sets the human readable description of this filter. For
231: * example: filter.setDescription("Gif and JPG Images");
232: *
233: * @see setDescription
234: * @see setExtensionListInDescription
235: * @see isExtensionListInDescription
236: */
237: public void setDescription(String description) {
238: this .description = description;
239: fullDescription = null;
240: }
241:
242: /**
243: * Determines whether the extension list (.jpg, .gif, etc) should
244: * show up in the human readable description.
245: *
246: * Only relevent if a description was provided in the constructor
247: * or using setDescription();
248: *
249: * @see getDescription
250: * @see setDescription
251: * @see isExtensionListInDescription
252: */
253: public void setExtensionListInDescription(boolean b) {
254: useExtensionsInDescription = b;
255: fullDescription = null;
256: }
257:
258: /**
259: * Returns whether the extension list (.jpg, .gif, etc) should
260: * show up in the human readable description.
261: *
262: * Only relevent if a description was provided in the constructor
263: * or using setDescription();
264: *
265: * @see getDescription
266: * @see setDescription
267: * @see setExtensionListInDescription
268: */
269: public boolean isExtensionListInDescription() {
270: return useExtensionsInDescription;
271: }
272:
273: }
|