KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > FileFilter


1 package org.hibernate.eclipse.console;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.eclipse.core.resources.IContainer;
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IFolder;
9 import org.eclipse.core.resources.IResource;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.jdt.internal.ui.JavaPlugin;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.viewers.ViewerFilter;
15
16 /**
17  * Viewer filter for file selection dialogs.
18  * Client provides a list of file extensions and a list of excluded files (possibly empty).
19  * The filter is not case sensitive.
20  *
21  * (Basically a generic version of ArchiveFileFilter)
22  */

23 public class FileFilter extends ViewerFilter {
24
25     private final String JavaDoc[] fileExtensions;
26
27     private List JavaDoc excludedFiles;
28     private boolean recursive ;
29
30     private final boolean allowDirectories;
31     
32     /**
33      * @param excludedFiles Excluded files will not pass the filter.
34      * <code>null</code> is allowed if no files should be excluded.
35      * @param recusive Folders are only shown if, searched recursively, contain
36      * a matching file
37      */

38     public FileFilter(String JavaDoc[] fileExtensions, IFile[] excludedFiles, boolean recusive) {
39         this.fileExtensions = fileExtensions;
40         if (excludedFiles != null) {
41             this.excludedFiles= Arrays.asList(excludedFiles);
42         } else {
43             this.excludedFiles= null;
44         }
45         recursive = recusive;
46         allowDirectories = false;
47     }
48     
49     public FileFilter(String JavaDoc[] fileExtensions, List JavaDoc excludedFiles, boolean recusive, boolean allowDirectories) {
50         
51         this.fileExtensions = fileExtensions;
52         this.excludedFiles= excludedFiles;
53         recursive = recusive;
54         this.allowDirectories = allowDirectories;
55     }
56     
57     /*
58      * @see ViewerFilter#select
59      */

60     public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
61         if ((element instanceof IFile)) {
62             if (this.excludedFiles != null && this.excludedFiles.contains(element)) {
63                 return false;
64             }
65             return isFileExtension(((IFile)element).getFullPath());
66         }
67         else if (allowDirectories && element instanceof IFolder) {
68             return true;
69         } else if (element instanceof IContainer) { // IProject, IFolder
70
if (!recursive ) {
71                 return true;
72             }
73             try {
74                 IResource[] resources= ((IContainer)element).members();
75                 for (int i= 0; i < resources.length; i++) {
76                     // recursive! Only show containers that contain a matching file
77
if (select(viewer, parent, resources[i])) {
78                         return true;
79                     }
80                 }
81             } catch (CoreException e) {
82                 JavaPlugin.log(e.getStatus());
83             }
84         }
85         return false;
86     }
87     
88     public boolean isFileExtension(IPath path) {
89         for (int i= 0; i < fileExtensions.length; i++) {
90             if (path.lastSegment().endsWith(fileExtensions[i])) {
91                 return true;
92             }
93         }
94         return false;
95     }
96             
97     
98             
99 }
100
Popular Tags