KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > JMeterFileFilter


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/JMeterFileFilter.java,v 1.4 2004/02/13 02:21:37 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui;
20
21 import java.io.File JavaDoc;
22 import java.util.Arrays JavaDoc;
23
24 /**
25  * A file filter which allows files to be filtered based on a list of allowed
26  * extensions.
27  *
28  * @author unknown
29  * @version $Revision: 1.4 $
30  */

31 public class JMeterFileFilter
32     extends javax.swing.filechooser.FileFilter JavaDoc
33     implements java.io.FileFilter JavaDoc
34 {
35     /** The list of extensions allowed by this filter. */
36     private String JavaDoc[] exts;
37
38     /**
39      * Create a new JMeter file filter which allows the specified extensions.
40      * If the array of extensions contains no elements, any file will be
41      * allowed.
42      *
43      * @param extensions non-null array of allowed file extensions
44      */

45     public JMeterFileFilter(String JavaDoc[] extensions)
46     {
47         exts = extensions;
48     }
49
50     /**
51      * Determine if the specified file is allowed by this filter. The file
52      * will be allowed if it is a directory, or if the end of the filename
53      * matches one of the extensions allowed by this filter. The filename is
54      * converted to lower-case before making the comparison.
55      *
56      * @param f the File being tested
57      *
58      * @return true if the file should be allowed, false otherwise
59      */

60     public boolean accept(File JavaDoc f)
61     {
62         return f.isDirectory() || accept(f.getName().toLowerCase());
63     }
64
65     /**
66      * Determine if the specified filename is allowed by this filter. The file
67      * will be allowed if the end of the filename matches one of the extensions
68      * allowed by this filter. The comparison is case-sensitive. If no
69      * extensions were provided for this filter, the file will always be
70      * allowed.
71      *
72      * @param filename the filename to test
73      * @return true if the file should be allowed, false otherwise
74      */

75     public boolean accept(String JavaDoc filename)
76     {
77         if (exts.length == 0)
78         {
79             return true;
80         }
81         
82         for (int i = 0; i < exts.length; i++)
83         {
84             if (filename.endsWith(exts[i]))
85             {
86                 return true;
87             }
88         }
89         
90         return false;
91     }
92
93     /**
94      * Get a description for this filter.
95      *
96      * @return a description for this filter
97      */

98     public String JavaDoc getDescription()
99     {
100         return "JMeter " + Arrays.asList(exts).toString();
101     }
102 }
103
Popular Tags