KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > ExtensionFilter


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.common;
25
26 // Standard imports
27
import java.io.File JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
30
31 /**
32  * An implementation of FileFilter that filters out all files
33  * except for those matching a set of given extensions.
34  */

35 public class ExtensionFilter extends FileFilter JavaDoc
36     implements java.io.FileFilter JavaDoc {
37     private Vector JavaDoc include = new Vector JavaDoc();
38     private Vector JavaDoc exclude = new Vector JavaDoc();
39     private String JavaDoc descTitle = new String JavaDoc();
40     private boolean directoryValid = true;
41     private boolean excludeOnly = false;
42
43     /**
44      * Creates an extension file filter without an extensions set.
45      */

46     public ExtensionFilter() {}
47
48     /**
49      * Return true if this file should be shown in a file chooser.
50      */

51     public boolean accept(File JavaDoc f) {
52         boolean acceptFile = false;
53         int count = include.size();
54         PathHandle handle = null;
55
56         if (f == null) {
57             acceptFile = true;
58         } else if (f.isDirectory()) {
59             acceptFile = isDirectoryValid();
60         } else {
61             handle = PathHandle.createPathHandle(f);
62             if (isExcludeOnly()) {
63                 acceptFile = (!exclude(handle));
64             } else {
65                 for (int i = 0; i < count; i++) {
66                     String JavaDoc current = include.elementAt(i).toString();
67
68                     if (handle.hasExtension(current)) {
69                         acceptFile = (!exclude(handle));
70                     }
71                 }
72             }
73         }
74         return acceptFile;
75     }
76
77     private boolean exclude(PathHandle handle) {
78         boolean ex = false;
79         String JavaDoc cursor = new String JavaDoc();
80
81         for (int i = 0; i < exclude.size(); i++) {
82             cursor = exclude.elementAt(i).toString();
83             if (handle.endsWith(cursor)) {
84                 ex = true;
85                 break;
86             }
87         }
88         return ex;
89     }
90
91     /**
92      * Adds an an extension to the list of acceptable file
93      * types.
94      */

95     public void addExtension(String JavaDoc in) {
96         if (in != null) {
97             in = in.toLowerCase().trim();
98         }
99         if (!include.contains(in)) {
100             include.addElement(in);
101         }
102     }
103
104     public void addExclusion(String JavaDoc ex) {
105         if (ex != null) {
106             ex = ex.toLowerCase().trim();
107         }
108         if (!exclude.contains(ex)) {
109             exclude.addElement(ex);
110         }
111     }
112
113     /**
114      * Returns the human readable description of this filter.
115      */

116     public String JavaDoc getDescription() {
117         StringBuffer JavaDoc desc = new StringBuffer JavaDoc();
118
119         desc.append(getDescriptionTitle());
120         desc.append(' ');
121         desc.append('(');
122         if (include.size() == 0) {
123             desc.append('*');
124             desc.append('.');
125             desc.append('*');
126         } else {
127             desc.append('*');
128             desc.append('.');
129             int count = include.size();
130
131             for (int i = 0; i < count; i++) {
132                 desc.append((String JavaDoc) include.elementAt(i));
133                 if (i < (count - 1)) {
134                     desc.append(',');
135                     desc.append(' ');
136                     desc.append('*');
137                     desc.append('.');
138                 }
139             }
140         }
141         desc.append(')');
142         return desc.toString();
143     }
144
145     /**
146      * Sets the portion of the description that does not
147      * include extensions.
148      */

149     public void setDescriptionTitle(String JavaDoc d) {
150         this.descTitle = d;
151     }
152
153     /**
154      * Gets the portion of the description that does not
155      * include extensions.
156      */

157     public String JavaDoc getDescriptionTitle() {
158         return descTitle;
159     }
160
161     public void setDirectoryValid(boolean valid) {
162         directoryValid = valid;
163     }
164
165     public boolean isDirectoryValid() {
166         return directoryValid;
167     }
168
169     public boolean isExcludeOnly() {
170         return excludeOnly;
171     }
172
173     public void setExcludeOnly(boolean b) {
174         excludeOnly = b;
175     }
176
177 }
178
Popular Tags