01: /*
02: * <copyright>
03: *
04: * Copyright 2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26: package org.cougaar.tools.csmart.ui.util;
27:
28: import javax.swing.filechooser.FileFilter;
29: import javax.swing.*;
30: import java.io.File;
31:
32: /**
33: * Created by IntelliJ IDEA.
34: * User: bkrisler
35: * Date: Apr 25, 2003
36: * Time: 6:50:05 AM
37: * To change this template use Options | File Templates.
38: */
39: public class ChooserUtils {
40:
41: public FileFilter getFileFilter(String ext, String desc) {
42: return new MyFileFilter(ext, desc);
43: }
44:
45: public FileFilter getFileFilter(String[] exts, String desc) {
46: return new MyFileFilter(exts, desc);
47: }
48:
49: private class MyFileFilter extends FileFilter {
50: String[] extensions;
51: String description;
52:
53: public MyFileFilter(String ext, String desc) {
54: this (new String[] { ext }, desc);
55: }
56:
57: public MyFileFilter(String[] exts, String desc) {
58: extensions = new String[exts.length];
59: for (int i = exts.length - 1; i >= 0; i--) {
60: extensions[i] = exts[i].toLowerCase();
61: }
62: description = (desc == null ? exts[0] + " files" : desc);
63: }
64:
65: public boolean accept(File f) {
66: if (f.isDirectory()) {
67: return true;
68: }
69:
70: String name = f.getName().toLowerCase();
71: for (int i = extensions.length - 1; i >= 0; i--) {
72: if (name.endsWith(extensions[i])) {
73: return true;
74: }
75: }
76: return false;
77: }
78:
79: public String getDescription() {
80: return description;
81: }
82: }
83: }
|