001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.monitor.generic;
028:
029: import javax.swing.filechooser.FileFilter;
030: import java.io.File;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033:
034: /**
035: * A convenience implementation of FileFilter that filters out
036: * all files except for those type extensions that it knows about.
037: *
038: * Extensions are of the type ".foo", which is typically found on
039: * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
040: *
041: * Example - create a new filter that filters out all files
042: * but gif and jpg image files:
043: *
044: * JFileChooser chooser = new JFileChooser();
045: * ExtensionFileFilter filter = new ExtensionFileFilter(
046: * new String{"gif", "jpg"}, "JPEG & GIF Images")
047: * chooser.addChoosableFileFilter(filter);
048: * chooser.showOpenDialog(this);
049: *
050: * From JDK1.2.1 demo.
051: */
052:
053: public class ExtensionFileFilter extends FileFilter {
054:
055: // private static String TYPE_UNKNOWN = "Type Unknown";
056: // private static String HIDDEN_FILE = "Hidden File";
057:
058: private Hashtable filters = null;
059: private String description = null;
060: private String fullDescription = null;
061: private boolean useExtensionsInDescription = true;
062:
063: /**
064: * Creates a file filter. If no filters are added, then all
065: * files are accepted.
066: *
067: * @see #addExtension
068: */
069: public ExtensionFileFilter() {
070: this .filters = new Hashtable();
071: }
072:
073: /**
074: * Creates a file filter that accepts files with the given extension.
075: * Example: new ExtensionFileFilter("jpg");
076: *
077: * @see #addExtension
078: */
079: public ExtensionFileFilter(String extension) {
080: this (extension, null);
081: }
082:
083: /**
084: * Creates a file filter that accepts the given file type.
085: * Example: new ExtensionFileFilter("jpg", "JPEG Image Images");
086: *
087: * Note that the "." before the extension is not needed. If
088: * provided, it will be ignored.
089: *
090: * @see #addExtension
091: */
092: public ExtensionFileFilter(String extension, String description) {
093: this ();
094: if (extension != null)
095: addExtension(extension);
096: if (description != null)
097: setDescription(description);
098: }
099:
100: /**
101: * Creates a file filter from the given string array.
102: * Example: new ExtensionFileFilter(String {"gif", "jpg"});
103: *
104: * Note that the "." before the extension is not needed and
105: * will be ignored.
106: *
107: * @see #addExtension
108: */
109: public ExtensionFileFilter(String[] filters) {
110: this (filters, null);
111: }
112:
113: /**
114: * Creates a file filter from the given string array and description.
115: * Example: new ExtensionFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
116: *
117: * Note that the "." before the extension is not needed and will be ignored.
118: *
119: * @see #addExtension
120: */
121: public ExtensionFileFilter(String[] filters, String description) {
122: this ();
123: for (int i = 0; i < filters.length; i++) {
124: // add filters one by one
125: addExtension(filters[i]);
126: }
127: if (description != null)
128: setDescription(description);
129: }
130:
131: /**
132: * Return true if this file should be shown in the directory pane,
133: * false if it shouldn't.
134: *
135: * Files that begin with "." are ignored.
136: *
137: * @see #getExtension
138: * @see FileFilter#accept
139: */
140: public boolean accept(File f) {
141: if (f != null) {
142: if (f.isDirectory()) {
143: return true;
144: }
145: String extension = getExtension(f);
146: if (extension != null
147: && filters.get(getExtension(f)) != null) {
148: return true;
149: }
150: ;
151: }
152: return false;
153: }
154:
155: /**
156: * Return the extension portion of the file's name .
157: *
158: * @see #getExtension
159: * @see FileFilter#accept
160: */
161: public String getExtension(File f) {
162: if (f != null) {
163: String filename = f.getName();
164: int i = filename.lastIndexOf('.');
165: if (i > 0 && i < filename.length() - 1) {
166: return filename.substring(i + 1).toLowerCase();
167: }
168: ;
169: }
170: return null;
171: }
172:
173: /**
174: * Adds a filetype "dot" extension to filter against.
175: *
176: * For example: the following code will create a filter that filters
177: * out all files except those that end in ".jpg" and ".tif":
178: *
179: * ExtensionFileFilter filter = new ExtensionFileFilter();
180: * filter.addExtension("jpg");
181: * filter.addExtension("tif");
182: *
183: * Note that the "." before the extension is not needed and will be ignored.
184: */
185: public void addExtension(String extension) {
186: if (filters == null) {
187: filters = new Hashtable(5);
188: }
189: filters.put(extension.toLowerCase(), this );
190: fullDescription = null;
191: }
192:
193: /**
194: * Returns the human readable description of this filter. For
195: * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
196: *
197: * @see #setDescription
198: * @see #setExtensionListInDescription
199: * @see #isExtensionListInDescription
200: * @see FileFilter#getDescription
201: */
202: public String getDescription() {
203: if (fullDescription == null) {
204: if (description == null || isExtensionListInDescription()) {
205: fullDescription = description == null ? "("
206: : description + " (";
207: // build the description from the extension list
208: Enumeration extensions = filters.keys();
209: if (extensions != null) {
210: // fullDescription += "." + (String) extensions.nextElement();
211: fullDescription += (String) extensions
212: .nextElement();
213: while (extensions.hasMoreElements()) {
214: fullDescription += ", "
215: + (String) extensions.nextElement();
216: }
217: }
218: fullDescription += ")";
219: } else {
220: fullDescription = description;
221: }
222: }
223: return fullDescription;
224: }
225:
226: /**
227: * Sets the human readable description of this filter. For
228: * example: filter.setDescription("Gif and JPG Images");
229: *
230: * @see #setDescription
231: * @see #setExtensionListInDescription
232: * @see #isExtensionListInDescription
233: */
234: public void setDescription(String description) {
235: this .description = description;
236: fullDescription = null;
237: }
238:
239: /**
240: * Determines whether the extension list (.jpg, .gif, etc) should
241: * show up in the human readable description.
242: *
243: * Only relevent if a description was provided in the constructor
244: * or using setDescription();
245: *
246: * @see #getDescription
247: * @see #setDescription
248: * @see #isExtensionListInDescription
249: */
250: public void setExtensionListInDescription(boolean b) {
251: useExtensionsInDescription = b;
252: fullDescription = null;
253: }
254:
255: /**
256: * Returns whether the extension list (.jpg, .gif, etc) should
257: * show up in the human readable description.
258: *
259: * Only relevent if a description was provided in the constructor
260: * or using setDescription();
261: *
262: * @see #getDescription
263: * @see #setDescription
264: * @see #setExtensionListInDescription
265: */
266: public boolean isExtensionListInDescription() {
267: return useExtensionsInDescription;
268: }
269: }
|