Source Code Cross Referenced for AFileFilter.java in  » XML-UI » xui32 » com » xoetrope » helper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.helper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.helper;
002:
003:        import java.io.File;
004:        import java.util.Enumeration;
005:        import java.util.Hashtable;
006:
007:        import javax.swing.filechooser.FileFilter;
008:
009:        /**
010:         * A convenience implementation of FileFilter that filters out
011:         * all files except for those type extensions that it knows about.
012:         *
013:         * Extensions are of the type ".foo", which is typically found on
014:         * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
015:         *
016:         * Example - create a new filter that filerts out all files
017:         * but gif and jpg image files:
018:         *
019:         *     JFileChooser chooser = new JFileChooser();
020:         *     AFileFilter filter = new AFileFilter(
021:         *                   new String{"gif", "jpg"}, "JPEG & GIF Images")
022:         *     chooser.addChoosableFileFilter(filter);
023:         *     chooser.showOpenDialog(this);
024:         *
025:         * @version 1.8 08/26/98
026:         * @author Jeff Dinkins
027:         */
028:        public class AFileFilter extends FileFilter {
029:            private Hashtable filters = null;
030:            private String description = null;
031:            private String fullDescription = null;
032:            private boolean useExtensionsInDescription = true;
033:            private boolean dirMustExist = false;
034:            private boolean fileMustExist = false;
035:
036:            /**
037:             * Creates a file filter. If no filters are added, then all
038:             * files are accepted.
039:             *
040:             * @see #addExtension
041:             */
042:            public AFileFilter() {
043:                this .filters = new Hashtable();
044:            }
045:
046:            /**
047:             * Creates a file filter that accepts files with the given extension.
048:             * Example: new AFileFilter("jpg");
049:             *
050:             * @see #addExtension
051:             */
052:            public AFileFilter(String extension) {
053:                this (extension, null);
054:            }
055:
056:            /**
057:             * Creates a file filter that accepts the given file type.
058:             * Example: new AFileFilter("jpg", "JPEG Image Images");
059:             *
060:             * Note that the "." before the extension is not needed. If
061:             * provided, it will be ignored.
062:             *
063:             * @see #addExtension
064:             */
065:            public AFileFilter(String extension, String description) {
066:                this ();
067:                if (extension != null)
068:                    addExtension(extension);
069:                if (description != null)
070:                    setDescription(description);
071:            }
072:
073:            /**
074:             * Creates a file filter from the given string array.
075:             * Example: new AFileFilter(String {"gif", "jpg"});
076:             *
077:             * Note that the "." before the extension is not needed adn
078:             * will be ignored.
079:             *
080:             * @see #addExtension
081:             */
082:            public AFileFilter(String[] filters) {
083:                this (filters, null);
084:            }
085:
086:            /**
087:             * Creates a file filter from the given string array and description.
088:             * Example: new AFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
089:             *
090:             * Note that the "." before the extension is not needed and will be ignored.
091:             *
092:             * @see #addExtension
093:             */
094:            public AFileFilter(String[] filters, String description) {
095:                this ();
096:                for (int i = 0; i < filters.length; i++) {
097:                    // add filters one by one
098:                    addExtension(filters[i]);
099:                }
100:                if (description != null)
101:                    setDescription(description);
102:            }
103:
104:            /**
105:             * Return true if this file should be shown in the directory pane,
106:             * false if it shouldn't.
107:             *
108:             * Files that begin with "." are ignored.
109:             *
110:             * @see #getExtension
111:             * @see FileFilter#accepts
112:             */
113:            public boolean accept(File f) {
114:                if (f != null) {
115:                    if (f.isDirectory() && (!dirMustExist || f.exists()))
116:                        return true;
117:
118:                    String extension = getExtension(f);
119:                    if (extension != null
120:                            && filters.get(getExtension(f)) != null) {
121:                        if (!fileMustExist || f.exists())
122:                            return true;
123:                    }
124:                }
125:                return false;
126:            }
127:
128:            /**
129:             * Return the extension portion of the file's name .
130:             *
131:             * @see #getExtension
132:             * @see FileFilter#accept
133:             */
134:            public String getExtension(File f) {
135:                if (f != null) {
136:                    String filename = f.getName();
137:                    int i = filename.lastIndexOf('.');
138:                    if (i > 0 && i < filename.length() - 1) {
139:                        return filename.substring(i + 1).toLowerCase();
140:                    }
141:                }
142:                return null;
143:            }
144:
145:            /**
146:             * Adds a filetype "dot" extension to filter against.
147:             *
148:             * For example: the following code will create a filter that filters
149:             * out all files except those that end in ".jpg" and ".tif":
150:             *
151:             *   AFileFilter filter = new AFileFilter();
152:             *   filter.addExtension("jpg");
153:             *   filter.addExtension("tif");
154:             *
155:             * Note that the "." before the extension is not needed and will be ignored.
156:             */
157:            public void addExtension(String extension) {
158:                if (filters == null) {
159:                    filters = new Hashtable(5);
160:                }
161:                filters.put(extension.toLowerCase(), this );
162:                fullDescription = null;
163:            }
164:
165:            /**
166:             * Returns the human readable description of this filter. For
167:             * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
168:             *
169:             * @see setDescription
170:             * @see setExtensionListInDescription
171:             * @see isExtensionListInDescription
172:             * @see FileFilter#getDescription
173:             */
174:            public String getDescription() {
175:                if (fullDescription == null) {
176:                    if (description == null || isExtensionListInDescription()) {
177:                        fullDescription = description == null ? "("
178:                                : description + " (";
179:                        // build the description from the extension list
180:                        Enumeration extensions = filters.keys();
181:                        if (extensions != null) {
182:                            fullDescription += "."
183:                                    + (String) extensions.nextElement();
184:                            while (extensions.hasMoreElements()) {
185:                                fullDescription += ", "
186:                                        + (String) extensions.nextElement();
187:                            }
188:                        }
189:                        fullDescription += ")";
190:                    } else {
191:                        fullDescription = description;
192:                    }
193:                }
194:                return fullDescription;
195:            }
196:
197:            /**
198:             * Sets the human readable description of this filter. For
199:             * example: filter.setDescription("Gif and JPG Images");
200:             *
201:             * @see setDescription
202:             * @see setExtensionListInDescription
203:             * @see isExtensionListInDescription
204:             */
205:            public void setDescription(String description) {
206:                this .description = description;
207:                fullDescription = null;
208:            }
209:
210:            /**
211:             * Determines whether the extension list (.jpg, .gif, etc) should
212:             * show up in the human readable description.
213:             *
214:             * Only relevent if a description was provided in the constructor
215:             * or using setDescription();
216:             *
217:             * @see getDescription
218:             * @see setDescription
219:             * @see isExtensionListInDescription
220:             */
221:            public void setExtensionListInDescription(boolean b) {
222:                useExtensionsInDescription = b;
223:                fullDescription = null;
224:            }
225:
226:            /**
227:             * Returns whether the extension list (.jpg, .gif, etc) should
228:             * show up in the human readable description.
229:             *
230:             * Only relevent if a description was provided in the constructor
231:             * or using setDescription();
232:             *
233:             * @see getDescription
234:             * @see setDescription
235:             * @see setExtensionListInDescription
236:             */
237:            public boolean isExtensionListInDescription() {
238:                return useExtensionsInDescription;
239:            }
240:
241:            /**
242:             * Set the directory must exist flag
243:             * @param mustExits true to only allow existing directories
244:             */
245:            public void setDirMustExist(boolean mustExist) {
246:                dirMustExist = mustExist;
247:            }
248:
249:            /**
250:             * Set the file must exist flag
251:             * @param mustExits true to only allow existing files
252:             */
253:            public void setFileMustExist(boolean mustExist) {
254:                fileMustExist = mustExist;
255:            }
256:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.