KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > FileFilters


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.util;
57
58 import java.io.File JavaDoc;
59
60 import javax.swing.filechooser.FileFilter JavaDoc;
61
62 import org.objectstyle.cayenne.conf.Configuration;
63 import org.objectstyle.cayenne.project.DataMapFile;
64
65 /**
66  * A collection of common file filters used by CayenneModeler JFileChoosers.
67  *
68  * @since 1.1
69  * @author Andrei Adamchik
70  */

71 public class FileFilters {
72
73     protected static final FileFilter JavaDoc applicationFilter = new ApplicationFileFilter();
74     protected static final FileFilter JavaDoc velotemplateFilter = new VelotemplateFileFilter();
75     protected static final FileFilter JavaDoc eomodelFilter = new EOModelFileFilter();
76     protected static final FileFilter JavaDoc eomodelSelectFilter = new EOModelSelectFilter();
77     protected static final FileFilter JavaDoc dataMapFilter = new DataMapFileFilter();
78     protected static final FileFilter JavaDoc classArchiveFilter = new JavaClassArchiveFilter();
79
80     /**
81      * Returns a FileFilter for java class archive files, such as JAR and ZIP.
82      */

83     public static FileFilter JavaDoc getClassArchiveFilter() {
84         return classArchiveFilter;
85     }
86
87     /**
88      * Returns a FileFilter used to select Cayenne Application project files.
89      */

90     public static FileFilter JavaDoc getApplicationFilter() {
91         return applicationFilter;
92     }
93
94     /**
95      * Returns a FileFilter used to select DataMap files.
96      */

97     public static FileFilter JavaDoc getDataMapFilter() {
98         return dataMapFilter;
99     }
100
101     /**
102      * Returns a FileFilter used to select Velocity template files.
103      * Filters files with ".vm" extension.
104      */

105     public static FileFilter JavaDoc getVelotemplateFilter() {
106         return velotemplateFilter;
107     }
108
109     /**
110      * Returns a FileFilter used to filter EOModels. This filter will only display
111      * directories and index.eomodeld files.
112      */

113     public static FileFilter JavaDoc getEOModelFilter() {
114         return eomodelFilter;
115     }
116
117     /**
118      * Returns FileFilter that defines the rules for EOModel selection.
119      * This filter will only allow selection of the following
120      * files/directories:
121      * <ul>
122      * <li>Directories with name matching <code>*.eomodeld</code>
123      * that contain <code>index.eomodeld</code>.</li>
124      * <li><code>index.eomodeld</code> files contained within
125      * <code>*.eomodeld</code> directory.</li>
126      * </ul>
127      */

128     public static FileFilter JavaDoc getEOModelSelectFilter() {
129         return eomodelSelectFilter;
130     }
131
132     static final class JavaClassArchiveFilter extends FileFilter JavaDoc {
133         public boolean accept(File JavaDoc f) {
134             if (f.isDirectory()) {
135                 return true;
136             }
137
138             String JavaDoc name = f.getName().toLowerCase();
139             return (name.length() > 4 && (name.endsWith(".jar") || name.endsWith(".zip")));
140         }
141
142         public String JavaDoc getDescription() {
143             return "Java Class Archive (*.jar,*.zip)";
144         }
145     }
146
147     static final class VelotemplateFileFilter extends FileFilter JavaDoc {
148         /**
149          * Accepts all *.vm files.
150          */

151         public boolean accept(File JavaDoc f) {
152             if (f.isDirectory()) {
153                 return true;
154             }
155
156             String JavaDoc name = f.getName();
157             return (name.endsWith(".vm") && !name.equals(".vm"));
158         }
159
160         public String JavaDoc getDescription() {
161             return "Velocity Templates (*.vm)";
162         }
163     }
164
165     static final class ApplicationFileFilter extends FileFilter JavaDoc {
166
167         /**
168          * Accepts all directories and all cayenne.xml files.
169          */

170         public boolean accept(File JavaDoc f) {
171             return f.isDirectory()
172                 || Configuration.DEFAULT_DOMAIN_FILE.equals(f.getName());
173         }
174
175         /**
176          * Returns description of this filter.
177          */

178         public String JavaDoc getDescription() {
179             return "Cayenne Applications (" + Configuration.DEFAULT_DOMAIN_FILE + ")";
180         }
181     }
182
183     static final class DataMapFileFilter extends FileFilter JavaDoc {
184
185         /**
186          * Accepts all directories and all *.map.xml files.
187          */

188         public boolean accept(File JavaDoc f) {
189             if (f.isDirectory()) {
190                 return true;
191             }
192
193             String JavaDoc name = f.getName();
194             if (name.endsWith(DataMapFile.LOCATION_SUFFIX)
195                 && !name.equals(DataMapFile.LOCATION_SUFFIX)) {
196                 return true;
197             }
198
199             return false;
200         }
201
202         /**
203          * Returns description of this filter.
204          */

205         public String JavaDoc getDescription() {
206             return "DataMaps (*" + DataMapFile.LOCATION_SUFFIX + ")";
207         }
208     }
209
210     static final class EOModelFileFilter extends FileFilter JavaDoc {
211         static final String JavaDoc EOM_SUFFIX = ".eomodeld";
212         static final String JavaDoc EOM_INDEX = "index" + EOM_SUFFIX;
213
214         /**
215          * Accepts all directories and <code>*.eomodeld/index.eomodeld</code> files.
216          */

217         public boolean accept(File JavaDoc f) {
218             if (f.isDirectory()) {
219                 return true;
220             }
221
222             File JavaDoc parent = f.getParentFile();
223             return parent != null
224                 && parent.getName().endsWith(EOM_SUFFIX)
225                 && EOM_INDEX.equals(f.getName());
226         }
227
228         public String JavaDoc getDescription() {
229             return "*" + EOM_SUFFIX;
230         }
231     }
232
233     static final class EOModelSelectFilter extends FileFilter JavaDoc {
234         /**
235          * Accepts all directories and <code>*.eomodeld/index.eomodeld</code> files.
236          *
237          * @see EOModelSelectFilter#accept(File)
238          */

239         public boolean accept(File JavaDoc f) {
240             if (f.isDirectory()) {
241                 if (f.getName().endsWith(EOModelFileFilter.EOM_SUFFIX)
242                     && new File JavaDoc(f, EOModelFileFilter.EOM_INDEX).exists()) {
243
244                     return true;
245                 }
246             }
247             else if (f.isFile()) {
248                 File JavaDoc parent = f.getParentFile();
249                 if (parent != null
250                     && parent.getName().endsWith(EOModelFileFilter.EOM_SUFFIX)
251                     && EOModelFileFilter.EOM_INDEX.equals(f.getName())) {
252                     return true;
253                 }
254             }
255
256             return false;
257         }
258
259         public String JavaDoc getDescription() {
260             return "*" + EOModelFileFilter.EOM_SUFFIX;
261         }
262     }
263 }
264
Popular Tags