~ubuntu-branches/ubuntu/precise/libjcommon-java/precise

« back to all changes in this revision

Viewing changes to source/org/jfree/ui/FilesystemFilter.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-10-01 14:12:07 UTC
  • mfrom: (1.1.2 upstream) (4 edgy)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20061001141207-2aipqlhpmn45t3ng
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ========================================================================
 
2
 * JCommon : a free general purpose class library for the Java(tm) platform
 
3
 * ========================================================================
 
4
 *
 
5
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 
6
 * 
 
7
 * Project Info:  http://www.jfree.org/jcommon/index.html
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or modify it 
 
10
 * under the terms of the GNU Lesser General Public License as published by 
 
11
 * the Free Software Foundation; either version 2.1 of the License, or 
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful, but 
 
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 
16
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 
17
 * License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 
22
 * USA.  
 
23
 *
 
24
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 
25
 * in the United States and other countries.]
 
26
 *
 
27
 * ------------------------
 
28
 * ExtensionFileFilter.java
 
29
 * ------------------------
 
30
 * (C) Copyright 2000-2004, by Object Refinery Limited.
 
31
 *
 
32
 * Original Author:  David Gilbert (for Object Refinery Limited);
 
33
 * Contributor(s):   -;
 
34
 *
 
35
 * $Id: FilesystemFilter.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
 
36
 *
 
37
 * Changes (from 26-Oct-2001)
 
38
 * --------------------------
 
39
 * 01-Jun-2005 : Updated javadoc.
 
40
 */
 
41
package org.jfree.ui;
 
42
 
 
43
import java.io.File;
 
44
import java.io.FilenameFilter;
 
45
import javax.swing.filechooser.FileFilter;
 
46
 
 
47
/**
 
48
 * A filesystem filter.
 
49
 *
 
50
 * @author David Gilbert
 
51
 */
 
52
public class FilesystemFilter extends FileFilter implements FilenameFilter {
 
53
 
 
54
    /** The file extension, which should be accepted. */
 
55
    private String[] fileext;
 
56
    /** The filter description. */
 
57
    private String descr;
 
58
    /** A flag indicating whether to accept directories. */
 
59
    private boolean accDirs;
 
60
 
 
61
    /**
 
62
     * Creates a new filter.
 
63
     *
 
64
     * @param fileext the file extension.
 
65
     * @param descr   the description.
 
66
     */
 
67
    public FilesystemFilter(final String fileext, final String descr) {
 
68
        this(fileext, descr, true);
 
69
    }
 
70
 
 
71
    /**
 
72
     * Creates a new filter.
 
73
     *
 
74
     * @param fileext the file extension.
 
75
     * @param descr   the description.
 
76
     * @param accDirs accept directories?
 
77
     */
 
78
    public FilesystemFilter(final String fileext, final String descr,
 
79
                            final boolean accDirs) {
 
80
        this(new String[]{fileext}, descr, accDirs);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Creates a new filter.
 
85
     *
 
86
     * @param fileext the file extension.
 
87
     * @param descr   the description.
 
88
     * @param accDirs accept directories?
 
89
     * @throws NullPointerException if the file extensions are null.
 
90
     */
 
91
    public FilesystemFilter(final String[] fileext, final String descr,
 
92
                            final boolean accDirs) {
 
93
        this.fileext = (String[]) fileext.clone();
 
94
        this.descr = descr;
 
95
        this.accDirs = accDirs;
 
96
    }
 
97
 
 
98
 
 
99
    /**
 
100
     * Returns <code>true</code> if the file is accepted, and <code>false</code> otherwise.
 
101
     *
 
102
     * @param dir  the directory.
 
103
     * @param name the file name.
 
104
     * @return A boolean.
 
105
     */
 
106
    public boolean accept(final File dir, final String name) {
 
107
        final File f = new File(dir, name);
 
108
        if (f.isDirectory() && acceptsDirectories()) {
 
109
            return true;
 
110
        }
 
111
 
 
112
        for (int i = 0; i < fileext.length; i++) {
 
113
            if (name.endsWith(this.fileext[i])) {
 
114
                return true;
 
115
            }
 
116
        }
 
117
        return false;
 
118
    }
 
119
 
 
120
    /**
 
121
     * Returns <code>true</code> if the specified file matches the requirements of this
 
122
     * filter, and <code>false</code> otherwise.
 
123
     *
 
124
     * @param dir the file or directory.
 
125
     * @return A boolean.
 
126
     */
 
127
    public boolean accept(final File dir) {
 
128
        if (dir.isDirectory() && acceptsDirectories()) {
 
129
            return true;
 
130
        }
 
131
 
 
132
        for (int i = 0; i < fileext.length; i++) {
 
133
            if (dir.getName().endsWith(this.fileext[i])) {
 
134
                return true;
 
135
            }
 
136
        }
 
137
        return false;
 
138
    }
 
139
 
 
140
    /**
 
141
     * Returns the filter description.
 
142
     *
 
143
     * @return The filter description.
 
144
     */
 
145
    public String getDescription() {
 
146
        return this.descr;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Sets the flag that controls whether or not the filter accepts directories.
 
151
     *
 
152
     * @param b a boolean.
 
153
     */
 
154
    public void acceptDirectories(final boolean b) {
 
155
        this.accDirs = b;
 
156
    }
 
157
 
 
158
    /**
 
159
     * Returns the flag that indicates whether or not the filter accepts directories.
 
160
     *
 
161
     * @return A boolean.
 
162
     */
 
163
    public boolean acceptsDirectories() {
 
164
        return this.accDirs;
 
165
    }
 
166
 
 
167
}