KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > ExtensionFileFilter


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import javax.swing.filechooser.*;
17 import java.io.File JavaDoc;
18 import java.io.Serializable JavaDoc;
19
20
21 /**
22  * Extension File Chooser
23  *
24  * @author Jorg Janke
25  * @version $Id: ExtensionFileFilter.java,v 1.3 2002/07/25 05:05:08 jjanke Exp $
26  */

27 public class ExtensionFileFilter extends FileFilter
28     implements Serializable JavaDoc
29 {
30     /**
31      * Constructor
32      */

33     public ExtensionFileFilter()
34     {
35         description = "";
36         extension = "";
37     } // ExtensionFileFilter
38

39     /**
40      * Constructor
41      * @param extension extension
42      * @param description description
43      */

44     public ExtensionFileFilter(String JavaDoc extension, String JavaDoc description)
45     {
46         this.description = description;
47         this.extension = extension;
48     } // ExtensionFileFilter
49

50     /**
51      * Description
52      * @return description
53      */

54     public String JavaDoc getDescription()
55     {
56         return description;
57     }
58     //
59
public void setDescription(String JavaDoc newDescription)
60     {
61         description = newDescription;
62     }
63     //
64
private String JavaDoc description;
65
66     /**
67      * Extension
68      * @param newExtension ext
69      */

70     public void setExtension(String JavaDoc newExtension)
71     {
72         extension = newExtension;
73     }
74     //
75
public String JavaDoc getExtension()
76     {
77         return extension;
78     }
79     //
80
private String JavaDoc extension;
81
82     /**
83      * Accept File
84      * @param file file to be tested
85      * @return true if OK
86      */

87     public boolean accept(File JavaDoc file)
88     {
89         if (file.isDirectory())
90             return true;
91
92         String JavaDoc ext = file.getName();
93         int pos = ext.lastIndexOf('.');
94
95         // No extension
96
if (pos == -1)
97             return false;
98
99         ext = ext.substring(pos+1);
100
101         if (ext.equalsIgnoreCase(extension))
102             return true;
103
104         return false;
105     } // accept
106

107
108     /**
109      * Verify file name with filer
110      * @param file file
111      * @param filter filter
112      * @return file name
113      */

114     public static String JavaDoc getFileName(File JavaDoc file, FileFilter filter)
115     {
116         return getFile(file, filter).getAbsolutePath();
117     } // getFileName
118

119     /**
120      * Verify file with filter
121      * @param file file
122      * @param filter filter
123      * @return file
124      */

125     public static File JavaDoc getFile(File JavaDoc file, FileFilter filter)
126     {
127         String JavaDoc fName = file.getAbsolutePath();
128         if (fName == null || fName.equals(""))
129             fName = "Compiere";
130         //
131
ExtensionFileFilter eff = null;
132         if (filter instanceof ExtensionFileFilter)
133             eff = (ExtensionFileFilter)filter;
134         else
135             return file;
136         //
137
int pos = fName.lastIndexOf('.');
138
139         // No extension
140
if (pos == -1)
141         {
142             fName += '.' + eff.getExtension();
143             return new File JavaDoc(fName);
144         }
145
146         String JavaDoc ext = fName.substring(pos+1);
147
148         // correct extension
149
if (ext.equalsIgnoreCase(eff.getExtension()))
150             return file;
151
152         fName += '.' + eff.getExtension();
153         return new File JavaDoc(fName);
154     } // getFile
155

156 } // ExtensionFileFilter
157
Popular Tags