KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > multiServer > launch > ExtensionFilter


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

22 package org.enhydra.multiServer.launch;
23 import java.io.File JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.swing.filechooser.FileFilter JavaDoc;
26
27 /**
28  * An implementation of FileFilter that filters out all files
29  * except for those matching a set of given extensions.
30  */

31 public class ExtensionFilter extends FileFilter JavaDoc
32     implements java.io.FileFilter JavaDoc {
33     private Vector JavaDoc ext = new Vector JavaDoc();
34     private String JavaDoc descTitle = new String JavaDoc();
35     private boolean directoryValid = true;
36
37     /**
38      * Creates an extension file filter without an extensions set.
39      */

40     public ExtensionFilter() {}
41
42     /**
43      * Return true if this file should be shown in a file chooser.
44      */

45     public boolean accept(File JavaDoc f) {
46         boolean acceptFile = false;
47         int count = ext.size();
48         String JavaDoc fileExt = getExtension(f);
49
50         if (f == null) {
51             acceptFile = true;
52         } else if (f.isDirectory()) {
53             acceptFile = isDirectoryValid();
54         } else if (fileExt != null) {
55             for (int i = 0; i < count; i++) {
56                 String JavaDoc current = (String JavaDoc) ext.elementAt(i);
57
58                 if (fileExt.equalsIgnoreCase(current)) {
59                     acceptFile = true;
60                 }
61             }
62         }
63         return acceptFile;
64     }
65
66     /**
67      * Return the file extension.
68      */

69     private String JavaDoc getExtension(File JavaDoc f) {
70         String JavaDoc ex = null;
71
72         if (f != null) {
73             String JavaDoc filename = f.getName();
74             int i = filename.lastIndexOf('.');
75
76             if (i > 0 && i < filename.length() - 1) {
77                 ex = filename.substring(i + 1).toLowerCase().trim();
78             }
79         }
80         return ex;
81     }
82
83     /**
84      * Adds an an extension to the list of acceptable file
85      * types.
86      */

87     public void addExtension(String JavaDoc e) {
88         ext.addElement(e.toLowerCase().trim());
89     }
90
91     /**
92      * Returns the human readable description of this filter.
93      */

94     public String JavaDoc getDescription() {
95         StringBuffer JavaDoc desc = new StringBuffer JavaDoc();
96
97         desc.append(getDescriptionTitle());
98         desc.append(" (");
99         if (ext == null) {
100             desc.append("*.*");
101         } else {
102             desc.append("*.");
103             int count = ext.size();
104
105             for (int i = 0; i < count; i++) {
106                 desc.append((String JavaDoc) ext.elementAt(i));
107                 if (i < (count - 1)) {
108                     desc.append(", ");
109                 }
110             }
111         }
112         desc.append(")");
113         return desc.toString();
114     }
115
116     /**
117      * Sets the portion of the description that does not
118      * include extensions.
119      */

120     public void setDescriptionTitle(String JavaDoc d) {
121         this.descTitle = d;
122     }
123
124     /**
125      * Gets the portion of the description that does not
126      * include extensions.
127      */

128     public String JavaDoc getDescriptionTitle() {
129         return descTitle;
130     }
131
132     public void setDirectoryValid(boolean valid) {
133         directoryValid = valid;
134     }
135
136     public boolean isDirectoryValid() {
137         return directoryValid;
138     }
139
140 }
141
Popular Tags